// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.protocol;

import java.util.Objects;
import java.util.Arrays;
import com.hypixel.hytale.protocol.io.ValidationResult;
import java.util.Iterator;
import com.hypixel.hytale.protocol.io.PacketIO;
import java.util.HashMap;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;

public class ItemArmor
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 10;
    public static final int VARIABLE_FIELD_COUNT = 5;
    public static final int VARIABLE_BLOCK_START = 30;
    public static final int MAX_SIZE = 1677721600;
    @Nonnull
    public ItemArmorSlot armorSlot;
    @Nullable
    public Cosmetic[] cosmeticsToHide;
    @Nullable
    public Map<Integer, Modifier[]> statModifiers;
    public double baseDamageResistance;
    @Nullable
    public Map<String, Modifier[]> damageResistance;
    @Nullable
    public Map<String, Modifier[]> damageEnhancement;
    @Nullable
    public Map<String, Modifier[]> damageClassEnhancement;
    
    public ItemArmor() {
        this.armorSlot = ItemArmorSlot.Head;
    }
    
    public ItemArmor(@Nonnull final ItemArmorSlot armorSlot, @Nullable final Cosmetic[] cosmeticsToHide, @Nullable final Map<Integer, Modifier[]> statModifiers, final double baseDamageResistance, @Nullable final Map<String, Modifier[]> damageResistance, @Nullable final Map<String, Modifier[]> damageEnhancement, @Nullable final Map<String, Modifier[]> damageClassEnhancement) {
        this.armorSlot = ItemArmorSlot.Head;
        this.armorSlot = armorSlot;
        this.cosmeticsToHide = cosmeticsToHide;
        this.statModifiers = statModifiers;
        this.baseDamageResistance = baseDamageResistance;
        this.damageResistance = damageResistance;
        this.damageEnhancement = damageEnhancement;
        this.damageClassEnhancement = damageClassEnhancement;
    }
    
    public ItemArmor(@Nonnull final ItemArmor other) {
        this.armorSlot = ItemArmorSlot.Head;
        this.armorSlot = other.armorSlot;
        this.cosmeticsToHide = other.cosmeticsToHide;
        this.statModifiers = other.statModifiers;
        this.baseDamageResistance = other.baseDamageResistance;
        this.damageResistance = other.damageResistance;
        this.damageEnhancement = other.damageEnhancement;
        this.damageClassEnhancement = other.damageClassEnhancement;
    }
    
    @Nonnull
    public static ItemArmor deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ItemArmor obj = new ItemArmor();
        final byte nullBits = buf.getByte(offset);
        obj.armorSlot = ItemArmorSlot.fromValue(buf.getByte(offset + 1));
        obj.baseDamageResistance = buf.getDoubleLE(offset + 2);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 30 + buf.getIntLE(offset + 10);
            final int cosmeticsToHideCount = VarInt.peek(buf, varPos0);
            if (cosmeticsToHideCount < 0) {
                throw ProtocolException.negativeLength("CosmeticsToHide", cosmeticsToHideCount);
            }
            if (cosmeticsToHideCount > 4096000) {
                throw ProtocolException.arrayTooLong("CosmeticsToHide", cosmeticsToHideCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + cosmeticsToHideCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("CosmeticsToHide", varPos0 + varIntLen + cosmeticsToHideCount * 1, buf.readableBytes());
            }
            obj.cosmeticsToHide = new Cosmetic[cosmeticsToHideCount];
            int elemPos = varPos0 + varIntLen;
            for (int i = 0; i < cosmeticsToHideCount; ++i) {
                obj.cosmeticsToHide[i] = Cosmetic.fromValue(buf.getByte(elemPos));
                ++elemPos;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 30 + buf.getIntLE(offset + 14);
            final int statModifiersCount = VarInt.peek(buf, varPos2);
            if (statModifiersCount < 0) {
                throw ProtocolException.negativeLength("StatModifiers", statModifiersCount);
            }
            if (statModifiersCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("StatModifiers", statModifiersCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            obj.statModifiers = new HashMap<Integer, Modifier[]>(statModifiersCount);
            int dictPos = varPos2 + varIntLen;
            for (int i = 0; i < statModifiersCount; ++i) {
                final int key = buf.getIntLE(dictPos);
                dictPos += 4;
                final int valLen = VarInt.peek(buf, dictPos);
                if (valLen < 0) {
                    throw ProtocolException.negativeLength("val", valLen);
                }
                if (valLen > 64) {
                    throw ProtocolException.arrayTooLong("val", valLen, 64);
                }
                final int valVarLen = VarInt.length(buf, dictPos);
                if (dictPos + valVarLen + valLen * 6L > buf.readableBytes()) {
                    throw ProtocolException.bufferTooSmall("val", dictPos + valVarLen + valLen * 6, buf.readableBytes());
                }
                dictPos += valVarLen;
                final Modifier[] val = new Modifier[valLen];
                for (int valIdx = 0; valIdx < valLen; ++valIdx) {
                    val[valIdx] = Modifier.deserialize(buf, dictPos);
                    dictPos += Modifier.computeBytesConsumed(buf, dictPos);
                }
                if (obj.statModifiers.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("statModifiers", key);
                }
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 30 + buf.getIntLE(offset + 18);
            final int damageResistanceCount = VarInt.peek(buf, varPos3);
            if (damageResistanceCount < 0) {
                throw ProtocolException.negativeLength("DamageResistance", damageResistanceCount);
            }
            if (damageResistanceCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("DamageResistance", damageResistanceCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            obj.damageResistance = new HashMap<String, Modifier[]>(damageResistanceCount);
            int dictPos = varPos3 + varIntLen;
            for (int i = 0; i < damageResistanceCount; ++i) {
                final int keyLen = VarInt.peek(buf, dictPos);
                if (keyLen < 0) {
                    throw ProtocolException.negativeLength("key", keyLen);
                }
                if (keyLen > 4096000) {
                    throw ProtocolException.stringTooLong("key", keyLen, 4096000);
                }
                final int keyVarLen = VarInt.length(buf, dictPos);
                final String key2 = PacketIO.readVarString(buf, dictPos);
                dictPos += keyVarLen + keyLen;
                final int valLen2 = VarInt.peek(buf, dictPos);
                if (valLen2 < 0) {
                    throw ProtocolException.negativeLength("val", valLen2);
                }
                if (valLen2 > 64) {
                    throw ProtocolException.arrayTooLong("val", valLen2, 64);
                }
                final int valVarLen2 = VarInt.length(buf, dictPos);
                if (dictPos + valVarLen2 + valLen2 * 6L > buf.readableBytes()) {
                    throw ProtocolException.bufferTooSmall("val", dictPos + valVarLen2 + valLen2 * 6, buf.readableBytes());
                }
                dictPos += valVarLen2;
                final Modifier[] val2 = new Modifier[valLen2];
                for (int valIdx2 = 0; valIdx2 < valLen2; ++valIdx2) {
                    val2[valIdx2] = Modifier.deserialize(buf, dictPos);
                    dictPos += Modifier.computeBytesConsumed(buf, dictPos);
                }
                if (obj.damageResistance.put(key2, val2) != null) {
                    throw ProtocolException.duplicateKey("damageResistance", key2);
                }
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos4 = offset + 30 + buf.getIntLE(offset + 22);
            final int damageEnhancementCount = VarInt.peek(buf, varPos4);
            if (damageEnhancementCount < 0) {
                throw ProtocolException.negativeLength("DamageEnhancement", damageEnhancementCount);
            }
            if (damageEnhancementCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("DamageEnhancement", damageEnhancementCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos4);
            obj.damageEnhancement = new HashMap<String, Modifier[]>(damageEnhancementCount);
            int dictPos = varPos4 + varIntLen;
            for (int i = 0; i < damageEnhancementCount; ++i) {
                final int keyLen = VarInt.peek(buf, dictPos);
                if (keyLen < 0) {
                    throw ProtocolException.negativeLength("key", keyLen);
                }
                if (keyLen > 4096000) {
                    throw ProtocolException.stringTooLong("key", keyLen, 4096000);
                }
                final int keyVarLen = VarInt.length(buf, dictPos);
                final String key2 = PacketIO.readVarString(buf, dictPos);
                dictPos += keyVarLen + keyLen;
                final int valLen2 = VarInt.peek(buf, dictPos);
                if (valLen2 < 0) {
                    throw ProtocolException.negativeLength("val", valLen2);
                }
                if (valLen2 > 64) {
                    throw ProtocolException.arrayTooLong("val", valLen2, 64);
                }
                final int valVarLen2 = VarInt.length(buf, dictPos);
                if (dictPos + valVarLen2 + valLen2 * 6L > buf.readableBytes()) {
                    throw ProtocolException.bufferTooSmall("val", dictPos + valVarLen2 + valLen2 * 6, buf.readableBytes());
                }
                dictPos += valVarLen2;
                final Modifier[] val2 = new Modifier[valLen2];
                for (int valIdx2 = 0; valIdx2 < valLen2; ++valIdx2) {
                    val2[valIdx2] = Modifier.deserialize(buf, dictPos);
                    dictPos += Modifier.computeBytesConsumed(buf, dictPos);
                }
                if (obj.damageEnhancement.put(key2, val2) != null) {
                    throw ProtocolException.duplicateKey("damageEnhancement", key2);
                }
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos5 = offset + 30 + buf.getIntLE(offset + 26);
            final int damageClassEnhancementCount = VarInt.peek(buf, varPos5);
            if (damageClassEnhancementCount < 0) {
                throw ProtocolException.negativeLength("DamageClassEnhancement", damageClassEnhancementCount);
            }
            if (damageClassEnhancementCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("DamageClassEnhancement", damageClassEnhancementCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos5);
            obj.damageClassEnhancement = new HashMap<String, Modifier[]>(damageClassEnhancementCount);
            int dictPos = varPos5 + varIntLen;
            for (int i = 0; i < damageClassEnhancementCount; ++i) {
                final int keyLen = VarInt.peek(buf, dictPos);
                if (keyLen < 0) {
                    throw ProtocolException.negativeLength("key", keyLen);
                }
                if (keyLen > 4096000) {
                    throw ProtocolException.stringTooLong("key", keyLen, 4096000);
                }
                final int keyVarLen = VarInt.length(buf, dictPos);
                final String key2 = PacketIO.readVarString(buf, dictPos);
                dictPos += keyVarLen + keyLen;
                final int valLen2 = VarInt.peek(buf, dictPos);
                if (valLen2 < 0) {
                    throw ProtocolException.negativeLength("val", valLen2);
                }
                if (valLen2 > 64) {
                    throw ProtocolException.arrayTooLong("val", valLen2, 64);
                }
                final int valVarLen2 = VarInt.length(buf, dictPos);
                if (dictPos + valVarLen2 + valLen2 * 6L > buf.readableBytes()) {
                    throw ProtocolException.bufferTooSmall("val", dictPos + valVarLen2 + valLen2 * 6, buf.readableBytes());
                }
                dictPos += valVarLen2;
                final Modifier[] val2 = new Modifier[valLen2];
                for (int valIdx2 = 0; valIdx2 < valLen2; ++valIdx2) {
                    val2[valIdx2] = Modifier.deserialize(buf, dictPos);
                    dictPos += Modifier.computeBytesConsumed(buf, dictPos);
                }
                if (obj.damageClassEnhancement.put(key2, val2) != null) {
                    throw ProtocolException.duplicateKey("damageClassEnhancement", key2);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 30;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 10);
            int pos0 = offset + 30 + fieldOffset0;
            final int arrLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + arrLen * 1;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 14);
            int pos2 = offset + 30 + fieldOffset2;
            final int dictLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < dictLen; ++i) {
                pos2 += 4;
                final int al = VarInt.peek(buf, pos2);
                pos2 += VarInt.length(buf, pos2);
                for (int j = 0; j < al; ++j) {
                    pos2 += Modifier.computeBytesConsumed(buf, pos2);
                }
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 18);
            int pos3 = offset + 30 + fieldOffset3;
            final int dictLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3);
            for (int i = 0; i < dictLen; ++i) {
                final int sl = VarInt.peek(buf, pos3);
                pos3 += VarInt.length(buf, pos3) + sl;
                final int al = VarInt.peek(buf, pos3);
                pos3 += VarInt.length(buf, pos3);
                for (int j = 0; j < al; ++j) {
                    pos3 += Modifier.computeBytesConsumed(buf, pos3);
                }
            }
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 22);
            int pos4 = offset + 30 + fieldOffset4;
            final int dictLen = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4);
            for (int i = 0; i < dictLen; ++i) {
                final int sl = VarInt.peek(buf, pos4);
                pos4 += VarInt.length(buf, pos4) + sl;
                final int al = VarInt.peek(buf, pos4);
                pos4 += VarInt.length(buf, pos4);
                for (int j = 0; j < al; ++j) {
                    pos4 += Modifier.computeBytesConsumed(buf, pos4);
                }
            }
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset5 = buf.getIntLE(offset + 26);
            int pos5 = offset + 30 + fieldOffset5;
            final int dictLen = VarInt.peek(buf, pos5);
            pos5 += VarInt.length(buf, pos5);
            for (int i = 0; i < dictLen; ++i) {
                final int sl = VarInt.peek(buf, pos5);
                pos5 += VarInt.length(buf, pos5) + sl;
                final int al = VarInt.peek(buf, pos5);
                pos5 += VarInt.length(buf, pos5);
                for (int j = 0; j < al; ++j) {
                    pos5 += Modifier.computeBytesConsumed(buf, pos5);
                }
            }
            if (pos5 - offset > maxEnd) {
                maxEnd = pos5 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.cosmeticsToHide != null) {
            nullBits |= 0x1;
        }
        if (this.statModifiers != null) {
            nullBits |= 0x2;
        }
        if (this.damageResistance != null) {
            nullBits |= 0x4;
        }
        if (this.damageEnhancement != null) {
            nullBits |= 0x8;
        }
        if (this.damageClassEnhancement != null) {
            nullBits |= 0x10;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.armorSlot.getValue());
        buf.writeDoubleLE(this.baseDamageResistance);
        final int cosmeticsToHideOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int statModifiersOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int damageResistanceOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int damageEnhancementOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int damageClassEnhancementOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.cosmeticsToHide != null) {
            buf.setIntLE(cosmeticsToHideOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.cosmeticsToHide.length > 4096000) {
                throw ProtocolException.arrayTooLong("CosmeticsToHide", this.cosmeticsToHide.length, 4096000);
            }
            VarInt.write(buf, this.cosmeticsToHide.length);
            for (final Cosmetic item : this.cosmeticsToHide) {
                buf.writeByte(item.getValue());
            }
        }
        else {
            buf.setIntLE(cosmeticsToHideOffsetSlot, -1);
        }
        if (this.statModifiers != null) {
            buf.setIntLE(statModifiersOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.statModifiers.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("StatModifiers", this.statModifiers.size(), 4096000);
            }
            VarInt.write(buf, this.statModifiers.size());
            for (final Map.Entry<Integer, Modifier[]> e : this.statModifiers.entrySet()) {
                buf.writeIntLE(e.getKey());
                VarInt.write(buf, e.getValue().length);
                for (final Modifier arrItem : e.getValue()) {
                    arrItem.serialize(buf);
                }
            }
        }
        else {
            buf.setIntLE(statModifiersOffsetSlot, -1);
        }
        if (this.damageResistance != null) {
            buf.setIntLE(damageResistanceOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.damageResistance.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("DamageResistance", this.damageResistance.size(), 4096000);
            }
            VarInt.write(buf, this.damageResistance.size());
            for (final Map.Entry<String, Modifier[]> e2 : this.damageResistance.entrySet()) {
                PacketIO.writeVarString(buf, e2.getKey(), 4096000);
                VarInt.write(buf, e2.getValue().length);
                for (final Modifier arrItem : e2.getValue()) {
                    arrItem.serialize(buf);
                }
            }
        }
        else {
            buf.setIntLE(damageResistanceOffsetSlot, -1);
        }
        if (this.damageEnhancement != null) {
            buf.setIntLE(damageEnhancementOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.damageEnhancement.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("DamageEnhancement", this.damageEnhancement.size(), 4096000);
            }
            VarInt.write(buf, this.damageEnhancement.size());
            for (final Map.Entry<String, Modifier[]> e2 : this.damageEnhancement.entrySet()) {
                PacketIO.writeVarString(buf, e2.getKey(), 4096000);
                VarInt.write(buf, e2.getValue().length);
                for (final Modifier arrItem : e2.getValue()) {
                    arrItem.serialize(buf);
                }
            }
        }
        else {
            buf.setIntLE(damageEnhancementOffsetSlot, -1);
        }
        if (this.damageClassEnhancement != null) {
            buf.setIntLE(damageClassEnhancementOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.damageClassEnhancement.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("DamageClassEnhancement", this.damageClassEnhancement.size(), 4096000);
            }
            VarInt.write(buf, this.damageClassEnhancement.size());
            for (final Map.Entry<String, Modifier[]> e2 : this.damageClassEnhancement.entrySet()) {
                PacketIO.writeVarString(buf, e2.getKey(), 4096000);
                VarInt.write(buf, e2.getValue().length);
                for (final Modifier arrItem : e2.getValue()) {
                    arrItem.serialize(buf);
                }
            }
        }
        else {
            buf.setIntLE(damageClassEnhancementOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 30;
        if (this.cosmeticsToHide != null) {
            size += VarInt.size(this.cosmeticsToHide.length) + this.cosmeticsToHide.length * 1;
        }
        if (this.statModifiers != null) {
            int statModifiersSize = 0;
            for (final Map.Entry<Integer, Modifier[]> kvp : this.statModifiers.entrySet()) {
                statModifiersSize += 4 + VarInt.size(kvp.getValue().length) + kvp.getValue().length * 6;
            }
            size += VarInt.size(this.statModifiers.size()) + statModifiersSize;
        }
        if (this.damageResistance != null) {
            int damageResistanceSize = 0;
            for (final Map.Entry<String, Modifier[]> kvp2 : this.damageResistance.entrySet()) {
                damageResistanceSize += PacketIO.stringSize(kvp2.getKey()) + VarInt.size(kvp2.getValue().length) + kvp2.getValue().length * 6;
            }
            size += VarInt.size(this.damageResistance.size()) + damageResistanceSize;
        }
        if (this.damageEnhancement != null) {
            int damageEnhancementSize = 0;
            for (final Map.Entry<String, Modifier[]> kvp2 : this.damageEnhancement.entrySet()) {
                damageEnhancementSize += PacketIO.stringSize(kvp2.getKey()) + VarInt.size(kvp2.getValue().length) + kvp2.getValue().length * 6;
            }
            size += VarInt.size(this.damageEnhancement.size()) + damageEnhancementSize;
        }
        if (this.damageClassEnhancement != null) {
            int damageClassEnhancementSize = 0;
            for (final Map.Entry<String, Modifier[]> kvp2 : this.damageClassEnhancement.entrySet()) {
                damageClassEnhancementSize += PacketIO.stringSize(kvp2.getKey()) + VarInt.size(kvp2.getValue().length) + kvp2.getValue().length * 6;
            }
            size += VarInt.size(this.damageClassEnhancement.size()) + damageClassEnhancementSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 30) {
            return ValidationResult.error("Buffer too small: expected at least 30 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int cosmeticsToHideOffset = buffer.getIntLE(offset + 10);
            if (cosmeticsToHideOffset < 0) {
                return ValidationResult.error("Invalid offset for CosmeticsToHide");
            }
            int pos = offset + 30 + cosmeticsToHideOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for CosmeticsToHide");
            }
            final int cosmeticsToHideCount = VarInt.peek(buffer, pos);
            if (cosmeticsToHideCount < 0) {
                return ValidationResult.error("Invalid array count for CosmeticsToHide");
            }
            if (cosmeticsToHideCount > 4096000) {
                return ValidationResult.error("CosmeticsToHide exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += cosmeticsToHideCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading CosmeticsToHide");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int statModifiersOffset = buffer.getIntLE(offset + 14);
            if (statModifiersOffset < 0) {
                return ValidationResult.error("Invalid offset for StatModifiers");
            }
            int pos = offset + 30 + statModifiersOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for StatModifiers");
            }
            final int statModifiersCount = VarInt.peek(buffer, pos);
            if (statModifiersCount < 0) {
                return ValidationResult.error("Invalid dictionary count for StatModifiers");
            }
            if (statModifiersCount > 4096000) {
                return ValidationResult.error("StatModifiers exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < statModifiersCount; ++i) {
                pos += 4;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                final int valueArrCount = VarInt.peek(buffer, pos);
                if (valueArrCount < 0) {
                    return ValidationResult.error("Invalid array count for value");
                }
                pos += VarInt.length(buffer, pos);
                for (int valueArrIdx = 0; valueArrIdx < valueArrCount; ++valueArrIdx) {
                    pos += 6;
                }
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int damageResistanceOffset = buffer.getIntLE(offset + 18);
            if (damageResistanceOffset < 0) {
                return ValidationResult.error("Invalid offset for DamageResistance");
            }
            int pos = offset + 30 + damageResistanceOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for DamageResistance");
            }
            final int damageResistanceCount = VarInt.peek(buffer, pos);
            if (damageResistanceCount < 0) {
                return ValidationResult.error("Invalid dictionary count for DamageResistance");
            }
            if (damageResistanceCount > 4096000) {
                return ValidationResult.error("DamageResistance exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < damageResistanceCount; ++i) {
                final int keyLen = VarInt.peek(buffer, pos);
                if (keyLen < 0) {
                    return ValidationResult.error("Invalid string length for key");
                }
                if (keyLen > 4096000) {
                    return ValidationResult.error("key exceeds max length 4096000");
                }
                pos += VarInt.length(buffer, pos);
                pos += keyLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                final int valueArrCount2 = VarInt.peek(buffer, pos);
                if (valueArrCount2 < 0) {
                    return ValidationResult.error("Invalid array count for value");
                }
                pos += VarInt.length(buffer, pos);
                for (int valueArrIdx2 = 0; valueArrIdx2 < valueArrCount2; ++valueArrIdx2) {
                    pos += 6;
                }
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int damageEnhancementOffset = buffer.getIntLE(offset + 22);
            if (damageEnhancementOffset < 0) {
                return ValidationResult.error("Invalid offset for DamageEnhancement");
            }
            int pos = offset + 30 + damageEnhancementOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for DamageEnhancement");
            }
            final int damageEnhancementCount = VarInt.peek(buffer, pos);
            if (damageEnhancementCount < 0) {
                return ValidationResult.error("Invalid dictionary count for DamageEnhancement");
            }
            if (damageEnhancementCount > 4096000) {
                return ValidationResult.error("DamageEnhancement exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < damageEnhancementCount; ++i) {
                final int keyLen = VarInt.peek(buffer, pos);
                if (keyLen < 0) {
                    return ValidationResult.error("Invalid string length for key");
                }
                if (keyLen > 4096000) {
                    return ValidationResult.error("key exceeds max length 4096000");
                }
                pos += VarInt.length(buffer, pos);
                pos += keyLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                final int valueArrCount2 = VarInt.peek(buffer, pos);
                if (valueArrCount2 < 0) {
                    return ValidationResult.error("Invalid array count for value");
                }
                pos += VarInt.length(buffer, pos);
                for (int valueArrIdx2 = 0; valueArrIdx2 < valueArrCount2; ++valueArrIdx2) {
                    pos += 6;
                }
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int damageClassEnhancementOffset = buffer.getIntLE(offset + 26);
            if (damageClassEnhancementOffset < 0) {
                return ValidationResult.error("Invalid offset for DamageClassEnhancement");
            }
            int pos = offset + 30 + damageClassEnhancementOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for DamageClassEnhancement");
            }
            final int damageClassEnhancementCount = VarInt.peek(buffer, pos);
            if (damageClassEnhancementCount < 0) {
                return ValidationResult.error("Invalid dictionary count for DamageClassEnhancement");
            }
            if (damageClassEnhancementCount > 4096000) {
                return ValidationResult.error("DamageClassEnhancement exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < damageClassEnhancementCount; ++i) {
                final int keyLen = VarInt.peek(buffer, pos);
                if (keyLen < 0) {
                    return ValidationResult.error("Invalid string length for key");
                }
                if (keyLen > 4096000) {
                    return ValidationResult.error("key exceeds max length 4096000");
                }
                pos += VarInt.length(buffer, pos);
                pos += keyLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                final int valueArrCount2 = VarInt.peek(buffer, pos);
                if (valueArrCount2 < 0) {
                    return ValidationResult.error("Invalid array count for value");
                }
                pos += VarInt.length(buffer, pos);
                for (int valueArrIdx2 = 0; valueArrIdx2 < valueArrCount2; ++valueArrIdx2) {
                    pos += 6;
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public ItemArmor clone() {
        final ItemArmor copy = new ItemArmor();
        copy.armorSlot = this.armorSlot;
        copy.cosmeticsToHide = (Cosmetic[])((this.cosmeticsToHide != null) ? ((Cosmetic[])Arrays.copyOf(this.cosmeticsToHide, this.cosmeticsToHide.length)) : null);
        if (this.statModifiers != null) {
            final Map<Integer, Modifier[]> m = new HashMap<Integer, Modifier[]>();
            for (final Map.Entry<Integer, Modifier[]> e : this.statModifiers.entrySet()) {
                m.put(e.getKey(), Arrays.stream(e.getValue()).map(x -> x.clone()).toArray(Modifier[]::new));
            }
            copy.statModifiers = m;
        }
        copy.baseDamageResistance = this.baseDamageResistance;
        if (this.damageResistance != null) {
            final Map<String, Modifier[]> i = new HashMap<String, Modifier[]>();
            for (final Map.Entry<String, Modifier[]> e2 : this.damageResistance.entrySet()) {
                i.put(e2.getKey(), Arrays.stream(e2.getValue()).map(x -> x.clone()).toArray(Modifier[]::new));
            }
            copy.damageResistance = i;
        }
        if (this.damageEnhancement != null) {
            final Map<String, Modifier[]> i = new HashMap<String, Modifier[]>();
            for (final Map.Entry<String, Modifier[]> e2 : this.damageEnhancement.entrySet()) {
                i.put(e2.getKey(), Arrays.stream(e2.getValue()).map(x -> x.clone()).toArray(Modifier[]::new));
            }
            copy.damageEnhancement = i;
        }
        if (this.damageClassEnhancement != null) {
            final Map<String, Modifier[]> i = new HashMap<String, Modifier[]>();
            for (final Map.Entry<String, Modifier[]> e2 : this.damageClassEnhancement.entrySet()) {
                i.put(e2.getKey(), Arrays.stream(e2.getValue()).map(x -> x.clone()).toArray(Modifier[]::new));
            }
            copy.damageClassEnhancement = i;
        }
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ItemArmor other) {
            return Objects.equals(this.armorSlot, other.armorSlot) && Arrays.equals(this.cosmeticsToHide, other.cosmeticsToHide) && Objects.equals(this.statModifiers, other.statModifiers) && this.baseDamageResistance == other.baseDamageResistance && Objects.equals(this.damageResistance, other.damageResistance) && Objects.equals(this.damageEnhancement, other.damageEnhancement) && Objects.equals(this.damageClassEnhancement, other.damageClassEnhancement);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.armorSlot);
        result = 31 * result + Arrays.hashCode(this.cosmeticsToHide);
        result = 31 * result + Objects.hashCode(this.statModifiers);
        result = 31 * result + Double.hashCode(this.baseDamageResistance);
        result = 31 * result + Objects.hashCode(this.damageResistance);
        result = 31 * result + Objects.hashCode(this.damageEnhancement);
        result = 31 * result + Objects.hashCode(this.damageClassEnhancement);
        return result;
    }
}
