mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 11:57:29 +00:00
Added ActionThread
IActions can now be threaded using action.threadify()
This commit is contained in:
1 parent
8b3ae8d73a
commit
9322cd7429
3 files changed
+111
-10
No files matched your search
@@ -16,8 +16,10 @@ public class Action implements IAction {
|
||||
protected int maxDuration = -1;
|
||||
protected int updateEveryXTick = 5;
|
||||
protected Consumer<IAction> task;
|
||||
private boolean done;
|
||||
private final Map<String, Object> dataStore = new HashMap<>();
|
||||
protected boolean done;
|
||||
protected final Map<String, Object> dataStore = new HashMap<>();
|
||||
protected boolean isThreaded;
|
||||
protected ActionThread threaded;
|
||||
|
||||
public Action(ScriptedActionManager manager, String name) {
|
||||
this.manager = manager;
|
||||
@@ -68,13 +70,17 @@ public class Action implements IAction {
|
||||
return;
|
||||
}
|
||||
if (ticksExisted % updateEveryXTick == 0 && task != null) {
|
||||
try {
|
||||
task.accept(this);
|
||||
count++;
|
||||
} catch (Throwable t) {
|
||||
System.err.println("Scripted action '" + name + "' threw an exception:");
|
||||
t.printStackTrace();
|
||||
markDone();
|
||||
if (isThreaded)
|
||||
threaded.run();
|
||||
else {
|
||||
try {
|
||||
task.accept(this);
|
||||
count++;
|
||||
} catch (Throwable t) {
|
||||
System.err.println("Scripted action '" + name + "' threw an exception:");
|
||||
t.printStackTrace();
|
||||
markDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
duration++;
|
||||
@@ -161,10 +167,34 @@ public class Action implements IAction {
|
||||
|
||||
@Override
|
||||
public IAction pauseFor(int ticks) {
|
||||
if (isThreaded)
|
||||
return threaded.pauseFor(ticks);
|
||||
|
||||
this.startAfterTicks = ticks;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAction pauseFor(long millis) {
|
||||
if (isThreaded)
|
||||
return threaded.pauseFor(millis);
|
||||
|
||||
return pauseFor((int) (millis / 50));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPaused() {
|
||||
if (isThreaded)
|
||||
return threaded.isPaused();
|
||||
|
||||
return startAfterTicks > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void threadify() {
|
||||
isThreaded = true;
|
||||
threaded = new ActionThread(this);
|
||||
}
|
||||
@Override
|
||||
public IAction getNext() {
|
||||
boolean seenMe = false;
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package noppes.npcs.controllers.data.action;
|
||||
|
||||
import noppes.npcs.api.handler.data.IAction;
|
||||
|
||||
public class ActionThread {
|
||||
private Action action;
|
||||
private Thread thread;
|
||||
private volatile boolean threadPaused;
|
||||
private boolean threadRunning;
|
||||
|
||||
public ActionThread(Action action) {
|
||||
this.action = action;
|
||||
createThread();
|
||||
}
|
||||
|
||||
private void createThread() {
|
||||
thread = new Thread(() -> {
|
||||
threadRunning = true;
|
||||
action.task.accept(action);
|
||||
threadRunning = false;
|
||||
action.count++;
|
||||
});
|
||||
}
|
||||
|
||||
public IAction pauseFor(long millis) {
|
||||
if (Thread.currentThread() == thread) {
|
||||
try {
|
||||
threadPaused = true;
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
threadPaused = false;
|
||||
return action;
|
||||
}
|
||||
} else
|
||||
return action.pauseFor((int) (millis / 50));
|
||||
}
|
||||
|
||||
public IAction pauseFor(int ticks) {
|
||||
if (Thread.currentThread() == thread) {
|
||||
try {
|
||||
threadPaused = true;
|
||||
Thread.sleep(ticks * 50);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
threadPaused = false;
|
||||
return action;
|
||||
}
|
||||
} else
|
||||
return action.pauseFor(ticks);
|
||||
}
|
||||
|
||||
public boolean isPaused() {
|
||||
return threadPaused;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if (threadRunning)
|
||||
return;
|
||||
|
||||
try {
|
||||
thread.start();
|
||||
} catch (Throwable t) {
|
||||
System.err.println("Scripted threaded action '" + action.name + "' threw an exception:");
|
||||
t.printStackTrace();
|
||||
action.markDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user