[1.21.5] Dategenning Crop Block leaves Seed Item with getBlock() that returns null
JKDevz opened this issue ยท 1 comments
Opening up my inventory crashes the game and passes the following Null Reference Exception:
Caused by: java.lang.NullPointerException: Cannot invoke "net.minecraft.block.Block.getRequiredFeatures()" because the return value of "net.minecraft.item.BlockItem.getBlock()" is null
I have created a Crop Block that functions like Carrots and Potatoes, where the Crop Item is both the yield and the seed.
(Note: I've depersonalised the code to use names such as EXAMPLE_CROP and CROP_ITEM for clarity)
Crop Block Class:
public class ExampleCrop extends CropBlock {
public static final int MAX_AGE = 4;
public static final IntProperty AGE = IntProperty.of("age", 0, 4);
public ExampleCrop(Settings settings) {
super(settings);
}
@Override
public Item asItem() {
return ModItems.CROP_ITEM;
}
@Override
protected Block asBlock() {
return ModBlocks.EXAMPLE_CROP;
}
@Override
protected ItemConvertible getSeedsItem() {
return ModItems.CROP_ITEM;
}
@Override
public IntProperty getAgeProperty() {
return AGE;
}
@Override
public int getMaxAge() {
return MAX_AGE;
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(AGE);
}
@Override
protected boolean canPlantOnTop(BlockState floor, BlockView world, BlockPos pos) {
return floor.isOf(ModBlocks.CUSTOM_BLOCK);
}
}
Registering the Block in ModBlocks class:
public static final Block EXAMPLE_CROP = registerBlock(
"example_crop",
ExampleCrop::new,
AbstractBlock.Settings.create()
.nonOpaque()
.noCollision()
.ticksRandomly()
.breakInstantly()
.sounds(BlockSoundGroup.CROP)
.pistonBehavior(PistonBehavior.DESTROY)
.luminance(light -> 3),
false
);
Registering Crop Item in ModItems class:
public static final Item CROP_ITEM = registerItem(
"crop_item",
(settings) -> new BlockItem(
ModBlocks.EXAMPLE_CROP,
settings
),
new Item.Settings().food(ModFoodComponents.EXAMPLE_FOOD_COMPONENT,
ModFoodComponents.EXAMPLE_CONSUMABLE_COMPONENT)
.useItemPrefixedTranslationKey()
);
Registering the Crop Block Model seems to have a random chance of failing, in more than one way, when datagenning.
Registering the Block Model in datagen:
blockStateModelGenerator.registerCrop(
ModBlocks.EXAMPLE_CROP,
ExampleCrop.AGE,
0, 1, 2, 3, 4);
I have encountered the following scenarios:
- The Crop Item model simply does not register and a corresponding model JSON file is not generated (original gets deleted).
Item does not exist in the inventory, no crash caused.
- The Crop Item model does successfully register, but the Block reference for the Block Item does not reference the Crop Block and is left as null.
Causes a crash if opening up the Inventory or attempting to use /give.
- The Crop Item model seemingly registers, gaining all the correct functionality, but is unable to find the game textures.
Simply no texture, no crash caused.
- The Crop Item model is successfully registered, and its' BlockItem.block reference is successfully set to the corresponding Crop Block.
No crash caused.
I can run datagen a number of times and get one of the 4 results above with seemingly no logical reason. This issue only occurred when implementing Configured and Placed Features, and World Gen with Terra Blender. I simply keep running datagen and running the client until I get a build where opening the inventory does not crash the game (highly inefficient, but necessary).
This Null Exception is called when the inventory opens due to the item being added to an Item Group in the inventory.
(I'm new to Fabric and Modding as a whole, so if I missed any important information, code blocks or anything, please let me know and I will gladly provide what is needed to solve this issue).
๐ We use the issue tracker exclusively for final bug reports and feature requests. However, this issue appears to be better suited for either a discussion thread, or a message on our discord server. Please post your request on one of these, and the conversation can continue there.