TheGridExpert / VampiresDelight

A minecraft addon mod for Vampirism and Farmer's Delight
https://www.curseforge.com/minecraft/mc-mods/vampires-delight
MIT License
6 stars 2 forks source link

crash with Plenty of golems (1.19.2) #2

Closed 5stigma closed 11 months ago

5stigma commented 11 months ago

crash-2023-12-18_03.16.47-server.txt

1.- Used jungle golem 2.- Crash

TheGridExpert commented 11 months ago

Do you mean that you spawned a golem in jungle and then it crashed? What were the conditions?

5stigma commented 11 months ago

The jungle golem can be picked up and used as a gun, it shoots cocoa bullets but when it collides with an enemy, it crashes.

TheGridExpert commented 11 months ago

What mods do you have installed except for vampire's delight and Plenty of golems?

5stigma commented 11 months ago

pfff, like 600 more mods, and among them, optimization mods, It was fixed with Neruina Mod but annoying messages appear

TheGridExpert commented 11 months ago

Oh, ok, I'll look what may cause the crush, the crash report looks quite weird

5stigma commented 11 months ago

Yes, It must be one of the optimization mods, thanks to one who told me which ones were possibly responsible for the crash, I was able to know that it was this mod that caused it

TheGridExpert commented 11 months ago

It looks like it tries to use vampire bite enchantment on cocoa bullet or something like that.

5stigma commented 11 months ago

yes, I noticed that in the report, but I couldn't figure out why it did it, and I suspect it may happen with more similar entities.

5stigma commented 11 months ago

I will try some things and combinations of mods that I think may be responsible and if I find something I will report it to you.

5stigma commented 11 months ago

Yes. is VampiresDelight, I have isolated the mods and I confirm that it is between the 2 mods

Cheaterpaul commented 11 months ago

Solution

@TheGridExpert you can optimize the method from https://github.com/TheGridExpert/VampiresDelight/blob/93ac0134a47f160c235a2869885b93c9930df8fe/src/main/java/net/grid/vampiresdelight/common/item/enchantment/VampireBiteEnchantment.java#L33-L43

to

@SubscribeEvent
public static void onVampireBite(LivingHurtEvent event) {
    if (event.getSource().getEntity() instanceof Player player) {
        ItemStack weapon = player.getMainHandItem();
        int enchantmentLevel = EnchantmentHelper.getItemEnchantmentLevel(VDEnchantments.VAMPIRE_BITE.get(), weapon);
        Level level = event.getEntity().getCommandSenderWorld();
        if (!level.isClientSide) {
            healFromDamage(player, enchantmentLevel, event.getAmount());
        }
    }
}

That way your code looks a little more fancy and you do not manually cast the Entity to LivingEntity.

Advice

And a piece of advice for the future. You should only cast objects, if you either make an instanceof check or if you are 100% sure that the cast will be successful. For your own code and APIs that might be ok, like in Vampirism we can safely cast IVampirePlayer to VampirePlayer without checking, but if you work with Minecraft, Forge or other APIs you should always assume that the return type of a methods or attribute only guarantees you that you will get the type it specifies. Vanilla Minecraft might give you a specific subclass of the type for all their use cases, but other mods might break it.

TheGridExpert commented 11 months ago

Thank you. I'll change it in the next update