Creating new variable types

Creating the new type

First of all, you need to tell MCreator what is your new variable type. To achieve this, create a new folder at the plugin's root variables. Inside this folder, each JSON file you will create will become a new variable type. The name of each file is the type of your variable.

For example, if I have a file called file.json, I tell to MCreator to create a new variable type with the registry name file. However, this doesn't end there. YOu also have a few parameters to define.

Parameter Description Default value
color The color for built-in procedure blocks (e.g. get and set). it can use both HUE (0-360) or hexadecimal values (#FFFFFF) as a String Provide an error if absent
blocklyVariableType When this value (as a String) is used inside input_value argument types of procedure blocks, MCreator will tell to Blockly, Blockly needs to accept everything that is related to this variable type. Provide an error if absent
ignoredByCoverage If true, this variable type will be ignored the coverage (when selecting a generator) and only the local variable scope will be usable. false
nullable If true, those variables can accept null as a valid value false

 

Making the code

Files and folders

YAML

First of all, you need to create a folder variables inside a folder for each generator you want to support. Then, create a file following this template theVariableType.yaml, where theVariableType is the same name as the JSON file.

Mappings

You will also need to create a new file called types.yaml inside a mappings folder (for each generator), so inside of it, you can define the Java value of your variable type. Each line is a different variable type and you only need to write your var types of this plugin. For example, the logic variable type uses this line logic: boolean while an itemstack has this line itemstack: ItemStack.

The template for those lines is the following: theVariableType: theJavaClass

The code

Inside the YAML file of the variables folder of a generator, there are some elements to define. For good examples, check files inside an official generator. For example, C:/Program Files/Pylo/MCreator/plugins/forge-1.19.2.zip/forge-1.19.2/variables/logic.yaml.

However, you need to set the default value of the variable. This determines what is the value when the variable is initialized before you first set a value to it inside a procedure.

Then, you have to define what is the code for some sections and blocks for each scope you want to support. If a scop;e is missing, the scope is considered as not supported by your variable and will not be usable (and seeable) when this scope is selected by users.

Example

The following example demonstrates how the File variable type of the File Manager plugin works.

defaultvalue: new File("")
scopes:
  local:
    init: ${var.getType().getJavaType(generator.getWorkspace())} ${var.getName()} = ${var.getType().getDefaultValue(generator.getWorkspace())};
    get: ${name}
    set: ${name} = ${opt.removeParentheses(value)};
  global_session:
    init: public static File ${var.getName()} = ${var.getValue()};
    get: ${JavaModName}Variables.${name}
    set: ${JavaModName}Variables.${name} = ${opt.removeParentheses(value)};

In this example, you can notice that the default value for those variables is an empty file with an empty location. Then, scopes are only defined for the local variables and the global session variables (same behaviour as a local, but it can be used by all procedures). This means that both Player scopes and the Map and World scopes are not supported and so, can not be used for this variable type.

init is the code to generates when creating the variable. get is what to generate when using a getter procedure block with a variable of this type selected. set is the same thing, but it is for the Set procedure block instead.

 

You can also notice some FreeMarker code. They are used to get values determined in other files or parameters inside MCreator.

${var.getType().getJavaType(generator.getWorkspace())} will get the value we determined inside the types.yaml mapping file of this generator.

${var.getName()} is the name given by the user to the specific variable.

${var.getType().getDefaultValue(generator.getWorkspace())} is the default value written at the beginning of the file. It is a separate value because defaultvalue is also used by the global variables panel, so the user can see what is the default value of each variable created.

${opt.removeParentheses(value)} contains 2 different things. value is what the user put as the value for the setter block, but to optimize the code, we added some code optimizers with one to remove useless parentheses. This is what opt.removeParentheses() achieves. This code is not mandatory to make your variable works, but it is highly suggested, so you can give to users a better code. If you don't want it, you can simply write ${value}.



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.