So I'm trying to create custom crops, and I'm trying to make the code reusable. However, I've hit a catch 22. My crop block object takes two items in its constructor (one for the seeds, one for the produce). However, the Items are initialized after the blocks, and as a result, the items being passed into the new crop block are null. The problem is that I've tried to have the items initialize first, but it causes problems with itemblocks elsewhere in the mod. Is this a truly impossible solution and I have to create a workaround, or am I missing something important?
Vanilla gets around this by extending a base class (which you do as well) which contains all the necessary code with each subclass defining its own drop items within the methods that get them (getSeedItem() and getCropItem(), which get the seed and crop items respectively) so they do not need to be initialized before the block (only when they are called, which is always much later). While you do need to create subclasses they only need a few methods to define their drops and any special behavior (e.g. potatoes add a 1/50 chance of a poisonous potato in dropBlockAsItemWithChance()). NB: I do not have MCP for any version later than 1.6.4 but they probably still do the same thing as of 1.12, the methods may even still have the same names.
Can you just have the block constructor register the items itself, rather than having them passed in?
One thing to be careful of is using constructors with things like blocks. For example tile entities get automatically instantiated by the game when they load from file. So for example if you have a custom tile entity and your main code sets a value in that class, the next time the world gets loaded that value will be null, and your value-setting code won't be called.
I know your question is for blocks not tile entities but the same issue might arise.
So I'm trying to create custom crops, and I'm trying to make the code reusable. However, I've hit a catch 22. My crop block object takes two items in its constructor (one for the seeds, one for the produce). However, the Items are initialized after the blocks, and as a result, the items being passed into the new crop block are null. The problem is that I've tried to have the items initialize first, but it causes problems with itemblocks elsewhere in the mod. Is this a truly impossible solution and I have to create a workaround, or am I missing something important?
Initialization sequence (in the main mod class):
https://pastebin.com/QW6rEzrK
ModBlocks:
https://pastebin.com/4ACeqBqG
ModItems:
https://pastebin.com/02RuHVxh
Crop block class:
https://pastebin.com/LrjzerXu
Further code can be provided upon request
My first mod =D
Vanilla gets around this by extending a base class (which you do as well) which contains all the necessary code with each subclass defining its own drop items within the methods that get them (getSeedItem() and getCropItem(), which get the seed and crop items respectively) so they do not need to be initialized before the block (only when they are called, which is always much later). While you do need to create subclasses they only need a few methods to define their drops and any special behavior (e.g. potatoes add a 1/50 chance of a poisonous potato in dropBlockAsItemWithChance()). NB: I do not have MCP for any version later than 1.6.4 but they probably still do the same thing as of 1.12, the methods may even still have the same names.
TheMasterCaver's First World - possibly the most caved-out world in Minecraft history - includes world download.
TheMasterCaver's World - my own version of Minecraft largely based on my views of how the game should have evolved since 1.6.4.
Why do I still play in 1.6.4?
So create a subclass of of the crop block called, like, leek_crop, which contains the item data?
My first mod =D
Can you just have the block constructor register the items itself, rather than having them passed in?
One thing to be careful of is using constructors with things like blocks. For example tile entities get automatically instantiated by the game when they load from file. So for example if you have a custom tile entity and your main code sets a value in that class, the next time the world gets loaded that value will be null, and your value-setting code won't be called.
I know your question is for blocks not tile entities but the same issue might arise.