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;
    private boolean usingSecondaryAction;
    
    public ServerboundInteractPacket() {
    }
    
    public ServerboundInteractPacket(final Entity aom, final boolean boolean2) {
        this.entityId = aom.getId();
        this.action = Action.ATTACK;
        this.usingSecondaryAction = boolean2;
    }
    
    public ServerboundInteractPacket(final Entity aom, final InteractionHand anf, final boolean boolean3) {
        this.entityId = aom.getId();
        this.action = Action.INTERACT;
        this.hand = anf;
        this.usingSecondaryAction = boolean3;
    }
    
    public ServerboundInteractPacket(final Entity aom, final InteractionHand anf, final Vec3 dem, final boolean boolean4) {
        this.entityId = aom.getId();
        this.action = Action.INTERACT_AT;
        this.hand = anf;
        this.location = dem;
        this.usingSecondaryAction = boolean4;
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.entityId = mg.readVarInt();
        this.action = mg.<Action>readEnum(Action.class);
        if (this.action == Action.INTERACT_AT) {
            this.location = new Vec3(mg.readFloat(), mg.readFloat(), mg.readFloat());
        }
        if (this.action == Action.INTERACT || this.action == Action.INTERACT_AT) {
            this.hand = mg.<InteractionHand>readEnum(InteractionHand.class);
        }
        this.usingSecondaryAction = mg.readBoolean();
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeVarInt(this.entityId);
        mg.writeEnum(this.action);
        if (this.action == Action.INTERACT_AT) {
            mg.writeFloat((float)this.location.x);
            mg.writeFloat((float)this.location.y);
            mg.writeFloat((float)this.location.z);
        }
        if (this.action == Action.INTERACT || this.action == Action.INTERACT_AT) {
            mg.writeEnum(this.hand);
        }
        mg.writeBoolean(this.usingSecondaryAction);
    }
    
    @Override
    public void handle(final ServerGamePacketListener qz) {
        qz.handleInteract(this);
    }
    
    @Nullable
    public Entity getTarget(final Level bqb) {
        return bqb.getEntity(this.entityId);
    }
    
    public Action getAction() {
        return this.action;
    }
    
    public InteractionHand getHand() {
        return this.hand;
    }
    
    public Vec3 getLocation() {
        return this.location;
    }
    
    public boolean isUsingSecondaryAction() {
        return this.usingSecondaryAction;
    }
    
    public enum Action {
        INTERACT, 
        ATTACK, 
        INTERACT_AT;
    }
}
