mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-25 09:54:25 +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
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user