package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import java.io.IOException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.damagesource.CombatTracker;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;

public class ClientboundPlayerCombatPacket implements Packet<ClientGamePacketListener> {
    public Event event;
    public int playerId;
    public int killerId;
    public int duration;
    public Component message;
    
    public ClientboundPlayerCombatPacket() {
    }
    
    public ClientboundPlayerCombatPacket(final CombatTracker ajy, final Event a) {
        this(ajy, a, new TextComponent(""));
    }
    
    public ClientboundPlayerCombatPacket(final CombatTracker ajy, final Event a, final Component lf) {
        this.event = a;
        final LivingEntity akz5 = ajy.getKiller();
        switch (a) {
            case END_COMBAT: {
                this.duration = ajy.getCombatDuration();
                this.killerId = ((akz5 == null) ? -1 : akz5.getId());
                break;
            }
            case ENTITY_DIED: {
                this.playerId = ajy.getMob().getId();
                this.killerId = ((akz5 == null) ? -1 : akz5.getId());
                this.message = lf;
                break;
            }
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.event = kv.<Event>readEnum(Event.class);
        if (this.event == Event.END_COMBAT) {
            this.duration = kv.readVarInt();
            this.killerId = kv.readInt();
        }
        else if (this.event == Event.ENTITY_DIED) {
            this.playerId = kv.readVarInt();
            this.killerId = kv.readInt();
            this.message = kv.readComponent();
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeEnum(this.event);
        if (this.event == Event.END_COMBAT) {
            kv.writeVarInt(this.duration);
            kv.writeInt(this.killerId);
        }
        else if (this.event == Event.ENTITY_DIED) {
            kv.writeVarInt(this.playerId);
            kv.writeInt(this.killerId);
            kv.writeComponent(this.message);
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener lw) {
        lw.handlePlayerCombat(this);
    }
    
    @Override
    public boolean isSkippable() {
        return this.event == Event.ENTITY_DIED;
    }
    
    public enum Event {
        ENTER_COMBAT, 
        END_COMBAT, 
        ENTITY_DIED;
    }
}
