package noppes.npcs.controllers.data; import cpw.mods.fml.common.eventhandler.Event; import io.netty.buffer.ByteBuf; import kamkeel.npcs.util.ByteBufUtils; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.Constants; import noppes.npcs.config.ConfigScript; import noppes.npcs.controllers.ScriptController; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * Base class for script handlers that manage a single script container. * Provides shared NBT/ByteBuf serialization and packet handling. *
* Subclasses should:
* - Store their parent object (e.g., CustomEffect, LinkedItem)
* - Override getHookContext() to return the appropriate context string
* - Override getContext() if needed (default is GLOBAL)
* - Override requestData() and sendSavePacket() for packet communication
*/
public abstract class SingleScriptHandler extends ScriptHandler {
public IScriptUnit container;
@Override
public boolean isSingleContainer() {
return true;
}
protected boolean canRunScripts() {
return enabled && ScriptController.HasStart && ConfigScript.ScriptingEnabled && container != null;
}
@Override
public void callScript(String hookName, Event event) {
if (!canRunScripts()) {
return;
}
container.run(hookName, event);
}
@Override
public void setScripts(List
* Protocol:
* - tab == 0: Script content NBT follows
* - tab != 0 (typically -1): Script metadata (language, enabled) follows
*/
public void saveScript(ByteBuf buffer) throws IOException {
int tab = buffer.readInt();
int totalScripts = buffer.readInt();
if (totalScripts == 0) {
this.container = null;
}
if (tab == 0) {
// Script content
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
this.container = IScriptUnit.createFromNBT(tabCompound, this);
} else {
// Metadata (language, enabled)
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
this.setLanguage(compound.getString("ScriptLanguage"));
normalizeLanguage();
this.setEnabled(compound.getBoolean("ScriptEnabled"));
}
}
/**
* Normalize the language to a valid one if the current language is not available.
*/
protected void normalizeLanguage() {
if (!ScriptController.Instance.languages.containsKey(this.getLanguage())) {
if (!ScriptController.Instance.languages.isEmpty()) {
this.setLanguage((String) ScriptController.Instance.languages.keySet().toArray()[0]);
} else {
this.setLanguage("ECMAScript");
}
}
}
}