mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-24 16:24:26 +00:00
Compare commits
9
Commits
dev
...
java21-scripting
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba2721d3df | ||
|
|
b1f5f90915 | ||
|
|
b4aab454fe | ||
|
|
a7706b5e72 | ||
|
|
92afb0a69e | ||
|
|
5e7828ff66 | ||
|
|
77ef063198 | ||
|
|
f2c7121912 | ||
|
|
44b3ddedd8 |
No files matched your search
@@ -5,6 +5,12 @@ plugins {
|
|||||||
id 'idea'
|
id 'idea'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
toolchain {
|
||||||
|
languageVersion.set(JavaLanguageVersion.of(8))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
version = "1.10.2"
|
version = "1.10.2"
|
||||||
group= "kamkeel.customnpc-plus"
|
group= "kamkeel.customnpc-plus"
|
||||||
archivesBaseName = "CustomNPC-Plus"
|
archivesBaseName = "CustomNPC-Plus"
|
||||||
|
|||||||
+1
-1
@@ -36,7 +36,7 @@
|
|||||||
def embedMixin = !project.hasProperty("nomixin");
|
def embedMixin = !project.hasProperty("nomixin");
|
||||||
dependencies {
|
dependencies {
|
||||||
runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.5.4-GTNH:dev")
|
runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.5.4-GTNH:dev")
|
||||||
runtimeOnlyNonPublishable("curse.maven:Nashorn-374413:2924946")
|
// runtimeOnlyNonPublishable("curse.maven:Nashorn-374413:2924946")
|
||||||
|
|
||||||
if(embedMixin){
|
if(embedMixin){
|
||||||
// Normal Jar will include built in Mixins
|
// Normal Jar will include built in Mixins
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package noppes.npcs.controllers;
|
package noppes.npcs.controllers;
|
||||||
|
|
||||||
import cpw.mods.fml.common.eventhandler.Event;
|
import cpw.mods.fml.common.eventhandler.Event;
|
||||||
import jdk.nashorn.api.scripting.ScriptObjectMirror;
|
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import noppes.npcs.NBTTags;
|
import noppes.npcs.NBTTags;
|
||||||
import noppes.npcs.config.ConfigScript;
|
import noppes.npcs.config.ConfigScript;
|
||||||
@@ -9,7 +8,10 @@ import noppes.npcs.constants.EnumScriptType;
|
|||||||
import noppes.npcs.controllers.data.IScriptHandler;
|
import noppes.npcs.controllers.data.IScriptHandler;
|
||||||
import noppes.npcs.scripted.NpcAPI;
|
import noppes.npcs.scripted.NpcAPI;
|
||||||
|
|
||||||
import javax.script.*;
|
import javax.script.Compilable;
|
||||||
|
import javax.script.CompiledScript;
|
||||||
|
import javax.script.Invocable;
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@@ -33,7 +35,6 @@ public class ScriptContainer {
|
|||||||
private static Method luaCoerce;
|
private static Method luaCoerce;
|
||||||
private static Method luaCall;
|
private static Method luaCall;
|
||||||
private CompiledScript compScript = null;
|
private CompiledScript compScript = null;
|
||||||
private final HashMap<String, ScriptObjectMirror> cachedFunctions = new HashMap<>();
|
|
||||||
|
|
||||||
public ScriptContainer(IScriptHandler handler) {
|
public ScriptContainer(IScriptHandler handler) {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
@@ -172,7 +173,6 @@ public class ScriptContainer {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (!evaluated) {
|
if (!evaluated) {
|
||||||
this.cachedFunctions.clear();
|
|
||||||
engine.eval(getFullCode());
|
engine.eval(getFullCode());
|
||||||
evaluated = true;
|
evaluated = true;
|
||||||
}
|
}
|
||||||
@@ -188,15 +188,7 @@ public class ScriptContainer {
|
|||||||
unknownFunctions.add(type);
|
unknownFunctions.add(type);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!this.cachedFunctions.containsKey(type)) {
|
((Invocable) engine).invokeFunction(type, event);
|
||||||
ScriptObjectMirror global = (ScriptObjectMirror) engine.getBindings(ScriptContext.ENGINE_SCOPE);
|
|
||||||
ScriptObjectMirror func = (ScriptObjectMirror) global.get(type);
|
|
||||||
this.cachedFunctions.put(type, func);
|
|
||||||
}
|
|
||||||
ScriptObjectMirror func = this.cachedFunctions.get(type);
|
|
||||||
if (func != null) {
|
|
||||||
func.call(null, event);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (NoSuchMethodException e) {
|
} catch (NoSuchMethodException e) {
|
||||||
unknownFunctions.add(type);
|
unknownFunctions.add(type);
|
||||||
|
|||||||
@@ -30,6 +30,10 @@ import java.io.BufferedReader;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static noppes.npcs.util.CustomNPCsThreader.customNPCThread;
|
import static noppes.npcs.util.CustomNPCsThreader.customNPCThread;
|
||||||
@@ -55,6 +59,7 @@ public class ScriptController {
|
|||||||
|
|
||||||
public GlobalNPCDataScript globalNpcScripts = new GlobalNPCDataScript((EntityNPCInterface) null);
|
public GlobalNPCDataScript globalNpcScripts = new GlobalNPCDataScript((EntityNPCInterface) null);
|
||||||
public long lastGlobalNpcUpdate = 0L;
|
public long lastGlobalNpcUpdate = 0L;
|
||||||
|
private URLClassLoader loader;
|
||||||
|
|
||||||
public ScriptController() {
|
public ScriptController() {
|
||||||
Instance = this;
|
Instance = this;
|
||||||
@@ -64,34 +69,31 @@ public class ScriptController {
|
|||||||
LogWriter.info("Script Engines Available:");
|
LogWriter.info("Script Engines Available:");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.nashornFactory = new NashornScriptEngineFactory();
|
File nashornJar = new File("mods/nashorn.jar"); // Adjust path if needed
|
||||||
LogWriter.info("→ standalone Nashorn loaded");
|
this.loader = new URLClassLoader(new URL[]{nashornJar.toURI().toURL()}, CustomNpcs.class.getClassLoader());
|
||||||
} catch (NoClassDefFoundError e) {
|
|
||||||
|
ClassLoader cl = CustomNpcs.class.getClassLoader();
|
||||||
|
Thread.currentThread().setContextClassLoader(cl);
|
||||||
|
LogWriter.info("ClassLoader: " + cl);
|
||||||
|
LogWriter.info("Parent: " + cl.getParent());
|
||||||
|
|
||||||
|
Class<?> factoryClass = Class.forName("jdk.nashorn.api.scripting.NashornScriptEngineFactory", true, loader);
|
||||||
|
this.nashornFactory = (ScriptEngineFactory) factoryClass.getDeclaredConstructor().newInstance();
|
||||||
|
this.languages.put("ECMAScript", ".js");
|
||||||
|
LogWriter.info("→ standalone Nashorn loaded: " + this.nashornFactory.getEngineName());
|
||||||
|
} catch (Exception e) {
|
||||||
// fallback to built-in (Java 8–11) — also guarded
|
// fallback to built-in (Java 8–11) — also guarded
|
||||||
try {
|
try {
|
||||||
ScriptEngine eng = manager.getEngineByName("nashorn");
|
ScriptEngine eng = manager.getEngineByName("nashorn");
|
||||||
if (eng != null) {
|
if (eng != null) {
|
||||||
this.nashornFactory = eng.getFactory();
|
this.nashornFactory = eng.getFactory();
|
||||||
|
this.languages.put("ECMAScript", ".js");
|
||||||
LogWriter.info("→ built-in Nashorn loaded");
|
LogWriter.info("→ built-in Nashorn loaded");
|
||||||
}
|
}
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
LogWriter.error("No Nashorn engine available");
|
LogWriter.error("No Nashorn engine available");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ScriptEngineFactory fac : manager.getEngineFactories()) {
|
|
||||||
if (fac.getExtensions().isEmpty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
ScriptEngine scriptEngine = fac.getScriptEngine();
|
|
||||||
try {
|
|
||||||
scriptEngine.put("$RunTest", null);
|
|
||||||
String ext = "." + fac.getExtensions().get(0).toLowerCase();
|
|
||||||
LogWriter.info("Engine " + fac.getEngineName() + " running " + fac.getLanguageName() + " with extension: " + ext);
|
|
||||||
languages.put(fac.getLanguageName(), ext);
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private File forgeScriptsFile() {
|
private File forgeScriptsFile() {
|
||||||
@@ -299,31 +301,113 @@ public class ScriptController {
|
|||||||
|
|
||||||
public ScriptEngine getEngineByName(String language) {
|
public ScriptEngine getEngineByName(String language) {
|
||||||
if (nashornNames.contains(language) && this.nashornFactory != null) {
|
if (nashornNames.contains(language) && this.nashornFactory != null) {
|
||||||
ScriptEngine scriptEngine;
|
|
||||||
if (ConfigScript.EnableBannedClasses) {
|
|
||||||
try {
|
try {
|
||||||
ClassFilter filter = s -> {
|
ScriptEngine scriptEngine;
|
||||||
for (String className : ConfigScript.BannedClasses) {
|
Class<?> factoryClass = Class.forName("jdk.nashorn.api.scripting.NashornScriptEngineFactory",
|
||||||
if (s.compareTo(className) == 0) {
|
true, this.loader);
|
||||||
return false;
|
|
||||||
}
|
if (ConfigScript.EnableBannedClasses) {
|
||||||
|
Class<?> classFilterClass = Class.forName("jdk.nashorn.api.scripting.ClassFilter",
|
||||||
|
true, this.loader);
|
||||||
|
|
||||||
|
Object classFilter = Proxy.newProxyInstance(
|
||||||
|
classFilterClass.getClassLoader(),
|
||||||
|
new Class[]{classFilterClass},
|
||||||
|
(proxy, method, args) -> {
|
||||||
|
if ("exposeToScripts".equals(method.getName()) && args.length == 1) {
|
||||||
|
String className = (String) args[0];
|
||||||
|
for (String banned : ConfigScript.BannedClasses) {
|
||||||
|
if (className.equals(banned)) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
};
|
|
||||||
NashornScriptEngineFactory nashornScriptEngineFactory = (NashornScriptEngineFactory) this.nashornFactory;
|
|
||||||
scriptEngine = nashornScriptEngineFactory.getScriptEngine(filter);
|
|
||||||
} catch (Exception e) {
|
|
||||||
scriptEngine = this.nashornFactory.getScriptEngine();
|
|
||||||
}
|
}
|
||||||
|
throw new UnsupportedOperationException("Unsupported method: " + method);
|
||||||
|
});
|
||||||
|
|
||||||
|
Method getScriptEngine = factoryClass.getMethod(
|
||||||
|
"getScriptEngine",
|
||||||
|
classFilterClass);
|
||||||
|
|
||||||
|
scriptEngine = (ScriptEngine) getScriptEngine.invoke(this.nashornFactory, classFilter);
|
||||||
} else {
|
} else {
|
||||||
scriptEngine = this.nashornFactory.getScriptEngine();
|
Method getScriptEngine = factoryClass.getMethod("getScriptEngine");
|
||||||
|
scriptEngine = (ScriptEngine) getScriptEngine.invoke(this.nashornFactory);
|
||||||
}
|
}
|
||||||
scriptEngine.setBindings(this.manager.getBindings(), ScriptContext.GLOBAL_SCOPE);
|
scriptEngine.setBindings(this.manager.getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||||
return scriptEngine;
|
return scriptEngine;
|
||||||
|
} catch (Exception exception) {
|
||||||
|
LogWriter.except(exception);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return manager.getEngineByName(language);
|
return manager.getEngineByName(language);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ScriptEngine getOpenJDKNashornEngine() {
|
||||||
|
ScriptEngine engine;
|
||||||
|
try {
|
||||||
|
// Load factory and class filter
|
||||||
|
Class<?> factoryClass = Class.forName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory");
|
||||||
|
Object factory = factoryClass.getConstructor().newInstance();
|
||||||
|
|
||||||
|
// Load ClassFilter interface (from Nashorn 15.6)
|
||||||
|
Class<?> classFilterClass = Class.forName("org.openjdk.nashorn.api.scripting.ClassFilter");
|
||||||
|
|
||||||
|
Object classFilter;
|
||||||
|
if (ConfigScript.EnableBannedClasses) {
|
||||||
|
// Create proxy instance of ClassFilter
|
||||||
|
classFilter = Proxy.newProxyInstance(
|
||||||
|
classFilterClass.getClassLoader(),
|
||||||
|
new Class[]{classFilterClass},
|
||||||
|
(proxy, method, args) -> {
|
||||||
|
if ("exposeToScripts".equals(method.getName()) && args.length == 1) {
|
||||||
|
String className = (String) args[0];
|
||||||
|
for (String banned : ConfigScript.BannedClasses) {
|
||||||
|
if (className.equals(banned)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
throw new UnsupportedOperationException("Unsupported method: " + method);
|
||||||
|
});
|
||||||
|
|
||||||
|
Method getEngineMethod = factoryClass.getMethod(
|
||||||
|
"getScriptEngine",
|
||||||
|
String[].class,
|
||||||
|
ClassLoader.class,
|
||||||
|
classFilterClass
|
||||||
|
);
|
||||||
|
|
||||||
|
String[] args = new String[] {
|
||||||
|
"--language=es6",
|
||||||
|
"--optimistic-types=true",
|
||||||
|
"--lazy-compilation=true"
|
||||||
|
};
|
||||||
|
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||||
|
|
||||||
|
engine = (ScriptEngine) getEngineMethod.invoke(
|
||||||
|
factory,
|
||||||
|
(Object) args,
|
||||||
|
loader,
|
||||||
|
classFilter
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
Method getEngineMethod = factoryClass.getMethod("getScriptEngine", String[].class);
|
||||||
|
String[] args = new String[] {
|
||||||
|
"--language=es6",
|
||||||
|
"--optimistic-types=true",
|
||||||
|
"--lazy-compilation=true"
|
||||||
|
};
|
||||||
|
engine = (ScriptEngine) getEngineMethod.invoke(factory, (Object) args);
|
||||||
|
}
|
||||||
|
|
||||||
|
engine.setBindings(this.manager.getBindings(), ScriptContext.GLOBAL_SCOPE);
|
||||||
|
return engine;
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
ignored.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static List<String> immutableList(String... elements) {
|
private static List<String> immutableList(String... elements) {
|
||||||
return Collections.unmodifiableList(Arrays.asList(elements));
|
return Collections.unmodifiableList(Arrays.asList(elements));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user