mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-26 23:12:55 +00:00
Some Ability Helpers
This commit is contained in:
1 parent
8fc0155df8
commit
8aa6e926f6
12 files changed
+326
-92
No files matched your search
@@ -281,11 +281,9 @@ public class SubGuiAbilityConfig extends SubGuiInterface implements ITextfieldLi
|
||||
|
||||
// Declarative field handling (effects list, booleans, enums, sub-guis, clear buttons)
|
||||
if (builder.handleButtonEvent(id, guibutton)) {
|
||||
// SUB_GUI needs special handling: open the sub-gui
|
||||
FieldDef subGuiDef = builder.getActiveSubGuiField();
|
||||
if (subGuiDef != null && subGuiDef.getSubGuiFactory() != null) {
|
||||
setSubGui(subGuiDef.getSubGuiFactory().get());
|
||||
} else {
|
||||
// SUB_GUI fields are opened directly by the builder via setSubGuiWithResult().
|
||||
// Only rebuild if no sub-gui was opened (e.g. boolean toggle, enum change).
|
||||
if (!hasSubGui()) {
|
||||
initGui();
|
||||
}
|
||||
return;
|
||||
|
||||
@@ -297,4 +297,55 @@ public class FieldDef {
|
||||
public FieldDef getLeftChild() { return leftChild; }
|
||||
public FieldDef getRightChild() { return rightChild; }
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// LIST MANIPULATION (for mod injection)
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Insert a field before the first field matching the target label.
|
||||
* Searches top-level labels and ROW children.
|
||||
*
|
||||
* @param fields The field list to modify
|
||||
* @param targetLabel The label of the field to insert before
|
||||
* @param newField The field to insert
|
||||
* @return true if the target was found and the field was inserted
|
||||
*/
|
||||
public static boolean insertBefore(java.util.List<FieldDef> fields, String targetLabel, FieldDef newField) {
|
||||
for (int i = 0; i < fields.size(); i++) {
|
||||
if (matchesLabel(fields.get(i), targetLabel)) {
|
||||
fields.add(i, newField);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a field after the first field matching the target label.
|
||||
* Searches top-level labels and ROW children.
|
||||
*
|
||||
* @param fields The field list to modify
|
||||
* @param targetLabel The label of the field to insert after
|
||||
* @param newField The field to insert
|
||||
* @return true if the target was found and the field was inserted
|
||||
*/
|
||||
public static boolean insertAfter(java.util.List<FieldDef> fields, String targetLabel, FieldDef newField) {
|
||||
for (int i = 0; i < fields.size(); i++) {
|
||||
if (matchesLabel(fields.get(i), targetLabel)) {
|
||||
fields.add(i + 1, newField);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean matchesLabel(FieldDef def, String targetLabel) {
|
||||
if (targetLabel.equals(def.label)) return true;
|
||||
if (def.type == FieldType.ROW) {
|
||||
if (def.leftChild != null && targetLabel.equals(def.leftChild.label)) return true;
|
||||
if (def.rightChild != null && targetLabel.equals(def.rightChild.label)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -333,11 +333,13 @@ public class GuiFieldBuilder {
|
||||
// EVENT HANDLING
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
private FieldDef activeSubGuiField;
|
||||
|
||||
/**
|
||||
* Handles button events for declarative fields.
|
||||
* Call from parent GUI's buttonEvent(). Returns true if the event was handled.
|
||||
* <p>
|
||||
* For SUB_GUI fields, the sub-gui is opened directly via {@code parent.setSubGuiWithResult()},
|
||||
* so the result handler survives initGui() rebuilds. After this returns true, check
|
||||
* {@code parent.hasSubGui()} to determine if initGui() should be called.
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public boolean handleButtonEvent(int buttonId, GuiButton button) {
|
||||
@@ -374,7 +376,13 @@ public class GuiFieldBuilder {
|
||||
return true;
|
||||
}
|
||||
case SUB_GUI:
|
||||
activeSubGuiField = def;
|
||||
if (def.getSubGuiFactory() != null) {
|
||||
parent.setSubGuiWithResult(def.getSubGuiFactory().get(), sub -> {
|
||||
if (def.getSubGuiResultHandler() != null) {
|
||||
def.getSubGuiResultHandler().accept(sub);
|
||||
}
|
||||
});
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
@@ -406,32 +414,16 @@ public class GuiFieldBuilder {
|
||||
|
||||
/**
|
||||
* Handles sub-gui close events. Call from parent GUI's subGuiClosed().
|
||||
* Returns true if the event was handled (activeSubGuiField was set).
|
||||
* <p>
|
||||
* Note: SUB_GUI fields opened via handleButtonEvent use
|
||||
* {@code parent.setSubGuiWithResult()}, so their results are handled
|
||||
* automatically by {@code GuiNPCInterface.closeSubGui()}. This method
|
||||
* only handles sub-guis opened through other means.
|
||||
*/
|
||||
public boolean handleSubGuiClosed(SubGuiInterface subgui) {
|
||||
if (activeSubGuiField != null) {
|
||||
FieldDef def = activeSubGuiField;
|
||||
activeSubGuiField = null;
|
||||
if (def.getSubGuiResultHandler() != null) {
|
||||
def.getSubGuiResultHandler().accept(subgui);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the FieldDef for the currently active sub-gui, or null.
|
||||
* After handleButtonEvent returns true for a SUB_GUI field, the parent
|
||||
* should check this and call setSubGui(def.getSubGuiFactory().get()).
|
||||
*/
|
||||
public FieldDef getActiveSubGuiField() { return activeSubGuiField; }
|
||||
|
||||
/**
|
||||
* Clears the active sub-gui field tracking.
|
||||
*/
|
||||
public void clearActiveSubGuiField() { activeSubGuiField = null; }
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
// GETTERS
|
||||
// ═══════════════════════════════════════════════════════════════════
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class GuiNPCInterface extends GuiScreen {
|
||||
@@ -52,6 +53,7 @@ public abstract class GuiNPCInterface extends GuiScreen {
|
||||
public Supplier<Boolean> closeOnEscSupplier = null;
|
||||
public int guiLeft, guiTop, xSize, ySize;
|
||||
private SubGuiInterface subgui;
|
||||
private Consumer<SubGuiInterface> pendingSubGuiResult;
|
||||
public int mouseX, mouseY, mouseScroll;
|
||||
public float bgScale = 1;
|
||||
public float bgScaleX = 1;
|
||||
@@ -519,7 +521,21 @@ public abstract class GuiNPCInterface extends GuiScreen {
|
||||
initGui();
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a sub-gui with a result handler that will be called when the sub-gui closes.
|
||||
* The handler survives initGui() rebuilds since it lives on the parent, not the builder.
|
||||
*/
|
||||
public void setSubGuiWithResult(SubGuiInterface gui, Consumer<SubGuiInterface> resultHandler) {
|
||||
this.pendingSubGuiResult = resultHandler;
|
||||
setSubGui(gui);
|
||||
}
|
||||
|
||||
public void closeSubGui(SubGuiInterface gui) {
|
||||
if (pendingSubGuiResult != null) {
|
||||
Consumer<SubGuiInterface> handler = pendingSubGuiResult;
|
||||
pendingSubGuiResult = null;
|
||||
handler.accept(gui);
|
||||
}
|
||||
subgui = null;
|
||||
initGui();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user