2025-03-03 23:59:49 +02:00
|
|
|
|
package noppes.npcs.scripted;
|
|
|
|
|
|
|
2025-03-03 20:05:19 -05:00
|
|
|
|
import noppes.npcs.api.handler.IActionManager;
|
|
|
|
|
|
import noppes.npcs.api.handler.data.IAction;
|
2025-04-27 23:10:55 -04:00
|
|
|
|
import noppes.npcs.api.handler.data.IActionChain;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
import java.util.Deque;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
|
import java.util.Queue;
|
|
|
|
|
|
import java.util.function.Consumer;
|
2025-04-26 20:52:08 -04:00
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Full-featured ActionManager:
|
|
|
|
|
|
* - delayed start, max-duration, tick-interval
|
|
|
|
|
|
* - chaining (getNext, getPrevious, scheduleAfter/Before)
|
|
|
|
|
|
* - per-action data
|
|
|
|
|
|
* - conditional actions
|
|
|
|
|
|
* - cancellation & clearing
|
|
|
|
|
|
*/
|
2025-03-03 23:59:49 +02:00
|
|
|
|
public class ScriptedActionManager implements IActionManager {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
private boolean isWorking = false;
|
|
|
|
|
|
private final Deque<IAction> actionQueue = new LinkedList<>();
|
2025-03-03 23:59:49 +02:00
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-04-26 20:52:08 -04:00
|
|
|
|
public IAction create(String name, int maxDuration, int startAfterTicks, Consumer<IAction> task) {
|
|
|
|
|
|
return new Action(name, maxDuration, startAfterTicks, task);
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void start() {
|
|
|
|
|
|
isWorking = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void stop() {
|
|
|
|
|
|
isWorking = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-04-28 06:47:34 +03:00
|
|
|
|
public IAction scheduleAction(IAction action) {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
actionQueue.addLast(action);
|
2025-04-28 06:47:34 +03:00
|
|
|
|
return action;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-04-28 06:47:34 +03:00
|
|
|
|
public IAction scheduleAction(String name, int maxDuration, int startAfterTicks, Consumer<IAction> task) {
|
|
|
|
|
|
return scheduleAction(create(name, maxDuration, startAfterTicks, task));
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-04-28 06:47:34 +03:00
|
|
|
|
public IAction scheduleActionAt(int index, IAction action) {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
if (index < 0 || index > actionQueue.size()) {
|
|
|
|
|
|
actionQueue.addLast(action);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
((LinkedList<IAction>) actionQueue).add(index, action);
|
|
|
|
|
|
}
|
2025-04-28 06:47:34 +03:00
|
|
|
|
|
|
|
|
|
|
return action;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-03-13 16:52:08 -04:00
|
|
|
|
public int getIndex(IAction action) {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
if (action == null) return -1;
|
|
|
|
|
|
return ((LinkedList<IAction>) actionQueue).indexOf(action);
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IAction getCurrentAction() {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
return actionQueue.peekFirst();
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public Queue<IAction> getActionQueue() {
|
|
|
|
|
|
return actionQueue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
@Override
|
2025-03-13 16:52:08 -04:00
|
|
|
|
public void clear() {
|
2025-03-03 23:59:49 +02:00
|
|
|
|
actionQueue.clear();
|
|
|
|
|
|
}
|
2025-03-13 16:52:08 -04:00
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
@Override
|
|
|
|
|
|
public boolean cancelAction(String name) {
|
|
|
|
|
|
for (IAction act : actionQueue) {
|
|
|
|
|
|
if (act.getName().equals(name)) {
|
|
|
|
|
|
((LinkedList<IAction>) actionQueue).remove(act);
|
|
|
|
|
|
return true;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-04-26 20:52:08 -04:00
|
|
|
|
return false;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
@Override
|
2025-04-28 06:47:34 +03:00
|
|
|
|
public IAction scheduleConditionalAction(String name,
|
2025-04-26 20:52:08 -04:00
|
|
|
|
int checkIntervalTicks,
|
|
|
|
|
|
Supplier<Boolean> predicate,
|
|
|
|
|
|
Consumer<IAction> task) {
|
2025-04-28 06:47:34 +03:00
|
|
|
|
return scheduleConditionalAction(name, checkIntervalTicks, predicate, task, -1);
|
2025-04-26 20:52:08 -04:00
|
|
|
|
}
|
2025-03-03 23:59:49 +02:00
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
@Override
|
2025-04-28 06:47:34 +03:00
|
|
|
|
public IAction scheduleConditionalAction(String name,
|
2025-04-26 20:52:08 -04:00
|
|
|
|
int checkIntervalTicks,
|
|
|
|
|
|
Supplier<Boolean> predicate,
|
|
|
|
|
|
Consumer<IAction> task,
|
|
|
|
|
|
int maxChecks) {
|
2025-04-28 06:47:34 +03:00
|
|
|
|
return scheduleAction(new ConditionalAction(name, checkIntervalTicks, predicate, task, maxChecks));
|
2025-04-26 20:52:08 -04:00
|
|
|
|
}
|
2025-03-03 23:59:49 +02:00
|
|
|
|
|
2025-04-27 07:33:50 +03:00
|
|
|
|
@Override
|
2025-04-28 06:47:34 +03:00
|
|
|
|
public IAction scheduleConditionalAction(String name, int checkIntervalTicks, Supplier<Boolean> predicate, Supplier<Boolean> terminateWhen, Consumer<IAction> task, int maxChecks) {
|
|
|
|
|
|
return scheduleAction(new ConditionalAction(name, checkIntervalTicks, predicate, terminateWhen, task, maxChecks));
|
2025-04-27 07:33:50 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Call once per tick from your main loop.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param ticksExisted the global tick count, used for modulo checks.
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void tick(int ticksExisted) {
|
|
|
|
|
|
if (!isWorking) return;
|
|
|
|
|
|
IAction current = getCurrentAction();
|
|
|
|
|
|
if (current instanceof ActionBase) {
|
|
|
|
|
|
ActionBase cab = (ActionBase) current;
|
|
|
|
|
|
cab.tick(ticksExisted);
|
|
|
|
|
|
if (cab.isDone() || cab.getDuration() >= cab.getMaxDuration()) {
|
|
|
|
|
|
actionQueue.pollFirst();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-27 23:10:55 -04:00
|
|
|
|
@Override
|
|
|
|
|
|
public IActionChain chain() {
|
2025-04-28 06:52:44 +03:00
|
|
|
|
return new ActionChain();
|
2025-04-27 23:10:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** helper to build a back‐to‐back chain of one‐shot actions */
|
2025-04-28 06:52:44 +03:00
|
|
|
|
public class ActionChain implements IActionChain {
|
2025-04-27 23:10:55 -04:00
|
|
|
|
private int offset = 0, index = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/** schedule the next task ‘delay’ ticks after the previous one */
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IActionChain after(int delay, Consumer<IAction> task) {
|
|
|
|
|
|
offset += delay;
|
2025-04-27 23:23:21 -04:00
|
|
|
|
Consumer<IAction> wrapper = act -> {
|
|
|
|
|
|
task.accept(act);
|
|
|
|
|
|
act.markDone();
|
|
|
|
|
|
};
|
2025-04-28 06:52:44 +03:00
|
|
|
|
IAction a = create("chain#" + (index++), Integer.MAX_VALUE, offset, wrapper);
|
2025-04-27 23:23:21 -04:00
|
|
|
|
a.setUpdateEveryXTick(1);
|
2025-04-28 06:52:44 +03:00
|
|
|
|
scheduleAction(a);
|
2025-04-27 23:10:55 -04:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
// ──── 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 = 0;
|
2025-04-28 07:19:59 +03:00
|
|
|
|
protected int maxDuration = -1;
|
2025-04-26 20:52:08 -04:00
|
|
|
|
protected int updateEveryXTick = 5;
|
|
|
|
|
|
protected final Consumer<IAction> task;
|
|
|
|
|
|
private boolean done = false;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
protected ActionBase(String name, int maxDuration, int startAfterTicks, Consumer<IAction> task) {
|
2025-03-03 23:59:49 +02:00
|
|
|
|
this.name = name;
|
|
|
|
|
|
this.maxDuration = maxDuration;
|
|
|
|
|
|
this.startAfterTicks = startAfterTicks;
|
2025-04-26 20:52:08 -04:00
|
|
|
|
this.task = task;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-28 07:19:59 +03:00
|
|
|
|
public ActionBase(String name, int startAfterTicks, Consumer<IAction> task) {
|
|
|
|
|
|
this.name = name;
|
|
|
|
|
|
this.startAfterTicks = startAfterTicks;
|
|
|
|
|
|
this.task = task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ActionBase(String name, Consumer<IAction> task) {
|
|
|
|
|
|
this.name = name;
|
|
|
|
|
|
this.task = task;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ActionBase(int startAfterTicks, Consumer<IAction> task) {
|
|
|
|
|
|
this(task);
|
|
|
|
|
|
this.startAfterTicks = startAfterTicks;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ActionBase(Consumer<IAction> task) {
|
|
|
|
|
|
this.name = task.toString();
|
|
|
|
|
|
this.task = task;
|
|
|
|
|
|
}
|
2025-04-26 20:52:08 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Called once per global tick; respects delay, interval, duration & done‐flag.
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void tick(int ticksExisted) {
|
|
|
|
|
|
if (done) return;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
if (startAfterTicks > 0) {
|
|
|
|
|
|
startAfterTicks--;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// only run on our update tick
|
|
|
|
|
|
if (ticksExisted % updateEveryXTick == 0) {
|
|
|
|
|
|
task.accept(this);
|
|
|
|
|
|
count++;
|
|
|
|
|
|
}
|
|
|
|
|
|
duration++;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
2025-03-13 16:52:08 -04:00
|
|
|
|
public void markDone() {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
done = true;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public boolean isDone() {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
return done;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-28 07:19:59 +03:00
|
|
|
|
private final java.util.Map<String, Object> dataStore = new java.util.HashMap<>();
|
2025-03-03 23:59:49 +02:00
|
|
|
|
@Override
|
|
|
|
|
|
public Object getData(String key) {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
return dataStore.get(key);
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-04-26 20:52:08 -04:00
|
|
|
|
public void addData(String key, Object v) {
|
|
|
|
|
|
dataStore.put(key, v);
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public int getUpdateEveryXTick() {
|
|
|
|
|
|
return updateEveryXTick;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-04-26 20:52:08 -04:00
|
|
|
|
public void setUpdateEveryXTick(int x) {
|
|
|
|
|
|
updateEveryXTick = x;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public int getStartAfterTicks() {
|
|
|
|
|
|
return startAfterTicks;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-04-26 20:52:08 -04:00
|
|
|
|
public IAction create(String n, int md, int sat, Consumer<IAction> t) {
|
|
|
|
|
|
return ScriptedActionManager.this.create(n, md, sat, t);
|
2025-03-13 16:52:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IAction getNext() {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
int idx = getIndex(this);
|
|
|
|
|
|
return (idx >= 0 && idx + 1 < actionQueue.size())
|
|
|
|
|
|
? ((LinkedList<IAction>) actionQueue).get(idx + 1)
|
|
|
|
|
|
: null;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2025-03-13 16:52:08 -04:00
|
|
|
|
public IAction getPrevious() {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
int idx = getIndex(this);
|
|
|
|
|
|
return (idx > 0)
|
|
|
|
|
|
? ((LinkedList<IAction>) actionQueue).get(idx - 1)
|
|
|
|
|
|
: null;
|
2025-03-13 16:52:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-28 07:19:59 +03:00
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
|
|
//After chains
|
2025-03-13 16:52:08 -04:00
|
|
|
|
@Override
|
2025-04-28 06:50:02 +03:00
|
|
|
|
public IAction after(IAction after) {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
int idx = getIndex(this);
|
|
|
|
|
|
if (idx >= 0) scheduleActionAt(idx + 1, after);
|
2025-04-28 06:50:02 +03:00
|
|
|
|
return after;
|
2025-03-13 16:52:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-28 07:19:59 +03:00
|
|
|
|
@Override
|
|
|
|
|
|
public IAction after(String name, int maxDuration, int delay, Consumer<IAction> t) {
|
|
|
|
|
|
IAction act = new Action(name, maxDuration, delay, t);
|
|
|
|
|
|
return after(act);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IAction after(String name, int delay, Consumer<IAction> t) {
|
|
|
|
|
|
IAction act = new Action(name, delay, t);
|
|
|
|
|
|
return after(act);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IAction after(int delay, Consumer<IAction> t) {
|
|
|
|
|
|
IAction act = new Action(delay, t);
|
|
|
|
|
|
return after(act);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IAction after(String name, Consumer<IAction> t) {
|
|
|
|
|
|
IAction act = new Action(name, t);
|
|
|
|
|
|
return after(act);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IAction after(Consumer<IAction> t) {
|
|
|
|
|
|
IAction act = new Action(t);
|
|
|
|
|
|
return after(act);
|
2025-04-28 06:50:02 +03:00
|
|
|
|
|
2025-04-28 07:19:59 +03:00
|
|
|
|
}
|
2025-04-28 06:50:02 +03:00
|
|
|
|
|
2025-04-28 07:19:59 +03:00
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
|
|
/////////////////////////////////////////////////
|
|
|
|
|
|
//Before chains
|
2025-03-13 16:52:08 -04:00
|
|
|
|
@Override
|
2025-04-28 06:50:02 +03:00
|
|
|
|
public IAction before(IAction before) {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
int idx = getIndex(this);
|
|
|
|
|
|
if (idx >= 0) scheduleActionAt(Math.max(0, idx), before);
|
2025-04-28 06:50:02 +03:00
|
|
|
|
return before;
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
2025-04-26 20:52:08 -04:00
|
|
|
|
|
2025-04-28 07:19:59 +03:00
|
|
|
|
@Override
|
|
|
|
|
|
public IAction before(String name, int maxDuration, int delay, Consumer<IAction> t) {
|
|
|
|
|
|
IAction act = new Action(name, maxDuration, delay, t);
|
|
|
|
|
|
return before(act);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IAction before(String name, int delay, Consumer<IAction> t) {
|
|
|
|
|
|
IAction act = new Action(name, delay, t);
|
|
|
|
|
|
return before(act);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IAction before(int delay, Consumer<IAction> t) {
|
|
|
|
|
|
IAction act = new Action(delay, t);
|
|
|
|
|
|
return before(act);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IAction before(String name, Consumer<IAction> t) {
|
|
|
|
|
|
IAction act = new Action(name, t);
|
|
|
|
|
|
return before(act);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public IAction before(Consumer<IAction> t) {
|
|
|
|
|
|
IAction act = new Action(t);
|
|
|
|
|
|
return before(act);
|
|
|
|
|
|
}
|
2025-03-03 23:59:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
private class Action extends ActionBase {
|
|
|
|
|
|
public Action(String name, int maxDuration, int startAfterTicks, Consumer<IAction> task) {
|
|
|
|
|
|
super(name, maxDuration, startAfterTicks, task);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-28 07:19:59 +03:00
|
|
|
|
public Action(String name, int startAfterTicks, Consumer<IAction> task) {
|
|
|
|
|
|
super(name, startAfterTicks, task);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Action(String name, Consumer<IAction> task) {
|
|
|
|
|
|
super(name, task);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Action(int startAfterTicks, Consumer<IAction> task) {
|
|
|
|
|
|
super(startAfterTicks, task);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Action(Consumer<IAction> task) {
|
|
|
|
|
|
super(task);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
@Override public int getCheckCount() { return 0; }
|
2025-03-03 23:59:49 +02:00
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
@Override public int getMaxChecks() { return 0; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class ConditionalAction extends ActionBase {
|
|
|
|
|
|
private final Supplier<Boolean> predicate;
|
2025-04-27 07:33:50 +03:00
|
|
|
|
private Supplier<Boolean> terminate;
|
2025-04-26 20:52:08 -04:00
|
|
|
|
private final int maxChecks;
|
|
|
|
|
|
private int checkCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
public ConditionalAction(String name,
|
|
|
|
|
|
int checkIntervalTicks,
|
|
|
|
|
|
Supplier<Boolean> predicate,
|
|
|
|
|
|
Consumer<IAction> task,
|
|
|
|
|
|
int maxChecks) {
|
|
|
|
|
|
super(name, Integer.MAX_VALUE, 0, task);
|
|
|
|
|
|
this.predicate = predicate;
|
|
|
|
|
|
this.updateEveryXTick = checkIntervalTicks;
|
|
|
|
|
|
this.maxChecks = maxChecks;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-27 07:33:50 +03:00
|
|
|
|
public ConditionalAction(String name, int checkIntervalTicks, Supplier<Boolean> predicate, Supplier<Boolean> terminate, Consumer<IAction> task, int maxChecks) {
|
|
|
|
|
|
this(name, checkIntervalTicks, predicate, task, maxChecks);
|
|
|
|
|
|
this.terminate = terminate;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-26 20:52:08 -04:00
|
|
|
|
@Override
|
|
|
|
|
|
public void tick(int ticksExisted) {
|
|
|
|
|
|
if (isDone()) return;
|
|
|
|
|
|
if (ticksExisted % updateEveryXTick == 0) {
|
|
|
|
|
|
checkCount++;
|
2025-04-27 08:01:29 +03:00
|
|
|
|
if ((maxChecks >= 0 && checkCount > maxChecks) || terminate != null && terminate.get()) {
|
2025-04-26 20:52:08 -04:00
|
|
|
|
markDone();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-04-27 07:13:26 +03:00
|
|
|
|
if (predicate.get())
|
2025-04-26 20:52:08 -04:00
|
|
|
|
task.accept(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override public int getCheckCount() { return checkCount; }
|
|
|
|
|
|
|
|
|
|
|
|
@Override public int getMaxChecks() { return maxChecks; }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|