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; 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 @Override
public int compareTo(GenericItemHandlerInfo other){ public int compareTo(GenericItemHandlerInfo other){
boolean thisWhitelist = this.relayInQuestion instanceof TileEntityLaserRelayItemWhitelist; 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){ private int transferEnergyToReceiverInNeed(EnumFacing from, Network network, int maxTransfer, boolean simulate){
int transmitted = 0; 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>(); List<BlockPos> alreadyChecked = new ArrayList<BlockPos>();
//Go through all of the connections in the network
for(ConnectionPair pair : network.connections){ for(ConnectionPair pair : network.connections){
//Go through both relays in the connection
for(BlockPos relay : pair.positions){ for(BlockPos relay : pair.positions){
if(relay != null && !alreadyChecked.contains(relay)){ if(relay != null && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay); alreadyChecked.add(relay);
@ -106,29 +106,32 @@ public class TileEntityLaserRelayEnergy extends TileEntityLaserRelay implements
EnumFacing side = receiver.getKey(); EnumFacing side = receiver.getKey();
EnumFacing opp = side.getOpposite(); EnumFacing opp = side.getOpposite();
TileEntity tile = receiver.getValue(); TileEntity tile = receiver.getValue();
if(theRelay != this || side != from){ if(!alreadyChecked.contains(tile.getPos())){
if(tile instanceof IEnergyReceiver){ alreadyChecked.add(tile.getPos());
IEnergyReceiver iReceiver = (IEnergyReceiver)tile; if(theRelay != this || side != from){
if(iReceiver.canConnectEnergy(opp)){ if(tile instanceof IEnergyReceiver){
int theoreticalReceived = iReceiver.receiveEnergy(opp, Math.min(maxTransfer, lowestCap)-transmitted, true); IEnergyReceiver iReceiver = (IEnergyReceiver)tile;
int deduct = this.calcDeduction(theoreticalReceived, highestLoss); if(iReceiver.canConnectEnergy(opp)){
transmitted += iReceiver.receiveEnergy(opp, theoreticalReceived-deduct, simulate); int theoreticalReceived = iReceiver.receiveEnergy(opp, Math.min(maxTransfer, lowestCap)-transmitted, true);
transmitted += deduct; int deduct = this.calcDeduction(theoreticalReceived, highestLoss);
transmitted += iReceiver.receiveEnergy(opp, theoreticalReceived-deduct, simulate);
transmitted += deduct;
}
} }
} else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, opp)){
else if(ActuallyAdditions.teslaLoaded && tile.hasCapability(TeslaUtil.teslaConsumer, opp)){ ITeslaConsumer cap = tile.getCapability(TeslaUtil.teslaConsumer, opp);
ITeslaConsumer cap = tile.getCapability(TeslaUtil.teslaConsumer, opp); if(cap != null){
if(cap != null){ int theoreticalReceived = (int)cap.givePower(Math.min(maxTransfer, lowestCap)-transmitted, true);
int theoreticalReceived = (int)cap.givePower(Math.min(maxTransfer, lowestCap)-transmitted, true); int deduct = this.calcDeduction(theoreticalReceived, highestLoss);
int deduct = this.calcDeduction(theoreticalReceived, highestLoss); transmitted += cap.givePower(theoreticalReceived-deduct, simulate);
transmitted += cap.givePower(theoreticalReceived-deduct, simulate); transmitted += deduct;
transmitted += deduct; }
} }
}
//If everything that could be transmitted was transmitted //If everything that could be transmitted was transmitted
if(transmitted >= maxTransfer){ if(transmitted >= maxTransfer){
return transmitted; return transmitted;
}
} }
} }
} }

View file

@ -22,11 +22,13 @@ import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class TileEntityLaserRelayItem extends TileEntityLaserRelay{ 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){ public TileEntityLaserRelayItem(String name){
super(name, true); super(name, true);
@ -48,35 +50,39 @@ public class TileEntityLaserRelayItem extends TileEntityLaserRelay{
EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i); EnumFacing side = WorldUtil.getDirectionBySidesInOrder(i);
BlockPos pos = this.getPos().offset(side); BlockPos pos = this.getPos().offset(side);
TileEntity tile = this.worldObj.getTileEntity(pos); 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()); IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
if(handler != null){ if(handler != null){
this.handlersAround.add(handler); this.handlersAround.put(tile.getPos(), handler);
} }
} }
} }
} }
public List<GenericItemHandlerInfo> getItemHandlersInNetwork(Network network){ 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>(); List<GenericItemHandlerInfo> handlers = new ArrayList<GenericItemHandlerInfo>();
for(ConnectionPair pair : network.connections){ for(ConnectionPair pair : network.connections){
for(BlockPos relay : pair.positions){ for(BlockPos relay : pair.positions){
if(relay != null){ if(relay != null && !alreadyChecked.contains(relay)){
alreadyChecked.add(relay);
TileEntity aRelayTile = this.worldObj.getTileEntity(relay); TileEntity aRelayTile = this.worldObj.getTileEntity(relay);
if(aRelayTile instanceof TileEntityLaserRelayItem){ if(aRelayTile instanceof TileEntityLaserRelayItem){
TileEntityLaserRelayItem relayTile = (TileEntityLaserRelayItem)aRelayTile; TileEntityLaserRelayItem relayTile = (TileEntityLaserRelayItem)aRelayTile;
if(!GenericItemHandlerInfo.containsTile(handlers, relayTile)){ GenericItemHandlerInfo info = new GenericItemHandlerInfo(relayTile);
GenericItemHandlerInfo info = new GenericItemHandlerInfo(relayTile);
List<IItemHandler> handlersAroundTile = relayTile.handlersAround; Map<BlockPos, IItemHandler> handlersAroundTile = relayTile.handlersAround;
for(IItemHandler handler : handlersAroundTile){ for(Map.Entry<BlockPos, IItemHandler> handler : handlersAroundTile.entrySet()){
if(!GenericItemHandlerInfo.containsHandler(handlers, handler)){ if(!alreadyChecked.contains(handler.getKey())){
info.handlers.add(handler); alreadyChecked.add(handler.getKey());
}
info.handlers.add(handler.getValue());
} }
handlers.add(info);
} }
handlers.add(info);
} }
} }
} }

View file

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