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

import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.items.FlashlightItem;
import java.util.function.Supplier;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
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/ClientboundFlashlightUpdatePacket.class */
public class ClientboundFlashlightUpdatePacket implements Packet<ClientGamePacketListener> {
    private int playerId;
    private boolean flashlightState;
    private static final Logger LOGGER = LogManager.getLogger();

    public ClientboundFlashlightUpdatePacket() {
    }

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

    public ClientboundFlashlightUpdatePacket(int playerId, boolean flashlightState) {
        this.playerId = playerId;
        this.flashlightState = flashlightState;
    }

    public void read(FriendlyByteBuf p_148837_1_) {
        this.playerId = p_148837_1_.readInt();
        this.flashlightState = p_148837_1_.readBoolean();
    }

    public void write(FriendlyByteBuf p_148840_1_) {
        p_148840_1_.writeInt(this.playerId);
        p_148840_1_.writeBoolean(this.flashlightState);
    }

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

    public void handle(ClientGamePacketListener handler) {
        ModernLifeCommon.LOGGER.debug("Handle ClientboundFlashlightUpdatePacket");
        if (this.playerId >= 0 && (handler instanceof ClientPacketListener)) {
            DistExecutor.runWhenOn(Dist.CLIENT, () -> {
                return new Runnable() { // from class: com.dairymoose.modernlife.network.play.client.ClientboundFlashlightUpdatePacket.1
                    final /* synthetic */ ClientGamePacketListener val$handler;

                    RunnableC01321(ClientGamePacketListener handler2) {
                        handler = handler2;
                    }

                    @Override // java.lang.Runnable
                    public void run() {
                        ClientPacketListener clientHandler = handler;
                        Player entity = clientHandler.getLevel().getEntity(ClientboundFlashlightUpdatePacket.this.playerId);
                        if (entity instanceof Player) {
                            Player player = entity;
                            FlashlightItem.usingFlashlight.put(player, Boolean.valueOf(ClientboundFlashlightUpdatePacket.this.flashlightState));
                        }
                    }
                };
            });
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.dairymoose.modernlife.network.play.client.ClientboundFlashlightUpdatePacket$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/network/play/client/ClientboundFlashlightUpdatePacket$1.class */
    public class RunnableC01321 implements Runnable {
        final /* synthetic */ ClientGamePacketListener val$handler;

        RunnableC01321(ClientGamePacketListener handler2) {
            handler = handler2;
        }

        @Override // java.lang.Runnable
        public void run() {
            ClientPacketListener clientHandler = handler;
            Player entity = clientHandler.getLevel().getEntity(ClientboundFlashlightUpdatePacket.this.playerId);
            if (entity instanceof Player) {
                Player player = entity;
                FlashlightItem.usingFlashlight.put(player, Boolean.valueOf(ClientboundFlashlightUpdatePacket.this.flashlightState));
            }
        }
    }
}
