This guide explores how to combine MCreator-generated code with custom-written code in its code editor effectively, including techniques like locking code and leveraging user code blocks.
Locking generated mod code
While there is a dedicated guide on locking generated mod code, we briefly cover this here too, as this is one of the options for altering generated code.
Each mod element can have its code locked, which means that MCreator won't generate the code of such mod element again. This means the user needs to take care of any potential changes to this file, such as changing the Minecraft version. Locked mod elements can't be edited in UI, since the user may have overridden generated logic with custom code.
User code blocks
This system replaces the old system where one could lock all base mod files (mod files needed for mod structure and not directly connected to single particulate mod element).
Instead of locking base mod files, one can add code within those custom user blocks and the code between those placeholder sections in the code will persist across builds, and if another generator specifies the same user code blocks, even across Minecraft versions, and even across modding APIs when possible.
Here are some questions we have seen in the past on the user code blocks:
Why not do it in other class files?
Our internal team picked quite some common locations where those blocks are needed, including mods.toml. If more are needed, we accept PRs adding them on suggested places.
Isn't it possible to let users choose where to put them?
Anchoring text in an arbitrary position when the previous anchor can change during code generator changes and data model changes is not a trivial task. This is why most code generators use the comment anchor approach that we decided to adopt in our software.
Can I disable part of the code using user code blocks?
In some cases yes. We have intentionally put user code blocks in some regions so that you can disable parts of the code using them. One such example is the main mod file that can be entirely overridden. To do so, insert the start and end of the multiline Java comment between the generated code to comment it out and then use the custom mod init code instead. See example below:
public MyMod() {
// Start of user code block mod constructor
/*
// End of user code block mod constructor
... MCreator generated code that is now disabled...
// Start of user code block mod init
*/
// End of user code block mod init
... custom mod init code instead ...
}
Custom code in procedures
You can also define parts of procedures using custom code. To do this, use the Advanced toolbox section and use the "custom code snippet" blocks.
Understanding what you are doing
Only disable code generation or put code into user code blocks if you 100% understand what you are doing. Not understanding how Java programming works will likely result in build errors and hard-to-trace bugs in your mod. If you disable part of the code, it can happen that parts of your mod will not work as intended.