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
|
|
|
*/
|
|
|
|
|
2016-05-14 17:50:10 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2016-09-12 16:13:39 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.IConnectionPair;
|
2016-09-01 18:02:03 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.LaserType;
|
2016-07-30 17:07:32 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.laser.Network;
|
2017-02-04 14:12:17 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2021-11-13 18:16:25 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2021-08-22 20:16:04 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityItemInterface.GenericItemHandlerInfo;
|
2017-02-16 18:47:14 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
|
2016-05-14 17:50:10 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
2017-02-04 14:12:17 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2016-05-14 17:50:10 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2016-05-14 17:50:10 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraft.tileentity.TileEntityType;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2016-05-14 17:50:10 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-12-05 15:09:37 +01:00
|
|
|
import net.minecraft.util.text.TextFormatting;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-05-14 17:50:10 +02:00
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
|
|
|
import net.minecraftforge.items.IItemHandler;
|
2021-02-26 22:15:48 +01:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class TileEntityLaserRelayItem extends TileEntityLaserRelay {
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2019-02-27 19:53:05 +01:00
|
|
|
public final Map<BlockPos, SlotlessableItemHandlerWrapper> handlersAround = new ConcurrentHashMap<>();
|
2016-11-16 20:31:16 +01:00
|
|
|
public int priority;
|
2016-06-11 15:33:44 +02:00
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
public TileEntityLaserRelayItem(TileEntityType<?> type) {
|
|
|
|
super(type, LaserType.ITEM);
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public TileEntityLaserRelayItem() {
|
2021-11-13 18:16:25 +01:00
|
|
|
this(ActuallyBlocks.LASER_RELAY_ITEM.getTileEntityType());
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-05-14 17:50:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:40:09 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean shouldSaveDataOnChangeOrWorldStart() {
|
2016-09-09 18:40:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-11 15:33:44 +02:00
|
|
|
@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);
|
2016-11-03 10:49:41 +01:00
|
|
|
boolean change = false;
|
2016-06-11 15:33:44 +02:00
|
|
|
|
2016-11-03 10:49:41 +01:00
|
|
|
this.handlersAround.clear();
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int i = 0; i <= 5; i++) {
|
2021-02-27 13:24:45 +01:00
|
|
|
Direction side = WorldUtil.getDirectionBySidesInOrder(i);
|
2021-08-22 17:09:06 +02:00
|
|
|
BlockPos pos = this.getBlockPos().relative(side);
|
|
|
|
if (this.level.hasChunkAt(pos)) {
|
|
|
|
TileEntity tile = this.level.getBlockEntity(pos);
|
2021-11-13 18:16:25 +01:00
|
|
|
if (tile != null && !(tile instanceof TileEntityItemInterface) && !(tile instanceof TileEntityLaserRelay)) {
|
2021-02-27 16:33:00 +01:00
|
|
|
LazyOptional<IItemHandler> itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
|
2017-02-04 14:12:17 +01:00
|
|
|
|
|
|
|
Object slotlessHandler = null;
|
2021-02-27 16:33:00 +01:00
|
|
|
// TODO: [port] add this back maybe?
|
2017-02-04 14:12:17 +01:00
|
|
|
|
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());
|
|
|
|
// }
|
|
|
|
// }
|
2017-01-18 14:26:56 +01:00
|
|
|
|
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;
|
2016-11-03 10:49:41 +01:00
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (change || old.size() != this.handlersAround.size()) {
|
2017-01-18 14:26:56 +01:00
|
|
|
Network network = this.getNetwork();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (network != null) {
|
2016-11-03 10:49:41 +01:00
|
|
|
network.changeAmount++;
|
|
|
|
}
|
2016-10-29 12:00:00 +02:00
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public void getItemHandlersInNetwork(Network network, List<GenericItemHandlerInfo> storeList) {
|
2016-08-06 02:17:11 +02:00
|
|
|
//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<>();
|
2016-08-06 02:17:11 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (IConnectionPair pair : network.connections) {
|
|
|
|
for (BlockPos relay : pair.getPositions()) {
|
2021-08-22 17:09:06 +02:00
|
|
|
if (relay != null && this.level.hasChunkAt(relay) && !alreadyChecked.contains(relay)) {
|
2016-08-06 02:17:11 +02:00
|
|
|
alreadyChecked.add(relay);
|
2021-08-22 17:09:06 +02:00
|
|
|
TileEntity aRelayTile = this.level.getBlockEntity(relay);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (aRelayTile instanceof TileEntityLaserRelayItem) {
|
|
|
|
TileEntityLaserRelayItem relayTile = (TileEntityLaserRelayItem) aRelayTile;
|
2016-08-06 02:17:11 +02:00
|
|
|
GenericItemHandlerInfo info = new GenericItemHandlerInfo(relayTile);
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (Map.Entry<BlockPos, SlotlessableItemHandlerWrapper> handler : relayTile.handlersAround.entrySet()) {
|
|
|
|
if (!alreadyChecked.contains(handler.getKey())) {
|
2016-08-06 02:17:11 +02:00
|
|
|
alreadyChecked.add(handler.getKey());
|
2016-05-14 17:50:10 +02:00
|
|
|
|
2016-08-06 02:17:11 +02:00
|
|
|
info.handlers.add(handler.getValue());
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
2016-08-06 02:17:11 +02:00
|
|
|
|
2016-10-29 12:00:00 +02:00
|
|
|
storeList.add(info);
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 18:51:23 +01:00
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT 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)
|
2019-05-02 09:10:29 +02:00
|
|
|
public String getExtraDisplayString() {
|
|
|
|
return StringUtil.localize("info." + ActuallyAdditions.MODID + ".laserRelay.item.extra") + ": " + TextFormatting.DARK_RED + this.getPriority() + TextFormatting.RESET;
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2019-05-02 09:10:29 +02:00
|
|
|
public String getCompassDisplayString() {
|
|
|
|
return TextFormatting.GREEN + StringUtil.localize("info." + ActuallyAdditions.MODID + ".laserRelay.item.display.1") + "\n" + StringUtil.localize("info." + ActuallyAdditions.MODID + ".laserRelay.item.display.2");
|
2016-12-05 15:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void onCompassAction(PlayerEntity player) {
|
2021-08-22 17:09:06 +02:00
|
|
|
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
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT 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
|
|
|
}
|
|
|
|
}
|
2016-05-14 17:50:10 +02:00
|
|
|
}
|