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

import com.dairymoose.entity.DummyEntity;
import com.dairymoose.modernlife.blocks.gui.HBlockPos;
import com.dairymoose.modernlife.core.BlockVolume;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.tileentities.WinchBlockEntity;
import java.util.function.Supplier;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.world.entity.Entity;
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/ClientboundWinchDummyPacket.class */
public class ClientboundWinchDummyPacket implements Packet<ClientGamePacketListener> {
    private int entityId;
    private BlockPos pos;
    private static final Logger LOGGER = LogManager.getLogger();

    public ClientboundWinchDummyPacket() {
    }

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

    public ClientboundWinchDummyPacket(int entityId, BlockPos pos) {
        this.entityId = entityId;
        this.pos = pos;
    }

    public void read(FriendlyByteBuf byteBuf) {
        this.entityId = byteBuf.readInt();
        this.pos = byteBuf.readBlockPos();
    }

    public void write(FriendlyByteBuf byteBuf) {
        byteBuf.writeInt(this.entityId);
        byteBuf.writeBlockPos(this.pos);
    }

    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 ClientboundWinchDummyPacket");
        if (handler instanceof ClientPacketListener) {
            DistExecutor.runWhenOn(Dist.CLIENT, () -> {
                return new Runnable() { // from class: com.dairymoose.modernlife.network.play.client.ClientboundWinchDummyPacket.1
                    final /* synthetic */ ClientGamePacketListener val$handler;

                    RunnableC01351(ClientGamePacketListener handler2) {
                        handler = handler2;
                    }

                    @Override // java.lang.Runnable
                    public void run() {
                        ClientPacketListener clientHandler = handler;
                        Entity e = clientHandler.getLevel().getEntity(ClientboundWinchDummyPacket.this.entityId);
                        if (e instanceof DummyEntity) {
                            DummyEntity dummy = (DummyEntity) e;
                            WinchBlockEntity.entityForWinchPos.put(new HBlockPos(ClientboundWinchDummyPacket.this.pos), dummy);
                            BlockVolume.dummy = dummy;
                        }
                    }
                };
            });
        }
    }

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

        RunnableC01351(ClientGamePacketListener handler2) {
            handler = handler2;
        }

        @Override // java.lang.Runnable
        public void run() {
            ClientPacketListener clientHandler = handler;
            Entity e = clientHandler.getLevel().getEntity(ClientboundWinchDummyPacket.this.entityId);
            if (e instanceof DummyEntity) {
                DummyEntity dummy = (DummyEntity) e;
                WinchBlockEntity.entityForWinchPos.put(new HBlockPos(ClientboundWinchDummyPacket.this.pos), dummy);
                BlockVolume.dummy = dummy;
            }
        }
    }
}
