mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-24 19:54:26 +00:00
Compare commits
7
Commits
dev
...
open-jdk-nashorn
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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(21))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
version = "1.10.2"
|
version = "1.10.2"
|
||||||
group= "kamkeel.customnpc-plus"
|
group= "kamkeel.customnpc-plus"
|
||||||
archivesBaseName = "CustomNPC-Plus"
|
archivesBaseName = "CustomNPC-Plus"
|
||||||
|
|||||||
+3
-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
|
||||||
@@ -49,4 +49,6 @@ dependencies {
|
|||||||
exclude module: 'log4j-core'
|
exclude module: 'log4j-core'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
implementation('org.openjdk.nashorn:nashorn-core:15.4')
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
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;
|
||||||
import noppes.npcs.constants.EnumScriptType;
|
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 org.openjdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||||
|
|
||||||
import javax.script.*;
|
import javax.script.*;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package noppes.npcs.controllers;
|
package noppes.npcs.controllers;
|
||||||
|
|
||||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||||
import jdk.nashorn.api.scripting.ClassFilter;
|
|
||||||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
|
||||||
import net.minecraft.entity.player.EntityPlayer;
|
import net.minecraft.entity.player.EntityPlayer;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraft.nbt.NBTTagList;
|
import net.minecraft.nbt.NBTTagList;
|
||||||
@@ -21,6 +19,8 @@ import noppes.npcs.entity.EntityNPCInterface;
|
|||||||
import noppes.npcs.scripted.ScriptWorld;
|
import noppes.npcs.scripted.ScriptWorld;
|
||||||
import noppes.npcs.util.JsonException;
|
import noppes.npcs.util.JsonException;
|
||||||
import noppes.npcs.util.NBTJsonUtil;
|
import noppes.npcs.util.NBTJsonUtil;
|
||||||
|
import org.openjdk.nashorn.api.scripting.ClassFilter;
|
||||||
|
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
||||||
|
|
||||||
import javax.script.ScriptContext;
|
import javax.script.ScriptContext;
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
@@ -30,6 +30,8 @@ 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.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static noppes.npcs.util.CustomNPCsThreader.customNPCThread;
|
import static noppes.npcs.util.CustomNPCsThreader.customNPCThread;
|
||||||
@@ -64,34 +66,25 @@ public class ScriptController {
|
|||||||
LogWriter.info("Script Engines Available:");
|
LogWriter.info("Script Engines Available:");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.nashornFactory = new NashornScriptEngineFactory();
|
// this.nashornFactory = new NashornScriptEngineFactory();
|
||||||
LogWriter.info("→ standalone Nashorn loaded");
|
// LogWriter.info("→ standalone Nashorn loaded");
|
||||||
} catch (NoClassDefFoundError e) {
|
Class<?> clazz = Class.forName("org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory");
|
||||||
|
this.nashornFactory = (ScriptEngineFactory) clazz.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,7 +292,9 @@ 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;
|
ScriptEngine scriptEngine = getOpenJDKNashornEngine();
|
||||||
|
if (scriptEngine != null) return scriptEngine;
|
||||||
|
|
||||||
if (ConfigScript.EnableBannedClasses) {
|
if (ConfigScript.EnableBannedClasses) {
|
||||||
try {
|
try {
|
||||||
ClassFilter filter = s -> {
|
ClassFilter filter = s -> {
|
||||||
@@ -324,6 +319,71 @@ public class ScriptController {
|
|||||||
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