This can be very useful for plugin developers to quickly test some code.
Examples:
Enter the following in the server console or ingame in your client console with rcon in front:
Code: Select all
Will print "hello world!" to everyone on the server.
> SayText2("hello world!").send()
You can also use multiline code by using \n for a new line and \t for one indention:
Will print 1 2 3 4 in server console
> for x in range(1, 5):\n\tprint(x)
Will print 3 4 in server console
> for x in range(1, 5):\n\tif x > 2:\n\t\tprint(x)
Will print "hello world!" in chat with a previously defined variable
> msg = "hello world!"
> SayText2(msg).send()
Print a random number in the server console if its bigger than 50
> import random
> number = random.randint(1, 100)
> if number > 50: \n\tprint(number)\nelse: \n\tprint("number is", number)
Force a player to say something (get <userid> from "status" command)
> player = PlayerEntity(index_from_userid(<userid>))
> player.say("hello world!")
Use the server command ">!reload" to reload the interpreter and clear all imports/defined variables. Once you defined a variable or imported a module you will be able to use it for your whole "session", until you reload the interpreter.
You can automatically import modules by defining them in the imported_modules list in the script.
Syntax: Select all
import code
from listeners.tick import tick_delays
from commands.server import ServerCommand
imported_modules = [
'from players.entity import PlayerEntity',
'from players.helpers import index_from_userid',
'from messages import SayText2',
]
@ServerCommand('>!reload')
def reload_command(command):
global interpreter
interpreter = Interpreter()
load_info(True)
@ServerCommand('>')
def interprete_command(command):
c = command.get_arg_string()
c = c.replace('\\n', '\n').replace('\\t', '\t')
if c:
interpreter.execute(c)
class Interpreter(code.InteractiveInterpreter):
def __init__(self):
super(Interpreter, self).__init__()
def execute(self, c):
print("\n[SP] Interactive Interpreter executing: \n\n" + c + "\n")
self.runcode(c)
interpreter = Interpreter()
def load():
tick_delays.delay(0.1, load_info)
def load_info(reloaded=False):
if reloaded:
print("\n[SP] Interactive Interpreter reloaded\n")
else:
print("\n[SP] Interactive Interpreter loaded\n")
if len(imported_modules):
print("Importing modules:")
for module in imported_modules:
print(" " + module)
interpreter.runcode(module)
print("\n")