Topic category: Troubleshooting, bugs, and solutions
Hi! I've been working on a mod for a few days, and when creating a food item in version 2024.1, I encountered a small issue: despite the checkbox being unchecked, the item is still always edible. After examining the generated code, which looks like this (only the relevant parts are shown):
public class RawBeefItem extends Item {
public RawBeefItem() {
super(new Item.Properties().stacksTo(64).rarity(Rarity.COMMON).food((new FoodProperties.Builder()).nutrition(4).saturationMod(1.8f).meat().build()));
}
}
It's clear that the checkbox itself is functioning correctly because when I check it, the code changes to ".alwaysEat().meat().build()));". However, there's an issue with this part of the code:
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player entity, InteractionHand hand) {
InteractionResultHolder<ItemStack> ar = super.use(world, entity, hand);
entity.startUsingItem(hand);
return ar;
}
For some reason, this code doesn't work as intended. I modified it to the following:
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player entity, InteractionHand hand) {
ItemStack itemstack = entity.getItemInHand(hand);
if (entity.canEat(this.getFoodProperties().canAlwaysEat())) {
entity.startUsingItem(hand);
return InteractionResultHolder.consume(itemstack);
} else {
return InteractionResultHolder.pass(itemstack);
}
}
@Override
public boolean isEdible() {
return true;
}
Now, the item works as intended: it can only be eaten when the hunger bar is not full. However, for this to work, I had to lock the item, meaning you should ensure everything else in your item is ready before locking the code, as you won't be able to edit it further.
Well, this is embarrassing. I unlocked the code again to change the texture one last time and closed MCreator. Since I forgot to change and lock it again, I reopened the program, and to my surprise, the entire code had changed and looked entirely different. I updated the code back to what I had posted earlier, but it didn't work anymore. I reverted to the newly generated code, opened the game, and somehow it worked!
I'm new to Java, so I'm not sure what happened here, but it seems there's no bug after all. This post is now irrelevant. If any mod wants to delete it, that would probably be for the best, as I don't want to confuse anyone.