Creating global triggers

Making the procedure block

The folder

To be seen by MCreator, JSON files defining procedure blocks have to be in the base root of the plugin (with the plugin.json) and then, create a new folder named "triggers". Then, all of your JSON files will have to create there.

Making the JSON file

Trigger's ID

The name of the file is important as it will be the block's ID. Your block's ID should:

  • Not contain capital letters
  • Not contain spaces (Use underscores)
  • Not contain other language words (English should always be used)
  • Avoid using the plugin ID

Displayed text

To set the text, you have 2 options, localization files or integrated into the JSON file

Localization files

Localization files are the files used by MCreator to be able to translate the UI. Every localization file has to be inside "base_root/lang/". Then, add a new file named "texts.properties". This is the basic file used by MCreator. It has to contain all English translations. To add other language support, you can add _fr_FR to support French, _it_IT to support Italian, etc. You can see all suffixes in the built-in plugin named "mcreator-localization".

When you are inside a localization file, add a new line starting with "trigger.theTriggerID=" and then, simply write the text to display.

This number will be its place in the argument list of the JSON file.

Note: All default procedure blocks use this way.

Integrated

Another way to add the text is by adding a "name" field at the beginning of the JSON file. Then, simply do the same thing as for localization files

Dependencies

A dependency is a variable provided by this trigger. Procedure blocks will be able to use this dependency. All triggers do not add dependencies (e.g. mod-clientload).

Dependencies will have to be added as an object in a list named "dependencies_provided".

A dependency has 2 fields, name and type. The name will only be the text displayed on the sidebar of the Blockly editor. The type (the class if you want) is the thing used by MCreator to generate the code and to check if the procedure can be used with a trigger. You can find the types in a mapping file named "types.yaml" inside each generator. You can also add your own dependency type by creating this file in your plugin for each generator you need.

Most used dependency types:

  • direction
  • entity
  • itemstack
  • number
  • world

Cancelable

(Optional) If set to true, users will be able to use the "Cancel event" block with this trigger.

Has result

(Optional) If set to true, users will be able to use the "Set result of global trigger event to" block with this block.

Example

{
  "dependencies_provided": [
    {
      "name": "entity",
      "type": "entity"
    },
    {
      "name": "world",
      "type": "world"
    },
    {
      "name": "x",
      "type": "number"
    },
    {
      "name": "y",
      "type": "number"
    },
    {
      "name": "z",
      "type": "number"
    }
  ],
  "cancelable": "true",
  "has_result": "true"
}

Make the code of your global trigger

The folder

Each generator you want to support will need to have a template file.

In the generator folder, create a new folder named "triggers" and then, create a new file following this template: triggerID.java.ftl.

The code

Simply paste the code with the provided dependencies in the file.

Example with the "Gem dropped" trigger:

@SubscribeEvent public void onGemDropped(ItemTossEvent event) {
	PlayerEntity entity=event.getPlayer();
	double i=entity.getPosX();
	double j=entity.getPosY();
	double k=entity.getPosZ();
	World world=entity.world;
	ItemStack itemstack=event.getEntityItem().getItem();
	Map<String, Object> dependencies = new HashMap<>();
	dependencies.put("x",i);
	dependencies.put("y",j);
	dependencies.put("z",k);
	dependencies.put("world",world);
	dependencies.put("entity",entity);
	dependencies.put("itemstack",itemstack);
	dependencies.put("event",event);
	this.executeProcedure(dependencies);
}


Donate to MCreator

By donating to developers you can speed up development, as with more resources, we can dedicate more time to MCreator. It is a free project made by developers working on it in their free time.