- New JaninoScriptHandler thats a concrete IScriptHandler impl and replaces JaninoScriptable

- Removed ScriptHandler based methods from JaninoScript
This commit is contained in:
bigguy345 committed 2026-01-18 03:26:31 +02:00
1 parent f829926f00
commit 86285ceff5
4 files changed
+139 -84

No files matched your search

@@ -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<Long, String> map = this.container.getConsoleText();
Map<Long, String> map = this.container.getConsole();
StringBuilder builder = new StringBuilder();
Iterator var3 = map.entrySet()
.iterator();
@@ -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<S extends JaninoScript<?>> extends ScriptHandler {
protected S script;
private final Supplier<S> factory;
private final Class<S> scriptClass;
public JaninoScriptHandler(Supplier<S> factory, Class<S> 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<T>, 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<IScriptUnit> 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<IScriptUnit> getScripts() {
if (script == null)
return Collections.emptyList();
return Collections.<IScriptUnit>singletonList(script);
}
@Override
public IScriptUnit createJaninoScriptUnit() {
return factory.get();
}
@Override
public boolean supportsJanino() {
return true;
}
}
@@ -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<T> implements IScriptUnit {
public boolean enabled;
public boolean errored = false;
private String language = "Java";
public TreeMap<Long, String> console = new TreeMap();
@@ -157,14 +157,6 @@ public abstract class JaninoScript<T> 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<T> 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<T> implements IScriptUnit {
public void setExternalScripts(List<String> externalScripts) {
this.externalScripts = externalScripts;
this.evaluated = false;
this.hookNameMap.clear();
this.missingHookNames.clear();
}
@Override
@@ -470,7 +461,6 @@ public abstract class JaninoScript<T> 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<T> 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 <T, S extends JaninoScript<T>> S readFromNBT(NBTTagCompound compound, S script, Supplier<S> 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<Long, String> getConsoleText() {
TreeMap<Long, String> map = new TreeMap();
int tab = 0;
public static <T> NBTTagCompound writeToNBT(NBTTagCompound compound, JaninoScript<T> script) {
if (script != null)
compound.setTag("Script", script.writeToNBT(new NBTTagCompound()));
Iterator var5 = console.entrySet()
.iterator();
while (var5.hasNext()) {
Map.Entry<Long, String> longStringEntry = (Map.Entry) var5.next();
map.put(longStringEntry.getKey(), " tab " + tab + ":\n" + (String) longStringEntry.getValue());
}
return map;
return compound;
}
@Override
@@ -1,38 +0,0 @@
package noppes.npcs.janino;
import net.minecraft.nbt.NBTTagCompound;
import java.util.function.Supplier;
/**
*/
public interface JaninoScriptable<T extends JaninoScript<T>> {
boolean hasScript();
JaninoScript<T> getScript();
JaninoScript<T> createScript();
void deleteScript();
void unloadScript();
static <T, S extends JaninoScript<T>> S readFromNBT(NBTTagCompound compound, S script, Supplier<S> factory) {
if (compound.hasKey("Script")) {
if (script == null)
script = factory.get();
script.readFromNBT(compound.getCompoundTag("Script"));
}
return script;
}
static <T> NBTTagCompound writeToNBT(NBTTagCompound compound, JaninoScript<T> script) {
if (script != null)
compound.setTag("Script", script.writeToNBT(new NBTTagCompound()));
return compound;
}
}