Skip to content
Alexander Gottwald edited this page Apr 14, 2018 · 8 revisions

ScriptRunner

The mod can run scripts on various hooks

Scripts

The scripts must be Javascript and run on the Nashorn Javascript engine included in the Java 8 runtime. The scripts are loaded from the respective subdirectories in mods/scriptrunner/scripts and must have the file ending .js

Each script has its own context which is kept until the script is reloaded. Storing information between runs would be possible by storing them in a global variable.

A special object context is added to each script context. This object is shared between all scripts run on a hook call.

Hooks

The supported hooks are:

  • onServerStarted: The server has started and is ready to process connections (Called after startup)
  • onServerShutdown: The server is about to shutdown (Called before shutdown)
  • onPlayerLogin: The player has logged in
  • onPlayerLogout: The player has logged out
  • onPlayerMessage: The player sent a chat message to the server
  • onItemTemplatesCreated: The item templates have been initialized
  • onServerPoll: once per second server loop

onServerStarted

The scripts will run once after the server has started.

var Server = Packages.com.wurmonline.server.Server;
function onServerStarted() {
} 

onPlayerLogin

The scripts will run every time a player logs in.

function onPlayerLogin(player) {
	var name = player.getName();
}

The scripts will run every time a player logs out.

onPlayerLogout

function onPlayerLogout(player) {
	var name = player.getName();
}

onPlayerMessage

The scripts will run every time a player sends a chat message to the server.

function onPlayerMessage(communicator, message, channel) {
	var player = communicator.getPlayer();
}

onItemTemplatesCreated

The scripts will run once when the server finished creating the item templates.

function onItemTemplatesCreated() {
}

onServerPoll

The scripts will run on each server loop which occur about once per second

function onServerPoll() {
}

Configuration

Configuration sample

Configuration options can be added to mods/scriptrunner.config.

  • scriptsFolder

Set a different folder for the scripts Default: scriptrunner/scripts

  • HOOKNAME.refresh

When set to true: Looks for new and changed scripts whenever a hook is run. This can have severe performance implications since all scripts must be parsed on each call. This is can add a lot of lag when used with the onServerPoll hook.

This will also reset the context of the script. Status stored in a global variable from the previous run will be reset. Default: false

Other mods

Scriptrunner will scan all other mods for the same configuration keys and read scripts from the mods scripts/HOOKNAME folders.

Examples

  • Greeting the player on login

via event message

var ProtoConstants = Packages.com.wurmonline.shared.constants.ProtoConstants;
function onPlayerLogin(player) {
	var message = "Welcome to Wurm, " + player.getName() + "!";
	player.getCommunicator().sendSafeServerMessage(message, ProtoConstants.M_SYSTEM);
}

via PM

function onPlayerLogin(player) {
	function showPM(message) {
		player.showPM("Server", "Server Info", message, false);
	}
	showPM("Hello " + play.getName() + "!");
	showPM("Welcome to our server.");
}
  • Announcing the player login
var MessageServer = Packages.com.wurmonline.server.MessageServer;
var ProtoConstants = Packages.com.wurmonline.shared.constants.ProtoConstants;
function onPlayerLogin(player) {
	if (player.getPower() == 0) {
		var message = "Player " + player.getName() + " has logged in.";
		MessageServer.broadCastSafe(message, ProtoConstants.M_SYSTEM);
	}
}
  • Log all player messages
var Logger = Packages.java.util.logging.Logger;
function onPlayerMessage(communicator, message, title) {
	var player = communicator.getPlayer().getName().toLowerCase();
	var channel = title.replace(/^:/,"").toLowerCase();
	var logger = "players." + player + "." + channel;
	Logger.getLogger(logger).info(message);
}
  • Shutdown the server right after startup

This could be useful when running automated tests

var Server = Packages.com.wurmonline.server.Server;
function onServerStarted() {
	Server.getInstance().shutDown();
}