Reworked item system a bit to allow for black- and whitelisting

This commit is contained in:
Ellpeck 2016-05-09 19:05:20 +02:00
parent b4fde39a33
commit b0ecc87a23
2 changed files with 102 additions and 46 deletions

View file

@ -1,14 +1,15 @@
package de.ellpeck.actuallyadditions.mod.tile; package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler; import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelay.TileEntityLaserRelayItem;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TileEntityItemViewer extends TileEntityInventoryBase{ public class TileEntityItemViewer extends TileEntityInventoryBase{
@ -17,8 +18,8 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
super(0, "itemViewer"); super(0, "itemViewer");
} }
private List<IItemHandler> getItemHandlers(){ private List<GenericItemHandlerInfo> getItemHandlerInfos(){
TileEntityLaserRelay.TileEntityLaserRelayItem relay = this.getConnectedRelay(); TileEntityLaserRelayItem relay = this.getConnectedRelay();
if(relay != null){ if(relay != null){
LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().getNetworkFor(relay.getPos()); LaserRelayConnectionHandler.Network network = LaserRelayConnectionHandler.getInstance().getNetworkFor(relay.getPos());
if(network != null){ if(network != null){
@ -28,37 +29,39 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
return null; return null;
} }
private Pair<IItemHandler, Integer> getSwitchedIndexHandler(int i){ private SpecificItemHandlerInfo getSwitchedIndexHandler(int i){
List<IItemHandler> handlers = this.getItemHandlers(); List<GenericItemHandlerInfo> infos = this.getItemHandlerInfos();
int currentI = 0; int currentI = 0;
if(handlers != null && !handlers.isEmpty()){ if(infos != null && !infos.isEmpty()){
for(IItemHandler handler : handlers){ for(GenericItemHandlerInfo info : infos){
for(IItemHandler handler : info.handlers){
int slotAmount = handler.getSlots(); int slotAmount = handler.getSlots();
if(currentI+slotAmount > i){ if(currentI+slotAmount > i){
return Pair.of(handler, i-currentI); return new SpecificItemHandlerInfo(handler, i-currentI, info.relayInQuestion);
} }
else{ else{
currentI += slotAmount; currentI += slotAmount;
} }
} }
} }
}
return null; return null;
} }
private TileEntityLaserRelay.TileEntityLaserRelayItem getConnectedRelay(){ private TileEntityLaserRelayItem getConnectedRelay(){
TileEntityLaserRelay.TileEntityLaserRelayItem tileFound = null; TileEntityLaserRelayItem tileFound = null;
if(this.worldObj != null){ //Why is that even possible..? if(this.worldObj != null){ //Why is that even possible..?
for(int i = 0; i <= 5; i++){ for(int i = 0; i <= 5; i++){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i); EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i);
BlockPos pos = WorldUtil.getCoordsFromSide(side, this.getPos(), 0); BlockPos pos = WorldUtil.getCoordsFromSide(side, this.getPos(), 0);
TileEntity tile = this.worldObj.getTileEntity(pos); TileEntity tile = this.worldObj.getTileEntity(pos);
if(tile instanceof TileEntityLaserRelay.TileEntityLaserRelayItem){ if(tile instanceof TileEntityLaserRelayItem){
if(tileFound != null){ if(tileFound != null){
return null; return null;
} }
else{ else{
tileFound = (TileEntityLaserRelay.TileEntityLaserRelayItem)tile; tileFound = (TileEntityLaserRelayItem)tile;
} }
} }
} }
@ -73,10 +76,10 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
@Override @Override
public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction){ public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction){
Pair<IItemHandler, Integer> handler = this.getSwitchedIndexHandler(index); SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index);
if(handler != null){ if(handler != null){
if(ItemStack.areItemsEqual(handler.getKey().getStackInSlot(handler.getValue()), stack)){ if(ItemStack.areItemsEqual(handler.handler.getStackInSlot(handler.switchedIndex), stack)){
ItemStack gaveBack = handler.getKey().extractItem(handler.getValue(), stack.stackSize, true); ItemStack gaveBack = handler.handler.extractItem(handler.switchedIndex, stack.stackSize, true);
return gaveBack != null; return gaveBack != null;
} }
} }
@ -85,9 +88,9 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
@Override @Override
public boolean isItemValidForSlot(int index, ItemStack stack){ public boolean isItemValidForSlot(int index, ItemStack stack){
Pair<IItemHandler, Integer> handler = this.getSwitchedIndexHandler(index); SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index);
if(handler != null){ 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 !ItemStack.areItemStacksEqual(gaveBack, stack);
} }
return false; return false;
@ -103,14 +106,14 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
@Override @Override
public void setInventorySlotContents(int i, ItemStack stack){ public void setInventorySlotContents(int i, ItemStack stack){
if(stack != null){ if(stack != null){
Pair<IItemHandler, Integer> handler = this.getSwitchedIndexHandler(i); SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i);
if(handler != null){ if(handler != null){
ItemStack toInsert = stack.copy(); ItemStack toInsert = stack.copy();
ItemStack inSlot = handler.getKey().getStackInSlot(handler.getValue()); ItemStack inSlot = handler.handler.getStackInSlot(handler.switchedIndex);
if(inSlot != null){ if(inSlot != null){
toInsert.stackSize -= inSlot.stackSize; toInsert.stackSize -= inSlot.stackSize;
} }
handler.getKey().insertItem(handler.getValue(), toInsert, false); handler.handler.insertItem(handler.switchedIndex, toInsert, false);
} }
} }
else{ else{
@ -121,43 +124,86 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
@Override @Override
public int getSizeInventory(){ public int getSizeInventory(){
int size = 0; int size = 0;
List<IItemHandler> handlers = this.getItemHandlers(); List<GenericItemHandlerInfo> infos = this.getItemHandlerInfos();
if(handlers != null){ if(infos != null){
for(IItemHandler handler : handlers){ for(GenericItemHandlerInfo info : infos){
for(IItemHandler handler : info.handlers){
size += handler.getSlots(); size += handler.getSlots();
} }
} }
}
return size; return size;
} }
@Override @Override
public ItemStack getStackInSlot(int i){ public ItemStack getStackInSlot(int i){
Pair<IItemHandler, Integer> handler = this.getSwitchedIndexHandler(i); SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i);
if(handler != null){ if(handler != null){
return handler.getKey().getStackInSlot(handler.getValue()); return handler.handler.getStackInSlot(handler.switchedIndex);
} }
return null; return null;
} }
@Override @Override
public ItemStack decrStackSize(int i, int j){ public ItemStack decrStackSize(int i, int j){
Pair<IItemHandler, Integer> handler = this.getSwitchedIndexHandler(i); SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(i);
if(handler != null){ if(handler != null){
return handler.getKey().extractItem(handler.getValue(), j, false); return handler.handler.extractItem(handler.switchedIndex, j, false);
} }
return null; return null;
} }
@Override @Override
public ItemStack removeStackFromSlot(int index){ public ItemStack removeStackFromSlot(int index){
Pair<IItemHandler, Integer> handler = this.getSwitchedIndexHandler(index); SpecificItemHandlerInfo handler = this.getSwitchedIndexHandler(index);
if(handler != null){ if(handler != null){
ItemStack stackInSlot = handler.getKey().getStackInSlot(handler.getValue()); ItemStack stackInSlot = handler.handler.getStackInSlot(handler.switchedIndex);
if(stackInSlot != null){ if(stackInSlot != null){
handler.getKey().extractItem(handler.getValue(), stackInSlot.stackSize, false); handler.handler.extractItem(handler.switchedIndex, stackInSlot.stackSize, false);
} }
return stackInSlot; return stackInSlot;
} }
return null; 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<IItemHandler> handlers = new ArrayList<IItemHandler>();
public TileEntityLaserRelayItem relayInQuestion;
public GenericItemHandlerInfo(TileEntityLaserRelayItem relayInQuestion){
this.relayInQuestion = relayInQuestion;
}
public static boolean containsHandler(List<GenericItemHandlerInfo> infos, IItemHandler handler){
for(GenericItemHandlerInfo info : infos){
if(info.handlers.contains(handler)){
return true;
}
}
return false;
}
public static boolean containsTile(List<GenericItemHandlerInfo> infos, TileEntityLaserRelayItem tile){
for(GenericItemHandlerInfo info : infos){
if(info.relayInQuestion == tile){
return true;
}
}
return false;
}
}
} }

View file

@ -15,6 +15,7 @@ import de.ellpeck.actuallyadditions.mod.config.ConfigValues;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues; import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler; import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketParticle; 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.PosUtil;
import de.ellpeck.actuallyadditions.mod.util.Util; import de.ellpeck.actuallyadditions.mod.util.Util;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil; 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.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import sun.net.www.content.text.Generic;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -115,23 +117,31 @@ public abstract class TileEntityLaserRelay extends TileEntityBase{
super("laserRelayItem", true); super("laserRelayItem", true);
} }
public List<IItemHandler> getItemHandlersInNetwork(LaserRelayConnectionHandler.Network network){ public List<GenericItemHandlerInfo> getItemHandlersInNetwork(LaserRelayConnectionHandler.Network network){
List<IItemHandler> handlers = new ArrayList<IItemHandler>(); List<GenericItemHandlerInfo> handlers = new ArrayList<GenericItemHandlerInfo>();
for(LaserRelayConnectionHandler.ConnectionPair pair : network.connections){ for(LaserRelayConnectionHandler.ConnectionPair pair : network.connections){
BlockPos[] relays = new BlockPos[]{pair.firstRelay, pair.secondRelay}; BlockPos[] relays = new BlockPos[]{pair.firstRelay, pair.secondRelay};
for(BlockPos relay : relays){ for(BlockPos relay : relays){
if(relay != null){ if(relay != null){
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++){ for(int i = 0; i <= 5; i++){
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i); EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i);
BlockPos pos = WorldUtil.getCoordsFromSide(side, relay, 0); BlockPos pos = WorldUtil.getCoordsFromSide(side, relay, 0);
TileEntity tile = this.worldObj.getTileEntity(pos); TileEntity tile = this.worldObj.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityItemViewer)){ if(tile != null && !(tile instanceof TileEntityItemViewer)){
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite()); IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
if(handler != null && !handlers.contains(handler)){ if(handler != null && !GenericItemHandlerInfo.containsHandler(handlers, handler)){
handlers.add(handler); info.handlers.add(handler);
} }
} }
} }
handlers.add(info);
}
}
} }
} }
} }