New IScriptHandlerPacket:

- Defines the packet-backed script GUI contract used by GuiScriptInterface for “streamed” script loading/saving.

- Updated EffectScript, LinkedItemScript and RecipeScript to implement IScriptHandlerPacket

- Removed GuiScriptEffect, GuiScriptLinkedItem and GuiScriptRecipe as their functionality got replaced with new interface handled by GuiScriptInterface
This commit is contained in:
bigguy345 committed 2026-01-18 17:57:10 +02:00
1 parent eecb5b0b3b
commit f5dbe19de2
11 files changed
+260 -437

No files matched your search

@@ -1,63 +1,55 @@
package noppes.npcs.controllers.data;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.util.ByteBufUtils;
import kamkeel.npcs.network.packets.request.script.EffectScriptPacket;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.Constants;
import noppes.npcs.controllers.ScriptController;
import noppes.npcs.api.handler.IScriptHookHandler;
import noppes.npcs.scripted.event.player.PlayerEvent;
import java.io.IOException;
public class EffectScript extends ScriptHandler {
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setString("ScriptLanguage", scriptLanguage);
compound.setBoolean("ScriptEnabled", enabled);
if (container != null)
compound.setTag("ScriptContent", container.writeToNBT(new NBTTagCompound()));
return compound;
/**
* Script handler for CustomEffect scripts.
* Manages a single script container for effect hooks (onEffectAdd, onEffectTick, onEffectRemove).
*/
public class EffectScript extends SingleScriptHandler implements IScriptHandlerPacket {
/** The effect ID for packet communication. -1 if not bound to an effect. */
private int effectId = -1;
/**
* Create an unbound EffectScript (for server-side use).
*/
public EffectScript() {
}
public EffectScript readFromNBT(NBTTagCompound compound) {
scriptLanguage = compound.getString("ScriptLanguage");
enabled = compound.getBoolean("ScriptEnabled");
if (compound.hasKey("ScriptContent", Constants.NBT.TAG_COMPOUND)) {
container = IScriptUnit.createFromNBT(compound.getCompoundTag("ScriptContent"), this);
}
return this;
/**
* Create an EffectScript bound to a specific effect (for GUI use).
* @param effectId The ID of the CustomEffect
*/
public EffectScript(int effectId) {
this.effectId = effectId;
}
@Override
public String getHookContext() {
return IScriptHookHandler.CONTEXT_EFFECT;
}
/**
* Convenience method to call a script with a typed event.
*/
public void callScript(ScriptType type, PlayerEvent.EffectEvent event) {
callScript(type.function, event);
}
public void saveScript(ByteBuf buffer) throws IOException {
int tab = buffer.readInt();
int totalScripts = buffer.readInt();
if (totalScripts == 0) {
this.container = null;
}
if (tab == 0) {
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
this.container = IScriptUnit.createFromNBT(tabCompound, this);
} else {
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
this.setLanguage(compound.getString("ScriptLanguage"));
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");
}
}
this.setEnabled(compound.getBoolean("ScriptEnabled"));
}
@Override
public void requestData() {
if (effectId >= 0)
EffectScriptPacket.Get(effectId);
}
@Override
public void sendSavePacket(int index, int totalCount, NBTTagCompound nbt) {
if (effectId >= 0)
EffectScriptPacket.Save(effectId, index, totalCount, nbt);
}
public enum ScriptType {
@@ -71,4 +63,4 @@ public class EffectScript extends ScriptHandler {
this.function = functionName;
}
}
}
}
@@ -0,0 +1,86 @@
package noppes.npcs.controllers.data;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.NBTTags;
import java.util.List;
public interface IScriptHandlerPacket extends IScriptHandler {
/**
* Client-side: request initial script data from the server (typically a {@code ...Packet.Get(...)}).
*/
void requestData();
/**
* Client-side: send a script container packet to the server.
*
* @param index {@code >= 0} for a script tab, {@code -1} for handler metadata (language/enabled/console)
* @param totalCount Total number of scripts for this handler
* @param nbt The script container NBT (tab content or metadata)
*/
void sendSavePacket(int index, int totalCount, NBTTagCompound nbt);
enum GuiDataKind {
LOAD_COMPLETE,
METADATA,
TAB
}
class GuiDataResult {
public final GuiDataKind kind;
public final int tabIndex;
public GuiDataResult(GuiDataKind kind, int tabIndex) {
this.kind = kind;
this.tabIndex = tabIndex;
}
}
/**
* Apply one incoming gui-data payload to this handler.
* <p>
* Notes:
* <ul>
* <li>{@code Languages} / {@code ScriptConsole} are not consumed here; GuiScriptInterface owns those UI concerns.</li>
* <li>Tab payloads are converted to an {@link IScriptUnit} via {@link IScriptUnit#createFromNBT}.</li>
* </ul>
*
* @return A small classification result so GuiScriptInterface can update GUI state.
*/
default GuiDataResult setGuiData(NBTTagCompound compound) {
if (compound.hasKey("LoadComplete")) {
return new GuiDataResult(GuiDataKind.LOAD_COMPLETE, -1);
}
if (!compound.hasKey("Tab")) {
setLanguage(compound.getString("ScriptLanguage"));
setEnabled(compound.getBoolean("ScriptEnabled"));
return new GuiDataResult(GuiDataKind.METADATA, -1);
}
int tab = compound.getInteger("Tab");
NBTTagCompound scriptCompound = compound.getCompoundTag("Script");
IScriptUnit unit = IScriptUnit.createFromNBT(scriptCompound, this);
replaceScriptUnit(tab, unit);
return new GuiDataResult(GuiDataKind.TAB, tab);
}
/**
* Emit save packets for all tabs plus one metadata packet.
* GuiScriptInterface is responsible for updating the active {@link IScriptUnit} with current editor text first.
*/
default void sync() {
List<IScriptUnit> containers = getScripts();
for (int i = 0; i < containers.size(); i++) {
IScriptUnit container = containers.get(i);
sendSavePacket(i, containers.size(), container.writeToNBT(new NBTTagCompound()));
}
NBTTagCompound scriptData = new NBTTagCompound();
scriptData.setString("ScriptLanguage", getLanguage());
scriptData.setBoolean("ScriptEnabled", getEnabled());
scriptData.setTag("ScriptConsole", NBTTags.NBTLongStringMap(getConsoleText()));
sendSavePacket(-1, containers.size(), scriptData);
}
}
@@ -1,60 +1,58 @@
package noppes.npcs.controllers.data;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.util.ByteBufUtils;
import kamkeel.npcs.network.packets.request.script.item.LinkedItemScriptPacket;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.Constants;
import noppes.npcs.controllers.ScriptController;
import noppes.npcs.api.handler.IScriptHookHandler;
import noppes.npcs.constants.ScriptContext;
import java.io.IOException;
public class LinkedItemScript extends ScriptHandler {
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setString("ScriptLanguage", scriptLanguage);
compound.setBoolean("ScriptEnabled", enabled);
if (container != null)
compound.setTag("ScriptContent", container.writeToNBT(new NBTTagCompound()));
return compound;
/**
* Script handler for LinkedItem scripts.
* Manages a single script container for linked item hooks.
*/
public class LinkedItemScript extends SingleScriptHandler implements IScriptHandlerPacket {
/** The linked item ID for packet communication. -1 if not bound. */
private int linkedItemId = -1;
/**
* Create an unbound LinkedItemScript (for server-side use).
*/
public LinkedItemScript() {
}
public LinkedItemScript readFromNBT(NBTTagCompound compound) {
scriptLanguage = compound.getString("ScriptLanguage");
enabled = compound.getBoolean("ScriptEnabled");
if (compound.hasKey("ScriptContent", Constants.NBT.TAG_COMPOUND)) {
container = IScriptUnit.createFromNBT(compound.getCompoundTag("ScriptContent"), this);
}
return this;
/**
* Create a LinkedItemScript bound to a specific linked item (for GUI use).
* @param linkedItemId The ID of the LinkedItem
*/
public LinkedItemScript(int linkedItemId) {
this.linkedItemId = linkedItemId;
}
@Override
public String getHookContext() {
return IScriptHookHandler.CONTEXT_LINKED_ITEM;
}
@Override
public ScriptContext getContext() {
return ScriptContext.ITEM;
}
@Override
public String noticeString() {
return "LinkedItem";
}
public void saveScript(ByteBuf buffer) throws IOException {
int tab = buffer.readInt();
int totalScripts = buffer.readInt();
if (totalScripts == 0) {
this.container = null;
}
if (tab == 0) {
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
this.container = IScriptUnit.createFromNBT(tabCompound, this);
} else {
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
this.setLanguage(compound.getString("ScriptLanguage"));
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");
}
}
this.setEnabled(compound.getBoolean("ScriptEnabled"));
}
@Override
public void requestData() {
if (linkedItemId >= 0)
LinkedItemScriptPacket.Get(linkedItemId);
}
@Override
public void sendSavePacket(int index, int totalCount, NBTTagCompound nbt) {
if (linkedItemId >= 0)
LinkedItemScriptPacket.Save(linkedItemId, index, totalCount, nbt);
}
}
@@ -1,57 +1,57 @@
package noppes.npcs.controllers.data;
import io.netty.buffer.ByteBuf;
import kamkeel.npcs.util.ByteBufUtils;
import kamkeel.npcs.network.packets.request.script.RecipeScriptPacket;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.Constants;
import noppes.npcs.controllers.ScriptController;
import noppes.npcs.api.handler.IScriptHookHandler;
import java.io.IOException;
public class RecipeScript extends ScriptHandler {
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setString("ScriptLanguage", scriptLanguage);
compound.setBoolean("ScriptEnabled", enabled);
if (container != null)
compound.setTag("ScriptContent", container.writeToNBT(new NBTTagCompound()));
return compound;
/**
* Script handler for Recipe scripts (both carpentry and anvil).
* Manages a single script container for recipe hooks (pre, post).
*/
public class RecipeScript extends SingleScriptHandler implements IScriptHandlerPacket {
/** The recipe ID for packet communication. -1 if not bound. */
private int recipeId = -1;
/** Whether this is an anvil recipe (vs carpentry). */
private boolean anvil = false;
/**
* Create an unbound RecipeScript (for server-side use).
*/
public RecipeScript() {
}
public RecipeScript readFromNBT(NBTTagCompound compound) {
scriptLanguage = compound.getString("ScriptLanguage");
enabled = compound.getBoolean("ScriptEnabled");
if (compound.hasKey("ScriptContent", Constants.NBT.TAG_COMPOUND)) {
container = IScriptUnit.createFromNBT(compound.getCompoundTag("ScriptContent"), this);
}
return this;
/**
* Create a RecipeScript bound to a specific recipe (for GUI use).
* @param recipeId The ID of the recipe
* @param anvil true for anvil recipe, false for carpentry recipe
*/
public RecipeScript(int recipeId, boolean anvil) {
this.recipeId = recipeId;
this.anvil = anvil;
}
@Override
public String getHookContext() {
return IScriptHookHandler.CONTEXT_RECIPE;
}
@Override
public String noticeString() {
return "RecipeScript";
}
public void saveScript(ByteBuf buffer) throws IOException {
int tab = buffer.readInt();
int totalScripts = buffer.readInt();
if (totalScripts == 0)
this.container = null;
if (tab == 0) {
NBTTagCompound tabCompound = ByteBufUtils.readNBT(buffer);
this.container = IScriptUnit.createFromNBT(tabCompound, this);
} else {
NBTTagCompound compound = ByteBufUtils.readNBT(buffer);
this.setLanguage(compound.getString("ScriptLanguage"));
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");
}
}
this.setEnabled(compound.getBoolean("ScriptEnabled"));
}
@Override
public void requestData() {
if (recipeId >= 0)
RecipeScriptPacket.Get(anvil, recipeId);
}
@Override
public void sendSavePacket(int index, int totalCount, NBTTagCompound nbt) {
if (recipeId >= 0)
RecipeScriptPacket.Save(anvil, recipeId, index, totalCount, nbt);
}
public enum ScriptType {