squeek502 / AppleCore

An API for modifying the food and hunger mechanics of Minecraft
The Unlicense
55 stars 24 forks source link

Crash When Eating Cake #113

Closed ChloeDawn closed 6 years ago

ChloeDawn commented 6 years ago

Game crashes when trying to right-click/eat a cake block.

Mod version: 1.12.2-3.1.0 Crash report: pastebin.com/Ng1CYAXE

It may be worth noting I apply a registry replacement for the cake block, which might be the issue? I'm not sure. If that is the case, let me know what needs changing here to prevent issues with your mod.

Replacement instance: net.insomniakitten.cake.PersistentCakeBlock

squeek502 commented 6 years ago

Two things:

@SubscribeEvent
public void onGetHarvestDrops(BlockEvent.HarvestDropsEvent event)
{
    if (event.getState().getBlock() == Blocks.CAKE && event.getState().getValue(BlockCake.BITES) == 0)
    {
        event.getDrops().add(new ItemStack(Items.CAKE));
    }
}
ChloeDawn commented 6 years ago

That single event hook would not account for all occasions where a cake block is broken in the world. I had been using events at one point, as seen here: PersistentCake.java#L25-L56. It was much cleaner and more reliable to replace the cake instance; which I also handle for Harvestcraft cakes, as you can see in the other packages of the current branch. I will add an Optional interface for IEdibleBlock to my replacement, thanks for the info.

squeek502 commented 6 years ago

Ah, ok, block breaking is a pretty crazy tangle of unexpected behaviors 😢 . I've run into similar edge-cases when trying to use HarvestDropsEvent.

Let me know if you run into any issues trying to use IEdibleBlock.

EDIT: Also worth noting that in your case, the onBlockActivated and onEatenCompatibility methods shown in the linked example above are irrelevant. getFoodValues and setEdibleAtMaxHunger is all you need to implement (and, if possible, it'd be best if you could just implement them as something like ((IEdibleBlock)super).getFoodValues(itemStack)).

ChloeDawn commented 6 years ago

Poking around, the root issue is you will be holding a hard reference to the cake block ItemFoodProxy.java#L21 which is incompatible with registry replacements.

EDIT:

Secondly, I realise my class isn't the issue at all. I extend BlockCake, which would implement the interface at runtime. This is 100% an issue with your internal references not being compatible with registry replacements. ;-;

Thirdly, discussing things inside an issue is frustrating lol, do you have Discord? There is a modded Minecraft discord (r/ftb) here. Ping @DiscordPolice to ask for Mod Developer role, which gives access to #developers

squeek502 commented 6 years ago

Ah, yes, you're totally right. Fixed in https://github.com/squeek502/AppleCore/commit/0515815840cea82a9040b634d61b8f5b16e3966f, and confirmed that it works without any interface compat. Will release new 1.12.x versions with the fix soon.

Just for the sake of completeness, an alternative (less complete) 'fix' would be to have your mod add your block to AppleCore's edible block registry by doing: AppleCoreAPI.registry.registerEdibleBlock(your_registered_cake_block, Items.CAKE). Up to you if want to add that to be compatible with AppleCore versions before the above fix.

EDIT: Actually didn't realize you had merged the applecore branch into master. Checking if the version without the interface compat works now.

EDIT#2: Confirmed to work without PersistentCakeBlock implementing IEdibleBlock.

squeek502 commented 6 years ago

Should be fixed in AppleCore v3.1.1. You can safely revert https://github.com/SleeplessDevelopment/PersistentCake/commit/ddb991d6709233b405690d18e37a62984a46b906 if you want.