2022-01-12 04:44:08 -06:00
|
|
|
package noppes.npcs.scripted.gui;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.nbt.NBTBase;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2022-11-15 15:25:53 -05:00
|
|
|
import noppes.npcs.api.entity.IPlayer;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.api.gui.*;
|
2022-11-15 15:25:53 -05:00
|
|
|
import noppes.npcs.api.item.IItemStack;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.controllers.CustomGuiController;
|
|
|
|
|
import noppes.npcs.controllers.ScriptController;
|
|
|
|
|
import noppes.npcs.controllers.data.PlayerDataScript;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2023-03-18 15:52:50 -05:00
|
|
|
import java.util.HashMap;
|
2023-01-12 21:33:06 -05:00
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
2022-01-12 04:44:08 -06:00
|
|
|
|
|
|
|
|
public class ScriptGui implements ICustomGui {
|
|
|
|
|
int id;
|
|
|
|
|
int width;
|
|
|
|
|
int height;
|
|
|
|
|
int playerInvX;
|
|
|
|
|
int playerInvY;
|
|
|
|
|
boolean pauseGame;
|
|
|
|
|
boolean showPlayerInv;
|
2024-08-11 20:22:35 +03:00
|
|
|
boolean closeOnEsc = true;
|
2022-01-12 04:44:08 -06:00
|
|
|
String backgroundTexture = "";
|
2024-08-11 20:22:35 +03:00
|
|
|
HashMap<Integer, ICustomGuiComponent> components = new HashMap<>();
|
2022-01-12 04:44:08 -06:00
|
|
|
List<IItemSlot> slots = new ArrayList();
|
|
|
|
|
|
|
|
|
|
public ScriptGui() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ScriptGui(int id, int width, int height, boolean pauseGame) {
|
|
|
|
|
this.id = id;
|
|
|
|
|
this.width = width;
|
|
|
|
|
this.height = height;
|
|
|
|
|
this.pauseGame = pauseGame;
|
|
|
|
|
//this.scriptHandler = ScriptContainer.Current;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getID() {
|
|
|
|
|
return this.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getWidth() {
|
|
|
|
|
return this.width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getHeight() {
|
|
|
|
|
return this.height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ICustomGuiComponent> getComponents() {
|
2023-03-29 02:19:04 -05:00
|
|
|
return new ArrayList<>(this.components.values());
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
2023-06-04 18:11:36 -05:00
|
|
|
public void clear() {
|
|
|
|
|
this.components.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 04:44:08 -06:00
|
|
|
public List<IItemSlot> getSlots() {
|
|
|
|
|
return this.slots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PlayerDataScript getScriptHandler() {
|
|
|
|
|
return ScriptController.Instance.playerScripts;//this.scriptHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setSize(int width, int height) {
|
|
|
|
|
this.width = width;
|
|
|
|
|
this.height = height;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 20:22:35 +03:00
|
|
|
public void setCloseOnEscape(boolean close) {
|
|
|
|
|
this.closeOnEsc = close;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean doesCloseOnEscape() {
|
|
|
|
|
return this.closeOnEsc;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 04:44:08 -06:00
|
|
|
public void setDoesPauseGame(boolean pauseGame) {
|
|
|
|
|
this.pauseGame = pauseGame;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-18 15:52:50 -05:00
|
|
|
public boolean doesPauseGame() {
|
2022-01-12 04:44:08 -06:00
|
|
|
return this.pauseGame;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setBackgroundTexture(String resourceLocation) {
|
|
|
|
|
this.backgroundTexture = resourceLocation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getBackgroundTexture() {
|
|
|
|
|
return this.backgroundTexture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IButton addButton(int id, String label, int x, int y) {
|
|
|
|
|
ScriptGuiButton component = new ScriptGuiButton(id, label, x, y);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(component);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (IButton) this.getComponent(id);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IButton addButton(int id, String label, int x, int y, int width, int height) {
|
|
|
|
|
ScriptGuiButton component = new ScriptGuiButton(id, label, x, y, width, height);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(component);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (IButton) this.getComponent(id);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IButton addTexturedButton(int id, String label, int x, int y, int width, int height, String texture) {
|
|
|
|
|
ScriptGuiButton component = new ScriptGuiButton(id, label, x, y, width, height, texture);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(component);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (IButton) this.getComponent(id);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IButton addTexturedButton(int id, String label, int x, int y, int width, int height, String texture, int textureX, int textureY) {
|
|
|
|
|
ScriptGuiButton component = new ScriptGuiButton(id, label, x, y, width, height, texture, textureX, textureY);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(component);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (IButton) this.getComponent(id);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ILabel addLabel(int id, String label, int x, int y, int width, int height) {
|
|
|
|
|
ScriptGuiLabel component = new ScriptGuiLabel(id, label, x, y, width, height);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(component);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (ILabel) this.getComponent(id);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ILabel addLabel(int id, String label, int x, int y, int width, int height, int color) {
|
|
|
|
|
ScriptGuiLabel component = new ScriptGuiLabel(id, label, x, y, width, height, color);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(component);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (ILabel) this.getComponent(id);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ITextField addTextField(int id, int x, int y, int width, int height) {
|
|
|
|
|
ScriptGuiTextField component = new ScriptGuiTextField(id, x, y, width, height);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(component);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (ITextField) this.getComponent(id);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ITexturedRect addTexturedRect(int id, String texture, int x, int y, int width, int height) {
|
|
|
|
|
ScriptGuiTexturedRect component = new ScriptGuiTexturedRect(id, texture, x, y, width, height);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(component);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (ITexturedRect) this.getComponent(id);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ITexturedRect addTexturedRect(int id, String texture, int x, int y, int width, int height, int textureX, int textureY) {
|
|
|
|
|
ScriptGuiTexturedRect component = new ScriptGuiTexturedRect(id, texture, x, y, width, height, textureX, textureY);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(component);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (ITexturedRect) this.getComponent(id);
|
2023-03-18 03:04:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IItemSlot addItemSlot(int id, int x, int y) {
|
|
|
|
|
ScriptGuiItemSlot slot = new ScriptGuiItemSlot(id, x, y);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(slot);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (IItemSlot) this.getComponent(id);
|
2023-03-18 03:04:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IItemSlot addItemSlot(int id, int x, int y, IItemStack stack) {
|
|
|
|
|
ScriptGuiItemSlot slot = new ScriptGuiItemSlot(id, x, y, stack);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(slot);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (IItemSlot) this.getComponent(id);
|
2023-03-18 15:52:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IItemSlot addItemSlot(int x, int y) {
|
|
|
|
|
return this.addItemSlot(-1, x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IItemSlot addItemSlot(int x, int y, IItemStack stack) {
|
|
|
|
|
return this.addItemSlot(-1, x, y, stack);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IScroll addScroll(int id, int x, int y, int width, int height, String[] list) {
|
|
|
|
|
ScriptGuiScroll component = new ScriptGuiScroll(id, x, y, width, height, list);
|
2023-03-18 15:52:50 -05:00
|
|
|
this.updateComponent(component);
|
2024-08-11 20:22:35 +03:00
|
|
|
return (IScroll) this.getComponent(id);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
2022-01-28 03:21:00 -06:00
|
|
|
public ILine addLine(int id, int x1, int y1, int x2, int y2, int color, int thickness) {
|
2023-03-18 15:52:50 -05:00
|
|
|
ScriptGuiLine component = new ScriptGuiLine(id, x1, y1, x2, y2, color, thickness);
|
|
|
|
|
this.updateComponent(component);
|
|
|
|
|
return (ILine) this.getComponent(id);
|
2022-01-28 03:21:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ILine addLine(int id, int x1, int y1, int x2, int y2) {
|
2023-03-18 15:52:50 -05:00
|
|
|
ScriptGuiLine component = new ScriptGuiLine(id, x1, y1, x2, y2);
|
|
|
|
|
this.updateComponent(component);
|
|
|
|
|
return (ILine) this.getComponent(id);
|
2022-01-28 03:21:00 -06:00
|
|
|
}
|
|
|
|
|
|
2022-01-12 04:44:08 -06:00
|
|
|
public void showPlayerInventory(int x, int y) {
|
|
|
|
|
this.showPlayerInv = true;
|
|
|
|
|
this.playerInvX = x;
|
|
|
|
|
this.playerInvY = y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ICustomGuiComponent getComponent(int componentID) {
|
2023-03-18 15:52:50 -05:00
|
|
|
return this.components.get(componentID);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeComponent(int componentID) {
|
2023-03-18 15:52:50 -05:00
|
|
|
if (this.components.containsKey(componentID)) {
|
|
|
|
|
ICustomGuiComponent component = this.getComponent(componentID);
|
|
|
|
|
if (component instanceof IItemSlot) {
|
|
|
|
|
for (IItemSlot slot : this.slots) {
|
|
|
|
|
if (slot.getID() == componentID) {
|
|
|
|
|
this.slots.remove(slot);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
2023-03-18 15:52:50 -05:00
|
|
|
this.components.remove(componentID);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateComponent(ICustomGuiComponent component) {
|
2023-03-18 15:52:50 -05:00
|
|
|
if (component != null) {
|
|
|
|
|
this.removeComponent(component.getID());
|
|
|
|
|
this.components.put(component.getID(), component);
|
|
|
|
|
if (component instanceof IItemSlot) {
|
|
|
|
|
this.slots.add((IItemSlot) component);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void update(IPlayer player) {
|
2022-05-11 22:08:31 -05:00
|
|
|
CustomGuiController.updateGui(player, this);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean getShowPlayerInv() {
|
|
|
|
|
return this.showPlayerInv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getPlayerInvX() {
|
|
|
|
|
return this.playerInvX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getPlayerInvY() {
|
|
|
|
|
return this.playerInvY;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-18 03:04:46 -05:00
|
|
|
private int getMaxId() {
|
|
|
|
|
int max = 0;
|
|
|
|
|
for (ICustomGuiComponent component : this.getComponents()) {
|
|
|
|
|
if (component.getID() >= max) {
|
|
|
|
|
max = component.getID();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return max;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 04:44:08 -06:00
|
|
|
public ICustomGui fromNBT(NBTTagCompound tag) {
|
|
|
|
|
this.id = tag.getInteger("id");
|
|
|
|
|
this.width = tag.getIntArray("size")[0];
|
|
|
|
|
this.height = tag.getIntArray("size")[1];
|
|
|
|
|
this.pauseGame = tag.getBoolean("pause");
|
|
|
|
|
this.backgroundTexture = tag.getString("bgTexture");
|
2024-08-11 20:22:35 +03:00
|
|
|
this.closeOnEsc = tag.getBoolean("closeOnEsc");
|
2022-01-12 04:44:08 -06:00
|
|
|
NBTTagList list = tag.getTagList("components", 10);
|
2024-08-11 20:22:35 +03:00
|
|
|
for (int i = 0; i < list.tagCount(); i++) {
|
2022-01-12 04:44:08 -06:00
|
|
|
NBTBase b = list.getCompoundTagAt(i);
|
2024-08-11 20:22:35 +03:00
|
|
|
ScriptGuiComponent component = ScriptGuiComponent.createFromNBT((NBTTagCompound) b);
|
2024-03-16 16:21:44 -04:00
|
|
|
if (component != null && !(component instanceof ScriptGuiItemSlot)) {
|
2023-03-18 15:52:50 -05:00
|
|
|
components.put(component.getID(), component);
|
|
|
|
|
}
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
2024-03-19 22:13:29 -05:00
|
|
|
List<IItemSlot> slots = new ArrayList<>();
|
2022-01-12 04:44:08 -06:00
|
|
|
list = tag.getTagList("slots", 10);
|
2024-08-11 20:22:35 +03:00
|
|
|
for (int i = 0; i < list.tagCount(); i++) {
|
2024-03-19 22:13:29 -05:00
|
|
|
NBTTagCompound slotNbt = list.getCompoundTagAt(i);
|
|
|
|
|
int id = slotNbt.getInteger("id");
|
|
|
|
|
if (!components.containsKey(id)) {
|
|
|
|
|
components.put(id, ScriptGuiComponent.createFromNBT(slotNbt));
|
|
|
|
|
} else {
|
|
|
|
|
components.get(id).fromNBT(slotNbt);
|
|
|
|
|
}
|
|
|
|
|
ScriptGuiItemSlot slot = (ScriptGuiItemSlot) components.get(id);
|
|
|
|
|
slots.add(slot);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
this.slots = slots;
|
|
|
|
|
this.showPlayerInv = tag.getBoolean("showPlayerInv");
|
|
|
|
|
if (this.showPlayerInv) {
|
|
|
|
|
this.playerInvX = tag.getIntArray("pInvPos")[0];
|
|
|
|
|
this.playerInvY = tag.getIntArray("pInvPos")[1];
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagCompound toNBT() {
|
|
|
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
|
|
|
tag.setInteger("id", this.id);
|
|
|
|
|
tag.setIntArray("size", new int[]{this.width, this.height});
|
|
|
|
|
tag.setBoolean("pause", this.pauseGame);
|
|
|
|
|
tag.setString("bgTexture", this.backgroundTexture);
|
2024-08-11 20:22:35 +03:00
|
|
|
tag.setBoolean("closeOnEsc", this.closeOnEsc);
|
2022-01-12 04:44:08 -06:00
|
|
|
NBTTagList list = new NBTTagList();
|
2023-03-18 15:52:50 -05:00
|
|
|
Iterator var3 = this.components.values().iterator();
|
2022-01-12 04:44:08 -06:00
|
|
|
ICustomGuiComponent c;
|
2024-08-11 20:22:35 +03:00
|
|
|
while (var3.hasNext()) {
|
|
|
|
|
c = (ICustomGuiComponent) var3.next();
|
|
|
|
|
list.appendTag(((ScriptGuiComponent) c).toNBT(new NBTTagCompound()));
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
tag.setTag("components", list);
|
|
|
|
|
list = new NBTTagList();
|
|
|
|
|
var3 = this.slots.iterator();
|
2024-08-11 20:22:35 +03:00
|
|
|
while (var3.hasNext()) {
|
|
|
|
|
c = (ICustomGuiComponent) var3.next();
|
2023-03-18 03:47:24 -05:00
|
|
|
if (c.getID() == -1) {
|
2024-08-11 20:22:35 +03:00
|
|
|
c.setID(this.getMaxId() + 1);
|
2023-03-18 03:47:24 -05:00
|
|
|
}
|
2024-08-11 20:22:35 +03:00
|
|
|
list.appendTag(((ScriptGuiComponent) c).toNBT(new NBTTagCompound()));
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
tag.setTag("slots", list);
|
|
|
|
|
tag.setBoolean("showPlayerInv", this.showPlayerInv);
|
|
|
|
|
if (this.showPlayerInv) {
|
|
|
|
|
tag.setIntArray("pInvPos", new int[]{this.playerInvX, this.playerInvY});
|
|
|
|
|
}
|
|
|
|
|
return tag;
|
|
|
|
|
}
|
|
|
|
|
}
|