package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import java.io.IOException;
import java.util.UUID;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import net.minecraft.world.entity.ai.attributes.Attribute;
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 apt5 : collection) {
            this.attributes.add(new AttributeSnapshot(apt5.getAttribute(), apt5.getBaseValue(), apt5.getModifiers()));
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.entityId = mg.readVarInt();
        for (int integer3 = mg.readInt(), integer4 = 0; integer4 < integer3; ++integer4) {
            final ResourceLocation uh5 = mg.readResourceLocation();
            final Attribute aps6 = Registry.ATTRIBUTE.get(uh5);
            final double double7 = mg.readDouble();
            final List<AttributeModifier> list9 = Lists.newArrayList();
            for (int integer5 = mg.readVarInt(), integer6 = 0; integer6 < integer5; ++integer6) {
                final UUID uUID12 = mg.readUUID();
                list9.add(new AttributeModifier(uUID12, "Unknown synced attribute modifier", mg.readDouble(), AttributeModifier.Operation.fromValue(mg.readByte())));
            }
            this.attributes.add(new AttributeSnapshot(aps6, double7, list9));
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeVarInt(this.entityId);
        mg.writeInt(this.attributes.size());
        for (final AttributeSnapshot a4 : this.attributes) {
            mg.writeResourceLocation(Registry.ATTRIBUTE.getKey(a4.getAttribute()));
            mg.writeDouble(a4.getBase());
            mg.writeVarInt(a4.getModifiers().size());
            for (final AttributeModifier apv6 : a4.getModifiers()) {
                mg.writeUUID(apv6.getId());
                mg.writeDouble(apv6.getAmount());
                mg.writeByte(apv6.getOperation().toValue());
            }
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener nl) {
        nl.handleUpdateAttributes(this);
    }
    
    public int getEntityId() {
        return this.entityId;
    }
    
    public List<AttributeSnapshot> getValues() {
        return this.attributes;
    }
    
    public class AttributeSnapshot {
        private final Attribute attribute;
        private final double base;
        private final Collection<AttributeModifier> modifiers;
        
        public AttributeSnapshot(final Attribute aps, final double double3, final Collection<AttributeModifier> collection) {
            this.attribute = aps;
            this.base = double3;
            this.modifiers = collection;
        }
        
        public Attribute getAttribute() {
            return this.attribute;
        }
        
        public double getBase() {
            return this.base;
        }
        
        public Collection<AttributeModifier> getModifiers() {
            return this.modifiers;
        }
    }
}
