2025-06-28 09:30:21 +03:00
|
|
|
package noppes.npcs.controllers.data.action;
|
|
|
|
|
|
2025-06-28 19:07:19 +03:00
|
|
|
import java.util.function.Supplier;
|
2025-06-28 09:30:21 +03:00
|
|
|
|
|
|
|
|
public class ActionThread {
|
2025-06-28 19:07:19 +03:00
|
|
|
private final Action action;
|
|
|
|
|
|
|
|
|
|
private final Object lock = new Object();
|
2025-06-28 09:30:21 +03:00
|
|
|
private Thread thread;
|
2025-06-28 19:07:19 +03:00
|
|
|
private volatile boolean threadPaused, threadSleeping;
|
2025-06-28 09:30:21 +03:00
|
|
|
private boolean threadRunning;
|
|
|
|
|
|
2025-06-28 19:07:19 +03:00
|
|
|
private Supplier<Boolean> pauseUntil;
|
|
|
|
|
|
2025-06-28 09:30:21 +03:00
|
|
|
public ActionThread(Action action) {
|
|
|
|
|
this.action = action;
|
|
|
|
|
createThread();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createThread() {
|
|
|
|
|
thread = new Thread(() -> {
|
|
|
|
|
threadRunning = true;
|
|
|
|
|
action.task.accept(action);
|
|
|
|
|
threadRunning = false;
|
|
|
|
|
action.count++;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-28 19:07:19 +03:00
|
|
|
|
|
|
|
|
public void pauseFor(long millis) {
|
|
|
|
|
if (isPaused())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (inActionThread()) {
|
2025-06-28 09:30:21 +03:00
|
|
|
try {
|
2025-06-28 19:07:19 +03:00
|
|
|
threadSleeping = true;
|
2025-06-28 09:30:21 +03:00
|
|
|
Thread.sleep(millis);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
} finally {
|
2025-06-28 19:07:19 +03:00
|
|
|
threadSleeping = false;
|
2025-06-28 09:30:21 +03:00
|
|
|
}
|
|
|
|
|
} else
|
2025-06-28 19:07:19 +03:00
|
|
|
action.pauseFor((int) (millis / 50));
|
2025-06-28 09:30:21 +03:00
|
|
|
}
|
|
|
|
|
|
2025-06-28 19:07:19 +03:00
|
|
|
public void pauseFor(int ticks) {
|
|
|
|
|
pauseFor(ticks * 50L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void pause() {
|
|
|
|
|
if (!inActionThread() || isPaused())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
threadPaused = true;
|
|
|
|
|
synchronized (lock) {
|
|
|
|
|
while (threadPaused) {
|
|
|
|
|
try {
|
|
|
|
|
lock.wait(); // Wait until notified
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void pauseUntil(Supplier<Boolean> until) {
|
|
|
|
|
this.pauseUntil = until;
|
|
|
|
|
pause();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void resume() {
|
|
|
|
|
synchronized (lock) {
|
|
|
|
|
if (threadPaused) {
|
2025-06-28 09:30:21 +03:00
|
|
|
threadPaused = false;
|
2025-06-28 19:07:19 +03:00
|
|
|
lock.notify();
|
2025-06-28 09:30:21 +03:00
|
|
|
}
|
2025-06-28 19:07:19 +03:00
|
|
|
|
|
|
|
|
if (threadSleeping)
|
|
|
|
|
thread.interrupt();
|
|
|
|
|
}
|
2025-06-28 09:30:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isPaused() {
|
2025-06-28 19:07:19 +03:00
|
|
|
return threadPaused || threadSleeping;
|
2025-06-28 09:30:21 +03:00
|
|
|
}
|
|
|
|
|
|
2025-06-28 19:07:19 +03:00
|
|
|
public boolean inActionThread() {
|
|
|
|
|
return Thread.currentThread() == thread;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void run() {
|
|
|
|
|
if (threadPaused && pauseUntil != null && pauseUntil.get())
|
|
|
|
|
resume();
|
|
|
|
|
|
2025-06-28 09:30:21 +03:00
|
|
|
if (threadRunning)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
thread.start();
|
|
|
|
|
} catch (Throwable t) {
|
|
|
|
|
System.err.println("Scripted threaded action '" + action.name + "' threw an exception:");
|
|
|
|
|
t.printStackTrace();
|
|
|
|
|
action.markDone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|