// 
// 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 java.util.HashMap;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import com.hypixel.hytale.protocol.io.PacketIO;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import java.util.Map;
import javax.annotation.Nullable;

public class DamageEntityInteraction extends Interaction
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 2;
    public static final int FIXED_BLOCK_SIZE = 24;
    public static final int VARIABLE_FIELD_COUNT = 9;
    public static final int VARIABLE_BLOCK_START = 60;
    public static final int MAX_SIZE = 1677721600;
    public int next;
    public int failed;
    public int blocked;
    @Nullable
    public DamageEffects damageEffects;
    @Nullable
    public AngledDamage[] angledDamage;
    @Nullable
    public Map<String, TargetedDamage> targetedDamage;
    @Nullable
    public EntityStatOnHit[] entityStatsOnHit;
    
    public DamageEntityInteraction() {
        this.next = Integer.MIN_VALUE;
        this.failed = Integer.MIN_VALUE;
        this.blocked = Integer.MIN_VALUE;
    }
    
    public DamageEntityInteraction(@Nonnull final WaitForDataFrom waitForDataFrom, @Nullable final InteractionEffects effects, final float horizontalSpeedMultiplier, final float runTime, final boolean cancelOnItemChange, @Nullable final Map<GameMode, InteractionSettings> settings, @Nullable final InteractionRules rules, @Nullable final int[] tags, @Nullable final InteractionCameraSettings camera, final int next, final int failed, final int blocked, @Nullable final DamageEffects damageEffects, @Nullable final AngledDamage[] angledDamage, @Nullable final Map<String, TargetedDamage> targetedDamage, @Nullable final EntityStatOnHit[] entityStatsOnHit) {
        this.next = Integer.MIN_VALUE;
        this.failed = Integer.MIN_VALUE;
        this.blocked = Integer.MIN_VALUE;
        this.waitForDataFrom = waitForDataFrom;
        this.effects = effects;
        this.horizontalSpeedMultiplier = horizontalSpeedMultiplier;
        this.runTime = runTime;
        this.cancelOnItemChange = cancelOnItemChange;
        this.settings = settings;
        this.rules = rules;
        this.tags = tags;
        this.camera = camera;
        this.next = next;
        this.failed = failed;
        this.blocked = blocked;
        this.damageEffects = damageEffects;
        this.angledDamage = angledDamage;
        this.targetedDamage = targetedDamage;
        this.entityStatsOnHit = entityStatsOnHit;
    }
    
    public DamageEntityInteraction(@Nonnull final DamageEntityInteraction other) {
        this.next = Integer.MIN_VALUE;
        this.failed = Integer.MIN_VALUE;
        this.blocked = Integer.MIN_VALUE;
        this.waitForDataFrom = other.waitForDataFrom;
        this.effects = other.effects;
        this.horizontalSpeedMultiplier = other.horizontalSpeedMultiplier;
        this.runTime = other.runTime;
        this.cancelOnItemChange = other.cancelOnItemChange;
        this.settings = other.settings;
        this.rules = other.rules;
        this.tags = other.tags;
        this.camera = other.camera;
        this.next = other.next;
        this.failed = other.failed;
        this.blocked = other.blocked;
        this.damageEffects = other.damageEffects;
        this.angledDamage = other.angledDamage;
        this.targetedDamage = other.targetedDamage;
        this.entityStatsOnHit = other.entityStatsOnHit;
    }
    
    @Nonnull
    public static DamageEntityInteraction deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final DamageEntityInteraction obj = new DamageEntityInteraction();
        final byte[] nullBits = PacketIO.readBytes(buf, offset, 2);
        obj.waitForDataFrom = WaitForDataFrom.fromValue(buf.getByte(offset + 2));
        obj.horizontalSpeedMultiplier = buf.getFloatLE(offset + 3);
        obj.runTime = buf.getFloatLE(offset + 7);
        obj.cancelOnItemChange = (buf.getByte(offset + 11) != 0);
        obj.next = buf.getIntLE(offset + 12);
        obj.failed = buf.getIntLE(offset + 16);
        obj.blocked = buf.getIntLE(offset + 20);
        if ((nullBits[0] & 0x1) != 0x0) {
            final int varPos0 = offset + 60 + buf.getIntLE(offset + 24);
            obj.effects = InteractionEffects.deserialize(buf, varPos0);
        }
        if ((nullBits[0] & 0x2) != 0x0) {
            final int varPos2 = offset + 60 + buf.getIntLE(offset + 28);
            final int settingsCount = VarInt.peek(buf, varPos2);
            if (settingsCount < 0) {
                throw ProtocolException.negativeLength("Settings", settingsCount);
            }
            if (settingsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Settings", settingsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            obj.settings = new HashMap<GameMode, InteractionSettings>(settingsCount);
            int dictPos = varPos2 + varIntLen;
            for (int i = 0; i < settingsCount; ++i) {
                final GameMode key = GameMode.fromValue(buf.getByte(dictPos));
                ++dictPos;
                final InteractionSettings val = InteractionSettings.deserialize(buf, dictPos);
                dictPos += InteractionSettings.computeBytesConsumed(buf, dictPos);
                if (obj.settings.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("settings", key);
                }
            }
        }
        if ((nullBits[0] & 0x4) != 0x0) {
            final int varPos3 = offset + 60 + buf.getIntLE(offset + 32);
            obj.rules = InteractionRules.deserialize(buf, varPos3);
        }
        if ((nullBits[0] & 0x8) != 0x0) {
            final int varPos4 = offset + 60 + buf.getIntLE(offset + 36);
            final int tagsCount = VarInt.peek(buf, varPos4);
            if (tagsCount < 0) {
                throw ProtocolException.negativeLength("Tags", tagsCount);
            }
            if (tagsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Tags", tagsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos4);
            if (varPos4 + varIntLen + tagsCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Tags", varPos4 + varIntLen + tagsCount * 4, buf.readableBytes());
            }
            obj.tags = new int[tagsCount];
            for (int j = 0; j < tagsCount; ++j) {
                obj.tags[j] = buf.getIntLE(varPos4 + varIntLen + j * 4);
            }
        }
        if ((nullBits[0] & 0x10) != 0x0) {
            final int varPos5 = offset + 60 + buf.getIntLE(offset + 40);
            obj.camera = InteractionCameraSettings.deserialize(buf, varPos5);
        }
        if ((nullBits[0] & 0x20) != 0x0) {
            final int varPos6 = offset + 60 + buf.getIntLE(offset + 44);
            obj.damageEffects = DamageEffects.deserialize(buf, varPos6);
        }
        if ((nullBits[0] & 0x40) != 0x0) {
            final int varPos7 = offset + 60 + buf.getIntLE(offset + 48);
            final int angledDamageCount = VarInt.peek(buf, varPos7);
            if (angledDamageCount < 0) {
                throw ProtocolException.negativeLength("AngledDamage", angledDamageCount);
            }
            if (angledDamageCount > 4096000) {
                throw ProtocolException.arrayTooLong("AngledDamage", angledDamageCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos7);
            if (varPos7 + varIntLen + angledDamageCount * 21L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("AngledDamage", varPos7 + varIntLen + angledDamageCount * 21, buf.readableBytes());
            }
            obj.angledDamage = new AngledDamage[angledDamageCount];
            int elemPos = varPos7 + varIntLen;
            for (int i = 0; i < angledDamageCount; ++i) {
                obj.angledDamage[i] = AngledDamage.deserialize(buf, elemPos);
                elemPos += AngledDamage.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits[0] & 0x80) != 0x0) {
            final int varPos8 = offset + 60 + buf.getIntLE(offset + 52);
            final int targetedDamageCount = VarInt.peek(buf, varPos8);
            if (targetedDamageCount < 0) {
                throw ProtocolException.negativeLength("TargetedDamage", targetedDamageCount);
            }
            if (targetedDamageCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("TargetedDamage", targetedDamageCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos8);
            obj.targetedDamage = new HashMap<String, TargetedDamage>(targetedDamageCount);
            int dictPos = varPos8 + varIntLen;
            for (int i = 0; i < targetedDamageCount; ++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 TargetedDamage val2 = TargetedDamage.deserialize(buf, dictPos);
                dictPos += TargetedDamage.computeBytesConsumed(buf, dictPos);
                if (obj.targetedDamage.put(key2, val2) != null) {
                    throw ProtocolException.duplicateKey("targetedDamage", key2);
                }
            }
        }
        if ((nullBits[1] & 0x1) != 0x0) {
            final int varPos9 = offset + 60 + buf.getIntLE(offset + 56);
            final int entityStatsOnHitCount = VarInt.peek(buf, varPos9);
            if (entityStatsOnHitCount < 0) {
                throw ProtocolException.negativeLength("EntityStatsOnHit", entityStatsOnHitCount);
            }
            if (entityStatsOnHitCount > 4096000) {
                throw ProtocolException.arrayTooLong("EntityStatsOnHit", entityStatsOnHitCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos9);
            if (varPos9 + varIntLen + entityStatsOnHitCount * 13L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("EntityStatsOnHit", varPos9 + varIntLen + entityStatsOnHitCount * 13, buf.readableBytes());
            }
            obj.entityStatsOnHit = new EntityStatOnHit[entityStatsOnHitCount];
            int elemPos = varPos9 + varIntLen;
            for (int i = 0; i < entityStatsOnHitCount; ++i) {
                obj.entityStatsOnHit[i] = EntityStatOnHit.deserialize(buf, elemPos);
                elemPos += EntityStatOnHit.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte[] nullBits = PacketIO.readBytes(buf, offset, 2);
        int maxEnd = 60;
        if ((nullBits[0] & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 24);
            int pos0 = offset + 60 + fieldOffset0;
            pos0 += InteractionEffects.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits[0] & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 28);
            int pos2 = offset + 60 + fieldOffset2;
            final int dictLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < dictLen; ++i) {
                pos2 = ++pos2 + InteractionSettings.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits[0] & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 32);
            int pos3 = offset + 60 + fieldOffset3;
            pos3 += InteractionRules.computeBytesConsumed(buf, pos3);
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits[0] & 0x8) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 36);
            int pos4 = offset + 60 + fieldOffset4;
            final int arrLen = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4) + arrLen * 4;
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        if ((nullBits[0] & 0x10) != 0x0) {
            final int fieldOffset5 = buf.getIntLE(offset + 40);
            int pos5 = offset + 60 + fieldOffset5;
            pos5 += InteractionCameraSettings.computeBytesConsumed(buf, pos5);
            if (pos5 - offset > maxEnd) {
                maxEnd = pos5 - offset;
            }
        }
        if ((nullBits[0] & 0x20) != 0x0) {
            final int fieldOffset6 = buf.getIntLE(offset + 44);
            int pos6 = offset + 60 + fieldOffset6;
            pos6 += DamageEffects.computeBytesConsumed(buf, pos6);
            if (pos6 - offset > maxEnd) {
                maxEnd = pos6 - offset;
            }
        }
        if ((nullBits[0] & 0x40) != 0x0) {
            final int fieldOffset7 = buf.getIntLE(offset + 48);
            int pos7 = offset + 60 + fieldOffset7;
            final int arrLen = VarInt.peek(buf, pos7);
            pos7 += VarInt.length(buf, pos7);
            for (int i = 0; i < arrLen; ++i) {
                pos7 += AngledDamage.computeBytesConsumed(buf, pos7);
            }
            if (pos7 - offset > maxEnd) {
                maxEnd = pos7 - offset;
            }
        }
        if ((nullBits[0] & 0x80) != 0x0) {
            final int fieldOffset8 = buf.getIntLE(offset + 52);
            int pos8 = offset + 60 + fieldOffset8;
            final int dictLen = VarInt.peek(buf, pos8);
            pos8 += VarInt.length(buf, pos8);
            for (int i = 0; i < dictLen; ++i) {
                final int sl = VarInt.peek(buf, pos8);
                pos8 += VarInt.length(buf, pos8) + sl;
                pos8 += TargetedDamage.computeBytesConsumed(buf, pos8);
            }
            if (pos8 - offset > maxEnd) {
                maxEnd = pos8 - offset;
            }
        }
        if ((nullBits[1] & 0x1) != 0x0) {
            final int fieldOffset9 = buf.getIntLE(offset + 56);
            int pos9 = offset + 60 + fieldOffset9;
            final int arrLen = VarInt.peek(buf, pos9);
            pos9 += VarInt.length(buf, pos9);
            for (int i = 0; i < arrLen; ++i) {
                pos9 += EntityStatOnHit.computeBytesConsumed(buf, pos9);
            }
            if (pos9 - offset > maxEnd) {
                maxEnd = pos9 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public int serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        final byte[] nullBits = new byte[2];
        if (this.effects != null) {
            final byte[] array = nullBits;
            final int n = 0;
            array[n] |= 0x1;
        }
        if (this.settings != null) {
            final byte[] array2 = nullBits;
            final int n2 = 0;
            array2[n2] |= 0x2;
        }
        if (this.rules != null) {
            final byte[] array3 = nullBits;
            final int n3 = 0;
            array3[n3] |= 0x4;
        }
        if (this.tags != null) {
            final byte[] array4 = nullBits;
            final int n4 = 0;
            array4[n4] |= 0x8;
        }
        if (this.camera != null) {
            final byte[] array5 = nullBits;
            final int n5 = 0;
            array5[n5] |= 0x10;
        }
        if (this.damageEffects != null) {
            final byte[] array6 = nullBits;
            final int n6 = 0;
            array6[n6] |= 0x20;
        }
        if (this.angledDamage != null) {
            final byte[] array7 = nullBits;
            final int n7 = 0;
            array7[n7] |= 0x40;
        }
        if (this.targetedDamage != null) {
            final byte[] array8 = nullBits;
            final int n8 = 0;
            array8[n8] |= (byte)128;
        }
        if (this.entityStatsOnHit != null) {
            final byte[] array9 = nullBits;
            final int n9 = 1;
            array9[n9] |= 0x1;
        }
        buf.writeBytes(nullBits);
        buf.writeByte(this.waitForDataFrom.getValue());
        buf.writeFloatLE(this.horizontalSpeedMultiplier);
        buf.writeFloatLE(this.runTime);
        buf.writeByte(this.cancelOnItemChange ? 1 : 0);
        buf.writeIntLE(this.next);
        buf.writeIntLE(this.failed);
        buf.writeIntLE(this.blocked);
        final int effectsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int settingsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int rulesOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int tagsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int cameraOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int damageEffectsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int angledDamageOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int targetedDamageOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int entityStatsOnHitOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.effects != null) {
            buf.setIntLE(effectsOffsetSlot, buf.writerIndex() - varBlockStart);
            this.effects.serialize(buf);
        }
        else {
            buf.setIntLE(effectsOffsetSlot, -1);
        }
        if (this.settings != null) {
            buf.setIntLE(settingsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.settings.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Settings", this.settings.size(), 4096000);
            }
            VarInt.write(buf, this.settings.size());
            for (final Map.Entry<GameMode, InteractionSettings> e : this.settings.entrySet()) {
                buf.writeByte(e.getKey().getValue());
                e.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(settingsOffsetSlot, -1);
        }
        if (this.rules != null) {
            buf.setIntLE(rulesOffsetSlot, buf.writerIndex() - varBlockStart);
            this.rules.serialize(buf);
        }
        else {
            buf.setIntLE(rulesOffsetSlot, -1);
        }
        if (this.tags != null) {
            buf.setIntLE(tagsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.tags.length > 4096000) {
                throw ProtocolException.arrayTooLong("Tags", this.tags.length, 4096000);
            }
            VarInt.write(buf, this.tags.length);
            for (final int item : this.tags) {
                buf.writeIntLE(item);
            }
        }
        else {
            buf.setIntLE(tagsOffsetSlot, -1);
        }
        if (this.camera != null) {
            buf.setIntLE(cameraOffsetSlot, buf.writerIndex() - varBlockStart);
            this.camera.serialize(buf);
        }
        else {
            buf.setIntLE(cameraOffsetSlot, -1);
        }
        if (this.damageEffects != null) {
            buf.setIntLE(damageEffectsOffsetSlot, buf.writerIndex() - varBlockStart);
            this.damageEffects.serialize(buf);
        }
        else {
            buf.setIntLE(damageEffectsOffsetSlot, -1);
        }
        if (this.angledDamage != null) {
            buf.setIntLE(angledDamageOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.angledDamage.length > 4096000) {
                throw ProtocolException.arrayTooLong("AngledDamage", this.angledDamage.length, 4096000);
            }
            VarInt.write(buf, this.angledDamage.length);
            for (final AngledDamage item2 : this.angledDamage) {
                item2.serialize(buf);
            }
        }
        else {
            buf.setIntLE(angledDamageOffsetSlot, -1);
        }
        if (this.targetedDamage != null) {
            buf.setIntLE(targetedDamageOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.targetedDamage.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("TargetedDamage", this.targetedDamage.size(), 4096000);
            }
            VarInt.write(buf, this.targetedDamage.size());
            for (final Map.Entry<String, TargetedDamage> e2 : this.targetedDamage.entrySet()) {
                PacketIO.writeVarString(buf, e2.getKey(), 4096000);
                e2.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(targetedDamageOffsetSlot, -1);
        }
        if (this.entityStatsOnHit != null) {
            buf.setIntLE(entityStatsOnHitOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.entityStatsOnHit.length > 4096000) {
                throw ProtocolException.arrayTooLong("EntityStatsOnHit", this.entityStatsOnHit.length, 4096000);
            }
            VarInt.write(buf, this.entityStatsOnHit.length);
            for (final EntityStatOnHit item3 : this.entityStatsOnHit) {
                item3.serialize(buf);
            }
        }
        else {
            buf.setIntLE(entityStatsOnHitOffsetSlot, -1);
        }
        return buf.writerIndex() - startPos;
    }
    
    @Override
    public int computeSize() {
        int size = 60;
        if (this.effects != null) {
            size += this.effects.computeSize();
        }
        if (this.settings != null) {
            size += VarInt.size(this.settings.size()) + this.settings.size() * 2;
        }
        if (this.rules != null) {
            size += this.rules.computeSize();
        }
        if (this.tags != null) {
            size += VarInt.size(this.tags.length) + this.tags.length * 4;
        }
        if (this.camera != null) {
            size += this.camera.computeSize();
        }
        if (this.damageEffects != null) {
            size += this.damageEffects.computeSize();
        }
        if (this.angledDamage != null) {
            int angledDamageSize = 0;
            for (final AngledDamage elem : this.angledDamage) {
                angledDamageSize += elem.computeSize();
            }
            size += VarInt.size(this.angledDamage.length) + angledDamageSize;
        }
        if (this.targetedDamage != null) {
            int targetedDamageSize = 0;
            for (final Map.Entry<String, TargetedDamage> kvp : this.targetedDamage.entrySet()) {
                targetedDamageSize += PacketIO.stringSize(kvp.getKey()) + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.targetedDamage.size()) + targetedDamageSize;
        }
        if (this.entityStatsOnHit != null) {
            int entityStatsOnHitSize = 0;
            for (final EntityStatOnHit elem2 : this.entityStatsOnHit) {
                entityStatsOnHitSize += elem2.computeSize();
            }
            size += VarInt.size(this.entityStatsOnHit.length) + entityStatsOnHitSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 60) {
            return ValidationResult.error("Buffer too small: expected at least 60 bytes");
        }
        final byte[] nullBits = PacketIO.readBytes(buffer, offset, 2);
        if ((nullBits[0] & 0x1) != 0x0) {
            final int effectsOffset = buffer.getIntLE(offset + 24);
            if (effectsOffset < 0) {
                return ValidationResult.error("Invalid offset for Effects");
            }
            int pos = offset + 60 + effectsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Effects");
            }
            final ValidationResult effectsResult = InteractionEffects.validateStructure(buffer, pos);
            if (!effectsResult.isValid()) {
                return ValidationResult.error("Invalid Effects: " + effectsResult.error());
            }
            pos += InteractionEffects.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits[0] & 0x2) != 0x0) {
            final int settingsOffset = buffer.getIntLE(offset + 28);
            if (settingsOffset < 0) {
                return ValidationResult.error("Invalid offset for Settings");
            }
            int pos = offset + 60 + settingsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Settings");
            }
            final int settingsCount = VarInt.peek(buffer, pos);
            if (settingsCount < 0) {
                return ValidationResult.error("Invalid dictionary count for Settings");
            }
            if (settingsCount > 4096000) {
                return ValidationResult.error("Settings exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < settingsCount; ++i) {
                ++pos;
                ++pos;
            }
        }
        if ((nullBits[0] & 0x4) != 0x0) {
            final int rulesOffset = buffer.getIntLE(offset + 32);
            if (rulesOffset < 0) {
                return ValidationResult.error("Invalid offset for Rules");
            }
            int pos = offset + 60 + rulesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Rules");
            }
            final ValidationResult rulesResult = InteractionRules.validateStructure(buffer, pos);
            if (!rulesResult.isValid()) {
                return ValidationResult.error("Invalid Rules: " + rulesResult.error());
            }
            pos += InteractionRules.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits[0] & 0x8) != 0x0) {
            final int tagsOffset = buffer.getIntLE(offset + 36);
            if (tagsOffset < 0) {
                return ValidationResult.error("Invalid offset for Tags");
            }
            int pos = offset + 60 + tagsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Tags");
            }
            final int tagsCount = VarInt.peek(buffer, pos);
            if (tagsCount < 0) {
                return ValidationResult.error("Invalid array count for Tags");
            }
            if (tagsCount > 4096000) {
                return ValidationResult.error("Tags exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += tagsCount * 4;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Tags");
            }
        }
        if ((nullBits[0] & 0x10) != 0x0) {
            final int cameraOffset = buffer.getIntLE(offset + 40);
            if (cameraOffset < 0) {
                return ValidationResult.error("Invalid offset for Camera");
            }
            int pos = offset + 60 + cameraOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Camera");
            }
            final ValidationResult cameraResult = InteractionCameraSettings.validateStructure(buffer, pos);
            if (!cameraResult.isValid()) {
                return ValidationResult.error("Invalid Camera: " + cameraResult.error());
            }
            pos += InteractionCameraSettings.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits[0] & 0x20) != 0x0) {
            final int damageEffectsOffset = buffer.getIntLE(offset + 44);
            if (damageEffectsOffset < 0) {
                return ValidationResult.error("Invalid offset for DamageEffects");
            }
            int pos = offset + 60 + damageEffectsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for DamageEffects");
            }
            final ValidationResult damageEffectsResult = DamageEffects.validateStructure(buffer, pos);
            if (!damageEffectsResult.isValid()) {
                return ValidationResult.error("Invalid DamageEffects: " + damageEffectsResult.error());
            }
            pos += DamageEffects.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits[0] & 0x40) != 0x0) {
            final int angledDamageOffset = buffer.getIntLE(offset + 48);
            if (angledDamageOffset < 0) {
                return ValidationResult.error("Invalid offset for AngledDamage");
            }
            int pos = offset + 60 + angledDamageOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for AngledDamage");
            }
            final int angledDamageCount = VarInt.peek(buffer, pos);
            if (angledDamageCount < 0) {
                return ValidationResult.error("Invalid array count for AngledDamage");
            }
            if (angledDamageCount > 4096000) {
                return ValidationResult.error("AngledDamage exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < angledDamageCount; ++i) {
                final ValidationResult structResult = AngledDamage.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid AngledDamage in AngledDamage[" + i + "]: " + structResult.error());
                }
                pos += AngledDamage.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits[0] & 0x80) != 0x0) {
            final int targetedDamageOffset = buffer.getIntLE(offset + 52);
            if (targetedDamageOffset < 0) {
                return ValidationResult.error("Invalid offset for TargetedDamage");
            }
            int pos = offset + 60 + targetedDamageOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for TargetedDamage");
            }
            final int targetedDamageCount = VarInt.peek(buffer, pos);
            if (targetedDamageCount < 0) {
                return ValidationResult.error("Invalid dictionary count for TargetedDamage");
            }
            if (targetedDamageCount > 4096000) {
                return ValidationResult.error("TargetedDamage exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < targetedDamageCount; ++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");
                }
                pos += TargetedDamage.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits[1] & 0x1) != 0x0) {
            final int entityStatsOnHitOffset = buffer.getIntLE(offset + 56);
            if (entityStatsOnHitOffset < 0) {
                return ValidationResult.error("Invalid offset for EntityStatsOnHit");
            }
            int pos = offset + 60 + entityStatsOnHitOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for EntityStatsOnHit");
            }
            final int entityStatsOnHitCount = VarInt.peek(buffer, pos);
            if (entityStatsOnHitCount < 0) {
                return ValidationResult.error("Invalid array count for EntityStatsOnHit");
            }
            if (entityStatsOnHitCount > 4096000) {
                return ValidationResult.error("EntityStatsOnHit exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < entityStatsOnHitCount; ++i) {
                final ValidationResult structResult = EntityStatOnHit.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid EntityStatOnHit in EntityStatsOnHit[" + i + "]: " + structResult.error());
                }
                pos += EntityStatOnHit.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public DamageEntityInteraction clone() {
        final DamageEntityInteraction copy = new DamageEntityInteraction();
        copy.waitForDataFrom = this.waitForDataFrom;
        copy.effects = ((this.effects != null) ? this.effects.clone() : null);
        copy.horizontalSpeedMultiplier = this.horizontalSpeedMultiplier;
        copy.runTime = this.runTime;
        copy.cancelOnItemChange = this.cancelOnItemChange;
        Map.Entry<GameMode, InteractionSettings> e = null;
        if (this.settings != null) {
            final Map<GameMode, InteractionSettings> m = new HashMap<GameMode, InteractionSettings>();
            final Iterator<Map.Entry<GameMode, InteractionSettings>> iterator = this.settings.entrySet().iterator();
            while (iterator.hasNext()) {
                e = iterator.next();
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.settings = m;
        }
        copy.rules = ((this.rules != null) ? this.rules.clone() : null);
        copy.tags = (int[])((this.tags != null) ? Arrays.copyOf(this.tags, this.tags.length) : null);
        copy.camera = ((this.camera != null) ? this.camera.clone() : null);
        copy.next = this.next;
        copy.failed = this.failed;
        copy.blocked = this.blocked;
        copy.damageEffects = ((this.damageEffects != null) ? this.damageEffects.clone() : null);
        copy.angledDamage = (AngledDamage[])((this.angledDamage != null) ? ((AngledDamage[])Arrays.stream(this.angledDamage).map(e -> e.clone()).toArray(AngledDamage[]::new)) : null);
        if (this.targetedDamage != null) {
            final Map<String, TargetedDamage> i = new HashMap<String, TargetedDamage>();
            for (final Map.Entry<String, TargetedDamage> e2 : this.targetedDamage.entrySet()) {
                i.put(e2.getKey(), e2.getValue().clone());
            }
            copy.targetedDamage = i;
        }
        copy.entityStatsOnHit = (EntityStatOnHit[])((this.entityStatsOnHit != null) ? ((EntityStatOnHit[])Arrays.stream(this.entityStatsOnHit).map(e -> e.clone()).toArray(EntityStatOnHit[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final DamageEntityInteraction other) {
            return Objects.equals(this.waitForDataFrom, other.waitForDataFrom) && Objects.equals(this.effects, other.effects) && this.horizontalSpeedMultiplier == other.horizontalSpeedMultiplier && this.runTime == other.runTime && this.cancelOnItemChange == other.cancelOnItemChange && Objects.equals(this.settings, other.settings) && Objects.equals(this.rules, other.rules) && Arrays.equals(this.tags, other.tags) && Objects.equals(this.camera, other.camera) && this.next == other.next && this.failed == other.failed && this.blocked == other.blocked && Objects.equals(this.damageEffects, other.damageEffects) && Arrays.equals(this.angledDamage, other.angledDamage) && Objects.equals(this.targetedDamage, other.targetedDamage) && Arrays.equals(this.entityStatsOnHit, other.entityStatsOnHit);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.waitForDataFrom);
        result = 31 * result + Objects.hashCode(this.effects);
        result = 31 * result + Float.hashCode(this.horizontalSpeedMultiplier);
        result = 31 * result + Float.hashCode(this.runTime);
        result = 31 * result + Boolean.hashCode(this.cancelOnItemChange);
        result = 31 * result + Objects.hashCode(this.settings);
        result = 31 * result + Objects.hashCode(this.rules);
        result = 31 * result + Arrays.hashCode(this.tags);
        result = 31 * result + Objects.hashCode(this.camera);
        result = 31 * result + Integer.hashCode(this.next);
        result = 31 * result + Integer.hashCode(this.failed);
        result = 31 * result + Integer.hashCode(this.blocked);
        result = 31 * result + Objects.hashCode(this.damageEffects);
        result = 31 * result + Arrays.hashCode(this.angledDamage);
        result = 31 * result + Objects.hashCode(this.targetedDamage);
        result = 31 * result + Arrays.hashCode(this.entityStatsOnHit);
        return result;
    }
}
