From 97c9e0f45444ccfdde48986f22f4311430885a6e Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 6 Aug 2016 02:17:11 +0200 Subject: [PATCH] Re-did the laser relay checking system so that it will never target one block twice --- .../mod/tile/TileEntityItemViewer.java | 18 ------- .../mod/tile/TileEntityLaserRelayEnergy.java | 47 ++++++++++--------- .../mod/tile/TileEntityLaserRelayItem.java | 32 ++++++++----- .../TileEntityLaserRelayItemWhitelist.java | 5 +- 4 files changed, 47 insertions(+), 55 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 e077ea8ef..ebf56a478 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityItemViewer.java @@ -231,24 +231,6 @@ public class TileEntityItemViewer extends TileEntityInventoryBase{ 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; - } - @Override public int compareTo(GenericItemHandlerInfo other){ boolean thisWhitelist = this.relayInQuestion instanceof TileEntityLaserRelayItemWhitelist; diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayEnergy.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayEnergy.java index a1e130084..2085346fd 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayEnergy.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayEnergy.java @@ -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 alreadyChecked = new ArrayList(); - //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,29 +106,32 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements EnumFacing side = receiver.getKey(); EnumFacing opp = side.getOpposite(); TileEntity tile = receiver.getValue(); - if(theRelay != this || side != from){ - if(tile instanceof IEnergyReceiver){ - IEnergyReceiver iReceiver = (IEnergyReceiver)tile; - if(iReceiver.canConnectEnergy(opp)){ - int theoreticalReceived = iReceiver.receiveEnergy(opp, Math.min(maxTransfer, lowestCap)-transmitted, true); - int deduct = this.calcDeduction(theoreticalReceived, highestLoss); - transmitted += iReceiver.receiveEnergy(opp, theoreticalReceived-deduct, simulate); - transmitted += deduct; + if(!alreadyChecked.contains(tile.getPos())){ + alreadyChecked.add(tile.getPos()); + if(theRelay != this || side != from){ + if(tile instanceof IEnergyReceiver){ + IEnergyReceiver iReceiver = (IEnergyReceiver)tile; + if(iReceiver.canConnectEnergy(opp)){ + int theoreticalReceived = iReceiver.receiveEnergy(opp, Math.min(maxTransfer, lowestCap)-transmitted, true); + int deduct = this.calcDeduction(theoreticalReceived, highestLoss); + transmitted += iReceiver.receiveEnergy(opp, theoreticalReceived-deduct, simulate); + transmitted += deduct; + } } - } - else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, opp)){ - ITeslaConsumer cap = tile.getCapability(TeslaUtil.teslaConsumer, opp); - if(cap != null){ - int theoreticalReceived = (int)cap.givePower(Math.min(maxTransfer, lowestCap)-transmitted, true); - int deduct = this.calcDeduction(theoreticalReceived, highestLoss); - transmitted += cap.givePower(theoreticalReceived-deduct, simulate); - transmitted += deduct; + else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, opp)){ + ITeslaConsumer cap = tile.getCapability(TeslaUtil.teslaConsumer, opp); + if(cap != null){ + int theoreticalReceived = (int)cap.givePower(Math.min(maxTransfer, lowestCap)-transmitted, true); + int deduct = this.calcDeduction(theoreticalReceived, highestLoss); + transmitted += cap.givePower(theoreticalReceived-deduct, simulate); + transmitted += deduct; + } } - } - //If everything that could be transmitted was transmitted - if(transmitted >= maxTransfer){ - return transmitted; + //If everything that could be transmitted was transmitted + if(transmitted >= maxTransfer){ + return transmitted; + } } } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java index 9ea4837c0..8dcb1f6b7 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItem.java @@ -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 handlersAround = new ArrayList(); + public final Map handlersAround = new HashMap(); public TileEntityLaserRelayItem(String name){ super(name, true); @@ -48,35 +50,39 @@ 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 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 alreadyChecked = new ArrayList(); + List handlers = new ArrayList(); 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); + GenericItemHandlerInfo info = new GenericItemHandlerInfo(relayTile); - List handlersAroundTile = relayTile.handlersAround; - for(IItemHandler handler : handlersAroundTile){ - if(!GenericItemHandlerInfo.containsHandler(handlers, handler)){ - info.handlers.add(handler); - } + Map handlersAroundTile = relayTile.handlersAround; + for(Map.Entry handler : handlersAroundTile.entrySet()){ + if(!alreadyChecked.contains(handler.getKey())){ + alreadyChecked.add(handler.getKey()); + + info.handlers.add(handler.getValue()); } - - handlers.add(info); } + + handlers.add(info); } } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItemWhitelist.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItemWhitelist.java index 70d9f4cc2..743d425dd 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItemWhitelist.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLaserRelayItemWhitelist.java @@ -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 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){