mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 04:57:27 +00:00
Added onStart(task) and onDone(task) for IAction
This commit is contained in:
1 parent
8d63134a72
commit
835b32daa1
3 files changed
+40
-1
No files matched your search
@@ -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<IAction> task;
|
||||
protected Consumer<IAction> task, onStart, onDone;
|
||||
protected boolean done;
|
||||
protected final Map<String, Object> dataStore = new HashMap<>();
|
||||
protected boolean isThreaded;
|
||||
@@ -115,6 +115,18 @@ public class Action implements IAction {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAction onStart(Consumer<IAction> task) {
|
||||
this.onStart = task;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAction onDone(Consumer<IAction> 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)
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user