From fe786babc6e9d1b84a6119f8df2b1bbc525e4782 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Mon, 7 Jul 2025 22:41:58 +0300 Subject: [PATCH] IAction optimisations --- .../npcs/controllers/data/action/Action.java | 31 +++++++++------- .../data/action/action/ConditionalAction.java | 36 +++---------------- 2 files changed, 24 insertions(+), 43 deletions(-) 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 9bad65fa8..b6d7dad2b 100644 --- a/src/main/java/noppes/npcs/controllers/data/action/Action.java +++ b/src/main/java/noppes/npcs/controllers/data/action/Action.java @@ -271,6 +271,12 @@ public class Action implements IAction { if (done) return; + if (startAfterTicks > 0) { + manager.LOGGER.log(String.format("Starts after %s ticks...", startAfterTicks), this); + startAfterTicks--; + return; + } + if (duration == 0 && onStart != null) execute("start", this::executeOnStart); @@ -279,13 +285,7 @@ public class Action implements IAction { duration++; - if (manager.debug) - manager.LOGGER.log(String.format("Ticking... (duration = %s/%s, count = %s/%s)", duration, maxDuration, count, maxCount), this); - - if (startAfterTicks > 0) { - startAfterTicks--; - return; - } + inheritedTick(); if (maxDuration > -1 && duration >= maxDuration || maxCount == 0) { if (manager.debug) @@ -295,18 +295,25 @@ public class Action implements IAction { return; } - if (duration % updateEveryXTick == 0 && task != null) - execute("task", this::executeTask); if (maxCount > -1 && count >= maxCount) { if (manager.debug) - manager.LOGGER.log("Reached max count of Action '%s' on queue '%s'", this); + manager.LOGGER.log("Reached max count", this); markDone(); } } + public void inheritedTick() { + if (manager.debug) + manager.LOGGER.log(String.format("Ticking... (duration = %s/%s, count = %s/%s)", duration, maxDuration, count, maxCount), this); + + + if (duration % updateEveryXTick == 0 && task != null) + execute("task", this::executeTask); + } + public void execute(String taskName, Runnable task) { if (isThreaded) actionThread.execute(taskName, task); @@ -341,7 +348,7 @@ public class Action implements IAction { } if (manager.debug) - manager.LOGGER.log("Finished executing task", this); + manager.LOGGER.log(String.format("Finished executing task (count = %s/%s)", count, maxCount), this); } protected void executeOnDone() { @@ -373,7 +380,7 @@ public class Action implements IAction { } public String toString() { - return String.format("IAction '%s' [queue='%s', scheduled=%s, done=%s, paused=%s, updateEvery=%s, duration=%d/%d, count=%d/%d, threaded=%s]", name != null ? name : "unnamed", getQueueName(), isScheduled, done, isPaused(), updateEveryXTick, duration, maxDuration, count, maxCount, isThreaded); + return String.format("%s '%s' [queue='%s', scheduled=%s, done=%s, paused=%s, updateEvery=%s, duration=%d/%d, count=%d/%d, threaded=%s]", getClass().getSimpleName(), name != null ? name : "unnamed", getQueueName(), isScheduled, done, isPaused(), updateEveryXTick, duration, maxDuration, count, maxCount, isThreaded); } /////////////////////////////////////////////////// ////////////////////////////////////////////////// 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 4b04ece5d..04eddd3b3 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 @@ -91,31 +91,11 @@ public class ConditionalAction extends Action implements IConditionalAction { return terminateWhen != null && terminateWhen.apply(this); } - @Override - public void tick() { - if (isDone()) - return; - - if (duration == 0 && onStart != null) - execute("start", this::executeOnStart); - - if (isDone()) - return; - - duration++; - + public void inheritedTick() { if (manager.inDebugMode()) manager.LOGGER.log(String.format("Ticking... (duration = %s/%s, count = %s/%s, checkCount = %s/%s)", duration, maxDuration, count, maxCount, checkCount, maxChecks), this); - if (maxDuration > -1 && duration >= maxDuration || maxCount == 0) { - if (manager.inDebugMode()) - manager.LOGGER.log("Reached max duration", this); - - markDone(); - return; - } - - if (maxChecks > -1 && checkCount > maxChecks) { + if (maxChecks > -1 && checkCount >= maxChecks) { if (manager.inDebugMode()) manager.LOGGER.log("Reached max check count", this); @@ -139,18 +119,12 @@ public class ConditionalAction extends Action implements IConditionalAction { execute("task", execute); } - - if (maxCount > -1 && count >= maxCount) { - if (manager.inDebugMode()) - manager.LOGGER.log("Reached max count", this); - - markDone(); - } } protected void executeTask() { if (manager.inDebugMode()) manager.LOGGER.log("Executing task...", this); + try { task.accept(this); count++; @@ -161,7 +135,7 @@ public class ConditionalAction extends Action implements IConditionalAction { } if (manager.inDebugMode()) - manager.LOGGER.log("Finished executing task", this); + manager.LOGGER.log(String.format("Finished executing task (count = %s/%s)", count, maxCount), this); } @@ -181,7 +155,7 @@ public class ConditionalAction extends Action implements IConditionalAction { public String toString() { - return String.format("IConditionalAction '%s' [queue='%s', scheduled=%s, done=%s, paused=%s, updateEvery=%s, duration=%d/%d, count=%d/%d, checks=%d/%d, taskExecuted=%s, threaded=%s]", + return String.format("%s '%s' [queue='%s', scheduled=%s, done=%s, paused=%s, updateEvery=%s, duration=%d/%d, count=%d/%d, checks=%d/%d, taskExecuted=%s, threaded=%s]", getClass().getSimpleName(), name != null ? name : "unnamed", getQueueName(), isScheduled, done,