package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import java.io.IOException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.network.protocol.Packet;

public class ClientboundUpdateMobEffectPacket implements Packet<ClientGamePacketListener> {
    private int entityId;
    private byte effectId;
    private byte effectAmplifier;
    private int effectDurationTicks;
    private byte flags;
    
    public ClientboundUpdateMobEffectPacket() {
    }
    
    public ClientboundUpdateMobEffectPacket(final int integer, final MobEffectInstance aog) {
        this.entityId = integer;
        this.effectId = (byte)(MobEffect.getId(aog.getEffect()) & 0xFF);
        this.effectAmplifier = (byte)(aog.getAmplifier() & 0xFF);
        if (aog.getDuration() > 32767) {
            this.effectDurationTicks = 32767;
        }
        else {
            this.effectDurationTicks = aog.getDuration();
        }
        this.flags = 0;
        if (aog.isAmbient()) {
            this.flags |= 0x1;
        }
        if (aog.isVisible()) {
            this.flags |= 0x2;
        }
        if (aog.showIcon()) {
            this.flags |= 0x4;
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.entityId = mg.readVarInt();
        this.effectId = mg.readByte();
        this.effectAmplifier = mg.readByte();
        this.effectDurationTicks = mg.readVarInt();
        this.flags = mg.readByte();
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeVarInt(this.entityId);
        mg.writeByte(this.effectId);
        mg.writeByte(this.effectAmplifier);
        mg.writeVarInt(this.effectDurationTicks);
        mg.writeByte(this.flags);
    }
    
    public boolean isSuperLongDuration() {
        return this.effectDurationTicks == 32767;
    }
    
    @Override
    public void handle(final ClientGamePacketListener nl) {
        nl.handleUpdateMobEffect(this);
    }
    
    public int getEntityId() {
        return this.entityId;
    }
    
    public byte getEffectId() {
        return this.effectId;
    }
    
    public byte getEffectAmplifier() {
        return this.effectAmplifier;
    }
    
    public int getEffectDurationTicks() {
        return this.effectDurationTicks;
    }
    
    public boolean isEffectVisible() {
        return (this.flags & 0x2) == 0x2;
    }
    
    public boolean isEffectAmbient() {
        return (this.flags & 0x1) == 0x1;
    }
    
    public boolean effectShowsIcon() {
        return (this.flags & 0x4) == 0x4;
    }
}
