mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-13 19:34:09 +00:00
Merge branch 'dev' of https://github.com/KAMKEEL/CustomNPC-Plus into dev
This commit is contained in:
commit
57ffcb8b0e
8 files changed
+106
-38
No files matched your search
@@ -11,7 +11,7 @@ import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import noppes.npcs.client.AnalyticsTracking;
|
||||
import noppes.npcs.controllers.data.PlayerData;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
import noppes.npcs.controllers.data.action.ActionManager;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
@@ -21,7 +21,7 @@ public class ServerTickHandler {
|
||||
@SubscribeEvent
|
||||
public void onServerTick(TickEvent.ServerTickEvent event) {
|
||||
if (event.phase == Phase.END) {
|
||||
NpcAPI.actionManager.tick();
|
||||
ActionManager.GLOBAL.tick();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,7 +107,7 @@ public class Action implements IAction {
|
||||
this.isScheduled = true;
|
||||
|
||||
if (manager.debug)
|
||||
manager.LOGGER.log(String.format("Scheduled Action '%s'", name), queue);
|
||||
manager.LOGGER.log(String.format("Scheduled %s", getIdentifier()), queue);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -278,7 +278,7 @@ public class Action implements IAction {
|
||||
}
|
||||
|
||||
if (duration == 0 && onStart != null)
|
||||
execute("start", this::executeOnStart);
|
||||
execute("onStart", this::executeOnStart);
|
||||
|
||||
if (done)
|
||||
return;
|
||||
@@ -368,6 +368,9 @@ public class Action implements IAction {
|
||||
|
||||
@Override
|
||||
public void kill() {
|
||||
if (manager.debug)
|
||||
manager.LOGGER.log(String.format("Killing %s...", getIdentifier()), queue);
|
||||
|
||||
if (actionThread != null)
|
||||
actionThread.stop();
|
||||
|
||||
@@ -375,12 +378,15 @@ public class Action implements IAction {
|
||||
isScheduled = false;
|
||||
done = true;
|
||||
|
||||
if (manager.debug)
|
||||
manager.LOGGER.log(String.format("Killed Action '%s'", name), queue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return String.format("%s '%s'", getClass().getSimpleName(), name);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
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);
|
||||
return String.format("%s [queue='%s', scheduled=%s, done=%s, paused=%s, updateEvery=%s, duration=%d/%d, count=%d/%d, threaded=%s]", getIdentifier(), getQueueName(), isScheduled, done, isPaused(), updateEveryXTick, duration, maxDuration, count, maxCount, isThreaded);
|
||||
}
|
||||
///////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
@@ -94,16 +94,14 @@ public class ActionLogger {
|
||||
|
||||
while (it.hasPrevious()) {
|
||||
TreeNode node = it.previous();
|
||||
if (it.hasPrevious()) {
|
||||
if (it.hasPrevious())
|
||||
prefix.append("│ ");
|
||||
} else {
|
||||
else
|
||||
prefix.append(isLast ? "└──── " : "├──── ");
|
||||
}
|
||||
}
|
||||
|
||||
String header = getLogHeader(source);
|
||||
LogWriter.info(prefix + header + message);
|
||||
|
||||
println(prefix + header + message);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -126,11 +124,20 @@ public class ActionLogger {
|
||||
name = ((IActionQueue) obj).getName();
|
||||
} else if (obj instanceof IAction) {
|
||||
name = ((IAction) obj).getName();
|
||||
} else if (obj instanceof ActionThread) {
|
||||
name = ((ActionThread) obj).getAction().getName();
|
||||
}
|
||||
|
||||
return String.format("[%s/'%s'] ", type, name);
|
||||
}
|
||||
|
||||
private static final String ANSI_CYAN = "\u001B[96m";
|
||||
private static final String ANSI_GOLD = "\u001B[93m";
|
||||
private static final String ANSI_RESET = "\u001B[0m";
|
||||
|
||||
public void println(String str) {
|
||||
System.out.printf("[%tT] %s[%s]:%s %s%s%n", new Date(), ANSI_CYAN, Thread.currentThread().getName(), ANSI_GOLD, str, ANSI_RESET);
|
||||
}
|
||||
|
||||
// public ActionLogger beginTick1(ActionManager manager) {
|
||||
// snapshot.clear();
|
||||
|
||||
@@ -7,6 +7,7 @@ import noppes.npcs.api.handler.data.IActionQueue;
|
||||
import noppes.npcs.api.handler.data.actions.IConditionalAction;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.data.action.action.ConditionalAction;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
@@ -21,6 +22,8 @@ import java.util.function.Function;
|
||||
* - cancellation & clearing
|
||||
*/
|
||||
public class ActionManager implements IActionManager {
|
||||
public static final ActionManager GLOBAL = new ActionManager("GLOBAL");
|
||||
|
||||
protected boolean isWorking = false;
|
||||
public ActionLogger LOGGER = new ActionLogger(this);
|
||||
protected String name = "";
|
||||
@@ -33,6 +36,19 @@ public class ActionManager implements IActionManager {
|
||||
protected boolean debug;
|
||||
protected ScriptContainer reportTo;
|
||||
|
||||
private static final Map<Object, IActionListener> listeners = new WeakHashMap<>();
|
||||
|
||||
public IActionListener getOrCreateListener(Object obj) {
|
||||
return listeners.computeIfAbsent(obj, o -> new ActionListener(o));
|
||||
}
|
||||
|
||||
public ActionManager() {
|
||||
}
|
||||
|
||||
public ActionManager(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IActionManager start() {
|
||||
@@ -66,6 +82,9 @@ public class ActionManager implements IActionManager {
|
||||
@Override
|
||||
public IActionManager setDebugMode(boolean debug) {
|
||||
this.debug = debug;
|
||||
|
||||
if (debug)
|
||||
LOGGER.log("Enabled debug mode", this);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ public class ActionQueue implements IActionQueue {
|
||||
protected boolean isParallel;
|
||||
|
||||
protected boolean isDead;
|
||||
protected boolean stopWhenEmpty;
|
||||
|
||||
protected boolean killWhenEmpty;
|
||||
protected int killWhenEmptyAfter = 100;
|
||||
|
||||
@@ -70,6 +72,18 @@ public class ActionQueue implements IActionQueue {
|
||||
isParallel = parallel;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStoppedWhenEmpty() {
|
||||
return stopWhenEmpty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IActionQueue stopWhenEmpty(boolean stopWhenEmpty) {
|
||||
this.stopWhenEmpty = stopWhenEmpty;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isKilledWhenEmpty() {
|
||||
return killWhenEmpty;
|
||||
@@ -261,31 +275,35 @@ public class ActionQueue implements IActionQueue {
|
||||
///////////////////////////////////////////////////
|
||||
// Handling
|
||||
|
||||
protected boolean tick(Action a) {
|
||||
if (a == null)
|
||||
return false;
|
||||
|
||||
if (manager.debug)
|
||||
manager.LOGGER.push(this).log(String.format("Started ticking Action '%s' ", a.name), this).push(a);
|
||||
|
||||
a.tick();
|
||||
|
||||
if (manager.debug)
|
||||
manager.LOGGER.pop().finish(String.format("Finished ticking Action '%s' ", a.name), this).pop();
|
||||
|
||||
public boolean finish(Action a) {
|
||||
if (a.isDone()) {
|
||||
if (a.onDone != null)
|
||||
a.execute("done", a::executeOnDone);
|
||||
a.execute("onDone", a::executeOnDone);
|
||||
|
||||
a.kill();
|
||||
|
||||
if (manager.debug)
|
||||
manager.LOGGER.finish(String.format("Removing Action '%s' from queue", a.name), this);
|
||||
manager.LOGGER.finish(String.format("Removing %s from queue", a.getIdentifier()), this);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
protected boolean tick(Action a) {
|
||||
if (a == null)
|
||||
return false;
|
||||
|
||||
boolean wasDone = a.isDone();
|
||||
if (manager.debug && !wasDone)
|
||||
manager.LOGGER.push(this).log(String.format("Started ticking %s", a.getIdentifier(), a.name), this).push(a);
|
||||
|
||||
a.tick();
|
||||
|
||||
if (manager.debug && !wasDone)
|
||||
manager.LOGGER.pop().finish(String.format("Finished ticking %s", a.getIdentifier(), a.name), this).pop();
|
||||
|
||||
return finish(a);
|
||||
}
|
||||
|
||||
protected void tick() {
|
||||
if (!isWorking || isDead)
|
||||
@@ -305,6 +323,9 @@ public class ActionQueue implements IActionQueue {
|
||||
pit.remove();
|
||||
}
|
||||
|
||||
if (stopWhenEmpty && !hasActiveTasks())
|
||||
stop();
|
||||
|
||||
killWhenEmpty();
|
||||
|
||||
if (manager.debug && active)
|
||||
|
||||
@@ -29,6 +29,9 @@ public class ActionThread {
|
||||
thread.setName(String.format("ActionThread (%s)", action.getName()));
|
||||
return thread;
|
||||
});
|
||||
|
||||
if (action.manager.debug)
|
||||
action.manager.LOGGER.log("Created Action thread!", this);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
@@ -41,7 +44,11 @@ public class ActionThread {
|
||||
|
||||
|
||||
if (action.manager.debug)
|
||||
action.manager.LOGGER.log("Stopping thread", action);
|
||||
action.manager.LOGGER.log("Stopping thread", this);
|
||||
}
|
||||
|
||||
public Action getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void pauseFor(long millis) {
|
||||
@@ -51,7 +58,7 @@ public class ActionThread {
|
||||
try {
|
||||
threadSleeping = true;
|
||||
if (action.manager.debug)
|
||||
action.manager.LOGGER.log(String.format("Sleeping thread for %s ticks", millis / 50), action);
|
||||
action.manager.LOGGER.log(String.format("Sleeping thread for %s ticks", millis / 50), this);
|
||||
|
||||
Thread.sleep(millis);
|
||||
} catch (InterruptedException e) {
|
||||
@@ -60,7 +67,7 @@ public class ActionThread {
|
||||
threadSleeping = false;
|
||||
|
||||
if (action.manager.debug)
|
||||
action.manager.LOGGER.log(String.format("Woken up thread after sleeping for %s ticks", millis / 50), action);
|
||||
action.manager.LOGGER.log(String.format("Woken up thread after sleeping for %s ticks", millis / 50), this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +84,7 @@ public class ActionThread {
|
||||
while (threadPaused) {
|
||||
try {
|
||||
if (action.manager.debug)
|
||||
action.manager.LOGGER.log(String.format("Pausing thread"), action);
|
||||
action.manager.LOGGER.log(String.format("Pausing thread"), this);
|
||||
|
||||
lock.wait(); // Wait until notified
|
||||
} catch (InterruptedException e) {
|
||||
@@ -99,7 +106,7 @@ public class ActionThread {
|
||||
lock.notify();
|
||||
|
||||
if (action.manager.debug)
|
||||
action.manager.LOGGER.log(String.format("Resumed thread"), action);
|
||||
action.manager.LOGGER.log(String.format("Resumed thread"), this);
|
||||
}
|
||||
|
||||
if (threadSleeping)
|
||||
@@ -130,14 +137,27 @@ public class ActionThread {
|
||||
if (!runningKeys.add(taskKey))
|
||||
return;
|
||||
|
||||
if (action.manager.debug)
|
||||
action.manager.LOGGER.log(String.format("Delegating task '%s' to Action thread...", taskKey), action);
|
||||
|
||||
final AtomicReference<Future<?>> taskRef = new AtomicReference<>();
|
||||
taskRef.set(executor.submit(() -> {
|
||||
if (action.manager.debug)
|
||||
action.manager.LOGGER.log(String.format("Started executing task '%s' on Action thread...", taskKey), this);
|
||||
|
||||
runningTasks.add(taskRef.get());
|
||||
try {
|
||||
task.run();
|
||||
} finally {
|
||||
runningTasks.remove(taskRef.get());
|
||||
runningKeys.remove(taskKey);
|
||||
|
||||
if (action.manager.debug)
|
||||
action.manager.LOGGER.log(String.format("Finished executing task '%s' on Action thread", taskKey), this);
|
||||
|
||||
if (((ActionQueue) action.queue).finish(action))
|
||||
action.queue.getQueue().remove(action);
|
||||
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -155,8 +155,7 @@ public class ConditionalAction extends Action implements IConditionalAction {
|
||||
|
||||
|
||||
public String toString() {
|
||||
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(),
|
||||
return String.format("%s [queue='%s', scheduled=%s, done=%s, paused=%s, updateEvery=%s, duration=%d/%d, count=%d/%d, checks=%d/%d, taskExecuted=%s, threaded=%s]", getIdentifier(), getQueueName(),
|
||||
isScheduled,
|
||||
done,
|
||||
isPaused(),
|
||||
|
||||
@@ -79,7 +79,6 @@ public class NpcAPI extends AbstractNpcAPI {
|
||||
private static final CacheHashMap<ItemStack, CacheHashMap.CachedObject<ScriptItemStack>> scriptItemCache = new CacheHashMap<>(60 * 1000);
|
||||
public static final HashMap<String, Object> engineObjects = new HashMap<>();
|
||||
public static final EventBus EVENT_BUS = new EventBus();
|
||||
public static final ActionManager actionManager = new ActionManager();
|
||||
private static AbstractNpcAPI instance = null;
|
||||
|
||||
private final static String API_USER_AGENT = "CNPC+API";
|
||||
@@ -569,10 +568,7 @@ public class NpcAPI extends AbstractNpcAPI {
|
||||
}
|
||||
|
||||
public IActionManager getActionManager() {
|
||||
if (actionManager.getName().isEmpty())
|
||||
actionManager.setName("Global");
|
||||
|
||||
return actionManager;
|
||||
return ActionManager.GLOBAL;
|
||||
}
|
||||
|
||||
private void checkWorld() {
|
||||
|
||||
Reference in new issue
Block a user