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 anv, final Event a) {
        this(anv, a, TextComponent.EMPTY);
    }
    
    public ClientboundPlayerCombatPacket(final CombatTracker anv, final Event a, final Component mr) {
        this.event = a;
        final LivingEntity aoy5 = anv.getKiller();
        switch (a) {
            case END_COMBAT: {
                this.duration = anv.getCombatDuration();
                this.killerId = ((aoy5 == null) ? -1 : aoy5.getId());
                break;
            }
            case ENTITY_DIED: {
                this.playerId = anv.getMob().getId();
                this.killerId = ((aoy5 == null) ? -1 : aoy5.getId());
                this.message = mr;
                break;
            }
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.event = mg.<Event>readEnum(Event.class);
        if (this.event == Event.END_COMBAT) {
            this.duration = mg.readVarInt();
            this.killerId = mg.readInt();
        }
        else if (this.event == Event.ENTITY_DIED) {
            this.playerId = mg.readVarInt();
            this.killerId = mg.readInt();
            this.message = mg.readComponent();
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeEnum(this.event);
        if (this.event == Event.END_COMBAT) {
            mg.writeVarInt(this.duration);
            mg.writeInt(this.killerId);
        }
        else if (this.event == Event.ENTITY_DIED) {
            mg.writeVarInt(this.playerId);
            mg.writeInt(this.killerId);
            mg.writeComponent(this.message);
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener nl) {
        nl.handlePlayerCombat(this);
    }
    
    @Override
    public boolean isSkippable() {
        return this.event == Event.ENTITY_DIED;
    }
    
    public enum Event {
        ENTER_COMBAT, 
        END_COMBAT, 
        ENTITY_DIED;
    }
}
