public interface IScriptEvent
This interface represent the event, the only argument that was passed into script's function. It contains many different useful methods to interact with Minecraft on the server side.
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Cancel the trigger event.
|
int |
executeCommand(java.lang.String command)
Execute a command.
|
java.lang.String |
getFunction()
Get script's function name.
|
IScriptNpc |
getNPC()
Get the first Mappet NPC from either subject or object (or
null , if there is no NPC). |
IScriptEntity |
getObject()
Get object (secondary) entity that was passed into the event.
|
IScriptPlayer |
getPlayer()
Get the first player from either subject or object (or
null , if there is no player). |
java.lang.String |
getScript()
Get script's ID to which this event was passed to.
|
IScriptServer |
getServer()
Get the server in which this event happened.
|
IScriptEntity |
getSubject()
Get subject (primary) entity that was passed into the event.
|
java.lang.Object |
getValue(java.lang.String key)
Get a value for given key (might be a
null ). |
java.util.Map<java.lang.String,java.lang.Object> |
getValues()
Get a map of extra context values that was passed into the event.
|
IScriptWorld |
getWorld()
Get the world in which this event happened.
|
default void |
scheduleScript(int delay)
Schedule execution of the same script (with same function)
given ticks forward.
|
void |
scheduleScript(int delay,
java.util.function.Consumer<IScriptEvent> consumer)
Schedule a function (instead of script).
|
void |
scheduleScript(int delay,
jdk.nashorn.api.scripting.ScriptObjectMirror function)
Schedule a JavaScript function (instead of script).
|
default void |
scheduleScript(java.lang.String function,
int delay)
Schedule execution of the same script with given function
given ticks forward.
|
void |
scheduleScript(java.lang.String script,
java.lang.String function,
int delay)
Schedule execution of given script with specific function
given ticks forward.
|
void |
send(java.lang.String message)
Send a message to all players in the chat.
|
void |
setValue(java.lang.String key,
java.lang.Object value)
Set a value for given key in extra data.
|
java.lang.String getScript()
java.lang.String getFunction()
IScriptEntity getSubject()
IScriptEntity getObject()
IScriptPlayer getPlayer()
null
, if there is no player).IScriptNpc getNPC()
null
, if there is no NPC).IScriptWorld getWorld()
IScriptServer getServer()
java.util.Map<java.lang.String,java.lang.Object> getValues()
java.lang.Object getValue(java.lang.String key)
null
).void setValue(java.lang.String key, java.lang.Object value)
void cancel()
Depending on the type of event, it can prevent the default behavior (for example for chat trigger, if you cancel it, it won't send the message into the chat).
// Assuming this script was attached to global trigger "On block placed,"
// this script will cancel placing of the block by a player
function main(c)
{
if (c.getValue("block") === "minecraft:stone")
{
c.cancel();
}
}
default void scheduleScript(int delay)
Read scheduleScript(String, String, int)
for more information.
function main(c)
{
var states = c.getServer().getStates();
var counter = states.getNumber("counter");
if (counter < 10)
{
c.send(counter + " Mississippi...");
states.add("counter", 1);
c.scheduleScript(20);
}
else
{
states.reset("counter");
c.send("Here I go!");
}
}
delay
- How many ticks should pass before scheduled script will be executed.default void scheduleScript(java.lang.String function, int delay)
Read scheduleScript(String, String, int)
for more information.
function main(c)
{
// Schedule script execution of function other
// within same script a second later
c.scheduleScript("other", 20);
}
function other(c)
{
c.send("A second ago, function \"main\" told me to say \"hi\" to you... :)")
}
void scheduleScript(java.lang.String script, java.lang.String function, int delay)
When scheduling a script, it will use same data which were passed into current script's function. I.e. subject, object, world, server and values.
ProTip: if you put some values into this context using
setValue(String, Object)
, then that value will be also available
when the scheduled script will be executed.
// Script "a"
function main(c)
{
// As ProTip states, you can pass some value using
// setValue() and getValue() event's functions
c.setValue("message", "Hello!");
// Schedule script "b" execution a second later
c.scheduleScript("b", "main", 20);
}
// Script "b"
function main(c)
{
c.send("A second ago, script \"a\" told me deliver this message: " + c.getValue("message"));
}
void scheduleScript(int delay, jdk.nashorn.api.scripting.ScriptObjectMirror function)
function main(c)
{
c.scheduleScript(60, function (context)
{
context.send("This was called three seconds later!");
});
}
void scheduleScript(int delay, java.util.function.Consumer<IScriptEvent> consumer)
int executeCommand(java.lang.String command)
c.executeCommand("/kick Creeper501");
void send(java.lang.String message)
c.send("Hi :)");