ModernLifePatch/src-source/main/java/com/dairymoose/modernlife/tileentities/PowerReceiverBlockEntity.java
2024-10-26 09:40:21 +08:00

112 lines
4.6 KiB
Java

package com.dairymoose.modernlife.tileentities;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
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.nbt.CompoundTag;
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/PowerReceiverBlockEntity.class */
public class PowerReceiverBlockEntity extends BlockEntity implements IChannelHolder {
private int currentChannel;
public static Map<Integer, Set<PowerReceiverBlockEntity>> perChannelPowerReceivers = new ConcurrentHashMap();
public static final BlockEntityType<PowerReceiverBlockEntity> POWER_RECEIVER = BlockEntityType.Builder.of(PowerReceiverBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_POWER_RECEIVER.get()}).build((Type) null);
private static final Logger LOGGER = LogManager.getLogger();
public String toString() {
return "(channel=" + getCurrentChannel() + ", pos=" + getBlockPos() + ")";
}
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 boolean equals(Object other) {
if (other instanceof PowerReceiverBlockEntity) {
PowerReceiverBlockEntity otherTransmitter = (PowerReceiverBlockEntity) other;
return getBlockPos().equals(otherTransmitter.getBlockPos());
}
return false;
}
public int hashCode() {
return getBlockPos().hashCode();
}
public void addToChannel(int channel) {
ModernLifeCommon.LOGGER.debug("adding receiver at " + getBlockPos() + " with channel: " + this.currentChannel);
Set<PowerReceiverBlockEntity> entities = perChannelPowerReceivers.get(Integer.valueOf(channel));
if (entities == null) {
entities = new HashSet();
perChannelPowerReceivers.put(Integer.valueOf(channel), entities);
}
entities.add(this);
ModernLifeCommon.LOGGER.debug("receiver count = " + perChannelPowerReceivers.size());
}
public void removeFromChannel(int channel) {
ModernLifeCommon.LOGGER.debug("removing receiver at " + getBlockPos() + " with channel: " + this.currentChannel);
Set<PowerReceiverBlockEntity> entities = perChannelPowerReceivers.get(Integer.valueOf(channel));
if (entities == null) {
entities = new HashSet();
perChannelPowerReceivers.put(Integer.valueOf(channel), entities);
}
entities.remove(this);
ModernLifeCommon.LOGGER.debug("receiver count = " + perChannelPowerReceivers.size());
}
public PowerReceiverBlockEntity(BlockPos pos, BlockState state) {
super(POWER_RECEIVER, pos, state);
this.currentChannel = 0;
ModernLifeCommon.LOGGER.debug("PowerReceiverBlockEntity 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);
this.currentChannel = newChannel;
addToChannel(newChannel);
ModernLifeCommon.LOGGER.debug("updating channel with world=" + getLevel());
getLevel().updateNeighborsAt(getBlockPos(), getBlockState().getBlock());
}
}