2021-12-01 15:31:05 -06:00
|
|
|
//
|
|
|
|
|
// Source code recreated from a .class file by IntelliJ IDEA
|
|
|
|
|
// (powered by Fernflower decompiler)
|
|
|
|
|
//
|
|
|
|
|
|
2018-09-13 12:03:28 -04:00
|
|
|
package noppes.npcs.controllers;
|
|
|
|
|
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.io.StringWriter;
|
2021-12-01 15:31:05 -06:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.*;
|
2018-09-13 12:03:28 -04:00
|
|
|
import javax.script.Compilable;
|
|
|
|
|
import javax.script.CompiledScript;
|
2021-12-01 15:31:05 -06:00
|
|
|
import javax.script.Invocable;
|
2018-09-13 12:03:28 -04:00
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
import cpw.mods.fml.common.eventhandler.Event;
|
2018-09-13 12:03:28 -04:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2021-12-01 15:31:05 -06:00
|
|
|
import noppes.npcs.CustomNpcs;
|
2018-09-13 12:03:28 -04:00
|
|
|
import noppes.npcs.NBTTags;
|
2021-12-01 15:31:05 -06:00
|
|
|
import noppes.npcs.constants.EnumScriptType;
|
2022-05-11 22:08:31 -05:00
|
|
|
import noppes.npcs.scripted.NpcAPI;
|
2018-09-13 12:03:28 -04:00
|
|
|
|
|
|
|
|
public class ScriptContainer {
|
2021-12-01 15:31:05 -06:00
|
|
|
private static final String lock = "lock";
|
|
|
|
|
public static ScriptContainer Current;
|
|
|
|
|
private static String CurrentType;
|
|
|
|
|
private static final HashMap<String, Object> Data = new HashMap();
|
|
|
|
|
public String fullscript = "";
|
|
|
|
|
public String script = "";
|
|
|
|
|
public TreeMap<Long, String> console = new TreeMap();
|
|
|
|
|
public boolean errored = false;
|
|
|
|
|
public List<String> scripts = new ArrayList();
|
|
|
|
|
private HashSet<String> unknownFunctions = new HashSet();
|
|
|
|
|
public long lastCreated = 0L;
|
|
|
|
|
private String currentScriptLanguage = null;
|
|
|
|
|
public ScriptEngine engine = null;
|
|
|
|
|
private IScriptHandler handler = null;
|
|
|
|
|
private boolean init = false;
|
|
|
|
|
private static Method luaCoerce;
|
|
|
|
|
private static Method luaCall;
|
|
|
|
|
private CompiledScript compScript = null;
|
|
|
|
|
|
|
|
|
|
public ScriptContainer(IScriptHandler handler) {
|
|
|
|
|
this.handler = handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
|
|
|
|
this.script = compound.getString("Script");
|
|
|
|
|
//this.type = compound.getString("Type");
|
|
|
|
|
this.console = NBTTags.GetLongStringMap(compound.getTagList("Console", 10));
|
|
|
|
|
this.scripts = NBTTags.getStringList(compound.getTagList("ScriptList", 10));
|
|
|
|
|
this.lastCreated = 0L;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
|
|
|
|
compound.setString("Script", this.script);
|
|
|
|
|
//compound.setString("Type", this.type);
|
|
|
|
|
compound.setTag("Console", NBTTags.NBTLongStringMap(this.console));
|
|
|
|
|
compound.setTag("ScriptList", NBTTags.nbtStringList(this.scripts));
|
|
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getFullCode() {
|
|
|
|
|
if(!this.init) {
|
|
|
|
|
this.fullscript = this.script;
|
|
|
|
|
if(!this.fullscript.isEmpty()) {
|
|
|
|
|
this.fullscript = this.fullscript + "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Iterator var1 = this.scripts.iterator();
|
|
|
|
|
|
|
|
|
|
while(var1.hasNext()) {
|
|
|
|
|
String loc = (String)var1.next();
|
|
|
|
|
String code = (String)ScriptController.Instance.scripts.get(loc);
|
|
|
|
|
if(code != null && !code.isEmpty()) {
|
|
|
|
|
this.fullscript = this.fullscript + code + "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.fullscript;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run(ScriptEngine engine){
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
2021-12-03 21:13:56 -06:00
|
|
|
this.engine.getContext().setWriter(pw);
|
|
|
|
|
this.engine.getContext().setErrorWriter(pw);
|
|
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
try {
|
|
|
|
|
if(compScript == null && engine instanceof Compilable)
|
|
|
|
|
compScript = ((Compilable)engine).compile(getFullCode());
|
|
|
|
|
|
|
|
|
|
if(compScript != null){
|
|
|
|
|
compScript.eval(engine.getContext());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
engine.eval(getFullCode());
|
2021-12-03 21:13:56 -06:00
|
|
|
} catch (Throwable var14) {
|
|
|
|
|
this.errored = true;
|
|
|
|
|
var14.printStackTrace(pw);
|
|
|
|
|
} finally {
|
2021-12-13 22:51:33 -06:00
|
|
|
String errorString = sw.getBuffer().toString().trim();
|
2021-12-20 17:03:54 -06:00
|
|
|
this.appandConsole(errorString);
|
2021-12-03 21:13:56 -06:00
|
|
|
pw.close();
|
2021-12-01 15:31:05 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run(EnumScriptType type, Event event) {
|
2022-03-23 17:42:35 -05:00
|
|
|
if(!CustomNpcs.ScriptingEnabled)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
this.run((String)type.function, (Object)event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run(String type, Object event) {
|
2022-03-23 17:42:35 -05:00
|
|
|
if(!CustomNpcs.ScriptingEnabled)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-12-01 15:31:05 -06:00
|
|
|
if (!this.errored && this.hasCode() && !this.unknownFunctions.contains(type)) {
|
|
|
|
|
this.setEngine(this.handler.getLanguage());
|
|
|
|
|
if (this.engine != null) {
|
|
|
|
|
if (ScriptController.Instance.lastLoaded > this.lastCreated) {
|
|
|
|
|
this.lastCreated = ScriptController.Instance.lastLoaded;
|
|
|
|
|
this.init = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 21:54:05 -06:00
|
|
|
engine.put("API", NpcAPI.Instance());
|
|
|
|
|
|
|
|
|
|
Current = this;
|
|
|
|
|
CurrentType = type;
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
|
this.engine.getContext().setWriter(pw);
|
|
|
|
|
this.engine.getContext().setErrorWriter(pw);
|
2021-12-01 15:31:05 -06:00
|
|
|
|
2021-12-16 21:54:05 -06:00
|
|
|
try {
|
|
|
|
|
if (!this.init) {
|
|
|
|
|
this.engine.eval(this.getFullCode());
|
|
|
|
|
this.init = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
((Invocable)this.engine).invokeFunction(type, new Object[]{event});
|
|
|
|
|
} catch (NoSuchMethodException var13) {
|
|
|
|
|
this.unknownFunctions.add(type);
|
|
|
|
|
} catch (Throwable var14) {
|
|
|
|
|
this.errored = true;
|
|
|
|
|
var14.printStackTrace(pw);
|
|
|
|
|
} finally {
|
|
|
|
|
String errorString = sw.getBuffer().toString().trim();
|
2021-12-20 17:03:54 -06:00
|
|
|
this.appandConsole(errorString);
|
2021-12-16 21:54:05 -06:00
|
|
|
pw.close();
|
|
|
|
|
Current = null;
|
2021-12-01 15:31:05 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-03 18:17:52 -06:00
|
|
|
}
|
2021-12-01 15:31:05 -06:00
|
|
|
|
|
|
|
|
public void appandConsole(String message) {
|
|
|
|
|
if (message != null && !message.isEmpty()) {
|
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
|
if (this.console.containsKey(time)) {
|
|
|
|
|
message = (String)this.console.get(time) + "\n" + message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.console.put(time, message);
|
|
|
|
|
|
|
|
|
|
while(this.console.size() > 40) {
|
|
|
|
|
this.console.remove(this.console.firstKey());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean hasCode() {
|
|
|
|
|
return !this.getFullCode().isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setEngine(String scriptLanguage) {
|
|
|
|
|
if(currentScriptLanguage != null && currentScriptLanguage.equals(scriptLanguage))
|
|
|
|
|
return;
|
|
|
|
|
engine = ScriptController.Instance.getEngineByName(scriptLanguage.toLowerCase());
|
|
|
|
|
currentScriptLanguage = scriptLanguage;
|
|
|
|
|
}
|
2018-09-13 12:03:28 -04:00
|
|
|
}
|