Fixed updateEveryXTicks being inconsistent due to using entity's ticksExisted. IAction instances now track their own tick

This commit is contained in:
bigguy345 committed 2025-07-03 18:29:53 +03:00
1 parent 7e1374d96a
commit e0c9bfffa5
8 files changed
+31 -34

No files matched your search

@@ -36,10 +36,8 @@ import noppes.npcs.config.ConfigScript;
import noppes.npcs.controllers.data.*;
import noppes.npcs.entity.EntityNPCInterface;
import noppes.npcs.items.ItemNpcTool;
import noppes.npcs.items.ItemScripted;
import noppes.npcs.quests.QuestItem;
import noppes.npcs.scripted.NpcAPI;
import noppes.npcs.scripted.event.ItemEvent;
import static noppes.npcs.config.ConfigMain.TrackedQuestUpdateFrequency;
@@ -74,7 +72,7 @@ public class ScriptPlayerEventHandler {
if (PlayerDataController.Instance != null) {
PlayerData playerData = PlayerData.get(player);
playerData.actionManager.tick(player.ticksExisted);
playerData.actionManager.tick();
if (playerData.updateClient) {
SyncController.syncPlayerData((EntityPlayerMP) player, true);
@@ -21,7 +21,7 @@ public class ServerTickHandler {
@SubscribeEvent
public void onServerTick(TickEvent.ServerTickEvent event) {
if (event.phase == Phase.END) {
NpcAPI.actionManager.tick(MinecraftServer.getServer().getTickCounter());
NpcAPI.actionManager.tick();
}
}
@@ -186,10 +186,6 @@ public class Action implements IAction {
@Override
public void markDone() {
done = true;
if (isThreaded && actionThread.inActionThread()) {
queue.cancel(this);
}
}
@Override
@@ -207,9 +203,14 @@ public class Action implements IAction {
return this;
}
public void tick(int ticksExisted) {
public void tick() {
if (done)
return;
duration++;
System.out.println(duration);
if (startAfterTicks > 0) {
startAfterTicks--;
return;
@@ -218,7 +219,8 @@ public class Action implements IAction {
markDone();
return;
}
if (ticksExisted % updateEveryXTick == 0 && task != null) {
if (duration % updateEveryXTick == 0 && task != null) {
if (isThreaded)
actionThread.execute("task", this::executeTask);
else
@@ -228,7 +230,6 @@ public class Action implements IAction {
if (maxCount > -1 && count >= maxCount)
markDone();
duration++;
}
protected void executeTask() {
@@ -369,26 +369,24 @@ public class ActionManager implements IActionManager {
/**
* Call once per tick from your main loop.
*
* @param ticksExisted the global tick count, used for modulo checks.
*/
public void tick(int ticksExisted) {
public void tick() {
if (!isWorking) return;
// ─── Sequential (head only) ─────────────────────────────────
sequentialQueue.tick(ticksExisted);
sequentialQueue.tick();
// ─── Parallel (all) ───────────────────────────────────────
parallelQueue.tick(ticksExisted);
parallelQueue.tick();
// ─── Conditionals ─────────────────────────────────────────
conditionalQueue.tick(ticksExisted);
conditionalQueue.tick();
// ─── Other Queues ─────────────────────────────────────────
Iterator<IActionQueue> it = otherQueues.values().iterator();
while (it.hasNext()) {
IActionQueue other = it.next();
((ActionQueue) other).tick(ticksExisted);
((ActionQueue) other).tick();
if (other.isKilled()) {
other.clear();
@@ -262,17 +262,17 @@ public class ActionQueue implements IActionQueue {
///////////////////////////////////////////////////
// Handling
protected void tick(int ticksExisted) {
protected void tick() {
if (!isWorking)
return;
killWhenEmpty(ticksExisted);
killWhenEmpty();
if (!isParallel) {
IAction current = getCurrentAction();
if (current instanceof Action) {
Action cab = (Action) current;
cab.tick(ticksExisted);
cab.tick();
if (cab.isDone()) {
cab.kill();
queue.pollFirst();
@@ -282,7 +282,7 @@ public class ActionQueue implements IActionQueue {
Iterator<IAction> pit = queue.iterator();
while (pit.hasNext()) {
Action a = (Action) pit.next();
a.tick(ticksExisted);
a.tick();
if (a.isDone()) {
a.kill();
pit.remove();
@@ -291,7 +291,7 @@ public class ActionQueue implements IActionQueue {
}
}
protected void killWhenEmpty(int ticksExisted) {
protected void killWhenEmpty() {
if (killWhenEmpty && !hasActiveTasks() && autoKill == null)
autoKill = (Action) manager.create(killAfterTicks, (act) -> {
if (hasActiveTasks())
@@ -301,7 +301,7 @@ public class ActionQueue implements IActionQueue {
}).updateEvery(1).once();
if (autoKill != null) {
autoKill.tick(ticksExisted);
autoKill.tick();
if (autoKill.isDone())
autoKill = null;
}
@@ -92,6 +92,10 @@ public class ActionThread {
}
}
public boolean isTaskRunning(String taskKey) {
return runningTasks.contains(taskKey);
}
public boolean inActionThread() {
return Thread.currentThread() == thread;
}
@@ -108,13 +112,12 @@ public class ActionThread {
if (threadPaused && pauseUntil != null && pauseUntil.apply(action))
resume();
if (runningKeys.contains(taskKey))
if (!runningKeys.add(taskKey))
return;
final AtomicReference<Future<?>> taskRef = new AtomicReference<>();
taskRef.set(executor.submit(() -> {
runningTasks.add(taskRef.get());
runningKeys.add(taskKey);
try {
task.run();
} finally {
@@ -93,18 +93,18 @@ public class ConditionalAction extends Action implements IConditionalAction {
}
@Override
public void tick(int ticksExisted) {
updateEveryXTick = 10;
public void tick() {
if (isDone())
return;
duration++;
if (maxChecks > -1 && checkCount > maxChecks || maxCount == 0) {
markDone();
return;
}
if (ticksExisted % updateEveryXTick == 0) {
System.out.println("start");
if (duration % updateEveryXTick == 0) {
checkCount++;
Runnable execute = () -> {
@@ -116,7 +116,6 @@ public class ConditionalAction extends Action implements IConditionalAction {
executeOnTermination();
markDone();
}
System.out.println("exec'd!");
};
if (isThreaded)
@@ -127,8 +126,6 @@ public class ConditionalAction extends Action implements IConditionalAction {
if (maxCount > -1 && count >= maxCount)
markDone();
duration++;
}
protected void executeTask() {
@@ -367,7 +367,7 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
combatHandler.update();
onCollide();
actionManager.tick(ticksExisted);
actionManager.tick();
}
if (wasKilled != isKilled() && wasKilled) {