From ae4b4c87022ed4947947c52d8e34753017bbcd90 Mon Sep 17 00:00:00 2001 From: Kam Date: Mon, 28 Apr 2025 03:25:01 -0400 Subject: [PATCH] Heavy Action Refactoring --- src/api | 2 +- .../npcs/controllers/data/action/Action.java | 228 +++++++++ .../controllers/data/action/ActionChain.java | 35 ++ .../data/action/ConditionalAction.java | 142 ++++++ .../data/action/ParallelActionChain.java | 35 ++ .../npcs/scripted/ScriptedActionManager.java | 482 +----------------- 6 files changed, 464 insertions(+), 460 deletions(-) create mode 100644 src/main/java/noppes/npcs/controllers/data/action/Action.java create mode 100644 src/main/java/noppes/npcs/controllers/data/action/ActionChain.java create mode 100644 src/main/java/noppes/npcs/controllers/data/action/ConditionalAction.java create mode 100644 src/main/java/noppes/npcs/controllers/data/action/ParallelActionChain.java diff --git a/src/api b/src/api index d70face41..9590f9fff 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit d70face418a71ccf8d393f051c4ab2da909960cc +Subproject commit 9590f9ffff6c33c96e1f6079a38ff87a727aed4c diff --git a/src/main/java/noppes/npcs/controllers/data/action/Action.java b/src/main/java/noppes/npcs/controllers/data/action/Action.java new file mode 100644 index 000000000..9dfabf688 --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/action/Action.java @@ -0,0 +1,228 @@ +package noppes.npcs.controllers.data.action; + +import noppes.npcs.api.handler.data.IAction; +import noppes.npcs.scripted.ScriptedActionManager; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; +import java.util.function.Consumer; + +public class Action implements IAction { + protected final ScriptedActionManager manager; + protected final String name; + protected int startAfterTicks; + protected int count; + protected int duration; + protected int maxDuration = -1; + protected int updateEveryXTick = 5; + protected Consumer task; + private boolean done; + private final Map dataStore = new HashMap<>(); + + public Action(ScriptedActionManager manager, String name) { + this.manager = manager; + this.name = name; + } + + public Action(ScriptedActionManager manager, Consumer task) { + this(manager, task.toString(), task); + } + + public Action(ScriptedActionManager manager, String name, int maxDuration, int startAfterTicks, Consumer task) { + this.manager = manager; + this.name = name; + this.maxDuration = maxDuration; + this.startAfterTicks = startAfterTicks; + this.task = task; + } + + public Action(ScriptedActionManager manager, String name, int startAfterTicks, Consumer task) { + this(manager, name, Integer.MAX_VALUE, startAfterTicks, task); + } + + public Action(ScriptedActionManager manager, String name, Consumer task) { + this(manager, name, Integer.MAX_VALUE, 0, task); + } + + public Action(ScriptedActionManager manager, int startAfterTicks, Consumer task) { + this(manager, task); + this.startAfterTicks = startAfterTicks; + } + + @Override + public IAction setTask(Consumer task) { + this.task = task; + return this; + } + + public void tick(int ticksExisted) { + if (done) return; + if (startAfterTicks > 0) { + startAfterTicks--; + return; + } + if (maxDuration != -1 && duration >= maxDuration) { + markDone(); + return; + } + if (ticksExisted % updateEveryXTick == 0 && task != null) { + task.accept(this); + count++; + } + duration++; + } + + @Override + public int getCount() { + return count; + } + + @Override + public int getDuration() { + return duration; + } + + @Override + public String getName() { + return name; + } + + @Override + public int getMaxDuration() { + return maxDuration; + } + + @Override + public IAction setMaxDuration(int x) { + this.maxDuration = x; + return this; + } + + @Override + public void markDone() { + done = true; + } + + @Override + public boolean isDone() { + return done; + } + + @Override + public Object getData(String key) { + return dataStore.get(key); + } + + @Override + public IAction addData(String key, Object v) { + dataStore.put(key, v); + return this; + } + + @Override + public IAction removeData(String key) { + dataStore.remove(key); + return this; + } + + @Override + public int getUpdateEveryXTick() { + return updateEveryXTick; + } + + @Override + public IAction setUpdateEveryXTick(int x) { + this.updateEveryXTick = x; + return this; + } + + @Override + public int getStartAfterTicks() { + return startAfterTicks; + } + + @Override + public IAction pauseFor(int ticks) { + this.startAfterTicks = ticks; + return this; + } + + @Override + public IAction getNext() { + int idx = manager.getIndex(this); + LinkedList list = (LinkedList) manager.getActionQueue(); + return (idx >= 0 && idx + 1 < list.size()) ? list.get(idx + 1) : null; + } + + @Override + public IAction getPrevious() { + int idx = manager.getIndex(this); + LinkedList list = (LinkedList) manager.getActionQueue(); + return (idx > 0) ? list.get(idx - 1) : null; + } + + @Override + public IAction after(IAction after) { + int idx = manager.getIndex(this); + if (idx >= 0) manager.scheduleActionAt(idx + 1, after); + return after; + } + + @Override + public IAction after(String name, int maxDuration, int delay, Consumer t) { + return after(manager.create(name, maxDuration, delay, t)); + } + + @Override + public IAction after(String name, int delay, Consumer t) { + return after(manager.create(name, delay, t)); + } + + @Override + public IAction after(int delay, Consumer t) { + return after(manager.create(delay, t)); + } + + @Override + public IAction after(String name, Consumer t) { + return after(manager.create(name, t)); + } + + @Override + public IAction after(Consumer t) { + return after(manager.create(t)); + } + + @Override + public IAction before(IAction before) { + int idx = manager.getIndex(this); + if (idx >= 0) manager.scheduleActionAt(Math.max(0, idx), before); + return before; + } + + @Override + public IAction before(String name, int maxDuration, int delay, Consumer t) { + return before(manager.create(name, maxDuration, delay, t)); + } + + @Override + public IAction before(String name, int delay, Consumer t) { + return before(manager.create(name, delay, t)); + } + + @Override + public IAction before(int delay, Consumer t) { + return before(manager.create(delay, t)); + } + + @Override + public IAction before(String name, Consumer t) { + return before(manager.create(name, t)); + } + + @Override + public IAction before(Consumer t) { + return before(manager.create(t)); + } +} diff --git a/src/main/java/noppes/npcs/controllers/data/action/ActionChain.java b/src/main/java/noppes/npcs/controllers/data/action/ActionChain.java new file mode 100644 index 000000000..e98914e04 --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/action/ActionChain.java @@ -0,0 +1,35 @@ +package noppes.npcs.controllers.data.action; + +import noppes.npcs.api.handler.data.IAction; +import noppes.npcs.api.handler.data.IActionChain; +import noppes.npcs.scripted.ScriptedActionManager; + +import java.util.function.Consumer; + +/** + * helper to build a back‐to‐back chain of one‐shot actions + */ +public class ActionChain implements IActionChain { + private final ScriptedActionManager scriptedActionManager; + private int offset = 0, index = 0; + + public ActionChain(ScriptedActionManager scriptedActionManager) { + this.scriptedActionManager = scriptedActionManager; + } + + /** + * schedule the next task ‘delay’ ticks after the previous one + */ + @Override + public IActionChain after(int delay, Consumer task) { + offset += delay; + Consumer wrapper = act -> { + task.accept(act); + act.markDone(); + }; + IAction a = scriptedActionManager.create("chain#" + (index++), Integer.MAX_VALUE, offset, wrapper); + a.setUpdateEveryXTick(1); + scriptedActionManager.scheduleAction(a); + return this; + } +} diff --git a/src/main/java/noppes/npcs/controllers/data/action/ConditionalAction.java b/src/main/java/noppes/npcs/controllers/data/action/ConditionalAction.java new file mode 100644 index 000000000..f3b50d4c6 --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/action/ConditionalAction.java @@ -0,0 +1,142 @@ +package noppes.npcs.controllers.data.action; + +import noppes.npcs.api.handler.data.IAction; +import noppes.npcs.api.handler.data.actions.IConditionalAction; +import noppes.npcs.scripted.ScriptedActionManager; + +import java.util.function.Consumer; +import java.util.function.Supplier; + +public class ConditionalAction extends Action implements IConditionalAction { + private Supplier predicate; + private Supplier terminateWhen; + private Consumer onTermination; + private int maxChecks = -1; + private int checkCount; + private boolean taskExecuted; + + public ConditionalAction(ScriptedActionManager manager, Supplier predicate, Consumer task) { + super(manager, "conditional", task); + this.predicate = predicate; + } + + public ConditionalAction(ScriptedActionManager manager, String name, Supplier predicate, Consumer task) { + super(manager, name, task); + this.predicate = predicate; + } + + public ConditionalAction(ScriptedActionManager manager, Supplier predicate, Supplier terminateWhen, Consumer task) { + this(manager, predicate, task); + this.terminateWhen = terminateWhen; + } + + public ConditionalAction(ScriptedActionManager manager, String name, Supplier predicate, Supplier terminateWhen, Consumer task) { + this(manager, name, predicate, task); + this.terminateWhen = terminateWhen; + } + + public ConditionalAction(ScriptedActionManager manager, Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task) { + this(manager, predicate, terminateWhen, task); + this.onTermination = onTermination; + } + + public ConditionalAction(ScriptedActionManager manager, String name, Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task) { + this(manager, name, predicate, terminateWhen, task); + this.onTermination = onTermination; + } + + @Override + public void tick(int ticksExisted) { + if (isDone()) return; + if (ticksExisted % updateEveryXTick == 0) { + checkCount++; + boolean terminated = terminateWhen != null && terminateWhen.get(); + if (terminated && onTermination != null) { + onTermination.accept(this); + } + if ((maxChecks >= 0 && checkCount > maxChecks) || terminated) { + markDone(); + return; + } + if (predicate.get()) { + task.accept(this); + taskExecuted = true; + } + } + duration++; + } + + @Override + public IConditionalAction setMaxChecks(int maxChecks) { + this.maxChecks = maxChecks; + return this; + } + + @Override + public IConditionalAction setPredicate(Supplier predicate) { + this.predicate = predicate; + return this; + } + + @Override + public IConditionalAction setTerminationPredicate(Supplier terminateWhen) { + this.terminateWhen = terminateWhen; + return this; + } + + @Override + public IConditionalAction setTerminationTask(Consumer onTermination) { + this.onTermination = onTermination; + return this; + } + + @Override + public boolean wasTaskExecuted() { + return taskExecuted; + } + + @Override + public int getCheckCount() { + return checkCount; + } + + @Override + public int getMaxChecks() { + return maxChecks; + } + + @Override + public IConditionalAction after(IConditionalAction after) { + return ((ScriptedActionManager) manager).scheduleAction(after); + } + + @Override + public IConditionalAction after(Supplier predicate, Consumer task) { + return after(manager.create(predicate, task)); + } + + @Override + public IConditionalAction after(String name, Supplier predicate, Consumer task) { + return after(manager.create(name, predicate, task)); + } + + @Override + public IConditionalAction after(Supplier predicate, Supplier terminateWhen, Consumer task) { + return after(manager.create(predicate, terminateWhen, task)); + } + + @Override + public IConditionalAction after(String name, Supplier predicate, Supplier terminateWhen, Consumer task) { + return after(manager.create(name, predicate, terminateWhen, task)); + } + + @Override + public IConditionalAction after(Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task) { + return after(manager.create(predicate, terminateWhen, onTermination, task)); + } + + @Override + public IConditionalAction after(String name, Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task) { + return after(manager.create(name, predicate, terminateWhen, onTermination, task)); + } +} diff --git a/src/main/java/noppes/npcs/controllers/data/action/ParallelActionChain.java b/src/main/java/noppes/npcs/controllers/data/action/ParallelActionChain.java new file mode 100644 index 000000000..26d7e2acf --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/action/ParallelActionChain.java @@ -0,0 +1,35 @@ +package noppes.npcs.controllers.data.action; + +import noppes.npcs.api.handler.data.IAction; +import noppes.npcs.api.handler.data.IActionChain; +import noppes.npcs.scripted.ScriptedActionManager; + +import java.util.function.Consumer; + +public class ParallelActionChain implements IActionChain { + private final ScriptedActionManager scriptedActionManager; + private int offset = 0, idx = 0; + + public ParallelActionChain(ScriptedActionManager scriptedActionManager) { + this.scriptedActionManager = scriptedActionManager; + } + + /** + * schedule the next task ‘delay’ ticks after the previous one, fully in parallel + */ + @Override + public IActionChain after(int delay, Consumer task) { + offset += delay; + Consumer wrapper = act -> { + task.accept(act); + act.markDone(); + }; + IAction a = scriptedActionManager.create("parallel#" + (idx++), + Integer.MAX_VALUE, // no max-duration + offset, + wrapper); + a.setUpdateEveryXTick(1); + scriptedActionManager.scheduleParallelAction(a); + return this; + } +} diff --git a/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java b/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java index 5c15af71c..d3ddc7cd2 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java +++ b/src/main/java/noppes/npcs/scripted/ScriptedActionManager.java @@ -4,6 +4,10 @@ import noppes.npcs.api.handler.IActionManager; import noppes.npcs.api.handler.data.IAction; import noppes.npcs.api.handler.data.IActionChain; import noppes.npcs.api.handler.data.actions.IConditionalAction; +import noppes.npcs.controllers.data.action.Action; +import noppes.npcs.controllers.data.action.ActionChain; +import noppes.npcs.controllers.data.action.ConditionalAction; +import noppes.npcs.controllers.data.action.ParallelActionChain; import java.util.*; import java.util.function.Consumer; @@ -26,62 +30,62 @@ public class ScriptedActionManager implements IActionManager { @Override public IAction create(String name) { - return new Action(name); + return new Action(this, name); } @Override public IAction create(Consumer t) { - return new Action(t); + return new Action(this, t); } @Override public IAction create(String name, int maxDuration, int startAfterTicks, Consumer task) { - return new Action(name, maxDuration, startAfterTicks, task); + return new Action(this, name, maxDuration, startAfterTicks, task); } @Override public IAction create(String name, int delay, Consumer t) { - return new Action(name, delay, t); + return new Action(this, name, delay, t); } @Override public IAction create(int delay, Consumer t) { - return new Action(delay, t); + return new Action(this, delay, t); } @Override public IAction create(String name, Consumer t) { - return new Action(name, t); + return new Action(this, name, t); } @Override public IConditionalAction create(Supplier predicate, Consumer task) { - return new ConditionalAction(predicate, task); + return new ConditionalAction(this, predicate, task); } @Override public IConditionalAction create(String name, Supplier predicate, Consumer task) { - return new ConditionalAction(name, predicate, task); + return new ConditionalAction(this, name, predicate, task); } @Override public IConditionalAction create(Supplier predicate, Supplier terminateWhen, Consumer task) { - return new ConditionalAction(predicate, terminateWhen, task); + return new ConditionalAction(this, predicate, terminateWhen, task); } @Override public IConditionalAction create(String name, Supplier predicate, Supplier terminateWhen, Consumer task) { - return new ConditionalAction(name, predicate, terminateWhen, task); + return new ConditionalAction(this, name, predicate, terminateWhen, task); } @Override public IConditionalAction create(Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task) { - return new ConditionalAction(predicate, terminateWhen, onTermination, task); + return new ConditionalAction(this, predicate, terminateWhen, onTermination, task); } @Override public IConditionalAction create(String name, Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task) { - return new ConditionalAction(name, predicate, terminateWhen, onTermination, task); + return new ConditionalAction(this, name, predicate, terminateWhen, onTermination, task); } @Override @@ -174,12 +178,12 @@ public class ScriptedActionManager implements IActionManager { // Conditionals @Override public IConditionalAction scheduleAction(String name, Supplier predicate, Consumer task) { - return scheduleAction(new ConditionalAction(name, predicate, task)); + return scheduleAction(new ConditionalAction(this, name, predicate, task)); } @Override public IConditionalAction scheduleAction(String name, Supplier predicate, Supplier terminateWhen, Consumer task) { - return scheduleAction(new ConditionalAction(name, predicate, terminateWhen, task)); + return scheduleAction(new ConditionalAction(this, name, predicate, terminateWhen, task)); } @Override @@ -210,8 +214,8 @@ public class ScriptedActionManager implements IActionManager { // ─── Sequential (head only) ───────────────────────────────── IAction current = getCurrentAction(); - if (current instanceof ActionBase) { - ActionBase cab = (ActionBase) current; + if (current instanceof Action) { + Action cab = (Action) current; cab.tick(ticksExisted); if (cab.isDone()) actionQueue.pollFirst(); } @@ -219,7 +223,7 @@ public class ScriptedActionManager implements IActionManager { // ─── Parallel (all) ─────────────────────────────────────── Iterator pit = parallelActions.iterator(); while (pit.hasNext()) { - ActionBase a = (ActionBase) pit.next(); + Action a = (Action) pit.next(); a.tick(ticksExisted); if (a.isDone()) pit.remove(); } @@ -235,451 +239,11 @@ public class ScriptedActionManager implements IActionManager { @Override public IActionChain chain() { - return new ActionChain(); + return new ActionChain(this); } @Override public IActionChain parallelChain() { - return new ParallelActionChain(); - } - - - /** helper to build a back‐to‐back chain of one‐shot actions */ - public class ActionChain implements IActionChain { - private int offset = 0, index = 0; - - /** schedule the next task ‘delay’ ticks after the previous one */ - @Override - public IActionChain after(int delay, Consumer task) { - offset += delay; - Consumer wrapper = act -> { - task.accept(act); - act.markDone(); - }; - IAction a = create("chain#" + (index++), Integer.MAX_VALUE, offset, wrapper); - a.setUpdateEveryXTick(1); - scheduleAction(a); - return this; - } - } - - private class ParallelActionChain implements IActionChain { - private int offset = 0, idx = 0; - - /** schedule the next task ‘delay’ ticks after the previous one, fully in parallel */ - @Override - public IActionChain after(int delay, Consumer task) { - offset += delay; - Consumer wrapper = act -> { - task.accept(act); - act.markDone(); - }; - IAction a = create("parallel#" + (idx++), - Integer.MAX_VALUE, // no max-duration - offset, - wrapper); - a.setUpdateEveryXTick(1); - scheduleParallelAction(a); - return this; - } - } - - // ──── base class for all custom actions ────────────────────────────────── - - private abstract class ActionBase implements IAction { - protected final String name; - protected int startAfterTicks; - protected int count = 0; - protected int duration; - protected int maxDuration = -1; - protected int updateEveryXTick = 5; - protected Consumer task; - private boolean done = false; - - protected ActionBase(String name) { - this.name = name; - } - - protected ActionBase(Consumer task) { - this(task.toString()); - this.task = task; - } - protected ActionBase(String name, int maxDuration, int startAfterTicks, Consumer task) { - this.name = name; - this.maxDuration = maxDuration; - this.startAfterTicks = startAfterTicks; - this.task = task; - } - - protected ActionBase(String name, int startAfterTicks, Consumer task) { - this.name = name; - this.startAfterTicks = startAfterTicks; - this.task = task; - } - - protected ActionBase(String name, Consumer task) { - this.name = name; - this.task = task; - } - - protected ActionBase(int startAfterTicks, Consumer task) { - this(task); - this.startAfterTicks = startAfterTicks; - } - - @Override - public IAction setTask(Consumer task) { - this.task = task; - return this; - } - - /** - * Called once per global tick; respects delay, interval, duration & done‐flag. - */ - public void tick(int ticksExisted) { - if (done) return; - - if (startAfterTicks > 0) { - startAfterTicks--; - return; - } - - if (maxDuration != -1 && duration >= maxDuration) - markDone(); - // only run on our update tick - if (ticksExisted % updateEveryXTick == 0) { - task.accept(this); - count++; - } - duration++; - } - - @Override - public int getCount() { - return count; - } - - @Override - public int getDuration() { - return duration; - } - - @Override - public String getName() { - return name; - } - - @Override - public int getMaxDuration() { - return maxDuration; - } - - @Override - public IAction setMaxDuration(int x) { - maxDuration = x; - return this; - } - - @Override - public void markDone() { - done = true; - } - - @Override - public boolean isDone() { - return done; - } - - private final java.util.Map dataStore = new java.util.HashMap<>(); - @Override - public Object getData(String key) { - return dataStore.get(key); - } - - @Override - public IAction addData(String key, Object v) { - dataStore.put(key, v); - return this; - } - - @Override - public IAction removeData(String key) { - dataStore.remove(key); - return this; - } - - @Override - public int getUpdateEveryXTick() { - return updateEveryXTick; - } - - @Override - public IAction setUpdateEveryXTick(int x) { - updateEveryXTick = x; - return this; - } - - @Override - public int getStartAfterTicks() { - return startAfterTicks; - } - - @Override - public IAction pauseFor(int ticks) { - startAfterTicks = ticks; - return this; - } - @Override - public IAction getNext() { - int idx = getIndex(this); - return (idx >= 0 && idx + 1 < actionQueue.size()) - ? ((LinkedList) actionQueue).get(idx + 1) - : null; - } - - @Override - public IAction getPrevious() { - int idx = getIndex(this); - return (idx > 0) - ? ((LinkedList) actionQueue).get(idx - 1) - : null; - } - - ///////////////////////////////////////////////// - ///////////////////////////////////////////////// - //After chains - @Override - public IAction after(IAction after) { - int idx = getIndex(this); - if (idx >= 0) scheduleActionAt(idx + 1, after); - return after; - } - - @Override - public IAction after(String name, int maxDuration, int delay, Consumer t) { - return after(create(name, maxDuration, delay, t)); - } - - @Override - public IAction after(String name, int delay, Consumer t) { - return after(create(name, delay, t)); - } - - @Override - public IAction after(int delay, Consumer t) { - return after(create(delay, t)); - } - - @Override - public IAction after(String name, Consumer t) { - return after(create(name, t)); - } - - @Override - public IAction after(Consumer t) { - return after(create(t)); - - } - - ///////////////////////////////////////////////// - ///////////////////////////////////////////////// - //Before chains - @Override - public IAction before(IAction before) { - int idx = getIndex(this); - if (idx >= 0) scheduleActionAt(Math.max(0, idx), before); - return before; - } - - @Override - public IAction before(String name, int maxDuration, int delay, Consumer t) { - return before(create(name, maxDuration, delay, t)); - } - - @Override - public IAction before(String name, int delay, Consumer t) { - return before(create(name, delay, t)); - } - - @Override - public IAction before(int delay, Consumer t) { - return before(create(delay, t)); - } - - @Override - public IAction before(String name, Consumer t) { - return before(create(name, t)); - } - - @Override - public IAction before(Consumer t) { - return before(create(t)); - - } - } - - private class Action extends ActionBase { - - public Action(String name) { - super(name); - } - - public Action(Consumer task) { - super(task); - } - public Action(String name, int maxDuration, int startAfterTicks, Consumer task) { - super(name, maxDuration, startAfterTicks, task); - } - - public Action(String name, int startAfterTicks, Consumer task) { - super(name, startAfterTicks, task); - } - - public Action(String name, Consumer task) { - super(name, task); - } - - public Action(int startAfterTicks, Consumer task) { - super(startAfterTicks, task); - } - - - @Override public int getCheckCount() { return 0; } - - @Override public int getMaxChecks() { return 0; } - } - - private class ConditionalAction extends ActionBase implements IConditionalAction { - private Supplier predicate; - private Supplier terminateWhen; - private Consumer onTermination; - private int maxChecks = -1; - private int checkCount = 0; - private boolean taskExecuted; - public ConditionalAction(Supplier predicate, Consumer task) { - super(task); - this.predicate = predicate; - } - - public ConditionalAction(String name, Supplier predicate, Consumer task) { - super(name, task); - this.predicate = predicate; - } - - public ConditionalAction(Supplier predicate, Supplier terminateWhen, Consumer task) { - this(predicate, task); - this.terminateWhen = terminateWhen; - } - - public ConditionalAction(String name, Supplier predicate, Supplier terminateWhen, Consumer task) { - this(name, predicate, task); - this.terminateWhen = terminateWhen; - } - - public ConditionalAction(Supplier predicate, Supplier terminateWhen, Consumer onTerminate, Consumer task) { - this(predicate, task); - this.terminateWhen = terminateWhen; - this.onTermination = onTerminate; - } - - public ConditionalAction(String name, Supplier predicate, Supplier terminateWhen, Consumer onTerminate, Consumer task) { - this(name, predicate, task); - this.terminateWhen = terminateWhen; - this.onTermination = onTerminate; - } - - - @Override - public void tick(int ticksExisted) { - if (isDone()) return; - if (ticksExisted % updateEveryXTick == 0) { - checkCount++; - boolean terminated = terminateWhen != null && terminateWhen.get(); - - if (terminated && onTermination != null) - onTermination.accept(this); - - if ((maxChecks >= 0 && checkCount > maxChecks) || terminated) { - markDone(); - return; - } - if (predicate.get()) { - task.accept(this); - taskExecuted = true; - } - } - duration++; - } - - @Override - public IConditionalAction setMaxChecks(int maxChecks) { - this.maxChecks = maxChecks; - return this; - } - - @Override - public IConditionalAction setPredicate(Supplier predicate) { - this.predicate = predicate; - return this; - } - - @Override - public IConditionalAction setTerminationPredicate(Supplier terminateWhen) { - this.terminateWhen = terminateWhen; - return this; - } - - @Override - public IConditionalAction setTerminationTask(Consumer onTermination) { - this.onTermination = onTermination; - return this; - } - - @Override - public boolean wasTaskExecuted() { - return taskExecuted; - } - - @Override public int getCheckCount() { return checkCount; } - - @Override public int getMaxChecks() { return maxChecks; } - - ///////////////////////////////////////////////// - ///////////////////////////////////////////////// - // Chaining methods - @Override - public IConditionalAction after(IConditionalAction after) { - return scheduleAction(after); - } - - @Override - public IConditionalAction after(Supplier predicate, Consumer task) { - return after(create(predicate, task)); - } - - @Override - public IConditionalAction after(String name, Supplier predicate, Consumer task) { - return after(create(name, predicate, task)); - } - - @Override - public IConditionalAction after(Supplier predicate, Supplier terminateWhen, Consumer task) { - return after(create(predicate, terminateWhen, task)); - } - - @Override - public IConditionalAction after(String name, Supplier predicate, Supplier terminateWhen, Consumer task) { - return after(create(name, predicate, terminateWhen, task)); - } - - @Override - public IConditionalAction after(Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task) { - return after(create(predicate, terminateWhen, onTermination, task)); - } - - @Override - public IConditionalAction after(String name, Supplier predicate, Supplier terminateWhen, Consumer onTermination, Consumer task) { - return after(create(name, predicate, terminateWhen, onTermination, task)); - } + return new ParallelActionChain(this); } }