Creating new AI task blocks

Works with

Making the AI block

Setting up the folders 

To create all of the AI blocks, you will need to create a new folder inside of your main folder. The name of this folder has to be ”aitasks”. If you put another name, it could not work correctly. 

Making the JSON file

When it’s done, create a new JSON file with the name of your AI task. When it’s created, you can open it. To explain the code of the file, I will use the code for the AI task ”Follow Parent”.

The name of custom AI task blocks should be prefixed with pluginid_ to avoid collisions with other plugins

{
  "message0": "Follow parent with speed %1",
  "args0": [
    {
      "type": "field_number",
      "name": "speed",
      "value": 1.0
    }
  ],
  "inputsInline": true,
  "previousStatement": null,
  "nextStatement": null,
  "colour": 20,
  "mcreator": {
    "toolbox_id": "wikiplugin",
    "fields": [
      "speed"
    ]
  }
}

The text

The first line is used to write the text displayed on the block. If you look correctly, you can see a %1. The % symbol followed by a number is used to tell to MCreator and Blockly there is a field to fill by the user. The number is only used to say his place in the args list after. You have to put % symbols as much as you want the user to change variables.

Texts.properties

To use this feature, your plugin needs to use, at least, the first snapshot of MCreator 2020.4

Instead of writing the text directly inside the JSON file, we can now, with MCreator 2020.4 write the text of all our blocks inside one file. In the future, it will allow us to translate the text of the blocks. To do this, create a new folder named "lang" (with the plugin.json), and create a new file named texts.properties inside it. Then, open this file. For a block, you have to write blockly.block.the_name_of_your_JSON_file=The message of your block with %1. If you write the text of your block in this file, you have to delete the "message0" section in the JSON file.


Arguments

After the first line, you have the ”args0” section. This section is used to declare the type of each variable you have put before. The first variable will be the %1 you have put in the ”message0”.

There are 2 argument types. The first one is a field value, and the second one is an input. To know what they can do, you should read this Wiki page.

The colour of the block

After, go to ”colour”. This section is to set up the colour of the block. You have to write a number between 0 and 360 for the colour. You can find the colour code here. However, there are 3 default String (for a value) you can use. The first one is "%{BKY_MATH_HUE}" used to have the colour of the Math procedure blocks (or blocks to get a number). The second is "%{BKY_TEXTS_HUE}", and it' used for procedure block of text type (e.g. Blocks to get a String value). The last one is "%{BKY_LOGIC_HUE}" used with all blocks that return a boolean value or set a boolean value.

The MCreator section

The toolbox_id is the section where your block will be. To have your own section check below.

The last section in this code is the fields/inputs. In the ”fields” section, you have to write all the field_number and field_checkbox you have used (write their names).

If you have one or many inputs_values, duplicate the ”fields” section, and change ”fields” by ”inputs”.

To have the list of all entities, you will need to add a new section after the ”args0” section. Copy and paste this code. You don’t have to put the % symbol.

   "extensions": [
    "entity_list_provider"
  ],

You will also have to add an ”entity” line in the ”fields” section.

 

Create your own AI task section

To have your section, return into your ”aitask” folder. Then, create a new JSON file. The name your file has to start with a $. After, put the name of your section without space. You will have to write this name (without the $) in the AI files. When it’s done, open it and paste this code.

{
  "name": "Wiki tutorial",
  "color": 110
}
  • ”name” is the same who will be displayed in MCreator.
  • ”colour” is the colour if the section.

After this, you can save and close your file.

Texts.properties

To use this feature, your plugin needs to use, at least, the first snapshot of MCreator 2020.4

Instead of writing the text directly inside the JSON file, we can now, with MCreator 2020.4 write the name of all our categories inside one file. In the future, it will allow us to translate the text of the blocks. To do this, create a new folder named "lang" (with the plugin.json), and create a new file named texts.properties inside it. Then, open this file. For a category, you have to write blockly.category.the_name_of_your_JSON_file(without the $)=The of your category. If you write the text of your block in this file, you have to delete the "name" section in the JSON file.


Great job! You have finished creating the AI task block. You have now to set the code.

Coding your AI task

In this section, I will only speak of the 1.14.4, but it is the same thing. You have only to create a folder named 1.12.2 instead.

Setting up the folders

First of all, go back into the main folder (you have to see your ”aitasks” folder). Then, create a new folder named 1.14.4. It is in this folder you will put all of the files with code. Go inside, and create a new folder name also ”aitasks”. Enter in this folder.

Making the code file

When you are inside of the folder, create a new file with the extension .java.ftl. For the name of the file, write the same name you have wrote for the JSON file. Open the file, and write the code of the AI task. This is the code of my block :

this.goalSelector.addGoal(${customBlockIndex+1}, new FollowParentGoal(this, ${field$speed}));

You can see a ${customBlockIndex+1} and a ${field$speed}. The first one is used to set the position number of the task whatever where it will be in the builder. The second is used to set the number (in this case) that the player has put for the argument ”speed”. For your fields, change ”speed” by the name you have set up. If you have inputs, change the ”field” for ”input”.

Save your file and you are done! You can now export your plugin in a .zip file and test it.

Video tutorial

If you prefer to watch the video, you can find the wiki page above summarized in a video:

 


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.