From d46202c5ba5fdfa56b6c22f2639a84ef88fbfd82 Mon Sep 17 00:00:00 2001 From: Jamz Date: Fri, 6 Sep 2019 21:42:17 -0500 Subject: [PATCH] Fix for ConcurrentModificationException * Make partialPaths thread safe and Close #328 Signed-off-by: JamzTheMan --- .../client/walker/AbstractZoneWalker.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/rptools/maptool/client/walker/AbstractZoneWalker.java b/src/main/java/net/rptools/maptool/client/walker/AbstractZoneWalker.java index f77bf83e25..e70e0bc839 100644 --- a/src/main/java/net/rptools/maptool/client/walker/AbstractZoneWalker.java +++ b/src/main/java/net/rptools/maptool/client/walker/AbstractZoneWalker.java @@ -14,16 +14,19 @@ */ package net.rptools.maptool.client.walker; -import java.util.ArrayList; -import java.util.List; -import java.util.ListIterator; import net.rptools.maptool.client.ui.zone.RenderPathWorker; import net.rptools.maptool.model.CellPoint; import net.rptools.maptool.model.Path; import net.rptools.maptool.model.Zone; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.ListIterator; + public abstract class AbstractZoneWalker implements ZoneWalker { - protected List partialPaths = new ArrayList(); + protected List partialPaths = + Collections.synchronizedList(new ArrayList()); protected final Zone zone; protected boolean restrictMovement = false; protected RenderPathWorker renderPathWorker; @@ -37,11 +40,10 @@ public Zone getZone() { } public CellPoint getLastPoint() { - if (partialPaths.isEmpty()) { - return null; + synchronized (partialPaths) { + if (partialPaths.isEmpty()) return null; + else return partialPaths.get(partialPaths.size() - 1).end; } - PartialPath lastPath = partialPaths.get(partialPaths.size() - 1); - return lastPath.end; } public void setWaypoints(CellPoint... points) { @@ -96,20 +98,26 @@ public Path getPath() { Path path = new Path(); PartialPath last = null; - for (PartialPath partial : partialPaths) { - if (partial.path != null && partial.path.size() > 1) { - path.addAllPathCells(partial.path.subList(0, partial.path.size() - 1)); + + synchronized (partialPaths) { + for (PartialPath partial : partialPaths) { + if (partial.path != null && partial.path.size() > 1) { + path.addAllPathCells(partial.path.subList(0, partial.path.size() - 1)); + } + last = partial; } - last = partial; } + if (last != null) { path.addPathCell(last.end); } + for (CellPoint cp : path.getCellPath()) { if (isWaypoint(cp)) { path.addWayPoint(cp); } } + return path; }