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

import com.dairymoose.inventory.container.TrashCanContainer;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import java.util.function.Supplier;
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.player.Player;
import net.minecraft.world.item.ItemStack;
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/ServerboundTrashCanPacket.class */
public class ServerboundTrashCanPacket implements Packet<ServerGamePacketListener> {
    private int containerId;
    private ItemStack toTrash;
    private static final Logger LOGGER = LogManager.getLogger();

    public ServerboundTrashCanPacket() {
        this.toTrash = ItemStack.EMPTY;
    }

    public ServerboundTrashCanPacket(FriendlyByteBuf buffer) {
        this.toTrash = ItemStack.EMPTY;
        read(buffer);
    }

    public ServerboundTrashCanPacket(int containerId, ItemStack toTrash) {
        this.toTrash = ItemStack.EMPTY;
        this.containerId = containerId;
        this.toTrash = toTrash;
    }

    public void read(FriendlyByteBuf p_148837_1_) {
        this.containerId = p_148837_1_.readInt();
        this.toTrash = p_148837_1_.readItem();
    }

    public void write(FriendlyByteBuf p_148840_1_) {
        p_148840_1_.writeInt(this.containerId);
        p_148840_1_.writeItem(this.toTrash);
    }

    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 boolean playerHasItem(Player player, ItemStack item) {
        if (player.containerMenu.getSlot(0).getItem().sameItem(item)) {
            return true;
        }
        return false;
    }

    public void handle(ServerGamePacketListener p_148833_1_) {
        ModernLifeCommon.LOGGER.debug("Handle ServerboundTrashCanPacket");
        if (p_148833_1_ instanceof ServerGamePacketListenerImpl) {
            ServerGamePacketListenerImpl serverHandler = (ServerGamePacketListenerImpl) p_148833_1_;
            serverHandler.player.resetLastActionTime();
            if (serverHandler.player.containerMenu.containerId == this.containerId && !serverHandler.player.isSpectator() && (serverHandler.player.containerMenu instanceof TrashCanContainer)) {
                TrashCanContainer trashCanContainer = (TrashCanContainer) serverHandler.player.containerMenu;
                if (playerHasItem(serverHandler.player, this.toTrash)) {
                    trashCanContainer.trashItem(this.toTrash);
                    serverHandler.player.containerMenu.broadcastChanges();
                }
            }
        }
    }
}
