mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-17 18:05:10 +00:00
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:
1 parent
eecb5b0b3b
commit
f5dbe19de2
11 files changed
+260
-437
No files matched your search
@@ -7,7 +7,8 @@ import net.minecraft.client.renderer.texture.TextureManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import noppes.npcs.client.ClientCacheHandler;
|
||||
import noppes.npcs.client.gui.global.GuiNPCManageEffects;
|
||||
import noppes.npcs.client.gui.script.GuiScriptEffect;
|
||||
import noppes.npcs.client.gui.script.GuiScriptInterface;
|
||||
import noppes.npcs.controllers.data.EffectScript;
|
||||
import noppes.npcs.client.gui.util.GuiMenuTopButton;
|
||||
import noppes.npcs.client.gui.util.GuiNpcButtonYesNo;
|
||||
import noppes.npcs.client.gui.util.GuiNpcLabel;
|
||||
@@ -202,10 +203,7 @@ public class SubGuiEffectGeneral extends SubGuiInterface implements ITextfieldLi
|
||||
}
|
||||
if (id == -2) {
|
||||
PacketClient.sendClient(new EffectSavePacket(effect.writeToNBT(false), originalName));
|
||||
GuiScriptEffect scriptGUI = new GuiScriptEffect(parent, effect);
|
||||
scriptGUI.setWorldAndResolution(mc, width, height);
|
||||
scriptGUI.initGui();
|
||||
mc.currentScreen = scriptGUI;
|
||||
GuiScriptInterface.open(parent, new EffectScript(effect.id));
|
||||
}
|
||||
if (id == 10) {
|
||||
GuiNpcButtonYesNo button = (GuiNpcButtonYesNo) guibutton;
|
||||
|
||||
@@ -12,7 +12,8 @@ import net.minecraft.util.StatCollector;
|
||||
import noppes.npcs.client.CustomNpcResourceListener;
|
||||
import noppes.npcs.client.NoppesUtil;
|
||||
import noppes.npcs.client.gui.SubGuiNpcAvailability;
|
||||
import noppes.npcs.client.gui.script.GuiScriptRecipe;
|
||||
import noppes.npcs.client.gui.script.GuiScriptInterface;
|
||||
import noppes.npcs.controllers.data.RecipeScript;
|
||||
import noppes.npcs.client.gui.util.GuiButtonBiDirectional;
|
||||
import noppes.npcs.client.gui.util.GuiContainerNPCInterface2;
|
||||
import noppes.npcs.client.gui.util.GuiCustomScroll;
|
||||
@@ -222,15 +223,9 @@ public class GuiNpcManageRecipes extends GuiContainerNPCInterface2 implements IS
|
||||
if (button.id == 16) {
|
||||
save();
|
||||
if (container.width == 1) {
|
||||
GuiScriptRecipe gui = new GuiScriptRecipe(this, container.recipeAnvil);
|
||||
gui.setWorldAndResolution(mc, width, height);
|
||||
gui.initGui();
|
||||
mc.currentScreen = gui;
|
||||
GuiScriptInterface.open(this, new RecipeScript(container.recipeAnvil.id, true));
|
||||
} else if (!container.recipe.isGlobal) {
|
||||
GuiScriptRecipe gui = new GuiScriptRecipe(this, container.recipe);
|
||||
gui.setWorldAndResolution(mc, width, height);
|
||||
gui.initGui();
|
||||
mc.currentScreen = gui;
|
||||
GuiScriptInterface.open(this, new RecipeScript(container.recipe.id, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ import noppes.npcs.client.ClientCacheHandler;
|
||||
import noppes.npcs.client.CustomNpcResourceListener;
|
||||
import noppes.npcs.client.gui.SubGuiColorSelector;
|
||||
import noppes.npcs.client.gui.global.GuiNPCManageLinked;
|
||||
import noppes.npcs.client.gui.script.GuiScriptLinkedItem;
|
||||
import noppes.npcs.client.gui.script.GuiScriptInterface;
|
||||
import noppes.npcs.controllers.data.LinkedItemScript;
|
||||
import noppes.npcs.client.gui.util.GuiButtonBiDirectional;
|
||||
import noppes.npcs.client.gui.util.GuiMenuTopButton;
|
||||
import noppes.npcs.client.gui.util.GuiNpcButton;
|
||||
@@ -307,10 +308,7 @@ public class SubGuiLinkedItem extends SubGuiInterface implements ITextfieldListe
|
||||
return;
|
||||
} else if (id == -3) {
|
||||
PacketClient.sendClient(new LinkedItemSavePacket(linkedItem.writeToNBT(false), originalName));
|
||||
GuiScriptLinkedItem scriptGUI = new GuiScriptLinkedItem((GuiNPCManageLinked) this.parent, linkedItem);
|
||||
scriptGUI.setWorldAndResolution(mc, width, height);
|
||||
scriptGUI.initGui();
|
||||
mc.currentScreen = scriptGUI;
|
||||
GuiScriptInterface.open(this.parent, new LinkedItemScript(linkedItem.id));
|
||||
return;
|
||||
} else if (id == 20) {
|
||||
// Open confirmation for version bump.
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
package noppes.npcs.client.gui.script;
|
||||
|
||||
import kamkeel.npcs.network.packets.request.script.EffectScriptPacket;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.api.handler.IScriptHookHandler;
|
||||
import noppes.npcs.client.gui.global.GuiNPCManageEffects;
|
||||
import noppes.npcs.constants.ScriptContext;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptHookController;
|
||||
import noppes.npcs.controllers.data.CustomEffect;
|
||||
import noppes.npcs.controllers.data.EffectScript;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class GuiScriptEffect extends GuiScriptInterface {
|
||||
|
||||
public final CustomEffect effect;
|
||||
private final EffectScript scriptHandler;
|
||||
|
||||
public GuiScriptEffect(GuiNPCManageEffects parent, CustomEffect effect) {
|
||||
super();
|
||||
this.parent = parent;
|
||||
this.effect = effect;
|
||||
this.scriptHandler = new EffectScript();
|
||||
this.handler = this.scriptHandler;
|
||||
this.singleContainer = true;
|
||||
|
||||
this.hookList = new ArrayList<>(ScriptHookController.Instance.getAllHooks(IScriptHookHandler.CONTEXT_EFFECT));
|
||||
|
||||
EffectScriptPacket.Get(effect.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ScriptContext getScriptContext() {
|
||||
return ScriptContext.GLOBAL;
|
||||
}
|
||||
|
||||
protected void setHandlerUnit(IScriptUnit container) {
|
||||
this.scriptHandler.container = container;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGuiData(NBTTagCompound compound) {
|
||||
setGuiDataWithOldContainer(compound);
|
||||
}
|
||||
|
||||
protected void sendSavePacket(int index, int totalCount, NBTTagCompound scriptNBT) {
|
||||
EffectScriptPacket.Save(effect.id, index, totalCount, scriptNBT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
saveWithPackets();
|
||||
}
|
||||
}
|
||||
@@ -91,16 +91,17 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
* Create a GuiScriptInterface for a single script without a handler.
|
||||
* Wraps the script in a SingleScriptHandler automatically.
|
||||
*/
|
||||
public static GuiScriptInterface create(GuiScreen parent, IScriptHandler handler) {
|
||||
public static GuiScriptInterface open(GuiScreen parent, IScriptHandler handler) {
|
||||
GuiScriptInterface gui = new GuiScriptInterface();
|
||||
gui.handler = handler;
|
||||
gui.parent = parent;
|
||||
gui.singleContainer = true;
|
||||
gui.displayGuiScreen(gui);
|
||||
|
||||
// Initialize hooks from handler
|
||||
gui.hookList = new ArrayList<>(handler.getHooks());
|
||||
|
||||
// Request data from server
|
||||
if (handler instanceof IScriptHandlerPacket)
|
||||
((IScriptHandlerPacket) handler).requestData();
|
||||
|
||||
Minecraft.getMinecraft().displayGuiScreen(gui);
|
||||
return gui;
|
||||
@@ -637,60 +638,29 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
}
|
||||
|
||||
public void setGuiData(NBTTagCompound compound) {
|
||||
NBTTagList data = compound.getTagList("Languages", 10);
|
||||
HashMap languages = new HashMap();
|
||||
|
||||
for (int i = 0; i < data.tagCount(); ++i) {
|
||||
NBTTagCompound comp = data.getCompoundTagAt(i);
|
||||
ArrayList scripts = new ArrayList();
|
||||
NBTTagList list = comp.getTagList("Scripts", 8);
|
||||
|
||||
for (int j = 0; j < list.tagCount(); ++j) {
|
||||
scripts.add(list.getStringTagAt(j));
|
||||
if (handler instanceof IScriptHandlerPacket) {
|
||||
IScriptHandlerPacket.GuiDataResult result = ((IScriptHandlerPacket) handler).setGuiData(compound);
|
||||
if (result.kind == IScriptHandlerPacket.GuiDataKind.LOAD_COMPLETE) {
|
||||
loaded = true;
|
||||
return;
|
||||
}
|
||||
if (result.kind == IScriptHandlerPacket.GuiDataKind.METADATA) {
|
||||
loadLanguagesData(compound);
|
||||
return;
|
||||
}
|
||||
if (result.kind == IScriptHandlerPacket.GuiDataKind.TAB) {
|
||||
initGui();
|
||||
return;
|
||||
}
|
||||
|
||||
languages.put(comp.getString("Language"), scripts);
|
||||
}
|
||||
|
||||
this.languages = languages;
|
||||
this.initGui();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the script context for this GUI.
|
||||
* Override in subclasses to return the appropriate context.
|
||||
*
|
||||
* @return The script context (default: GLOBAL)
|
||||
*/
|
||||
protected ScriptContext getScriptContext() {
|
||||
return ScriptContext.GLOBAL;
|
||||
}
|
||||
|
||||
// ==================== UNIFIED SCRIPT DATA HANDLING ====================
|
||||
|
||||
/**
|
||||
* Unified setGuiData for script GUIs with old container system.
|
||||
* Handles both language data and tab-specific script loading.
|
||||
*/
|
||||
protected void setGuiDataWithOldContainer(NBTTagCompound compound) {
|
||||
if (compound.hasKey("LoadComplete")) {
|
||||
loaded = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!compound.hasKey("Tab")) {
|
||||
this.handler.setLanguage(compound.getString("ScriptLanguage"));
|
||||
this.handler.setEnabled(compound.getBoolean("ScriptEnabled"));
|
||||
this.loadLanguagesData(compound);
|
||||
} else {
|
||||
int tab = compound.getInteger("Tab");
|
||||
NBTTagCompound scriptCompound = compound.getCompoundTag("Script");
|
||||
IScriptUnit container = IScriptUnit.createFromNBT(scriptCompound, this.handler);
|
||||
this.setHandlerUnit(container);
|
||||
this.initGui();
|
||||
}
|
||||
|
||||
loadLanguagesData(compound);
|
||||
}
|
||||
|
||||
|
||||
// ==================== UNIFIED SCRIPT DATA HANDLING ====================
|
||||
|
||||
/**
|
||||
* Load languages data from NBT.
|
||||
* Separated for potential override in subclasses if needed.
|
||||
@@ -715,50 +685,16 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
|
||||
this.initGui();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the handler's container. Override if handler is not IScriptHandler.
|
||||
*/
|
||||
protected void setHandlerUnit(IScriptUnit container) {
|
||||
// Default implementation - subclasses may need to cast and set differently
|
||||
// e.g., ((LinkedItemScript) handler).container = container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unified save method for script GUIs with packet-based saving.
|
||||
* Subclasses only need to override sendScriptPackets() and sendMetadataPacket().
|
||||
*/
|
||||
protected void saveWithPackets() {
|
||||
if (loaded) {
|
||||
this.setScript();
|
||||
|
||||
List<IScriptUnit> containers = this.handler.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", this.handler.getLanguage());
|
||||
scriptData.setBoolean("ScriptEnabled", this.handler.getEnabled());
|
||||
scriptData.setTag("ScriptConsole", noppes.npcs.NBTTags.NBTLongStringMap(this.handler.getConsoleText()));
|
||||
|
||||
sendSavePacket(-1, containers.size(), scriptData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a script container packet. Override in subclasses.
|
||||
* @param index The index of this script
|
||||
* @param totalCount Total number of scripts
|
||||
* @param scriptNBT The script container NBT data
|
||||
*/
|
||||
protected void sendSavePacket(int index, int totalCount, NBTTagCompound scriptNBT) {
|
||||
// Default: do nothing (for non-packet-based GUIs like GuiScriptItem)
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (loaded)
|
||||
this.setScript();
|
||||
if (!loaded)
|
||||
return;
|
||||
|
||||
this.setScript();
|
||||
|
||||
if (handler instanceof IScriptHandlerPacket)
|
||||
((IScriptHandlerPacket) handler).sync();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void textUpdate(String text) {
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
package noppes.npcs.client.gui.script;
|
||||
|
||||
import kamkeel.npcs.network.packets.request.script.item.LinkedItemScriptPacket;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.api.handler.IScriptHookHandler;
|
||||
import noppes.npcs.client.gui.global.GuiNPCManageLinked;
|
||||
import noppes.npcs.constants.EnumScriptType;
|
||||
import noppes.npcs.constants.ScriptContext;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptHookController;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import noppes.npcs.controllers.data.LinkedItem;
|
||||
import noppes.npcs.controllers.data.LinkedItemScript;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class GuiScriptLinkedItem extends GuiScriptInterface {
|
||||
|
||||
public final LinkedItem linkedItem;
|
||||
|
||||
public GuiScriptLinkedItem(GuiNPCManageLinked parent, LinkedItem linkedItem) {
|
||||
super();
|
||||
this.parent = parent;
|
||||
this.linkedItem = linkedItem;
|
||||
this.handler = new LinkedItemScript();
|
||||
this.singleContainer = true;
|
||||
|
||||
this.hookList = new ArrayList<>(ScriptHookController.Instance.getAllHooks(IScriptHookHandler.CONTEXT_LINKED_ITEM));
|
||||
|
||||
LinkedItemScriptPacket.Get(linkedItem.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ScriptContext getScriptContext() {
|
||||
return ScriptContext.ITEM;
|
||||
}
|
||||
|
||||
protected void setHandlerUnit(IScriptUnit container) {
|
||||
((LinkedItemScript) handler).container = container;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGuiData(NBTTagCompound compound) {
|
||||
setGuiDataWithOldContainer(compound);
|
||||
}
|
||||
|
||||
protected void sendSavePacket(int index, int totalCount, NBTTagCompound scriptNBT) {
|
||||
LinkedItemScriptPacket.Save(linkedItem.id, index, totalCount, scriptNBT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
saveWithPackets();
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package noppes.npcs.client.gui.script;
|
||||
|
||||
import kamkeel.npcs.network.packets.request.script.RecipeScriptPacket;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.api.handler.IScriptHookHandler;
|
||||
import noppes.npcs.client.gui.global.GuiNpcManageRecipes;
|
||||
import noppes.npcs.constants.ScriptContext;
|
||||
import noppes.npcs.controllers.ScriptContainer;
|
||||
import noppes.npcs.controllers.ScriptHookController;
|
||||
import noppes.npcs.controllers.data.IScriptUnit;
|
||||
import noppes.npcs.controllers.data.RecipeAnvil;
|
||||
import noppes.npcs.controllers.data.RecipeCarpentry;
|
||||
import noppes.npcs.controllers.data.RecipeScript;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class GuiScriptRecipe extends GuiScriptInterface {
|
||||
|
||||
public final int recipeId;
|
||||
public final boolean anvil;
|
||||
|
||||
public GuiScriptRecipe(GuiNpcManageRecipes parent, RecipeCarpentry recipe) {
|
||||
super();
|
||||
this.parent = parent;
|
||||
this.handler = new RecipeScript();
|
||||
this.anvil = false;
|
||||
this.recipeId = recipe.id;
|
||||
this.singleContainer = true;
|
||||
|
||||
this.hookList = new ArrayList<>(ScriptHookController.Instance.getAllHooks(IScriptHookHandler.CONTEXT_RECIPE));
|
||||
|
||||
RecipeScriptPacket.Get(false, recipeId);
|
||||
}
|
||||
|
||||
public GuiScriptRecipe(GuiNpcManageRecipes parent, RecipeAnvil recipe) {
|
||||
super();
|
||||
this.parent = parent;
|
||||
this.handler = new RecipeScript();
|
||||
this.anvil = true;
|
||||
this.recipeId = recipe.id;
|
||||
|
||||
this.hookList = new ArrayList<>(ScriptHookController.Instance.getAllHooks(IScriptHookHandler.CONTEXT_RECIPE));
|
||||
|
||||
RecipeScriptPacket.Get(true, recipeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ScriptContext getScriptContext() {
|
||||
return ScriptContext.GLOBAL;
|
||||
}
|
||||
|
||||
protected void setHandlerUnit(IScriptUnit container) {
|
||||
((RecipeScript) handler).container = container;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGuiData(NBTTagCompound compound) {
|
||||
setGuiDataWithOldContainer(compound);
|
||||
}
|
||||
|
||||
protected void sendSavePacket(int index, int totalCount, NBTTagCompound scriptNBT) {
|
||||
RecipeScriptPacket.Save(anvil, recipeId, index, totalCount, scriptNBT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
saveWithPackets();
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user