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

import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import java.util.function.Supplier;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerGamePacketListener;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
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/ServerboundCameraPacket.class */
public class ServerboundCameraPacket implements Packet<ServerGamePacketListener> {
    private byte[] image;
    private int texSize;
    private static final Logger LOGGER = LogManager.getLogger();
    private static long lastPacketTimestamp = 0;
    private static int incomingPacketCounter = 0;
    private static long reenableCamera = 0;
    private boolean enabled = false;

    public ServerboundCameraPacket() {
    }

    public ServerboundCameraPacket(byte[] image, int texSize) {
        this.image = image;
        this.texSize = texSize;
    }

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

    public void read(FriendlyByteBuf buffer) {
        this.image = buffer.readByteArray();
        this.texSize = buffer.readInt();
    }

    public void write(FriendlyByteBuf buffer) {
        buffer.writeByteArray(this.image);
        buffer.writeInt(this.texSize);
    }

    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 p_148833_1_) {
        if (!this.enabled) {
            return;
        }
        ModernLifeCommon.LOGGER.debug("Handle ServerboundCameraPacket");
        if (p_148833_1_ instanceof ServerGamePacketListenerImpl) {
            ServerGamePacketListenerImpl serverHandler = (ServerGamePacketListenerImpl) p_148833_1_;
            new CompoundTag();
            ServerLevel level = serverHandler.player.getLevel();
            ModernLifeCommon.LOGGER.debug("world = " + level);
            if (level != null) {
                ModernLifeCommon.LOGGER.debug("image length = " + this.image.length);
                if (this.image != null && this.image.length != 0 && this.image.length < 197000 && this.texSize <= 256) {
                    long currentTimestamp = System.currentTimeMillis();
                    long timeDiff = currentTimestamp - lastPacketTimestamp;
                    long timeDiffReenableCamera = currentTimestamp - reenableCamera;
                    if (timeDiffReenableCamera >= 0) {
                        ItemStack itemStack = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.network.play.client.ServerboundCameraPacket.1
                            C01361() {
                            }

                            public Item asItem() {
                                return CustomBlocks.ITEM_CANVAS.get();
                            }
                        });
                        itemStack.getOrCreateTag().putByteArray("image", this.image);
                        if (this.texSize > 0) {
                            itemStack.getTag().putInt("TextureSize", this.texSize);
                        }
                        serverHandler.player.addItem(itemStack);
                        incomingPacketCounter++;
                        if (timeDiff >= 5000) {
                            if (incomingPacketCounter > 50) {
                                reenableCamera = currentTimestamp + 3600000;
                            }
                            incomingPacketCounter = 0;
                            lastPacketTimestamp = System.currentTimeMillis();
                            return;
                        }
                        return;
                    }
                    return;
                }
                ModernLifeCommon.LOGGER.debug("incoming image is too big");
            }
        }
    }

    /* renamed from: com.dairymoose.modernlife.network.play.client.ServerboundCameraPacket$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/network/play/client/ServerboundCameraPacket$1.class */
    public class C01361 implements ItemLike {
        C01361() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CANVAS.get();
        }
    }

    public byte[] getImage() {
        return this.image;
    }
}
