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

import com.dairymoose.modernlife.blocks.ModernBookshelfBlock;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import java.util.function.Supplier;
import net.minecraft.core.BlockPos;
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.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
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/ServerboundBookCancelPacket.class */
public class ServerboundBookCancelPacket implements Packet<ServerGamePacketListener> {
    private int oldItemSlot;
    private BlockPos bookshelfBlockPos;
    private static final Logger LOGGER = LogManager.getLogger();

    public ServerboundBookCancelPacket() {
    }

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

    public ServerboundBookCancelPacket(int oldItemSlot, BlockPos bookshelfBlockPos) {
        this.oldItemSlot = oldItemSlot;
        this.bookshelfBlockPos = bookshelfBlockPos;
    }

    public void read(FriendlyByteBuf byteBuf) {
        this.oldItemSlot = byteBuf.readInt();
        this.bookshelfBlockPos = byteBuf.readBlockPos();
    }

    public void write(FriendlyByteBuf byteBuf) {
        byteBuf.writeInt(this.oldItemSlot);
        if (this.bookshelfBlockPos == null) {
            this.bookshelfBlockPos = new BlockPos(0, 0, 0);
        }
        byteBuf.writeBlockPos(this.bookshelfBlockPos);
    }

    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 static void execute(BlockPos bookshelfBlockPos, int oldItemSlot, Player player) {
        if (bookshelfBlockPos != null && player.level.isLoaded(bookshelfBlockPos)) {
            BlockState state = player.level.getBlockState(bookshelfBlockPos);
            Block block = state.getBlock();
            if (block instanceof ModernBookshelfBlock) {
                block.use(state, player.level, bookshelfBlockPos, player, InteractionHand.MAIN_HAND, (BlockHitResult) null);
                if (player.getItemInHand(InteractionHand.MAIN_HAND).isEmpty()) {
                    ItemStack itemStack = player.getInventory().getItem(oldItemSlot);
                    player.setItemInHand(InteractionHand.MAIN_HAND, itemStack);
                    player.getInventory().setItem(oldItemSlot, ItemStack.EMPTY);
                }
            }
        }
    }

    public void handle(ServerGamePacketListener p_148833_1_) {
        ModernLifeCommon.LOGGER.debug("Handle ServerboundBookCancelPacket");
        if (p_148833_1_ instanceof ServerGamePacketListenerImpl) {
            ServerGamePacketListenerImpl serverHandler = (ServerGamePacketListenerImpl) p_148833_1_;
            if (this.oldItemSlot != -1) {
                execute(this.bookshelfBlockPos, this.oldItemSlot, serverHandler.player);
            }
        }
    }
}
