Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/CustomGuiController.java
T

161 lines
6.6 KiB
Java
Raw Normal View History

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;
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;
2025-04-09 01:55:34 -04:00
import noppes.npcs.controllers.data.PlayerDataScript;
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;
import noppes.npcs.scripted.overlay.ScriptOverlay;
2022-01-12 04:44:08 -06:00
import java.io.IOException;
public class CustomGuiController {
public CustomGuiController() {
}
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
}
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;
}
}
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());
}
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());
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();
2025-05-20 13:53:32 -04:00
if (player != null) {
2025-04-09 01:55:34 -04:00
ScriptGui gui = getOpenGui(player);
if (checkGui(event) && gui != null) {
gui.getScriptHandler(player).callScript(EnumScriptType.CUSTOM_GUI_BUTTON, event);
}
2022-01-12 04:44:08 -06: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();
2025-05-20 13:53:32 -04:00
if (player != null) {
2025-04-09 01:55:34 -04:00
ScriptGui gui = getOpenGui(player);
if (checkGui(event) && gui != null) {
gui.getScriptHandler(player).callScript(EnumScriptType.CUSTOM_GUI_SLOT, event);
}
2022-01-12 04:44:08 -06: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-05-20 13:53:32 -04:00
if (player != null) {
2025-04-09 01:55:34 -04:00
ScriptGui gui = getOpenGui(player);
if (checkGui(event) && gui != null) {
gui.getScriptHandler(player).callScript(EnumScriptType.CUSTOM_GUI_SLOT_CLICKED, event);
}
2022-06-18 12:52:23 -05:00
}
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();
2025-05-20 13:53:32 -04:00
if (player != null) {
2025-04-09 01:55:34 -04:00
ScriptGui gui = getOpenGui(player);
if (checkGui(event) && gui != null) {
gui.getScriptHandler(player).callScript(EnumScriptType.CUSTOM_GUI_TEXTFIELD, event);
}
2022-01-14 16:09:27 -06: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();
2025-05-20 13:53:32 -04:00
if (player != null) {
2025-04-09 01:55:34 -04:00
ScriptGui gui = getOpenGui(player);
if (checkGui(event) && gui != null) {
gui.getScriptHandler(player).callScript(EnumScriptType.CUSTOM_GUI_SCROLL, event);
}
2022-01-12 04:44:08 -06:00
}
NpcAPI.EVENT_BUS.post(event);
2022-01-12 04:44:08 -06:00
}
public static void onClose(CustomGuiEvent.CloseEvent event) {
2025-04-09 01:55:34 -04:00
EntityPlayer player = (EntityPlayer) event.player.getMCEntity();
2025-05-20 13:53:32 -04:00
if (player != null) {
2025-04-09 01:55:34 -04:00
PlayerDataScript handler = ScriptController.Instance.getPlayerScripts(player);
handler.callScript(EnumScriptType.CUSTOM_GUI_CLOSED, event);
2022-01-12 04:44:08 -06: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;
}
}
}