From b0ecc87a238574677263e48125922fd691beae53 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 9 May 2016 19:05:20 +0200 Subject: [PATCH] Reworked item system a bit to allow for black- and whitelisting --- .../mod/tile/TileEntityItemViewer.java | 118 ++++++++++++------ .../mod/tile/TileEntityLaserRelay.java | 30 +++-- 2 files changed, 102 insertions(+), 46 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewer.java index da5186ba4..6d238157c 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewer.java @@ -1,14 +1,15 @@ package de.ellpeck.actuallyadditions.mod.tile; import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler; +import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelay.TileEntityLaserRelayItem; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraftforge.items.IItemHandler; -import org.apache.commons.lang3.tuple.Pair; +import java.util.ArrayList; import java.util.List; public class TileEntityItemViewer extends TileEntityInventoryBase{ @@ -17,8 +18,8 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{ super(0, "itemViewer"); } - private List getItemHandlers(){ - TileEntityLaserRelay.TileEntityLaserRelayItem relay = this.getConnectedRelay(); + private List getItemHandlerInfos(){ + TileEntityLaserRelayItem relay = this.getConnectedRelay(); if(relay != null){ LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().getNetworkFor(relay.getPos()); if(network != null){ @@ -28,37 +29,39 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{ return null; } - private Pair getSwitchedIndexHandler(int i){ - List handlers = this.getItemHandlers(); + private SpecificItemHandlerInfo getSwitchedIndexHandler(int i){ + List infos = this.getItemHandlerInfos(); int currentI = 0; - if(handlers != null && !handlers.isEmpty()){ - for(IItemHandler handler : handlers){ - int slotAmount = handler.getSlots(); - if(currentI+slotAmount > i){ - return Pair.of(handler, i-currentI); - } - else{ - currentI += slotAmount; + if(infos != null && !infos.isEmpty()){ + for(GenericItemHandlerInfo info : infos){ + for(IItemHandler handler : info.handlers){ + int slotAmount = handler.getSlots(); + if(currentI+slotAmount > i){ + return new SpecificItemHandlerInfo(handler, i-currentI, info.relayInQuestion); + } + else{ + currentI += slotAmount; + } } } } return null; } - private TileEntityLaserRelay.TileEntityLaserRelayItem getConnectedRelay(){ - TileEntityLaserRelay.TileEntityLaserRelayItem tileFound = null; + private TileEntityLaserRelayItem getConnectedRelay(){ + TileEntityLaserRelayItem tileFound = null; if(this.worldObj != null){ //Why is that even possible..? for(int i = 0; i <= 5; i++){ EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i); BlockPos pos = WorldUtil.getCoordsFromSide(side, this.getPos(), 0); TileEntity tile = this.worldObj.getTileEntity(pos); - if(tile instanceof TileEntityLaserRelay.TileEntityLaserRelayItem){ + if(tile instanceof TileEntityLaserRelayItem){ if(tileFound != null){ return null; } else{ - tileFound = (TileEntityLaserRelay.TileEntityLaserRelayItem)tile; + tileFound = (TileEntityLaserRelayItem)tile; } } } @@ -73,10 +76,10 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{ @Override public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction){ - Pair handler = this.getSwitchedIndexHandler(index); + SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index); if(handler != null){ - if(ItemStack.areItemsEqual(handler.getKey().getStackInSlot(handler.getValue()), stack)){ - ItemStack gaveBack = handler.getKey().extractItem(handler.getValue(), stack.stackSize, true); + if(ItemStack.areItemsEqual(handler.handler.getStackInSlot(handler.switchedIndex), stack)){ + ItemStack gaveBack = handler.handler.extractItem(handler.switchedIndex, stack.stackSize, true); return gaveBack != null; } } @@ -85,9 +88,9 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{ @Override public boolean isItemValidForSlot(int index, ItemStack stack){ - Pair handler = this.getSwitchedIndexHandler(index); + SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index); if(handler != null){ - ItemStack gaveBack = handler.getKey().insertItem(handler.getValue(), stack, true); + ItemStack gaveBack = handler.handler.insertItem(handler.switchedIndex, stack, true); return !ItemStack.areItemStacksEqual(gaveBack, stack); } return false; @@ -103,14 +106,14 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{ @Override public void setInventorySlotContents(int i, ItemStack stack){ if(stack != null){ - Pair handler = this.getSwitchedIndexHandler(i); + SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i); if(handler != null){ ItemStack toInsert = stack.copy(); - ItemStack inSlot = handler.getKey().getStackInSlot(handler.getValue()); + ItemStack inSlot = handler.handler.getStackInSlot(handler.switchedIndex); if(inSlot != null){ toInsert.stackSize -= inSlot.stackSize; } - handler.getKey().insertItem(handler.getValue(), toInsert, false); + handler.handler.insertItem(handler.switchedIndex, toInsert, false); } } else{ @@ -121,10 +124,12 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{ @Override public int getSizeInventory(){ int size = 0; - List handlers = this.getItemHandlers(); - if(handlers != null){ - for(IItemHandler handler : handlers){ - size += handler.getSlots(); + List infos = this.getItemHandlerInfos(); + if(infos != null){ + for(GenericItemHandlerInfo info : infos){ + for(IItemHandler handler : info.handlers){ + size += handler.getSlots(); + } } } return size; @@ -132,32 +137,73 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{ @Override public ItemStack getStackInSlot(int i){ - Pair handler = this.getSwitchedIndexHandler(i); + SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i); if(handler != null){ - return handler.getKey().getStackInSlot(handler.getValue()); + return handler.handler.getStackInSlot(handler.switchedIndex); } return null; } @Override public ItemStack decrStackSize(int i, int j){ - Pair handler = this.getSwitchedIndexHandler(i); + SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i); if(handler != null){ - return handler.getKey().extractItem(handler.getValue(), j, false); + return handler.handler.extractItem(handler.switchedIndex, j, false); } return null; } @Override public ItemStack removeStackFromSlot(int index){ - Pair handler = this.getSwitchedIndexHandler(index); + SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index); if(handler != null){ - ItemStack stackInSlot = handler.getKey().getStackInSlot(handler.getValue()); + ItemStack stackInSlot = handler.handler.getStackInSlot(handler.switchedIndex); if(stackInSlot != null){ - handler.getKey().extractItem(handler.getValue(), stackInSlot.stackSize, false); + handler.handler.extractItem(handler.switchedIndex, stackInSlot.stackSize, false); } return stackInSlot; } return null; } + + private static class SpecificItemHandlerInfo{ + + public IItemHandler handler; + public int switchedIndex; + public TileEntityLaserRelayItem relayInQuestion; + + public SpecificItemHandlerInfo(IItemHandler handler, int switchedIndex, TileEntityLaserRelayItem relayInQuestion){ + this.handler = handler; + this.switchedIndex = switchedIndex; + this.relayInQuestion = relayInQuestion; + } + } + + public static class GenericItemHandlerInfo{ + + public List handlers = new ArrayList(); + public TileEntityLaserRelayItem relayInQuestion; + + public GenericItemHandlerInfo(TileEntityLaserRelayItem relayInQuestion){ + this.relayInQuestion = relayInQuestion; + } + + public static boolean containsHandler(List infos, IItemHandler handler){ + for(GenericItemHandlerInfo info : infos){ + if(info.handlers.contains(handler)){ + return true; + } + } + return false; + } + + public static boolean containsTile(List infos, TileEntityLaserRelayItem tile){ + for(GenericItemHandlerInfo info : infos){ + if(info.relayInQuestion == tile){ + return true; + } + } + return false; + } + } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java index 0f09451f0..ef1963b19 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelay.java @@ -15,6 +15,7 @@ import de.ellpeck.actuallyadditions.mod.config.ConfigValues; import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues; import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler; import de.ellpeck.actuallyadditions.mod.network.PacketParticle; +import de.ellpeck.actuallyadditions.mod.tile.TileEntityItemViewer.GenericItemHandlerInfo; import de.ellpeck.actuallyadditions.mod.util.PosUtil; import de.ellpeck.actuallyadditions.mod.util.Util; import de.ellpeck.actuallyadditions.mod.util.WorldUtil; @@ -28,6 +29,7 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; +import sun.net.www.content.text.Generic; import java.util.ArrayList; import java.util.List; @@ -115,21 +117,29 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{ super("laserRelayItem", true); } - public List getItemHandlersInNetwork(LaserRelayConnectionHandler.Network network){ - List handlers = new ArrayList(); + public List getItemHandlersInNetwork(LaserRelayConnectionHandler.Network network){ + List handlers = new ArrayList(); for(LaserRelayConnectionHandler.ConnectionPair pair : network.connections){ BlockPos[] relays = new BlockPos[]{pair.firstRelay, pair.secondRelay}; for(BlockPos relay : relays){ if(relay != null){ - for(int i = 0; i <= 5; i++){ - EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i); - BlockPos pos = WorldUtil.getCoordsFromSide(side, relay, 0); - TileEntity tile = this.worldObj.getTileEntity(pos); - if(tile != null && !(tile instanceof TileEntityItemViewer)){ - IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite()); - if(handler != null && !handlers.contains(handler)){ - handlers.add(handler); + TileEntity aRelayTile = this.worldObj.getTileEntity(relay); + if(aRelayTile instanceof TileEntityLaserRelayItem){ + TileEntityLaserRelayItem relayTile = (TileEntityLaserRelayItem)aRelayTile; + if(!GenericItemHandlerInfo.containsTile(handlers, relayTile)){ + GenericItemHandlerInfo info = new GenericItemHandlerInfo(relayTile); + for(int i = 0; i <= 5; i++){ + EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i); + BlockPos pos = WorldUtil.getCoordsFromSide(side, relay, 0); + TileEntity tile = this.worldObj.getTileEntity(pos); + if(tile != null && !(tile instanceof TileEntityItemViewer)){ + IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite()); + if(handler != null && !GenericItemHandlerInfo.containsHandler(handlers, handler)){ + info.handlers.add(handler); + } + } } + handlers.add(info); } } }