More recursion -> queue for
package mekanism.common.multiblock;
import java.util.ArrayList;
+import java.util.Deque;
import java.util.HashSet;
+import java.util.LinkedList;
import java.util.List;
import java.util.Set;
@@ -35,7 +37,7 @@ public abstract class UpdateProtocol<T extends SynchronizedData<T>>
* Recursively loops through each node connected to the given TileEntity.
* @param coord - the TileEntity coords to loop over
*/
- public void loopThrough(Coord4D coord)
+ public void loopThrough(Coord4D coord, Deque<Coord4D> queue)
{
int origX = coord.x, origY = coord.y, origZ = coord.z;
@@ -221,7 +223,7 @@ public abstract class UpdateProtocol<T extends SynchronizedData<T>>
{
if(!iteratedNodes.contains(sideCoord))
{
- loopThrough(sideCoord);
+ queue.addLast(sideCoord);
}
}
}
@@ -453,7 +455,14 @@ public abstract class UpdateProtocol<T extends SynchronizedData<T>>
*/
public void doUpdate()
{
- loopThrough(Coord4D.get(pointer));
+ Deque<Coord4D> pathingQueue = new LinkedList<>();
+ pathingQueue.add(Coord4D.get(pointer));
+ while (pathingQueue.peek() != null) {
+ Coord4D next = pathingQueue.removeFirst();
+ if (!iteratedNodes.contains(next)) {
+ loopThrough(next, pathingQueue);
+ }
+ }
if(structureFound != null)
{
