-
Notifications
You must be signed in to change notification settings - Fork 10
Scripts
Scripts are programs that allow Mappet map makers to program custom game logic in JS (JavaScript, it has nothing to do with Java) programming language powered by Nashorn (ES5) scripting engine.
If you don't know or have very little experience with JS, try finding some tutorials online that cover basics, like this one. This wiki isn't a JS wiki, you need to learn it elsewhere.
Scripts can be executed either from triggers or from /mp script exec
command. Scripts can be edited by going to world's mappet/scripts/
folder, and creating there a .js
file, and edit in a text editor of your choice, or directly in the game in the Mappet's dashboard scripting panel:
The way scripts work: you define a function in the JS file, and then that function passes one argument, which is a IScriptEvent
object which contains all the information you need about the event that was passed from a trigger or from /mp script exec
command, like server (IScriptServer
), world (IScriptWorld
) and event related entities (IScriptEntity
, subject and object).
Depending on the trigger, there might be no subject/object, or even world, for example server tick and server on load global triggers have only access to the server (IScriptServer
).
For example, assuming that we have a script named up.js
in world's mappet/scripts/
folder with following code:
function main(c)
{
c.getSubject().setMotion(0, 1, 0);
}
If we execute it with /mp script exec @r up
, it will throw a random player in the air. To fully utilize scripts, you need to learn more about Mappet's scripting API, which can be found here for the latest version of Mappet.
Alternatively, Mappet comes with an in-game up-to-date documentation which also features code samples. You can open the in-game documentation in scripts panel, by pressing ❔ icon button under REPL icon, and there you'll find all the available scripting functions and APIs along with examples of how to use majority of them:
Script editor is a simple text editor that allows editing scripts directly in the game. It offers:
- Syntax highlighting similar to Sublime Text 3
- Syntax highlighting themes (it comes with 2 by default)
- Simple undo (
Ctrl + Z
) and redo (Ctrl + Y
) - Basic code editing features:
- Keeping indentation when inserting a new line
- Insertion of closing
)
,]
,}
,"
,'
- Unwrapping of
{}
- Shifting selection by pressing
Tab
forward orShift + Tab
backward
- Typing sounds 🗿 (they can be disabled in Mappet's mod options in
Ctrl + 0
, Mappet, Script editor)
Script themes can be managed in Ctrl + 0
, Mappet, Script editor. Click on Edit themes... button and it will open theme editor where you can edit colors, add/remove themes. To pick current theme, simply click on desired theme in the sidebar on the left and close the theme editor.
Another useful feature that Mappet's scripts panel is offering is a REPL (Read-Eval-Print Loop). REPL is basically an interactive console. You type into the console some JS code, it evaluates it, and then sends the result of the code's evaluation.
Using REPL, you can experiment with short JS code snippets (for example you can throw yourself into the air by executing c.getSubject().setMotion(0, 1, 0)
in the REPL) or using mappet.dump(Object)
you can analyze what fields and methods Java objects have.
REPL's code editor is same as script editor, for a couple of exceptions:
-
Enter
key evaluates the code, so if you want to insert a new line, you need to pressShift + Enter
key combo -
Ctrl + Arrow up
andCtrl + Arrow down
key combos allow to cycle through history of evaluated code snippets
Since Nashorn works directly on top of Java, you can technically use Forge and Minecraft code directly from JS using functions like Java.type(full_class_name)
, however you'll quickly run into problems as most of Minecraft methods are obfuscated.
You can use MCP mappings that come with Forge's MDK (after you setup gradlew setupDecompWorkspace
). They will be located in user's .gradle/caches/minecraft/de/oceanlabs/mcp/mcp_snapshot/SNAPSHOT_DATE/1.12.2/srgs/
. You will need to open the srg-mcp.srg
file with a text editor, and there will be all mappings. I would recommend setting up a development environment with IntelliJ/Eclipse and going through the source, this way it will be easier.
For example, here is a script that outputs the max player the server is allowed to host:
function main(c)
{
// According to srg-mcp.srg, MinecraftServer.getMaxPlayers() is called "func_71275_y"
c.send(c.getServer().getMinecraftServer().func_71275_y());
}
Every wrapper in the scripting API (like IScriptServer
, IScriptWorld
, etc.) can return the original object upon which
Forge code can also be called. Here is an example of getting blocks registry and outputting every block ID in the chat (probably not a good idea):
function main(c)
{
// Get Forge's registry
var forgeRegistries = Java.type("net.minecraftforge.fml.common.registry.ForgeRegistries");
var entities = [];
// Don't try with BLOCKS, it will be too much text and it will crash client or server
for each (var key in forgeRegistries.ENTITIES.getKeys())
{
entities.push(key.toString());
}
// Send list of all loaded entities to all players
c.send(entities.join(", "));
}
Here is a simple script that demonstrates ray tracing API:
function main(c)
{
// Get the player
var subject = c.getSubject();
// Ray trace up to 64 blocks ignoring entities
var ray = subject.rayTraceBlock(64);
// Important check as ray tracing could fail
if (ray.isBlock())
{
var pos = ray.getBlock();
// Teleport the player on top of the block
subject.setPosition(pos.x + 0.5, pos.y + 1, pos.z + 0.5);
}
}
You can bind this script to a keybind using trigger hotkeys and then every time player presses the key it will teleport them to where they look.
If parts of the wiki don't make sense with Mappet's latest version, or you weren't able to recreate examples (i.e. outdated information), feel free to report the page and section that is out of date on community's Discord server (make sure to mention which Mappet’s version did you use).