package com.dairymoose.modernlife.network.play.client;

import com.dairymoose.modernlife.items.ChainsawItem;
import java.util.function.Supplier;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerGamePacketListener;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.network.NetworkEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/network/play/client/ServerboundChainsawTargetPacket.class */
public class ServerboundChainsawTargetPacket implements Packet<ServerGamePacketListener> {
    private BlockHitResult rayTrace;
    private int targetedEntityId;
    private static final Logger LOGGER = LogManager.getLogger();

    public ServerboundChainsawTargetPacket() {
    }

    public ServerboundChainsawTargetPacket(FriendlyByteBuf buffer) {
        read(buffer);
    }

    public ServerboundChainsawTargetPacket(BlockHitResult rayTrace, int targetedEntityId) {
        this.rayTrace = rayTrace;
        this.targetedEntityId = targetedEntityId;
    }

    public void read(FriendlyByteBuf packet) {
        this.rayTrace = packet.readBlockHitResult();
        this.targetedEntityId = packet.readInt();
    }

    public void write(FriendlyByteBuf packet) {
        if (this.rayTrace == null) {
            this.rayTrace = BlockHitResult.miss(new Vec3(0.0d, 0.0d, 0.0d), Direction.UP, new BlockPos(0, 0, 0));
        }
        packet.writeBlockHitResult(this.rayTrace);
        packet.writeInt(this.targetedEntityId);
    }

    public void handle(Supplier<NetworkEvent.Context> ctx) {
        ctx.get().enqueueWork(() -> {
            ((NetworkEvent.Context) ctx.get()).getSender();
            handle((ServerGamePacketListener) ((NetworkEvent.Context) ctx.get()).getNetworkManager().getPacketListener());
        });
        ctx.get().setPacketHandled(true);
    }

    public void handle(ServerGamePacketListener iServerHandler) {
        if (iServerHandler instanceof ServerGamePacketListenerImpl) {
            ServerGamePacketListenerImpl serverHandler = (ServerGamePacketListenerImpl) iServerHandler;
            if (this.rayTrace == null) {
                this.rayTrace = BlockHitResult.miss(new Vec3(0.0d, 0.0d, 0.0d), Direction.UP, new BlockPos(0, 0, 0));
            }
            ChainsawItem.MiningInfo mInfo = ChainsawItem.mineMap.get(serverHandler.player);
            if (mInfo == null) {
                mInfo = new ChainsawItem.MiningInfo();
                ChainsawItem.mineMap.put(serverHandler.player, mInfo);
            }
            if (this.targetedEntityId != -1) {
                Entity entity = serverHandler.player.level.getEntity(this.targetedEntityId);
                if (entity != null) {
                    if (this.rayTrace == null) {
                        mInfo.rayTrace = new EntityHitResult(entity);
                        return;
                    } else {
                        mInfo.rayTrace = new EntityHitResult(entity, this.rayTrace.getLocation());
                        return;
                    }
                }
                return;
            }
            mInfo.rayTrace = this.rayTrace;
        }
    }
}
