diff --git a/src/main/java/noppes/npcs/client/gui/script/GuiJaninoScript.java b/src/main/java/noppes/npcs/client/gui/script/GuiJaninoScript.java index 2f609b205..04d078fda 100644 --- a/src/main/java/noppes/npcs/client/gui/script/GuiJaninoScript.java +++ b/src/main/java/noppes/npcs/client/gui/script/GuiJaninoScript.java @@ -166,7 +166,8 @@ public class GuiJaninoScript extends GuiNPCInterface implements GuiYesNoCallback this.getScriptIndex())); // this.getButton(103).enabled = !this.languages.isEmpty(); this.addLabel(new GuiNpcLabel(2, "gui.enabled", var9, this.guiTop + 36)); - this.addButton(new GuiNpcButton(104, var9 + 60, this.guiTop + 31, 50, 20, new String[]{"gui.no", "gui.yes"}, this.container.getEnabled() ? 1 : 0)); + // Note: GuiJaninoScript is deprecated; enabled button disabled + this.addButton(new GuiNpcButton(104, var9 + 60, this.guiTop + 31, 50, 20, new String[]{"gui.no", "gui.yes"}, 0)); this.addButton(new GuiNpcButton(109, var9, this.guiTop + 78, 80, 20, "gui.website")); this.addButton(new GuiNpcButton(112, var9 + 81, this.guiTop + 78, 80, 20, "gui.forum")); @@ -280,7 +281,7 @@ public class GuiJaninoScript extends GuiNPCInterface implements GuiYesNoCallback } //Enable else if (guibutton.id == 104) { - this.container.setEnabled(((GuiNpcButton) guibutton).getValue() == 1); + // Deprecated GUI - enabled is now per-handler, not per-unit } //Remove else if (guibutton.id == 105) { @@ -347,7 +348,7 @@ public class GuiJaninoScript extends GuiNPCInterface implements GuiYesNoCallback } private String getConsoleText() { - Map map = this.container.getConsoleText(); + Map map = this.container.getConsole(); StringBuilder builder = new StringBuilder(); Iterator var3 = map.entrySet() .iterator(); diff --git a/src/main/java/noppes/npcs/controllers/data/JaninoScriptHandler.java b/src/main/java/noppes/npcs/controllers/data/JaninoScriptHandler.java new file mode 100644 index 000000000..81046844e --- /dev/null +++ b/src/main/java/noppes/npcs/controllers/data/JaninoScriptHandler.java @@ -0,0 +1,121 @@ +package noppes.npcs.controllers.data; + +import cpw.mods.fml.common.eventhandler.Event; +import net.minecraft.nbt.NBTTagCompound; +import noppes.npcs.constants.EnumScriptType; +import noppes.npcs.janino.JaninoScript; + +import java.util.Collections; +import java.util.List; +import java.util.function.Supplier; + +/** + * Reusable IScriptHandler implementation for objects that own an optional single JaninoScript unit. + * + * This is meant for "function provider" scripts (not EnumScriptType driven). Execution is typically + * initiated by the owning system calling script.call(...) directly. + * + * NBT storage is kept under the parent compound key "Script" (same convention as JaninoScriptable). + */ +public class JaninoScriptHandler> extends ScriptHandler { + + protected S script; + + private final Supplier factory; + private final Class scriptClass; + + public JaninoScriptHandler(Supplier factory, Class scriptClass) { + this.factory = factory; + this.scriptClass = scriptClass; + scriptLanguage = "Java"; + } + + // -------------------- Script ownership (JaninoScriptable) -------------------- + + public boolean hasScript() { + return script != null; + } + + public S getScript() { + return script; + } + + public JaninoScript createScript() { + if (script == null) + script = factory.get(); + + return script; + } + + public void deleteScript() { + unloadScript(); + script = null; + } + + public void unloadScript() { + if (script != null) { + script.unload(); + } + } + + // -------------------- NBT helpers -------------------- + + public void readFromNBT(NBTTagCompound compound) { + // JaninoScriptable's helper generics assume JaninoScript, but most of our scripts are JaninoScript. + // Keep it simple and safe: treat it as raw. + script = (S) JaninoScript.readFromNBT(compound, (JaninoScript) script, (Supplier) factory); + } + + public void writeToNBT(NBTTagCompound compound) { + JaninoScript.writeToNBT(compound, (JaninoScript) script); + } + + // -------------------- IScriptHandler -------------------- + + @Override + public void callScript(String hookName, Event event) { + // No-op: these scripts are not executed through the global hook dispatcher. + } + + @Override + public void callScript(EnumScriptType type, Event event) { + // No-op + } + + @Override + public void setScripts(List list) { + if (list == null || list.isEmpty() || list.get(0) == null) { + deleteScript(); + return; + } + + IScriptUnit unit = list.get(0); + if (scriptClass.isInstance(unit)) { + this.script = scriptClass.cast(unit); + return; + } + + // Fallback: copy code into a real instance of this script type. + S target = scriptClass.cast(createScript()); + target.setScript(unit.getScript()); + target.setExternalScripts(unit.getExternalScripts()); + } + + @Override + public List getScripts() { + if (script == null) + return Collections.emptyList(); + + return Collections.singletonList(script); + } + + @Override + public IScriptUnit createJaninoScriptUnit() { + return factory.get(); + } + + @Override + public boolean supportsJanino() { + return true; + } +} diff --git a/src/main/java/noppes/npcs/janino/JaninoScript.java b/src/main/java/noppes/npcs/janino/JaninoScript.java index 22fb9ee95..bc3d8e915 100644 --- a/src/main/java/noppes/npcs/janino/JaninoScript.java +++ b/src/main/java/noppes/npcs/janino/JaninoScript.java @@ -22,10 +22,10 @@ import java.security.*; import java.util.*; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; public abstract class JaninoScript implements IScriptUnit { - public boolean enabled; public boolean errored = false; private String language = "Java"; public TreeMap console = new TreeMap(); @@ -157,14 +157,6 @@ public abstract class JaninoScript implements IScriptUnit { } public void ensureCompiled() { - if (!isEnabled()) { - if (!evaluated) { - compileScript(""); - evaluated = true; - } - return; - } - int global = ScriptController.Instance.globalRevision; if (!evaluated || global != lastSeenGlobalRevision) { @@ -182,9 +174,6 @@ public abstract class JaninoScript implements IScriptUnit { private int lastSeenGlobalRevision; private String getFullCode() { - if (!isEnabled()) - return ""; - StringBuilder sb = new StringBuilder(); if (ConfigScript.RunLoadedScriptsFirst) @@ -306,6 +295,8 @@ public abstract class JaninoScript implements IScriptUnit { public void setExternalScripts(List externalScripts) { this.externalScripts = externalScripts; this.evaluated = false; + this.hookNameMap.clear(); + this.missingHookNames.clear(); } @Override @@ -470,7 +461,6 @@ public abstract class JaninoScript implements IScriptUnit { @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { compound.setString(IScriptUnit.NBT_TYPE_KEY, IScriptUnit.TYPE_JANINO); - compound.setBoolean("enabled", enabled); compound.setTag("console", NBTTags.NBTLongStringMap(this.console)); compound.setTag("externalScripts", NBTTags.nbtStringList(this.externalScripts)); compound.setString("script", script); @@ -479,46 +469,27 @@ public abstract class JaninoScript implements IScriptUnit { @Override public void readFromNBT(NBTTagCompound compound) { - this.enabled = compound.getBoolean("enabled"); + // Read legacy enabled field but ignore it (handler owns enabled now) this.console = NBTTags.GetLongStringMap(compound.getTagList("console", 10)); setExternalScripts(NBTTags.getStringList(compound.getTagList("externalScripts", 10))); setScript(compound.getString("script")); } - public boolean isEnabled() { - return this.enabled && ScriptController.HasStart && ConfigScript.ScriptingEnabled; - } + public static > S readFromNBT(NBTTagCompound compound, S script, Supplier factory) { + if (compound.hasKey("Script")) { + if (script == null) + script = factory.get(); - public boolean isClient() { - return FMLCommonHandler.instance() - .getEffectiveSide() - .isClient(); - } - - public boolean getEnabled() { - return this.enabled; - } - - public void setEnabled(boolean b) { - if (this.enabled != b) { - this.enabled = b; - this.evaluated = false; + script.readFromNBT(compound.getCompoundTag("Script")); } + return script; } - public Map getConsoleText() { - TreeMap map = new TreeMap(); - int tab = 0; + public static NBTTagCompound writeToNBT(NBTTagCompound compound, JaninoScript script) { + if (script != null) + compound.setTag("Script", script.writeToNBT(new NBTTagCompound())); - Iterator var5 = console.entrySet() - .iterator(); - - while (var5.hasNext()) { - Map.Entry longStringEntry = (Map.Entry) var5.next(); - map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + (String) longStringEntry.getValue()); - } - - return map; + return compound; } @Override diff --git a/src/main/java/noppes/npcs/janino/JaninoScriptable.java b/src/main/java/noppes/npcs/janino/JaninoScriptable.java deleted file mode 100644 index 9700bf893..000000000 --- a/src/main/java/noppes/npcs/janino/JaninoScriptable.java +++ /dev/null @@ -1,38 +0,0 @@ -package noppes.npcs.janino; - -import net.minecraft.nbt.NBTTagCompound; - -import java.util.function.Supplier; - -/** - - */ -public interface JaninoScriptable> { - - boolean hasScript(); - - JaninoScript getScript(); - - JaninoScript createScript(); - - void deleteScript(); - - void unloadScript(); - - static > S readFromNBT(NBTTagCompound compound, S script, Supplier factory) { - if (compound.hasKey("Script")) { - if (script == null) - script = factory.get(); - - script.readFromNBT(compound.getCompoundTag("Script")); - } - return script; - } - - static NBTTagCompound writeToNBT(NBTTagCompound compound, JaninoScript script) { - if (script != null) - compound.setTag("Script", script.writeToNBT(new NBTTagCompound())); - - return compound; - } -}