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

import com.dairymoose.modernlife.blocks.RadiatorBlock;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.tileentities.RadiatorBlockEntity;
import java.util.function.Supplier;
import net.minecraft.core.BlockPos;
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.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
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/ServerboundRadiatorPacket.class */
public class ServerboundRadiatorPacket implements Packet<ServerGamePacketListener> {
    private RadiatorBlock.HeatType heatType;
    private BlockPos radiatorPos;
    private static final Logger LOGGER = LogManager.getLogger();

    public ServerboundRadiatorPacket() {
    }

    public ServerboundRadiatorPacket(RadiatorBlock.HeatType heatType, BlockPos easelPos) {
        this.heatType = heatType;
        this.radiatorPos = easelPos;
    }

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

    public void read(FriendlyByteBuf buffer) {
        this.heatType = (RadiatorBlock.HeatType) buffer.readEnum(RadiatorBlock.HeatType.class);
        this.radiatorPos = buffer.readBlockPos();
    }

    public void write(FriendlyByteBuf buffer) {
        buffer.writeEnum(this.heatType);
        buffer.writeBlockPos(this.radiatorPos);
    }

    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_) {
        BlockState radiatorState;
        ModernLifeCommon.LOGGER.debug("Handle ServerboundRadiatorPacket");
        if (this.radiatorPos != null && (p_148833_1_ instanceof ServerGamePacketListenerImpl)) {
            ServerGamePacketListenerImpl serverHandler = (ServerGamePacketListenerImpl) p_148833_1_;
            new CompoundTag();
            ServerLevel level = serverHandler.player.getLevel();
            if (level != null && level.isLoaded(this.radiatorPos) && (radiatorState = level.getBlockState(this.radiatorPos)) != null && radiatorState.is(CustomBlocks.BLOCK_RADIATOR.get())) {
                BlockEntity tileEntity = level.getChunkAt(this.radiatorPos).getBlockEntity(this.radiatorPos);
                if (tileEntity instanceof RadiatorBlockEntity) {
                    RadiatorBlockEntity radiatorBlockEntity = (RadiatorBlockEntity) tileEntity;
                    if (radiatorBlockEntity != null) {
                        level.setBlockAndUpdate(this.radiatorPos, (BlockState) radiatorState.setValue(RadiatorBlock.HEAT, this.heatType));
                    }
                }
            }
        }
    }
}
