Merge remote-tracking branch 'upstream/dev' into dev

# Conflicts:
#	src/api
This commit is contained in:
bigguy345 committed 2025-04-28 10:11:02 +03:00
commit d3cae9e9cb
3 files changed
+63 -13

No files matched your search

@@ -19,7 +19,9 @@ import java.util.function.Supplier;
*/
public class ScriptedActionManager implements IActionManager {
private boolean isWorking = false;
private final Deque<IAction> actionQueue = new LinkedList<>();
private final List<IAction> parallelActions = new LinkedList<>();
private final List<IConditionalAction> conditionalActions = new LinkedList<>();
@Override
@@ -133,6 +135,7 @@ public class ScriptedActionManager implements IActionManager {
@Override
public void clear() {
actionQueue.clear();
parallelActions.clear();
conditionalActions.clear();
}
@@ -147,6 +150,14 @@ public class ScriptedActionManager implements IActionManager {
}
}
Iterator<IAction> pit = parallelActions.iterator();
while (pit.hasNext()) {
if (pit.next().getName().equals(name)) {
pit.remove();
return true;
}
}
Iterator<IConditionalAction> cons = conditionalActions.iterator();
while (cons.hasNext()) {
IConditionalAction con = cons.next();
@@ -182,6 +193,13 @@ public class ScriptedActionManager implements IActionManager {
return conditionalActions;
}
@Override
public IAction scheduleParallelAction(IAction action) {
parallelActions.add(action);
return action;
}
/**
* Call once per tick from your main loop.
*
@@ -189,22 +207,30 @@ public class ScriptedActionManager implements IActionManager {
*/
public void tick(int ticksExisted) {
if (!isWorking) return;
// ─── Sequential (head only) ─────────────────────────────────
IAction current = getCurrentAction();
if (current instanceof ActionBase) {
ActionBase cab = (ActionBase) current;
cab.tick(ticksExisted);
if (cab.isDone())
actionQueue.pollFirst();
if (cab.isDone()) actionQueue.pollFirst();
}
Iterator<IConditionalAction> it = conditionalActions.iterator();
while (it.hasNext()) {
ConditionalAction con = (ConditionalAction) it.next();
// ─── Parallel (all) ───────────────────────────────────────
Iterator<IAction> pit = parallelActions.iterator();
while (pit.hasNext()) {
ActionBase a = (ActionBase) pit.next();
a.tick(ticksExisted);
if (a.isDone()) pit.remove();
}
// ─── Conditionals ─────────────────────────────────────────
Iterator<IConditionalAction> cit = conditionalActions.iterator();
while (cit.hasNext()) {
ConditionalAction con = (ConditionalAction) cit.next();
con.tick(ticksExisted);
if (con.isDone())
it.remove();
if (con.isDone()) cit.remove();
}
}
@Override
@@ -212,6 +238,12 @@ public class ScriptedActionManager implements IActionManager {
return new ActionChain();
}
@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;
@@ -231,6 +263,27 @@ public class ScriptedActionManager implements IActionManager {
}
}
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<IAction> task) {
offset += delay;
Consumer<IAction> 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 {
@@ -628,8 +681,5 @@ public class ScriptedActionManager implements IActionManager {
public IConditionalAction after(String name, Supplier<Boolean> predicate, Supplier<Boolean> terminateWhen, Consumer<IAction> onTermination, Consumer<IAction> task) {
return after(create(name, predicate, terminateWhen, onTermination, task));
}
}
}