diff --git a/src/main/java/noppes/npcs/controllers/data/action/Action.java b/src/main/java/noppes/npcs/controllers/data/action/Action.java index b19748495..34c3a662b 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/Action.java +++ b/src/main/java/noppes/npcs/controllers/data/action/Action.java @@ -22,7 +22,7 @@ public class Action implements IAction { protected int count, maxCount = -1; protected int duration, maxDuration = -1; protected int updateEveryXTick = 5; - protected Consumer task; + protected Consumer task, onStart, onDone; protected boolean done; protected final Map dataStore = new HashMap<>(); protected boolean isThreaded; @@ -115,6 +115,18 @@ public class Action implements IAction { return this; } + @Override + public IAction onStart(Consumer task) { + this.onStart = task; + return this; + } + + @Override + public IAction onDone(Consumer task) { + this.onDone = task; + return this; + } + @Override public int getDuration() { return duration; @@ -244,6 +256,9 @@ public class Action implements IAction { if (done) return; + if (duration == 0 && onStart != null) + execute("start", this::executeOnStart); + duration++; if (startAfterTicks > 0) { @@ -270,6 +285,14 @@ public class Action implements IAction { task.run(); } + protected void executeOnStart() { + try { + onStart.accept(this); + } catch (Throwable t) { + logDebug("Start Task of " + this + " threw an exception:", t); + } + } + protected void executeTask() { try { task.accept(this); @@ -280,6 +303,15 @@ public class Action implements IAction { } } + protected void executeOnDone() { + try { + onDone.accept(this); + } catch (Throwable t) { + logDebug("Done Task of " + this + " threw an exception:", t); + } + } + + @Override public void kill() { if (actionThread != null) diff --git a/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java b/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java index 1d2f47e26..8becaf38a 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java +++ b/src/main/java/noppes/npcs/controllers/data/action/ActionQueue.java @@ -267,6 +267,8 @@ public class ActionQueue implements IActionQueue { Action cab = (Action) current; cab.tick(); if (cab.isDone()) { + if (cab.onDone != null) + cab.execute("task", cab::executeOnDone); cab.kill(); queue.pollFirst(); } @@ -277,6 +279,8 @@ public class ActionQueue implements IActionQueue { Action a = (Action) pit.next(); a.tick(); if (a.isDone()) { + if (a.onDone != null) + a.execute("complete", a::executeOnDone); a.kill(); pit.remove(); } diff --git a/src/main/java/noppes/npcs/controllers/data/action/action/ConditionalAction.java b/src/main/java/noppes/npcs/controllers/data/action/action/ConditionalAction.java index 7d2159915..2d4cab4cc 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/action/ConditionalAction.java +++ b/src/main/java/noppes/npcs/controllers/data/action/action/ConditionalAction.java @@ -96,6 +96,9 @@ public class ConditionalAction extends Action implements IConditionalAction { if (isDone()) return; + if (duration == 0 && onStart != null) + execute("start", this::executeOnStart); + duration++; if (maxChecks > -1 && checkCount > maxChecks || maxCount == 0) {