rewrote CapBankNetwork.setNetwork to use a work list instead of recursion - issue #1454

This commit is contained in:
Matthias Mann committed 2014-12-27 21:01:20 +01:00
1 parent e0d160cb65
commit ad9e856307
2 files changed
+25 -11

No files matched your search

@@ -98,17 +98,27 @@ public class CapBankNetwork implements ICapBankNetwork {
protected void setNetwork(World world, TileCapBank cap) {
if(cap != null && cap.setNetwork(this)) {
addMember(cap);
Collection<TileCapBank> neighbours = NetworkUtil.getNeigbours(cap);
for (TileCapBank neighbour : neighbours) {
if(neighbour.getNetwork() == null) {
setNetwork(world, neighbour);
} else if(neighbour.getNetwork() != this) {
neighbour.getNetwork().destroyNetwork();
setNetwork(world, neighbour);
if(cap == null) {
return;
}
Set<TileCapBank> work = new HashSet<TileCapBank>();
for(;;) {
ICapBankNetwork network = cap.getNetwork();
if(network != this) {
if(network != null) {
network.destroyNetwork();
}
if(cap.setNetwork(this)) {
addMember(cap);
NetworkUtil.getNeigbours(cap, work);
}
}
if(work.isEmpty()) {
return;
}
Iterator<TileCapBank> iter = work.iterator();
cap = iter.next();
iter.remove();
}
}
@@ -32,7 +32,12 @@ public class NetworkUtil {
if(!cap.getType().isMultiblock()) {
return Collections.emptyList();
}
List<TileCapBank> res = new ArrayList<TileCapBank>();
Collection<TileCapBank> res = new ArrayList<TileCapBank>();
getNeigbours(cap, res);
return res;
}
public static void getNeigbours(TileCapBank cap, Collection<TileCapBank> res) {
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
BlockCoord bc = cap.getLocation().getLocation(dir);
TileEntity te = cap.getWorldObj().getTileEntity(bc.x, bc.y, bc.z);
@@ -43,7 +48,6 @@ public class NetworkUtil {
}
}
}
return res;
}
private static boolean reuseNetwork(TileCapBank cap, Collection<TileCapBank> neighbours, World world) {