ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java

188 lines
7.5 KiB
Java
Raw Normal View History

2016-05-16 22:52:27 +02:00
/*
* This file ("TileEntityLaserRelayItem.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2016-05-16 22:52:27 +02:00
*/
package de.ellpeck.actuallyadditions.mod.tile;
2016-09-12 16:13:39 +02:00
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
import de.ellpeck.actuallyadditions.api.laser.LaserType;
import de.ellpeck.actuallyadditions.api.laser.Network;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2021-11-13 18:16:25 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityItemInterface.GenericItemHandlerInfo;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper;
2024-03-02 21:23:08 +01:00
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
2024-03-02 21:23:08 +01:00
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
2024-03-04 20:21:48 +01:00
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
2024-03-04 21:38:02 +01:00
import net.neoforged.neoforge.capabilities.Capabilities;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.items.IItemHandler;
2021-02-26 22:15:48 +01:00
2024-03-02 21:23:08 +01:00
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
2021-02-26 22:15:48 +01:00
import java.util.concurrent.ConcurrentHashMap;
2019-05-02 09:10:29 +02:00
public class TileEntityLaserRelayItem extends TileEntityLaserRelay {
2019-02-27 19:53:05 +01:00
public final Map<BlockPos, SlotlessableItemHandlerWrapper> handlersAround = new ConcurrentHashMap<>();
public int priority;
2024-03-02 21:23:08 +01:00
public TileEntityLaserRelayItem(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state, LaserType.ITEM);
}
2024-03-02 21:23:08 +01:00
public TileEntityLaserRelayItem(BlockPos pos, BlockState state) {
this(ActuallyBlocks.LASER_RELAY_ITEM.getTileEntityType(), pos, state);
}
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityLaserRelayItem tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityLaserRelayItem tile) {
tile.serverTick();
}
}
2019-05-02 09:10:29 +02:00
public int getPriority() {
2016-11-16 18:51:23 +01:00
return this.priority;
}
2019-05-02 09:10:29 +02:00
public boolean isWhitelisted(ItemStack stack, boolean output) {
return true;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean shouldSaveDataOnChangeOrWorldStart() {
return true;
}
@Override
2019-05-02 09:10:29 +02:00
public void saveDataOnChangeOrWorldStart() {
2019-02-27 19:53:05 +01:00
Map<BlockPos, SlotlessableItemHandlerWrapper> old = new HashMap<>(this.handlersAround);
boolean change = false;
this.handlersAround.clear();
2019-05-02 09:10:29 +02:00
for (int i = 0; i <= 5; i++) {
Direction side = WorldUtil.getDirectionBySidesInOrder(i);
BlockPos pos = this.getBlockPos().relative(side);
if (this.level.hasChunkAt(pos)) {
2024-03-02 21:23:08 +01:00
BlockEntity tile = this.level.getBlockEntity(pos);
2021-11-13 18:16:25 +01:00
if (tile != null && !(tile instanceof TileEntityItemInterface) && !(tile instanceof TileEntityLaserRelay)) {
2024-03-04 21:38:02 +01:00
IItemHandler itemHandler = this.level.getCapability(Capabilities.ItemHandler.BLOCK, tile.getBlockPos(), side.getOpposite());
Object slotlessHandler = null;
2021-02-27 16:33:00 +01:00
// TODO: [port] add this back maybe?
2021-02-27 16:33:00 +01:00
// if (ActuallyAdditions.commonCapsLoaded) {
// if (tile.hasCapability(SlotlessItemHandlerConfig.CAPABILITY, side.getOpposite())) {
// slotlessHandler = tile.getCapability(SlotlessItemHandlerConfig.CAPABILITY, side.getOpposite());
// }
// }
2021-02-27 16:33:00 +01:00
SlotlessableItemHandlerWrapper handler = new SlotlessableItemHandlerWrapper(itemHandler, slotlessHandler);
this.handlersAround.put(pos, handler);
SlotlessableItemHandlerWrapper oldHandler = old.get(pos);
if (!handler.equals(oldHandler)) {
change = true;
}
}
}
}
2019-05-02 09:10:29 +02:00
if (change || old.size() != this.handlersAround.size()) {
Network network = this.getNetwork();
2019-05-02 09:10:29 +02:00
if (network != null) {
network.changeAmount++;
}
}
}
2019-05-02 09:10:29 +02:00
public void getItemHandlersInNetwork(Network network, List<GenericItemHandlerInfo> storeList) {
//Keeps track of all the Laser Relays and Item Handlers that have been checked already to make nothing run multiple times
2019-02-27 19:53:05 +01:00
Set<BlockPos> alreadyChecked = new HashSet<>();
2019-05-02 09:10:29 +02:00
for (IConnectionPair pair : network.connections) {
for (BlockPos relay : pair.getPositions()) {
if (relay != null && this.level.hasChunkAt(relay) && !alreadyChecked.contains(relay)) {
alreadyChecked.add(relay);
2024-03-02 21:23:08 +01:00
BlockEntity aRelayTile = this.level.getBlockEntity(relay);
2024-03-03 01:20:53 +01:00
if (aRelayTile instanceof TileEntityLaserRelayItem relayTile) {
GenericItemHandlerInfo info = new GenericItemHandlerInfo(relayTile);
2019-05-02 09:10:29 +02:00
for (Map.Entry<BlockPos, SlotlessableItemHandlerWrapper> handler : relayTile.handlersAround.entrySet()) {
if (!alreadyChecked.contains(handler.getKey())) {
alreadyChecked.add(handler.getKey());
info.handlers.add(handler.getValue());
}
}
storeList.add(info);
}
}
}
}
}
2016-11-16 18:51:23 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
2016-11-16 18:51:23 +01:00
super.writeSyncableNBT(compound, type);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("Priority", this.priority);
2016-11-16 18:51:23 +01:00
}
}
2016-12-05 15:09:37 +01:00
@Override
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
public Component getExtraDisplayString() {
return Component.translatable("info." + ActuallyAdditions.MODID + ".laserRelay.item.extra").append(": ").append(Component.literal(String.valueOf(this.getPriority())).withStyle(ChatFormatting.DARK_RED));
2016-12-05 15:09:37 +01:00
}
@Override
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
public Component getCompassDisplayString() {
return Component.translatable("info." + ActuallyAdditions.MODID + ".laserRelay.item.display.1").append("\n").append(Component.translatable("info." + ActuallyAdditions.MODID + ".laserRelay.item.display.2")).withStyle(ChatFormatting.GREEN);
2016-12-05 15:09:37 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public void onCompassAction(Player player) {
if (player.isShiftKeyDown()) {
2016-12-05 15:09:37 +01:00
this.priority--;
2019-05-02 09:10:29 +02:00
} else {
2016-12-05 15:09:37 +01:00
this.priority++;
}
}
2016-11-16 18:51:23 +01:00
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
2016-11-16 18:51:23 +01:00
super.readSyncableNBT(compound, type);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
this.priority = compound.getInt("Priority");
2016-11-16 18:51:23 +01:00
}
}
}