package com.dairymoose.modernlife.tileentities;

import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.network.play.client.ServerboundWirelessChannelPacket;
import com.mojang.datafixers.types.Type;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/PowerTransmitterBlockEntity.class */
public class PowerTransmitterBlockEntity extends BlockEntity implements IChannelHolder {
    private int currentChannel;
    public static Map<Integer, Set<PowerTransmitterBlockEntity>> perChannelPowerTransmitters = new ConcurrentHashMap();
    public static final BlockEntityType<PowerTransmitterBlockEntity> POWER_TRANSMITTER = BlockEntityType.Builder.of(PowerTransmitterBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_POWER_TRANSMITTER.get()}).build((Type) null);
    private static final Logger LOGGER = LogManager.getLogger();

    public String toString() {
        return "(channel=" + getCurrentChannel() + ", pos=" + getBlockPos() + ")";
    }

    public void updateReceivers(Level world) {
        Set<PowerReceiverBlockEntity> receivers = PowerReceiverBlockEntity.perChannelPowerReceivers.get(Integer.valueOf(this.currentChannel));
        if (receivers != null) {
            ServerboundWirelessChannelPacket.purgeRemovedChannelHolders(receivers);
            for (PowerReceiverBlockEntity receiver : receivers) {
                ModernLifeCommon.LOGGER.debug("issued update to " + receiver.getBlockPos());
                if (world.isLoaded(receiver.getBlockPos())) {
                    world.updateNeighborsAt(receiver.getBlockPos(), receiver.getBlockState().getBlock());
                    for (Direction d : Direction.values()) {
                        world.updateNeighborsAt(receiver.getBlockPos().relative(d), receiver.getBlockState().getBlock());
                    }
                }
            }
        }
    }

    public CompoundTag getUpdateTag() {
        CompoundTag nbt = super.getUpdateTag();
        nbt.putInt("channel", this.currentChannel);
        return nbt;
    }

    public void handleUpdateTag(CompoundTag tag) {
        if (tag.contains("channel")) {
            this.currentChannel = tag.getInt("channel");
        }
        super.handleUpdateTag(tag);
    }

    protected void saveAdditional(CompoundTag nbt) {
        nbt.putInt("channel", this.currentChannel);
        ModernLifeCommon.LOGGER.debug("save with channel=" + this.currentChannel + " at pos=" + getBlockPos());
    }

    public void load(CompoundTag nbt) {
        super.load(nbt);
        if (nbt.contains("channel")) {
            this.currentChannel = nbt.getInt("channel");
            addToChannel(this.currentChannel);
        }
        ModernLifeCommon.LOGGER.debug("load with channel=" + this.currentChannel);
    }

    public void setChanged() {
        super.setChanged();
        ModernLifeCommon.LOGGER.debug("setChanged");
    }

    public boolean equals(Object other) {
        if (other instanceof PowerTransmitterBlockEntity) {
            PowerTransmitterBlockEntity otherTransmitter = (PowerTransmitterBlockEntity) other;
            return getBlockPos().equals(otherTransmitter.getBlockPos());
        }
        return false;
    }

    public int hashCode() {
        return getBlockPos().hashCode();
    }

    public void addToChannel(int channel) {
        ModernLifeCommon.LOGGER.debug("adding transmitter at " + getBlockPos() + " with channel: " + this.currentChannel);
        Set<PowerTransmitterBlockEntity> entities = perChannelPowerTransmitters.get(Integer.valueOf(channel));
        if (entities == null) {
            entities = new HashSet();
            perChannelPowerTransmitters.put(Integer.valueOf(channel), entities);
        }
        ModernLifeCommon.LOGGER.debug("add " + this + " to channel " + channel + " with BlockPos=" + getBlockPos());
        entities.add(this);
        ModernLifeCommon.LOGGER.debug("transmitter count = " + perChannelPowerTransmitters.size());
    }

    public void removeFromChannel(int channel) {
        ModernLifeCommon.LOGGER.debug("removing transmitter at " + getBlockPos() + " with channel: " + this.currentChannel);
        Set<PowerTransmitterBlockEntity> entities = perChannelPowerTransmitters.get(Integer.valueOf(channel));
        if (entities == null) {
            entities = new HashSet();
            perChannelPowerTransmitters.put(Integer.valueOf(channel), entities);
        }
        ModernLifeCommon.LOGGER.debug("remove " + this);
        entities.remove(this);
        ModernLifeCommon.LOGGER.debug("transmitter count = " + perChannelPowerTransmitters.size());
    }

    public PowerTransmitterBlockEntity(BlockPos pos, BlockState state) {
        super(POWER_TRANSMITTER, pos, state);
        this.currentChannel = 0;
        ModernLifeCommon.LOGGER.debug("PowerTransmitterBlockEntity CONSTRUCTOR");
    }

    @Override // com.dairymoose.modernlife.tileentities.IChannelHolder
    public int getCurrentChannel() {
        return this.currentChannel;
    }

    @Override // com.dairymoose.modernlife.tileentities.IChannelHolder
    public void setCurrentChannel(int newChannel) {
        setChanged();
        removeFromChannel(this.currentChannel);
        updateReceivers(getLevel());
        this.currentChannel = newChannel;
        addToChannel(this.currentChannel);
        updateReceivers(getLevel());
    }
}
