Vampire's Delight

Vampire's Delight

161k Downloads

crash with Plenty of golems (1.19.2)

5stigma opened this issue ยท 12 comments

commented

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

1.- Used jungle golem
2.- Crash

commented

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.

commented

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

commented

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

commented

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

commented

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

commented

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

commented

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.

commented

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.

commented

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

commented

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

commented

Solution

@TheGridExpert
you can optimize the method from

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

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.

commented

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