package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import java.io.IOException;
import java.util.UUID;
import net.minecraft.network.FriendlyByteBuf;
import java.util.Iterator;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import java.util.Collection;
import com.google.common.collect.Lists;
import java.util.List;
import net.minecraft.network.protocol.Packet;

public class ClientboundUpdateAttributesPacket implements Packet<ClientGamePacketListener> {
    private int entityId;
    private final List<AttributeSnapshot> attributes;
    
    public ClientboundUpdateAttributesPacket() {
        this.attributes = Lists.newArrayList();
    }
    
    public ClientboundUpdateAttributesPacket(final int integer, final Collection<AttributeInstance> collection) {
        this.attributes = Lists.newArrayList();
        this.entityId = integer;
        for (final AttributeInstance alr5 : collection) {
            this.attributes.add(new AttributeSnapshot(alr5.getAttribute().getName(), alr5.getBaseValue(), alr5.getModifiers()));
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.entityId = kv.readVarInt();
        for (int integer3 = kv.readInt(), integer4 = 0; integer4 < integer3; ++integer4) {
            final String string5 = kv.readUtf(64);
            final double double6 = kv.readDouble();
            final List<AttributeModifier> list8 = Lists.newArrayList();
            for (int integer5 = kv.readVarInt(), integer6 = 0; integer6 < integer5; ++integer6) {
                final UUID uUID11 = kv.readUUID();
                list8.add(new AttributeModifier(uUID11, "Unknown synced attribute modifier", kv.readDouble(), AttributeModifier.Operation.fromValue(kv.readByte())));
            }
            this.attributes.add(new AttributeSnapshot(string5, double6, list8));
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeVarInt(this.entityId);
        kv.writeInt(this.attributes.size());
        for (final AttributeSnapshot a4 : this.attributes) {
            kv.writeUtf(a4.getName());
            kv.writeDouble(a4.getBase());
            kv.writeVarInt(a4.getModifiers().size());
            for (final AttributeModifier als6 : a4.getModifiers()) {
                kv.writeUUID(als6.getId());
                kv.writeDouble(als6.getAmount());
                kv.writeByte(als6.getOperation().toValue());
            }
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener lw) {
        lw.handleUpdateAttributes(this);
    }
    
    public int getEntityId() {
        return this.entityId;
    }
    
    public List<AttributeSnapshot> getValues() {
        return this.attributes;
    }
    
    public class AttributeSnapshot {
        private final String name;
        private final double base;
        private final Collection<AttributeModifier> modifiers;
        
        public AttributeSnapshot(final String string, final double double3, final Collection<AttributeModifier> collection) {
            this.name = string;
            this.base = double3;
            this.modifiers = collection;
        }
        
        public String getName() {
            return this.name;
        }
        
        public double getBase() {
            return this.base;
        }
        
        public Collection<AttributeModifier> getModifiers() {
            return this.modifiers;
        }
    }
}
