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 akk) {
        this.entityId = integer;
        this.effectId = (byte)(MobEffect.getId(akk.getEffect()) & 0xFF);
        this.effectAmplifier = (byte)(akk.getAmplifier() & 0xFF);
        if (akk.getDuration() > 32767) {
            this.effectDurationTicks = 32767;
        }
        else {
            this.effectDurationTicks = akk.getDuration();
        }
        this.flags = 0;
        if (akk.isAmbient()) {
            this.flags |= 0x1;
        }
        if (akk.isVisible()) {
            this.flags |= 0x2;
        }
        if (akk.showIcon()) {
            this.flags |= 0x4;
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.entityId = kv.readVarInt();
        this.effectId = kv.readByte();
        this.effectAmplifier = kv.readByte();
        this.effectDurationTicks = kv.readVarInt();
        this.flags = kv.readByte();
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeVarInt(this.entityId);
        kv.writeByte(this.effectId);
        kv.writeByte(this.effectAmplifier);
        kv.writeVarInt(this.effectDurationTicks);
        kv.writeByte(this.flags);
    }
    
    public boolean isSuperLongDuration() {
        return this.effectDurationTicks == 32767;
    }
    
    @Override
    public void handle(final ClientGamePacketListener lw) {
        lw.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;
    }
}
