package net.minecraft.world.entity.monster;

import java.util.AbstractList;
import net.minecraft.world.entity.ai.attributes.RangedAttribute;
import org.apache.logging.log4j.LogManager;
import javax.annotation.Nullable;
import java.util.UUID;
import java.util.Collection;
import net.minecraft.nbt.Tag;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.nbt.CompoundTag;
import java.util.Iterator;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.entity.ai.attributes.BaseAttributeMap;
import net.minecraft.world.entity.ai.attributes.Attribute;
import org.apache.logging.log4j.Logger;

public class SharedMonsterAttributes {
    private static final Logger LOGGER;
    public static final Attribute MAX_HEALTH;
    public static final Attribute FOLLOW_RANGE;
    public static final Attribute KNOCKBACK_RESISTANCE;
    public static final Attribute MOVEMENT_SPEED;
    public static final Attribute FLYING_SPEED;
    public static final Attribute ATTACK_DAMAGE;
    public static final Attribute ATTACK_KNOCKBACK;
    public static final Attribute ATTACK_SPEED;
    public static final Attribute ARMOR;
    public static final Attribute ARMOR_TOUGHNESS;
    public static final Attribute LUCK;
    
    public static ListTag saveAttributes(final BaseAttributeMap alu) {
        final ListTag jz2 = new ListTag();
        for (final AttributeInstance alr4 : alu.getAttributes()) {
            ((AbstractList<CompoundTag>)jz2).add(saveAttribute(alr4));
        }
        return jz2;
    }
    
    private static CompoundTag saveAttribute(final AttributeInstance alr) {
        final CompoundTag jt2 = new CompoundTag();
        final Attribute alq3 = alr.getAttribute();
        jt2.putString("Name", alq3.getName());
        jt2.putDouble("Base", alr.getBaseValue());
        final Collection<AttributeModifier> collection4 = alr.getModifiers();
        if (collection4 != null && !collection4.isEmpty()) {
            final ListTag jz5 = new ListTag();
            for (final AttributeModifier als7 : collection4) {
                if (als7.isSerializable()) {
                    ((AbstractList<CompoundTag>)jz5).add(saveAttributeModifier(als7));
                }
            }
            jt2.put("Modifiers", jz5);
        }
        return jt2;
    }
    
    public static CompoundTag saveAttributeModifier(final AttributeModifier als) {
        final CompoundTag jt2 = new CompoundTag();
        jt2.putString("Name", als.getName());
        jt2.putDouble("Amount", als.getAmount());
        jt2.putInt("Operation", als.getOperation().toValue());
        jt2.putUUID("UUID", als.getId());
        return jt2;
    }
    
    public static void loadAttributes(final BaseAttributeMap alu, final ListTag jz) {
        for (int integer3 = 0; integer3 < jz.size(); ++integer3) {
            final CompoundTag jt4 = jz.getCompound(integer3);
            final AttributeInstance alr5 = alu.getInstance(jt4.getString("Name"));
            if (alr5 == null) {
                SharedMonsterAttributes.LOGGER.warn("Ignoring unknown attribute '{}'", jt4.getString("Name"));
            }
            else {
                loadAttribute(alr5, jt4);
            }
        }
    }
    
    private static void loadAttribute(final AttributeInstance alr, final CompoundTag jt) {
        alr.setBaseValue(jt.getDouble("Base"));
        if (jt.contains("Modifiers", 9)) {
            final ListTag jz3 = jt.getList("Modifiers", 10);
            for (int integer4 = 0; integer4 < jz3.size(); ++integer4) {
                final AttributeModifier als5 = loadAttributeModifier(jz3.getCompound(integer4));
                if (als5 != null) {
                    final AttributeModifier als6 = alr.getModifier(als5.getId());
                    if (als6 != null) {
                        alr.removeModifier(als6);
                    }
                    alr.addModifier(als5);
                }
            }
        }
    }
    
    @Nullable
    public static AttributeModifier loadAttributeModifier(final CompoundTag jt) {
        final UUID uUID2 = jt.getUUID("UUID");
        try {
            final AttributeModifier.Operation a3 = AttributeModifier.Operation.fromValue(jt.getInt("Operation"));
            return new AttributeModifier(uUID2, jt.getString("Name"), jt.getDouble("Amount"), a3);
        }
        catch (Exception exception3) {
            SharedMonsterAttributes.LOGGER.warn("Unable to create attribute: {}", exception3.getMessage());
            return null;
        }
    }
    
    static {
        LOGGER = LogManager.getLogger();
        MAX_HEALTH = new RangedAttribute(null, "generic.maxHealth", 20.0, 0.0, 1024.0).importLegacyName("Max Health").setSyncable(true);
        FOLLOW_RANGE = new RangedAttribute(null, "generic.followRange", 32.0, 0.0, 2048.0).importLegacyName("Follow Range");
        KNOCKBACK_RESISTANCE = new RangedAttribute(null, "generic.knockbackResistance", 0.0, 0.0, 1.0).importLegacyName("Knockback Resistance");
        MOVEMENT_SPEED = new RangedAttribute(null, "generic.movementSpeed", 0.699999988079071, 0.0, 1024.0).importLegacyName("Movement Speed").setSyncable(true);
        FLYING_SPEED = new RangedAttribute(null, "generic.flyingSpeed", 0.4000000059604645, 0.0, 1024.0).importLegacyName("Flying Speed").setSyncable(true);
        ATTACK_DAMAGE = new RangedAttribute(null, "generic.attackDamage", 2.0, 0.0, 2048.0);
        ATTACK_KNOCKBACK = new RangedAttribute(null, "generic.attackKnockback", 0.0, 0.0, 5.0);
        ATTACK_SPEED = new RangedAttribute(null, "generic.attackSpeed", 4.0, 0.0, 1024.0).setSyncable(true);
        ARMOR = new RangedAttribute(null, "generic.armor", 0.0, 0.0, 30.0).setSyncable(true);
        ARMOR_TOUGHNESS = new RangedAttribute(null, "generic.armorToughness", 0.0, 0.0, 20.0).setSyncable(true);
        LUCK = new RangedAttribute(null, "generic.luck", 0.0, -1024.0, 1024.0).setSyncable(true);
    }
}
