mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-28 21:38:44 +00:00
Add Script Optimizations / Caching
This commit is contained in:
1 parent
b231f65bfb
commit
8119515705
6 files changed
+74
-15
No files matched your search
@@ -45,6 +45,7 @@ public class DataScript implements IScriptHandlerPacket {
|
||||
public String scriptLanguage = "ECMAScript";
|
||||
private EntityNPCInterface npc;
|
||||
public boolean enabled = false;
|
||||
private long lastGlobalsVersion = -1;
|
||||
|
||||
public ICustomNpc dummyNpc;
|
||||
public IWorld dummyWorld;
|
||||
@@ -271,9 +272,13 @@ public class DataScript implements IScriptHandlerPacket {
|
||||
engine.put(entry.getKey(), entry.getValue().value);
|
||||
}
|
||||
|
||||
engine.put("API", NpcAPI.Instance());
|
||||
for (Map.Entry<String, Object> engineObjects : NpcAPI.engineObjects.entrySet()) {
|
||||
engine.put(engineObjects.getKey(), engineObjects.getValue());
|
||||
long currentGlobalsVersion = NpcAPI.engineObjectsVersion;
|
||||
if (lastGlobalsVersion != currentGlobalsVersion) {
|
||||
engine.put("API", NpcAPI.Instance());
|
||||
for (Map.Entry<String, Object> engineObjects : NpcAPI.engineObjects.entrySet()) {
|
||||
engine.put(engineObjects.getKey(), engineObjects.getValue());
|
||||
}
|
||||
lastGlobalsVersion = currentGlobalsVersion;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.io.IOException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
public class ForgeDataScript extends MultiScriptHandler {
|
||||
@@ -32,6 +33,7 @@ public class ForgeDataScript extends MultiScriptHandler {
|
||||
private static List<String> cachedHooks;
|
||||
|
||||
private long lastForgeUpdate = -1L;
|
||||
private final HashSet<String> globalUnknownEvents = new HashSet<>();
|
||||
|
||||
public ForgeDataScript() {
|
||||
}
|
||||
@@ -59,6 +61,7 @@ public class ForgeDataScript extends MultiScriptHandler {
|
||||
protected void reInitScripts() {
|
||||
lastInited = ScriptController.Instance.lastLoaded;
|
||||
lastForgeUpdate = ScriptController.Instance.lastForgeUpdate;
|
||||
globalUnknownEvents.clear();
|
||||
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script instanceof ScriptContainer)
|
||||
@@ -79,10 +82,25 @@ public class ForgeDataScript extends MultiScriptHandler {
|
||||
}
|
||||
}
|
||||
|
||||
// Skip events that no script tab handles
|
||||
if (globalUnknownEvents.contains(type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean anyHandled = false;
|
||||
for (IScriptUnit script : this.scripts) {
|
||||
if (script == null || script.hasErrored() || !script.hasCode())
|
||||
continue;
|
||||
boolean wasUnknown = script.isUnknownFunction(type);
|
||||
script.run(type, event);
|
||||
if (!wasUnknown || !script.isUnknownFunction(type)) {
|
||||
anyHandled = true;
|
||||
}
|
||||
}
|
||||
|
||||
// If no script tab handled this event type, cache it globally
|
||||
if (!anyHandled) {
|
||||
globalUnknownEvents.add(type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -168,6 +168,13 @@ public interface IScriptUnit {
|
||||
*/
|
||||
IScriptUnit createInstanceScope(IScriptHandler instanceHandler);
|
||||
|
||||
/**
|
||||
* Check if the given function name is known to be absent from this script unit.
|
||||
*/
|
||||
default boolean isUnknownFunction(String type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// ==================== EXECUTION ====================
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user