From b45cad77dd036d61481b69d1512ed150d10095cd Mon Sep 17 00:00:00 2001 From: "Luis C.M" Date: Sat, 18 Jun 2022 12:52:23 -0500 Subject: [PATCH] Added CustomGuiEvent.SlotClickEvent. --- src/main/java/noppes/npcs/EventHooks.java | 5 +++++ .../noppes/npcs/constants/EnumScriptType.java | 1 + .../npcs/containers/ContainerCustomGui.java | 21 +++++++++++++++++++ .../npcs/controllers/CustomGuiController.java | 8 +++++++ .../npcs/scripted/event/CustomGuiEvent.java | 20 ++++++++++++++++++ 5 files changed, 55 insertions(+) diff --git a/src/main/java/noppes/npcs/EventHooks.java b/src/main/java/noppes/npcs/EventHooks.java index b3b0f8151..e677c9fb6 100644 --- a/src/main/java/noppes/npcs/EventHooks.java +++ b/src/main/java/noppes/npcs/EventHooks.java @@ -512,6 +512,11 @@ public class EventHooks { CustomGuiController.onSlotChange(event); } + public static boolean onCustomGuiSlotClicked(IPlayer player, ICustomGui gui, int slotId, int dragType, int clickType) { + CustomGuiEvent.SlotClickEvent event = new CustomGuiEvent.SlotClickEvent(player, gui, slotId, player.getOpenContainer().getSlot(slotId), dragType, clickType); + return CustomGuiController.onSlotClick(event); + } + public static void onCustomGuiUnfocused(IPlayer player, ICustomGui gui, int textfieldId) { CustomGuiEvent.UnfocusedEvent event = new CustomGuiEvent.UnfocusedEvent(player, gui, textfieldId); CustomGuiController.onCustomGuiUnfocused(event); diff --git a/src/main/java/noppes/npcs/constants/EnumScriptType.java b/src/main/java/noppes/npcs/constants/EnumScriptType.java index a103ba293..fa5cbf537 100644 --- a/src/main/java/noppes/npcs/constants/EnumScriptType.java +++ b/src/main/java/noppes/npcs/constants/EnumScriptType.java @@ -56,6 +56,7 @@ public enum EnumScriptType { CUSTOM_GUI_CLOSED("customGuiClosed"), CUSTOM_GUI_BUTTON("customGuiButton"), CUSTOM_GUI_SLOT("customGuiSlot"), + CUSTOM_GUI_SLOT_CLICKED("customGuiSlotClicked"), CUSTOM_GUI_SCROLL("customGuiScroll"), CUSTOM_GUI_TEXTFIELD("customGuiTextfield"), PICKUP_XP("pickupXP"), diff --git a/src/main/java/noppes/npcs/containers/ContainerCustomGui.java b/src/main/java/noppes/npcs/containers/ContainerCustomGui.java index a23de67b7..aa9ac8bdb 100644 --- a/src/main/java/noppes/npcs/containers/ContainerCustomGui.java +++ b/src/main/java/noppes/npcs/containers/ContainerCustomGui.java @@ -7,10 +7,14 @@ package noppes.npcs.containers; import java.util.Iterator; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; +import noppes.npcs.EventHooks; +import noppes.npcs.scripted.NpcAPI; +import noppes.npcs.scripted.interfaces.entity.IPlayer; import noppes.npcs.scripted.interfaces.gui.IItemSlot; import noppes.npcs.scripted.gui.ScriptGui; import noppes.npcs.client.gui.custom.components.CustomGuiSlot; @@ -99,5 +103,22 @@ public class ContainerCustomGui extends Container { return true; } + @Override + public ItemStack slotClick(int slotId, int dragType, int clickTypeIn, EntityPlayer player) { + if(slotId < 0) + return super.slotClick(slotId, dragType, clickTypeIn, player); + if(!player.worldObj.isRemote) { + if(!EventHooks.onCustomGuiSlotClicked((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, slotId, dragType, clickTypeIn)) { + ItemStack item = super.slotClick(slotId, dragType, clickTypeIn, player); + EntityPlayerMP p = (EntityPlayerMP) player; + p.sendContainerToPlayer(this); + + return item; + } + + } + return null; + } + public boolean canDragIntoSlot(Slot p_94531_1_) { return true; } } diff --git a/src/main/java/noppes/npcs/controllers/CustomGuiController.java b/src/main/java/noppes/npcs/controllers/CustomGuiController.java index 6627fc649..abc2b4da1 100644 --- a/src/main/java/noppes/npcs/controllers/CustomGuiController.java +++ b/src/main/java/noppes/npcs/controllers/CustomGuiController.java @@ -89,6 +89,14 @@ public class CustomGuiController { NpcAPI.EVENT_BUS.post(event); } + public static boolean onSlotClick(CustomGuiEvent.SlotClickEvent event) { + EntityPlayer player = (EntityPlayer) event.player.getMCEntity(); + if(checkGui(event) && getOpenGui(player).getScriptHandler()!=null) { + (getOpenGui(player).getScriptHandler()).callScript(EnumScriptType.CUSTOM_GUI_SLOT_CLICKED, event); + } + return NpcAPI.EVENT_BUS.post(event); + } + public static void onCustomGuiUnfocused(CustomGuiEvent.UnfocusedEvent event) { EntityPlayer player = (EntityPlayer) event.player.getMCEntity(); if (checkGui(event) && getOpenGui(player).getScriptHandler() != null) { diff --git a/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java b/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java index 2fa0b09eb..5402d022c 100644 --- a/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java +++ b/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java @@ -1,5 +1,6 @@ package noppes.npcs.scripted.event; +import cpw.mods.fml.common.eventhandler.Cancelable; import cpw.mods.fml.common.eventhandler.Event; import noppes.npcs.scripted.interfaces.gui.ICustomGui; import noppes.npcs.scripted.interfaces.item.IItemStack; @@ -63,4 +64,23 @@ public class CustomGuiEvent extends Event { this.stack = stack; } } + + @Cancelable + public static class SlotClickEvent extends CustomGuiEvent { + public final int slotId; + public final IItemStack stack; + public final int dragType; + /** + * + */ + public final int clickType; + + public SlotClickEvent(IPlayer player, ICustomGui gui, int slotId, IItemStack stack, int dragType, int clickType) { + super(player,gui); + this.slotId = slotId; + this.stack = stack; + this.dragType = dragType; + this.clickType = clickType; + } + } }