Deleagted all ScriptContainer NBT based creation to IScriptUnit.createFromNBT

This commit is contained in:
bigguy345 committed 2026-01-13 22:16:51 +02:00
1 parent fbcda09fe2
commit 73db727c47
14 files changed
+61 -36

No files matched your search

@@ -251,8 +251,8 @@ public class PacketUtil {
data.getScripts().add(new ScriptContainer(data));
}
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
ScriptContainer script = new ScriptContainer(data);
script.readFromNBT(tabCompound);
// Use factory method to create correct script unit type based on NBT
IScriptUnit script = IScriptUnit.createFromNBT(tabCompound, data);
data.getScripts().set(tab, script);
} else {
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
+2 -4
View File
@@ -546,8 +546,7 @@ public class NBTTags {
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound compoundd = list.getCompoundTagAt(i);
ScriptContainer script = new ScriptContainer(handler);
script.readFromNBT(compoundd);
IScriptUnit script = IScriptUnit.createFromNBT(compoundd, handler);
scripts.add(script);
}
@@ -559,8 +558,7 @@ public class NBTTags {
for (int i = 0; i < compound.getInteger("TotalScripts"); ++i) {
NBTTagCompound containerCompound = compound.getCompoundTag("Tab" + i);
ScriptContainer script = new ScriptContainer(handler);
script.readFromNBT(containerCompound);
IScriptUnit script = IScriptUnit.createFromNBT(containerCompound, handler);
scripts.add(script);
}
@@ -35,8 +35,8 @@ public class GuiNPCEventScripts extends GuiScriptInterface {
super.setGuiData(compound);
} else {
int tab = compound.getInteger("Tab");
ScriptContainer container = new ScriptContainer(script);
container.readFromNBT(compound.getCompoundTag("Script"));
NBTTagCompound scriptCompound = compound.getCompoundTag("Script");
IScriptUnit container = IScriptUnit.createFromNBT(scriptCompound, script);
if (script.getScripts().isEmpty()) {
for (int i = 0; i < compound.getInteger("TotalScripts"); i++) {
script.getScripts().add(new ScriptContainer(script));
@@ -33,8 +33,8 @@ public class GuiScriptAllNPCs extends GuiScriptInterface {
super.setGuiData(compound);
} else {
int tab = compound.getInteger("Tab");
ScriptContainer container = new ScriptContainer(script);
container.readFromNBT(compound.getCompoundTag("Script"));
NBTTagCompound scriptCompound = compound.getCompoundTag("Script");
IScriptUnit container = IScriptUnit.createFromNBT(scriptCompound, script);
if (script.getScripts().isEmpty()) {
for (int i = 0; i < compound.getInteger("TotalScripts"); i++) {
script.getScripts().add(new ScriptContainer(script));
@@ -88,8 +88,8 @@ public class GuiScriptForge extends GuiScriptInterface {
super.setGuiData(compound);
} else {
int tab = compound.getInteger("Tab");
ScriptContainer container = new ScriptContainer(script);
container.readFromNBT(compound.getCompoundTag("Script"));
NBTTagCompound scriptCompound = compound.getCompoundTag("Script");
IScriptUnit container = IScriptUnit.createFromNBT(scriptCompound, script);
if (script.getScripts().isEmpty()) {
for (int i = 0; i < compound.getInteger("TotalScripts"); i++) {
script.getScripts().add(new ScriptContainer(script));
@@ -656,8 +656,8 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
this.loadLanguagesData(compound);
} else {
int tab = compound.getInteger("Tab");
ScriptContainer container = new ScriptContainer(this.handler);
container.readFromNBT(compound.getCompoundTag("Script"));
NBTTagCompound scriptCompound = compound.getCompoundTag("Script");
IScriptUnit container = IScriptUnit.createFromNBT(scriptCompound, this.handler);
this.setHandlerContainer(container);
this.initGui();
}
@@ -36,8 +36,8 @@ public class GuiScriptPlayers extends GuiScriptInterface {
super.setGuiData(compound);
} else {
int tab = compound.getInteger("Tab");
ScriptContainer container = new ScriptContainer(script);
container.readFromNBT(compound.getCompoundTag("Script"));
NBTTagCompound scriptCompound = compound.getCompoundTag("Script");
IScriptUnit container = IScriptUnit.createFromNBT(scriptCompound, script);
if (script.getScripts().isEmpty()) {
for (int i = 0; i < compound.getInteger("TotalScripts"); i++) {
script.getScripts().add(new ScriptContainer(script));
@@ -70,6 +70,7 @@ public class ScriptContainer implements IScriptUnit {
}
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setString(IScriptUnit.NBT_TYPE_KEY, IScriptUnit.TYPE_ECMASCRIPT);
if (this.script.length() < 65535) {
compound.setString("Script", this.script);
} else {
@@ -39,8 +39,7 @@ public class EffectScript implements INpcScriptHandler {
enabled = compound.getBoolean("ScriptEnabled");
if (compound.hasKey("ScriptContent", Constants.NBT.TAG_COMPOUND)) {
container = new ScriptContainer(this);
container.readFromNBT(compound.getCompoundTag("ScriptContent"));
container = IScriptUnit.createFromNBT(compound.getCompoundTag("ScriptContent"), this);
}
return this;
}
@@ -142,9 +141,7 @@ public class EffectScript implements INpcScriptHandler {
if (tab == 0) {
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
ScriptContainer script = new ScriptContainer(this);
script.readFromNBT(tabCompound);
this.container = script;
this.container = IScriptUnit.createFromNBT(tabCompound, this);
} else {
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
this.setLanguage(compound.getString("ScriptLanguage"));
@@ -1,6 +1,8 @@
package noppes.npcs.controllers.data;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.controllers.ScriptContainer;
import noppes.npcs.janino.JaninoScript;
import java.util.List;
import java.util.Map;
@@ -13,6 +15,39 @@ import java.util.TreeMap;
*/
public interface IScriptUnit {
// NBT type identifiers
String NBT_TYPE_KEY = "ScriptUnitType";
String TYPE_ECMASCRIPT = "ECMAScript";
String TYPE_JANINO = "Janino";
/**
* Create an IScriptUnit from NBT data.
* Uses the "ScriptUnitType" tag to determine which implementation to create.
*
* @param compound The NBT data
* @param handler The script handler (used for ScriptContainer)
* @return A ScriptContainer or JaninoScript based on the NBT type
*/
static IScriptUnit createFromNBT(NBTTagCompound compound, IScriptHandler handler) {
String type = compound.getString(NBT_TYPE_KEY);
IScriptUnit unit;
if (TYPE_JANINO.equals(type)) {
// Create appropriate JaninoScript based on handler
unit = handler.createJaninoScriptUnit();
if (unit == null) {
// Fallback to ScriptContainer if handler doesn't support Janino
unit = new ScriptContainer(handler);
}
} else {
// Default to ScriptContainer for ECMAScript or missing type
unit = new ScriptContainer(handler);
}
unit.readFromNBT(compound);
return unit;
}
// ==================== SCRIPT CONTENT ====================
/**
@@ -37,8 +37,7 @@ public class LinkedItemScript implements INpcScriptHandler {
enabled = compound.getBoolean("ScriptEnabled");
if (compound.hasKey("ScriptContent", Constants.NBT.TAG_COMPOUND)) {
container = new ScriptContainer(this);
container.readFromNBT(compound.getCompoundTag("ScriptContent"));
container = IScriptUnit.createFromNBT(compound.getCompoundTag("ScriptContent"), this);
}
return this;
}
@@ -136,9 +135,7 @@ public class LinkedItemScript implements INpcScriptHandler {
if (tab == 0) {
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
ScriptContainer script = new ScriptContainer(this);
script.readFromNBT(tabCompound);
this.container = script;
this.container = IScriptUnit.createFromNBT(tabCompound, this);
} else {
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
this.setLanguage(compound.getString("ScriptLanguage"));
@@ -105,11 +105,10 @@ public class PlayerDataScript implements INpcScriptHandler {
if (player != null) {
scripts.clear();
for (IScriptUnit script : ScriptController.Instance.playerScripts.scripts) {
if (script instanceof ScriptContainer) {
ScriptContainer s = new ScriptContainer(this);
s.readFromNBT(((ScriptContainer) script).writeToNBT(new NBTTagCompound()));
scripts.add(s);
}
// Clone the script unit by writing to NBT and reading back with factory
NBTTagCompound nbt = script.writeToNBT(new NBTTagCompound());
IScriptUnit cloned = IScriptUnit.createFromNBT(nbt, this);
scripts.add(cloned);
}
}
lastPlayerUpdate = ScriptController.Instance.lastPlayerUpdate;
@@ -35,8 +35,7 @@ public class RecipeScript implements INpcScriptHandler {
scriptLanguage = compound.getString("ScriptLanguage");
enabled = compound.getBoolean("ScriptEnabled");
if (compound.hasKey("ScriptContent", Constants.NBT.TAG_COMPOUND)) {
container = new ScriptContainer(this);
container.readFromNBT(compound.getCompoundTag("ScriptContent"));
container = IScriptUnit.createFromNBT(compound.getCompoundTag("ScriptContent"), this);
}
return this;
}
@@ -131,9 +130,7 @@ public class RecipeScript implements INpcScriptHandler {
this.container = null;
if (tab == 0) {
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
ScriptContainer script = new ScriptContainer(this);
script.readFromNBT(tabCompound);
this.container = script;
this.container = IScriptUnit.createFromNBT(tabCompound, this);
} else {
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
this.setLanguage(compound.getString("ScriptLanguage"));
@@ -381,6 +381,7 @@ 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));