Re-did the laser relay checking system so that it will never target one block twice

This commit is contained in:
Ellpeck 2016-08-06 02:17:11 +02:00
parent af2ef37554
commit 97c9e0f454
4 changed files with 47 additions and 55 deletions

View file

@ -231,24 +231,6 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{
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;
}
@Override
public int compareTo(GenericItemHandlerInfo other){
boolean thisWhitelist = this.relayInQuestion instanceof TileEntityLaserRelayItemWhitelist;

View file

@ -89,10 +89,10 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
private int transferEnergyToReceiverInNeed(EnumFacing from, Network network, int maxTransfer, boolean simulate){
int transmitted = 0;
//Keeps track of all the Laser Relays and Energy Acceptors that have been checked already to make nothing run multiple times
List<BlockPos> alreadyChecked = new ArrayList<BlockPos>();
//Go through all of the connections in the network
for(ConnectionPair pair : network.connections){
//Go through both relays in the connection
for(BlockPos relay : pair.positions){
if(relay != null && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay);
@ -106,6 +106,8 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
EnumFacing side = receiver.getKey();
EnumFacing opp = side.getOpposite();
TileEntity tile = receiver.getValue();
if(!alreadyChecked.contains(tile.getPos())){
alreadyChecked.add(tile.getPos());
if(theRelay != this || side != from){
if(tile instanceof IEnergyReceiver){
IEnergyReceiver iReceiver = (IEnergyReceiver)tile;
@ -137,6 +139,7 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
}
}
}
}
return transmitted;
}

View file

@ -22,11 +22,13 @@ import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
public final List<IItemHandler> handlersAround = new ArrayList<IItemHandler>();
public final Map<BlockPos, IItemHandler> handlersAround = new HashMap<BlockPos, IItemHandler>();
public TileEntityLaserRelayItem(String name){
super(name, true);
@ -48,30 +50,35 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i);
BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.worldObj.getTileEntity(pos);
if(tile != null && !(tile instanceof TileEntityItemViewer)){
if(tile != null && !(tile instanceof TileEntityItemViewer) && !(tile instanceof TileEntityLaserRelay)){
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
if(handler != null){
this.handlersAround.add(handler);
this.handlersAround.put(tile.getPos(), handler);
}
}
}
}
public List<GenericItemHandlerInfo> getItemHandlersInNetwork(Network network){
//Keeps track of all the Laser Relays and Item Handlers that have been checked already to make nothing run multiple times
List<BlockPos> alreadyChecked = new ArrayList<BlockPos>();
List<GenericItemHandlerInfo> handlers = new ArrayList<GenericItemHandlerInfo>();
for(ConnectionPair pair : network.connections){
for(BlockPos relay : pair.positions){
if(relay != null){
if(relay != null && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay);
TileEntity aRelayTile = this.worldObj.getTileEntity(relay);
if(aRelayTile instanceof TileEntityLaserRelayItem){
TileEntityLaserRelayItem relayTile = (TileEntityLaserRelayItem)aRelayTile;
if(!GenericItemHandlerInfo.containsTile(handlers, relayTile)){
GenericItemHandlerInfo info = new GenericItemHandlerInfo(relayTile);
List<IItemHandler> handlersAroundTile = relayTile.handlersAround;
for(IItemHandler handler : handlersAroundTile){
if(!GenericItemHandlerInfo.containsHandler(handlers, handler)){
info.handlers.add(handler);
Map<BlockPos, IItemHandler> handlersAroundTile = relayTile.handlersAround;
for(Map.Entry<BlockPos, IItemHandler> handler : handlersAroundTile.entrySet()){
if(!alreadyChecked.contains(handler.getKey())){
alreadyChecked.add(handler.getKey());
info.handlers.add(handler.getValue());
}
}
@ -80,7 +87,6 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
}
}
}
}
return handlers;
}
}

View file

@ -18,11 +18,13 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.items.IItemHandler;
import java.util.List;
import java.util.Map;
public class TileEntityLaserRelayItemWhitelist extends TileEntityLaserRelayItem implements IButtonReactor{
@ -204,8 +206,7 @@ public class TileEntityLaserRelayItemWhitelist extends TileEntityLaserRelayItem
private void addWhitelistSmart(boolean output){
FilterSettings usedSettings = output ? this.rightFilter : this.leftFilter;
List<IItemHandler> handlers = this.handlersAround;
for(IItemHandler handler : handlers){
for(IItemHandler handler : this.handlersAround.values()){
for(int i = 0; i < handler.getSlots(); i++){
ItemStack stack = handler.getStackInSlot(i);
if(stack != null){