diff --git a/src/main/java/me/retrodaredevil/action/ActionQueue.java b/src/main/java/me/retrodaredevil/action/ActionQueue.java index a04c990..bd5085d 100644 --- a/src/main/java/me/retrodaredevil/action/ActionQueue.java +++ b/src/main/java/me/retrodaredevil/action/ActionQueue.java @@ -17,6 +17,8 @@ public interface ActionQueue extends SingleActiveActionHolder, ActionCollection /** * Ends the current action if there is one and removes it. This happens immediately. + *

+ * Note: This is not thread safe * @return true if there was a current action to end, false otherwise */ boolean removeCurrentAction(); @@ -24,6 +26,8 @@ public interface ActionQueue extends SingleActiveActionHolder, ActionCollection * Ends the current action and moves it to the end of the queue if there is one. This happens immediately. *

* NOTE: This requires the current action to be recyclable + *

+ * Note: This is not thread safe * @param doNothingIfEmpty true if you want to do nothing if the queue is empty, false otherwise. * If set to true, the current action will not be ended (nothing will happen). */ @@ -36,6 +40,8 @@ public interface ActionQueue extends SingleActiveActionHolder, ActionCollection * will allow you to stop the current running action and put another one in. *

* NOTE: This requires the current action to be recyclable + *

+ * Note: This is not thread safe * @return true if there was a current action to put at the front of the queue, false otherwise */ boolean moveCurrentToNext(); diff --git a/src/main/java/me/retrodaredevil/action/DequeActionQueue.java b/src/main/java/me/retrodaredevil/action/DequeActionQueue.java index 17b2408..1cd2026 100644 --- a/src/main/java/me/retrodaredevil/action/DequeActionQueue.java +++ b/src/main/java/me/retrodaredevil/action/DequeActionQueue.java @@ -40,11 +40,14 @@ public DequeActionQueue(boolean canRecycle, Deque actionQueue, boolean c @Override public Action getActiveAction() { - return currentAction; + synchronized (this) { + return currentAction; + } } @Override public Collection getActiveActions() { + Action currentAction = getActiveAction(); if(currentAction == null){ return Collections.emptySet(); } @@ -57,18 +60,24 @@ public boolean add(Action action){ if(action == this){ throw new IllegalArgumentException(); } - actionQueue.addLast(action); + synchronized (this) { + actionQueue.addLast(action); + } return true; } @Override public boolean removeQueued(Action action) { - return actionQueue.remove(action); + synchronized (this) { + return actionQueue.remove(action); + } } @Override public void clear() { - actionQueue.clear(); + synchronized (this) { + actionQueue.clear(); + } } /** @@ -82,7 +91,9 @@ public boolean addBeginning(Action action){ if(action.isActive()){ throw new IllegalArgumentException("action cannot be active when you add it!"); } - actionQueue.addFirst(action); + synchronized (this) { + actionQueue.addFirst(action); + } return true; } @@ -102,11 +113,16 @@ public boolean removeCurrentAction(){ @Override public boolean moveCurrentToEnd(boolean doNothingIfEmpty){ - if((doNothingIfEmpty && actionQueue.isEmpty()) || currentAction == null) - return false; + synchronized (this) { + if ((doNothingIfEmpty && actionQueue.isEmpty()) || currentAction == null) { + return false; + } + } currentAction.end(); - actionQueue.addLast(currentAction); + synchronized (this) { + actionQueue.addLast(currentAction); + } currentAction = null; return true; } @@ -116,7 +132,9 @@ public boolean moveCurrentToNext(){ return false; currentAction.end(); - actionQueue.addFirst(currentAction); + synchronized (this) { + actionQueue.addFirst(currentAction); + } currentAction = null; return true; } @@ -128,7 +146,9 @@ protected void onUpdate() { } private void updateAction(){ if(currentAction == null){ - currentAction = actionQueue.poll(); + synchronized (this) { + currentAction = actionQueue.poll(); + } } if(currentAction != null){ currentAction.update(); @@ -154,14 +174,18 @@ protected void onEnd(boolean peacefullyEnded) { } if(clearQueuedOnEnd){ currentAction = null; - actionQueue.clear(); + synchronized (this) { + actionQueue.clear(); + } } } @Override protected void onIsDoneRequest() { if (canBeDone) { - setDone(currentAction == null && actionQueue.isEmpty()); + synchronized (this) { + setDone(currentAction == null && actionQueue.isEmpty()); + } } } }