2022-01-12 04:44:08 -06:00
|
|
|
package noppes.npcs.controllers;
|
|
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
2025-01-30 12:44:39 -05:00
|
|
|
import kamkeel.npcs.network.PacketHandler;
|
2025-01-30 13:54:49 -05:00
|
|
|
import kamkeel.npcs.network.packets.data.large.GuiDataPacket;
|
|
|
|
|
import kamkeel.npcs.network.packets.data.script.ScriptOverlayDataPacket;
|
2025-01-30 12:44:39 -05:00
|
|
|
import kamkeel.npcs.util.ByteBufUtils;
|
2022-01-12 04:44:08 -06:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
|
import noppes.npcs.CustomNpcs;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.api.entity.IPlayer;
|
|
|
|
|
import noppes.npcs.api.item.IItemStack;
|
2022-01-12 04:44:08 -06:00
|
|
|
import noppes.npcs.constants.EnumGuiType;
|
|
|
|
|
import noppes.npcs.constants.EnumScriptType;
|
|
|
|
|
import noppes.npcs.containers.ContainerCustomGui;
|
2023-01-12 21:33:06 -05:00
|
|
|
import noppes.npcs.scripted.NpcAPI;
|
2025-04-08 22:37:26 -04:00
|
|
|
import noppes.npcs.scripted.event.player.CustomGuiEvent;
|
2022-01-12 04:44:08 -06:00
|
|
|
import noppes.npcs.scripted.gui.ScriptGui;
|
2022-01-30 04:05:01 -06:00
|
|
|
import noppes.npcs.scripted.overlay.ScriptOverlay;
|
2022-01-12 04:44:08 -06:00
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
public class CustomGuiController {
|
|
|
|
|
public CustomGuiController() {
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 22:08:31 -05:00
|
|
|
public static void openGui(IPlayer player, ScriptGui gui) {
|
2022-01-12 04:44:08 -06:00
|
|
|
EntityPlayerMP entityPlayerMP = (EntityPlayerMP) player.getMCEntity();
|
|
|
|
|
entityPlayerMP.openGui(CustomNpcs.instance, EnumGuiType.CustomGui.ordinal(), player.getWorld().getMCWorld(), gui.getSlots().size(), 0, 0);
|
2025-03-13 17:14:45 -04:00
|
|
|
((ContainerCustomGui) ((EntityPlayerMP) player.getMCEntity()).openContainer).setGui(gui, (EntityPlayer) player.getMCEntity());
|
|
|
|
|
GuiDataPacket.sendGuiData((EntityPlayerMP) player.getMCEntity(), gui.toNBT());
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
2022-05-11 22:08:31 -05:00
|
|
|
public static boolean updateGui(IPlayer player, ScriptGui gui) {
|
2025-03-13 17:14:45 -04:00
|
|
|
if (((EntityPlayerMP) player.getMCEntity()).openContainer instanceof ContainerCustomGui) {
|
|
|
|
|
((ContainerCustomGui) ((EntityPlayerMP) player.getMCEntity()).openContainer).setGui(gui, (EntityPlayer) player.getMCEntity());
|
|
|
|
|
GuiDataPacket.sendGuiData((EntityPlayerMP) player.getMCEntity(), gui.toNBT());
|
2022-01-12 04:44:08 -06:00
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 22:08:31 -05:00
|
|
|
public static void openOverlay(IPlayer player, ScriptOverlay gui) {
|
2025-03-13 17:14:45 -04:00
|
|
|
PacketHandler.Instance.sendToPlayer(new ScriptOverlayDataPacket(gui.toNBT()), (EntityPlayerMP) player.getMCEntity());
|
2022-01-30 04:05:01 -06:00
|
|
|
}
|
|
|
|
|
|
2022-05-11 22:08:31 -05:00
|
|
|
public static boolean updateOverlay(IPlayer player, ScriptOverlay gui) {
|
2025-03-13 17:14:45 -04:00
|
|
|
PacketHandler.Instance.sendToPlayer(new ScriptOverlayDataPacket(gui.toNBT()), (EntityPlayerMP) player.getMCEntity());
|
2022-01-30 04:05:01 -06:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 04:44:08 -06:00
|
|
|
static boolean checkGui(CustomGuiEvent event) {
|
|
|
|
|
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
|
|
|
|
|
if (!(player.openContainer instanceof ContainerCustomGui)) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2025-03-13 17:14:45 -04:00
|
|
|
return ((ContainerCustomGui) player.openContainer).customGui.getID() == event.gui.getID();
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IItemStack[] getSlotContents(EntityPlayer player) {
|
|
|
|
|
IItemStack[] slotContents = new IItemStack[0];
|
|
|
|
|
if (player.openContainer instanceof ContainerCustomGui) {
|
2025-03-13 17:14:45 -04:00
|
|
|
ContainerCustomGui container = (ContainerCustomGui) player.openContainer;
|
2022-01-12 04:44:08 -06:00
|
|
|
slotContents = new IItemStack[container.guiInventory.getSizeInventory()];
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
for (int i = 0; i < container.guiInventory.getSizeInventory(); ++i) {
|
2022-01-12 04:44:08 -06:00
|
|
|
slotContents[i] = NpcAPI.Instance().getIItemStack(container.guiInventory.getStackInSlot(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return slotContents;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void onButton(CustomGuiEvent.ButtonEvent event) {
|
|
|
|
|
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
|
|
|
|
|
if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) {
|
|
|
|
|
getOpenGui(player).getScriptHandler().callScript(EnumScriptType.CUSTOM_GUI_BUTTON, event);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 17:12:48 -05:00
|
|
|
NpcAPI.EVENT_BUS.post(event);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void onSlotChange(CustomGuiEvent.SlotEvent event) {
|
|
|
|
|
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
|
|
|
|
|
if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) {
|
|
|
|
|
getOpenGui(player).getScriptHandler().callScript(EnumScriptType.CUSTOM_GUI_SLOT, event);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 17:12:48 -05:00
|
|
|
NpcAPI.EVENT_BUS.post(event);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
2022-06-18 12:52:23 -05:00
|
|
|
public static boolean onSlotClick(CustomGuiEvent.SlotClickEvent event) {
|
|
|
|
|
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
|
2025-03-13 17:14:45 -04:00
|
|
|
if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) {
|
2022-06-18 12:52:23 -05:00
|
|
|
(getOpenGui(player).getScriptHandler()).callScript(EnumScriptType.CUSTOM_GUI_SLOT_CLICKED, event);
|
|
|
|
|
}
|
|
|
|
|
return NpcAPI.EVENT_BUS.post(event);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-14 16:09:27 -06:00
|
|
|
public static void onCustomGuiUnfocused(CustomGuiEvent.UnfocusedEvent event) {
|
|
|
|
|
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
|
|
|
|
|
if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) {
|
|
|
|
|
getOpenGui(player).getScriptHandler().callScript(EnumScriptType.CUSTOM_GUI_TEXTFIELD, event);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 17:12:48 -05:00
|
|
|
NpcAPI.EVENT_BUS.post(event);
|
2022-01-14 16:09:27 -06:00
|
|
|
}
|
|
|
|
|
|
2022-01-12 04:44:08 -06:00
|
|
|
public static void onScrollClick(CustomGuiEvent.ScrollEvent event) {
|
|
|
|
|
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
|
|
|
|
|
if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) {
|
|
|
|
|
getOpenGui(player).getScriptHandler().callScript(EnumScriptType.CUSTOM_GUI_SCROLL, event);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 17:12:48 -05:00
|
|
|
NpcAPI.EVENT_BUS.post(event);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void onClose(CustomGuiEvent.CloseEvent event) {
|
|
|
|
|
if (ScriptController.Instance.playerScripts != null) {
|
|
|
|
|
ScriptController.Instance.playerScripts.callScript(EnumScriptType.CUSTOM_GUI_CLOSED, event);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 17:12:48 -05:00
|
|
|
NpcAPI.EVENT_BUS.post(event);
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ScriptGui getOpenGui(EntityPlayer player) {
|
2025-03-13 17:14:45 -04:00
|
|
|
return player.openContainer instanceof ContainerCustomGui ? ((ContainerCustomGui) player.openContainer).customGui : null;
|
2022-01-12 04:44:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String[] readScrollSelection(ByteBuf buffer) {
|
|
|
|
|
try {
|
2025-01-30 12:44:39 -05:00
|
|
|
NBTTagList list = ByteBufUtils.readNBT(buffer).getTagList("selection", 8);
|
2022-01-12 04:44:08 -06:00
|
|
|
String[] selection = new String[list.tagCount()];
|
|
|
|
|
|
2025-03-13 17:14:45 -04:00
|
|
|
for (int i = 0; i < list.tagCount(); ++i) {
|
2022-01-12 04:44:08 -06:00
|
|
|
selection[i] = list.getStringTagAt(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return selection;
|
|
|
|
|
} catch (IOException var4) {
|
|
|
|
|
var4.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|