package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import javax.annotation.Nullable;
import net.minecraft.world.level.Level;
import java.io.IOException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.phys.Vec3;
import net.minecraft.network.protocol.Packet;

public class ServerboundInteractPacket implements Packet<ServerGamePacketListener> {
    private int entityId;
    private Action action;
    private Vec3 location;
    private InteractionHand hand;
    
    public ServerboundInteractPacket() {
    }
    
    public ServerboundInteractPacket(final Entity akq) {
        this.entityId = akq.getId();
        this.action = Action.ATTACK;
    }
    
    public ServerboundInteractPacket(final Entity akq, final InteractionHand ajk) {
        this.entityId = akq.getId();
        this.action = Action.INTERACT;
        this.hand = ajk;
    }
    
    public ServerboundInteractPacket(final Entity akq, final InteractionHand ajk, final Vec3 cvl) {
        this.entityId = akq.getId();
        this.action = Action.INTERACT_AT;
        this.hand = ajk;
        this.location = cvl;
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.entityId = kv.readVarInt();
        this.action = kv.<Action>readEnum(Action.class);
        if (this.action == Action.INTERACT_AT) {
            this.location = new Vec3(kv.readFloat(), kv.readFloat(), kv.readFloat());
        }
        if (this.action == Action.INTERACT || this.action == Action.INTERACT_AT) {
            this.hand = kv.<InteractionHand>readEnum(InteractionHand.class);
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeVarInt(this.entityId);
        kv.writeEnum(this.action);
        if (this.action == Action.INTERACT_AT) {
            kv.writeFloat((float)this.location.x);
            kv.writeFloat((float)this.location.y);
            kv.writeFloat((float)this.location.z);
        }
        if (this.action == Action.INTERACT || this.action == Action.INTERACT_AT) {
            kv.writeEnum(this.hand);
        }
    }
    
    @Override
    public void handle(final ServerGamePacketListener pl) {
        pl.handleInteract(this);
    }
    
    @Nullable
    public Entity getTarget(final Level bjw) {
        return bjw.getEntity(this.entityId);
    }
    
    public Action getAction() {
        return this.action;
    }
    
    public InteractionHand getHand() {
        return this.hand;
    }
    
    public Vec3 getLocation() {
        return this.location;
    }
    
    public enum Action {
        INTERACT, 
        ATTACK, 
        INTERACT_AT;
    }
}
