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

348 lines
14 KiB
Java
Raw Normal View History

2016-05-16 22:52:27 +02:00
/*
* This file ("TileEntityItemViewer.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-08 21:09:58 +02:00
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.api.laser.Network;
2021-11-13 18:02:42 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
2016-12-27 17:40:27 +01:00
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-05-08 21:09:58 +02:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import de.ellpeck.actuallyadditions.mod.util.compat.SlotlessableItemHandlerWrapper;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
2016-05-08 21:09:58 +02:00
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
2016-05-08 21:09:58 +02:00
import net.minecraft.tileentity.TileEntity;
2021-02-27 16:33:00 +01:00
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
2016-05-08 21:09:58 +02:00
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.Capability;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.common.util.LazyOptional;
2016-05-08 21:09:58 +02:00
import net.minecraftforge.items.IItemHandler;
2021-02-26 22:15:48 +01:00
2021-02-27 16:33:00 +01:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2021-02-26 22:15:48 +01:00
import java.util.*;
2016-05-08 21:09:58 +02:00
public class TileEntityItemInterface extends TileEntityBase {
2016-05-08 21:09:58 +02:00
2019-02-27 19:53:05 +01:00
public final List<GenericItemHandlerInfo> genericInfos = new ArrayList<>();
public final Map<Integer, IItemHandlerInfo> itemHandlerInfos = new HashMap<>();
public final List<SlotlessItemHandlerInfo> slotlessInfos = new ArrayList<>();
2017-02-04 16:48:22 +01:00
protected final SlotlessableItemHandlerWrapper itemHandler;
2021-02-27 16:33:00 +01:00
private final LazyOptional<IItemHandler> lazyHandlers;
2016-10-31 19:05:29 +01:00
public TileEntityLaserRelayItem connectedRelay;
private int lastNetworkChangeAmount = -1;
private int slotCount;
public TileEntityItemInterface(TileEntityType<?> type) {
2021-02-27 16:33:00 +01:00
super(type);
2019-05-02 09:10:29 +02:00
IItemHandler normalHandler = new IItemHandler() {
@Override
2019-05-02 09:10:29 +02:00
public int getSlots() {
return TileEntityItemInterface.this.getSlotCount();
}
@Override
2019-05-02 09:10:29 +02:00
public ItemStack getStackInSlot(int slot) {
IItemHandlerInfo handler = TileEntityItemInterface.this.getSwitchedIndexHandler(slot);
2021-02-26 22:15:48 +01:00
if (handler != null && handler.isLoaded()) {
return handler.handler.getStackInSlot(handler.switchedIndex);
}
2017-11-02 22:49:53 +01:00
return StackUtil.getEmpty();
}
@Override
2019-05-02 09:10:29 +02:00
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
IItemHandlerInfo info = TileEntityItemInterface.this.getSwitchedIndexHandler(slot);
if (info != null && info.isLoaded() && TileEntityItemInterface.this.isWhitelisted(info, stack, false)) {
2016-12-27 17:40:27 +01:00
ItemStack remain = info.handler.insertItem(info.switchedIndex, stack, simulate);
2021-11-13 18:02:42 +01:00
if (!ItemStack.isSame(remain, stack) && !simulate) {
TileEntityItemInterface.this.setChanged();
TileEntityItemInterface.this.doItemParticle(stack, info.relayInQuestion.getBlockPos(), TileEntityItemInterface.this.connectedRelay.getBlockPos());
}
2016-12-27 17:40:27 +01:00
return remain;
}
return stack;
}
@Override
2019-05-02 09:10:29 +02:00
public ItemStack extractItem(int slot, int amount, boolean simulate) {
ItemStack stackIn = this.getStackInSlot(slot);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(stackIn)) {
IItemHandlerInfo info = TileEntityItemInterface.this.getSwitchedIndexHandler(slot);
if (info != null && info.isLoaded() && TileEntityItemInterface.this.isWhitelisted(info, stackIn, true)) {
ItemStack extracted = info.handler.extractItem(info.switchedIndex, amount, simulate);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(extracted) && !simulate) {
2021-11-13 18:02:42 +01:00
TileEntityItemInterface.this.setChanged();
TileEntityItemInterface.this.doItemParticle(extracted, TileEntityItemInterface.this.connectedRelay.getBlockPos(), info.relayInQuestion.getBlockPos());
}
return extracted;
}
}
2017-11-02 22:49:53 +01:00
return StackUtil.getEmpty();
}
2016-12-05 17:10:23 +01:00
@Override
2019-05-02 09:10:29 +02:00
public int getSlotLimit(int slot) {
IItemHandlerInfo info = TileEntityItemInterface.this.getSwitchedIndexHandler(slot);
2019-05-02 09:10:29 +02:00
if (info != null && info.isLoaded()) {
2016-12-05 17:10:23 +01:00
return info.handler.getSlotLimit(info.switchedIndex);
2019-05-02 09:10:29 +02:00
} else {
2016-12-05 17:10:23 +01:00
return 0;
}
}
2021-02-27 16:33:00 +01:00
@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return true;
}
};
Object slotlessHandler = null;
2021-02-27 16:33:00 +01:00
// if (ActuallyAdditions.commonCapsLoaded) {
// slotlessHandler = CommonCapsUtil.createSlotlessItemViewerHandler(this, normalHandler);
// }
2021-02-27 16:33:00 +01:00
this.lazyHandlers = LazyOptional.of(() -> normalHandler);
this.itemHandler = new SlotlessableItemHandlerWrapper(this.lazyHandlers, slotlessHandler);
}
public TileEntityItemInterface() {
2021-11-13 18:02:42 +01:00
this(ActuallyBlocks.ITEM_INTERFACE.getTileEntityType());
}
@Override
2021-02-27 16:33:00 +01:00
public LazyOptional<IItemHandler> getItemHandler(Direction facing) {
return this.itemHandler.getNormalHandler();
}
2021-02-27 16:33:00 +01:00
@Nonnull
2019-02-27 19:53:05 +01:00
@Override
2021-02-27 16:33:00 +01:00
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction side) {
return super.getCapability(capability, side);
}
2021-02-27 16:33:00 +01:00
// TODO: [port] Maybe add back
// @SuppressWarnings("unchecked")
// @Override
// public <T> T getCapability(Capability<T> capability, Direction facing) {
// if (ActuallyAdditions.commonCapsLoaded) {
// if (capability == SlotlessItemHandlerConfig.CAPABILITY) {
// Object handler = this.itemHandler.getSlotlessHandler();
// if (handler != null) {
// return (T) handler;
// }
// }
// }
// return super.getCapability(capability, facing);
// }
private int getSlotCount() {
this.queryAndSaveData();
return this.slotCount;
2016-05-08 21:09:58 +02:00
}
2019-05-02 09:10:29 +02:00
public void doItemParticle(ItemStack stack, BlockPos input, BlockPos output) {
2021-11-13 18:02:42 +01:00
if (!this.level.isClientSide) {
2021-02-26 22:15:48 +01:00
CompoundNBT compound = new CompoundNBT();
2021-11-13 18:02:42 +01:00
stack.save(compound);
2016-12-27 17:40:27 +01:00
2021-02-27 16:33:00 +01:00
compound.putDouble("InX", input.getX());
compound.putDouble("InY", input.getY());
compound.putDouble("InZ", input.getZ());
2016-12-27 17:40:27 +01:00
2021-02-27 16:33:00 +01:00
compound.putDouble("OutX", output.getX());
compound.putDouble("OutY", output.getY());
compound.putDouble("OutZ", output.getZ());
2016-12-27 17:40:27 +01:00
2019-05-02 09:10:29 +02:00
int rangeSq = 16 * 16;
2021-11-13 18:02:42 +01:00
for (PlayerEntity player : this.level.players()) {
2021-02-26 22:15:48 +01:00
if (player instanceof ServerPlayerEntity) {
2021-11-13 18:02:42 +01:00
if (player.distanceToSqr(input.getX(), input.getY(), input.getZ()) <= rangeSq || player.distanceToSqr(output.getX(), output.getY(), output.getZ()) <= rangeSq) {
2021-02-27 16:33:00 +01:00
PacketHandler.sendTo(new PacketServerToClient(compound, PacketHandler.LASER_PARTICLE_HANDLER), (ServerPlayerEntity) player);
}
}
}
2016-12-27 17:40:27 +01:00
}
}
2019-05-02 09:10:29 +02:00
private void queryAndSaveData() {
if (this.connectedRelay != null) {
Network network = this.connectedRelay.getNetwork();
2019-05-02 09:10:29 +02:00
if (network != null) {
if (this.lastNetworkChangeAmount != network.changeAmount) {
this.clearInfos();
this.connectedRelay.getItemHandlersInNetwork(network, this.genericInfos);
2019-05-02 09:10:29 +02:00
if (!this.genericInfos.isEmpty()) {
Collections.sort(this.genericInfos);
int slotsQueried = 0;
2019-05-02 09:10:29 +02:00
for (GenericItemHandlerInfo info : this.genericInfos) {
for (SlotlessableItemHandlerWrapper handler : info.handlers) {
2021-02-27 16:33:00 +01:00
LazyOptional<IItemHandler> normalHandler = handler.getNormalHandler();
slotsQueried += normalHandler.map(cap -> {
int queried = 0;
for (int i = 0; i < cap.getSlots(); i++) {
this.itemHandlerInfos.put(queried, new IItemHandlerInfo(cap, i, info.relayInQuestion));
queried++;
}
2021-02-27 16:33:00 +01:00
return queried;
}).orElse(0);
// TODO: [port] add back
// if (ActuallyAdditions.commonCapsLoaded) {
// Object slotlessHandler = handler.getSlotlessHandler();
// if (slotlessHandler instanceof ISlotlessItemHandler) {
// this.slotlessInfos.add(new SlotlessItemHandlerInfo(slotlessHandler, info.relayInQuestion));
// }
// }
}
2016-05-16 22:52:27 +02:00
}
this.slotCount = slotsQueried;
}
this.lastNetworkChangeAmount = network.changeAmount;
2016-05-08 21:09:58 +02:00
}
return;
2016-05-08 21:09:58 +02:00
}
}
this.clearInfos();
this.lastNetworkChangeAmount = -1;
}
2019-05-02 09:10:29 +02:00
private void clearInfos() {
if (!this.genericInfos.isEmpty()) {
this.genericInfos.clear();
}
2019-05-02 09:10:29 +02:00
if (!this.itemHandlerInfos.isEmpty()) {
this.itemHandlerInfos.clear();
}
2019-05-02 09:10:29 +02:00
if (!this.slotlessInfos.isEmpty()) {
this.slotlessInfos.clear();
}
}
2019-05-02 09:10:29 +02:00
private IItemHandlerInfo getSwitchedIndexHandler(int i) {
this.queryAndSaveData();
return this.itemHandlerInfos.get(i);
2016-05-08 21:09:58 +02:00
}
@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() {
TileEntityLaserRelayItem tileFound = null;
2021-11-13 18:02:42 +01:00
if (this.level != null) { //Why is that even possible..?
2019-05-02 09:10:29 +02:00
for (int i = 0; i <= 5; i++) {
Direction side = WorldUtil.getDirectionBySidesInOrder(i);
2021-11-13 18:02:42 +01:00
BlockPos pos = this.getBlockPos().relative(side);
2016-05-08 21:09:58 +02:00
2021-11-13 18:02:42 +01:00
if (this.level.isLoaded(pos)) {
TileEntity tile = this.level.getBlockEntity(pos);
2019-05-02 09:10:29 +02:00
if (tile instanceof TileEntityLaserRelayItem) {
if (tileFound != null) {
this.connectedRelay = null;
return;
2019-05-02 09:10:29 +02:00
} else {
tileFound = (TileEntityLaserRelayItem) tile;
}
2016-05-08 21:09:58 +02:00
}
}
}
}
this.connectedRelay = tileFound;
2016-05-08 21:09:58 +02:00
}
2019-05-02 09:10:29 +02:00
public boolean isWhitelisted(SpecificItemHandlerInfo handler, ItemStack stack, boolean output) {
boolean whitelisted = handler.relayInQuestion.isWhitelisted(stack, output);
TileEntityLaserRelayItem connected = this.connectedRelay;
2019-05-02 09:10:29 +02:00
if (connected != null && connected != handler.relayInQuestion) {
return whitelisted && connected.isWhitelisted(stack, output);
2019-05-02 09:10:29 +02:00
} else {
2016-05-11 20:59:38 +02:00
return whitelisted;
}
}
2019-05-02 09:10:29 +02:00
public static class SlotlessItemHandlerInfo extends SpecificItemHandlerInfo {
public final Object handler;
2019-05-02 09:10:29 +02:00
public SlotlessItemHandlerInfo(Object handler, TileEntityLaserRelayItem relayInQuestion) {
super(relayInQuestion);
this.handler = handler;
}
}
2019-05-02 09:10:29 +02:00
private static class IItemHandlerInfo extends SpecificItemHandlerInfo {
2016-05-19 20:05:12 +02:00
public final IItemHandler handler;
public final int switchedIndex;
2019-05-02 09:10:29 +02:00
public IItemHandlerInfo(IItemHandler handler, int switchedIndex, TileEntityLaserRelayItem relayInQuestion) {
super(relayInQuestion);
this.handler = handler;
this.switchedIndex = switchedIndex;
}
}
2019-05-02 09:10:29 +02:00
private static class SpecificItemHandlerInfo {
public final TileEntityLaserRelayItem relayInQuestion;
2019-05-02 09:10:29 +02:00
public SpecificItemHandlerInfo(TileEntityLaserRelayItem relayInQuestion) {
this.relayInQuestion = relayInQuestion;
}
2019-05-02 09:10:29 +02:00
public boolean isLoaded() {
2021-11-13 18:02:42 +01:00
return this.relayInQuestion.hasLevel() && this.relayInQuestion.getLevel().isLoaded(this.relayInQuestion.getBlockPos());
}
}
2019-05-02 09:10:29 +02:00
public static class GenericItemHandlerInfo implements Comparable<GenericItemHandlerInfo> {
2019-02-27 19:53:05 +01:00
public final List<SlotlessableItemHandlerWrapper> handlers = new ArrayList<>();
2016-05-19 20:05:12 +02:00
public final TileEntityLaserRelayItem relayInQuestion;
2019-05-02 09:10:29 +02:00
public GenericItemHandlerInfo(TileEntityLaserRelayItem relayInQuestion) {
this.relayInQuestion = relayInQuestion;
}
2019-05-02 09:10:29 +02:00
public boolean isLoaded() {
2021-11-13 18:02:42 +01:00
return this.relayInQuestion.hasLevel() && this.relayInQuestion.getLevel().isLoaded(this.relayInQuestion.getBlockPos());
}
@Override
2019-05-02 09:10:29 +02:00
public int compareTo(GenericItemHandlerInfo other) {
2016-11-16 18:51:23 +01:00
int thisPrio = this.relayInQuestion.getPriority();
int otherPrio = other.relayInQuestion.getPriority();
2019-05-02 09:10:29 +02:00
if (thisPrio == otherPrio) {
2016-11-16 18:51:23 +01:00
return 0;
2019-05-02 09:10:29 +02:00
} else if (thisPrio > otherPrio) {
2016-05-16 22:52:27 +02:00
return -1;
2019-05-02 09:10:29 +02:00
} else {
2016-11-16 18:51:23 +01:00
return 1;
}
}
}
2016-05-08 21:09:58 +02:00
}