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

@@ -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"));