mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-22 13:10:13 +00:00
Created generic round robin iterator for use by fluid and item conduits.
This commit is contained in:
1 parent
2fb3dc69ec
commit
2d61560f42
3 files changed
+51
-77
No files matched your search
@@ -20,6 +20,7 @@ import crazypants.enderio.conduit.item.filter.IItemFilter;
|
||||
import crazypants.util.BlockCoord;
|
||||
import crazypants.util.InventoryWrapper;
|
||||
import crazypants.util.ItemUtil;
|
||||
import crazypants.util.RoundRobinIterator;
|
||||
|
||||
public class NetworkedInventory {
|
||||
|
||||
@@ -30,7 +31,7 @@ public class NetworkedInventory {
|
||||
int inventorySide;
|
||||
|
||||
List<Target> sendPriority = new ArrayList<Target>();
|
||||
RoundRobinIterable rrIter = new RoundRobinIterable();
|
||||
RoundRobinIterator<Target> rrIter = new RoundRobinIterator<Target>(sendPriority);
|
||||
|
||||
private int extractFromSlot = -1;
|
||||
|
||||
@@ -353,43 +354,6 @@ public class NetworkedInventory {
|
||||
public void setInventorySide(int inventorySide) {
|
||||
this.inventorySide = inventorySide;
|
||||
}
|
||||
|
||||
|
||||
|
||||
class RoundRobinIterable implements Iterable<Target>, Iterator<Target> {
|
||||
|
||||
int index = -1;
|
||||
int currentCount = 0;
|
||||
|
||||
@Override
|
||||
public Iterator<Target> iterator() {
|
||||
currentCount = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !sendPriority.isEmpty() && currentCount <= sendPriority.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Target next() {
|
||||
if(sendPriority.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
currentCount++;
|
||||
index++;
|
||||
if(index >= sendPriority.size()) {
|
||||
index = 0;
|
||||
}
|
||||
return sendPriority.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class Target implements Comparable<Target> {
|
||||
NetworkedInventory inv;
|
||||
|
||||
@@ -16,6 +16,7 @@ import crazypants.enderio.conduit.AbstractConduitNetwork;
|
||||
import crazypants.enderio.conduit.ConnectionMode;
|
||||
import crazypants.util.BlockCoord;
|
||||
import crazypants.util.FluidUtil;
|
||||
import crazypants.util.RoundRobinIterator;
|
||||
|
||||
public class EnderLiquidConduitNetwork extends AbstractConduitNetwork<ILiquidConduit, EnderLiquidConduit> {
|
||||
|
||||
@@ -24,7 +25,7 @@ public class EnderLiquidConduitNetwork extends AbstractConduitNetwork<ILiquidCon
|
||||
|
||||
List<NetworkTank> tanks = new ArrayList<NetworkTank>();
|
||||
|
||||
Map<NetworkTank, RoundRobinIterable> iterators;
|
||||
Map<NetworkTank, RoundRobinIterator<NetworkTank>> iterators;
|
||||
|
||||
public EnderLiquidConduitNetwork() {
|
||||
super(EnderLiquidConduit.class);
|
||||
@@ -93,11 +94,11 @@ public class EnderLiquidConduitNetwork extends AbstractConduitNetwork<ILiquidCon
|
||||
|
||||
private Iterable<NetworkTank> getIteratorForTank(NetworkTank tank) {
|
||||
if(iterators == null) {
|
||||
iterators = new HashMap<NetworkTank, RoundRobinIterable>();
|
||||
iterators = new HashMap<NetworkTank, RoundRobinIterator<NetworkTank>>();
|
||||
}
|
||||
RoundRobinIterable res = iterators.get(tank);
|
||||
RoundRobinIterator<NetworkTank> res = iterators.get(tank);
|
||||
if(res == null) {
|
||||
res = new RoundRobinIterable();
|
||||
res = new RoundRobinIterator<NetworkTank>(tanks);
|
||||
iterators.put(tank, res);
|
||||
}
|
||||
return res;
|
||||
@@ -168,40 +169,5 @@ public class EnderLiquidConduitNetwork extends AbstractConduitNetwork<ILiquidCon
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class RoundRobinIterable implements Iterable<NetworkTank>, Iterator<NetworkTank> {
|
||||
|
||||
int index = -1;
|
||||
int currentCount = 0;
|
||||
|
||||
@Override
|
||||
public Iterator<NetworkTank> iterator() {
|
||||
currentCount = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !tanks.isEmpty() && currentCount <= tanks.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkTank next() {
|
||||
if(tanks.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
currentCount++;
|
||||
index++;
|
||||
if(index >= tanks.size()) {
|
||||
index = 0;
|
||||
}
|
||||
return tanks.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package crazypants.util;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class RoundRobinIterator<T> implements Iterable<T>, Iterator<T> {
|
||||
|
||||
private int index = -1;
|
||||
private int currentCount = 0;
|
||||
private final List<T> itOver;
|
||||
|
||||
public RoundRobinIterator(List<T> itOver) {
|
||||
this.itOver = itOver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
currentCount = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return !itOver.isEmpty() && currentCount <= itOver.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
if(itOver.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
currentCount++;
|
||||
index++;
|
||||
if(index >= itOver.size()) {
|
||||
index = 0;
|
||||
}
|
||||
return itOver.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user