Skip to content

Creating a Script

Matryoshika edited this page Jun 7, 2018 · 3 revisions

The file

There are 3 requirements for Scripter to be able to see script-files:

  1. They have to be located in /config/Scripter, or a subdirectory therein
  2. They have to contain the word scripter in their file-name
  3. They have to have the extension .js

File-names need to contain scripter as this allows one to create other JS scripts in the same folder, that will not be read by Scripter. This allows one to have a central file that houses commonly used functions across several scripts etc.


Inside the file

The very first line has to be a comment. This comment is read by Scripter as it maps what scripts to call during a specific event.
If one uses the comment //PlayerInteractEvent.RightClickBlock on the first line, then Scripter will execute this script when this event is called.
For a full list of events one can use, please read [events].

There is one further limitation on scripts. Scripter will only call one function from each script-file, and this function has to be named onEvent and it has to have a single parameter, for example: var onEvent = function(event){}
This function can be seen as a miniature main method, for this script. You can call other functions from this, and they will be executed during the specified event.


Example script

//PlayerInteractEvent.RightClickBlock

var onEvent = function(event){
	//Events with hands are often fired twice, once for each hand!
	if(event.getHand().name() === "MAIN_HAND"){
		var ItemStack = Java.type("net.minecraft.item.ItemStack");
		var Helper = Java.type("matryoshika.scripter.HelperMethods");
		var Item = Helper.getItem("minecraft:bone");
		var EntityItem = Java.type("net.minecraft.entity.item.EntityItem");
		var pos = Helper.getPosition(event.getEntityPlayer());
		var Entity = new EntityItem(event.getWorld(), pos[0], pos[1], pos[2], new ItemStack(Item));
		//func_72838_d = spawnEntity()
		event.getWorld().func_72838_d(Entity);
	}
}
Clone this wiki locally