Guard Villagers

Guard Villagers

56M Downloads

Guard Villager crashing and corrupting save on 1.20.1 with accessing armor slot 4

Closed this issue · 11 comments

commented

I use this pack for Better MC. Not sure if a patch can be added. Seems like a bad type reference.

Playing with guards will eventually result in accessing armor slot 4. occurs random. Might even be an issue when the server crashes for any reason. Upon reboot, this error occurs.

commented

Please send a full crash report for further diagnosis.

commented

Doesnt exactly show up in the error logs but it does in the console

I reported it to Better MC:
LunaPixelStudios/Better-MC#2112

I suspect it occurs everytime the guard takes damage

This is the primary error message:
Caused by: java.lang.IllegalArgumentException: Invalid slot 'ARMOR': 4

commented

Maybe it is this function:

    protected void hurtArmor(DamageSource damageSource, float damage) {
        if (damage >= 0.0F) {
            damage = damage / 4.0F;
            if (damage < 1.0F) {
                damage = 1.0F;
            }
            for (int i = 0; i < this.guardInventory.getContainerSize(); ++i) {
                ItemStack itemstack = this.guardInventory.getItem(i);
                if ((!damageSource.is(DamageTypes.ON_FIRE) || !itemstack.getItem().isFireResistant()) && itemstack.getItem() instanceof ArmorItem) {
                    int j = i;
                    this.damageGuardItem(1, EquipmentSlot.byTypeAndIndex(EquipmentSlot.Type.ARMOR, j), itemstack);
                }
            }
        }
    }

Maybe j is going beyond 3?

commented

What do you think of this fix for 1.20.1?

    protected void hurtArmor(DamageSource damageSource, float damage) {
        if (damage >= 0.0F) {
            damage /= 4.0F;
            if (damage < 1.0F) {
                damage = 1.0F;
            }

            for (int i = 0; i < this.guardInventory.getContainerSize(); ++i) {
                ItemStack itemstack = this.guardInventory.getItem(i);
                if ((!damageSource.is(DamageTypes.ON_FIRE) || !itemstack.getItem().isFireResistant())
                        && itemstack.getItem() instanceof ArmorItem armorItem) {
                    EquipmentSlot slot = armorItem.getSlot(); // ✅ Safe and valid in 1.20.1
                    this.damageGuardItem(1, slot, itemstack);
                }
            }
        }
    }

note: I haven't tested this

commented

Doesnt exactly show up in the error logs but it does in the console

Then send the console log.

What do you think of this fix for 1.20.1?

I am currently being held up by life stuff so I'm not in the position to test out code changes like yours right now. What you can do is test it out yourself by making a fork that fixes the code and if it works then you can make a pull request that I will merge.

commented

I got some free time this week to implement your suggested fix into the mod. Could you please test out this jar file and check if the crash still happens? Thanks.

guard villagers crash test.zip

commented
commented
commented

I think it worked. Not getting anymore armor 4 ref when I crash and boot up again. Was getting that corruption every time I crashed but no longer.

Do you mean the game still crashes because of guard villagers here? Asking so when I release this the bug is completely fixed

commented

Hello. I would just like to check in again to ask if the crash is completely gone or if you mean that save file is no longer corrupted.