From 638cb4b8b0024c99e9dd5e5da992f1abbb73fce2 Mon Sep 17 00:00:00 2001 From: _tud Date: Sat, 25 Feb 2023 01:17:55 +0300 Subject: [PATCH 1/5] add inventory drag event --- .../classes/data/BukkitEventValues.java | 63 +++++++++++++++++++ .../ch/njol/skript/events/SimpleEvents.java | 15 ++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java index 314fd98bab7..595b6526018 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java @@ -101,9 +101,11 @@ import org.bukkit.event.hanging.HangingPlaceEvent; import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.CraftItemEvent; +import org.bukkit.event.inventory.DragType; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.PrepareAnvilEvent; @@ -145,13 +147,17 @@ import org.bukkit.event.world.WorldEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.Recipe; import org.bukkit.potion.PotionEffectType; import org.eclipse.jdt.annotation.Nullable; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * @author Peter Güttinger @@ -1087,6 +1093,63 @@ public Inventory get(final InventoryClickEvent e) { return e.getClickedInventory(); } }, 0); + // InventoryDragEvent + EventValues.registerEventValue(InventoryDragEvent.class, Player.class, new Getter() { + @Override + public @Nullable Player get(InventoryDragEvent event) { + return event.getWhoClicked() instanceof Player ? (Player) event.getWhoClicked() : null; + } + }, EventValues.TIME_NOW); + EventValues.registerEventValue(InventoryDragEvent.class, World.class, new Getter() { + @Override + public @Nullable World get(InventoryDragEvent event) { + return event.getWhoClicked().getWorld(); + } + }, EventValues.TIME_NOW); + EventValues.registerEventValue(InventoryDragEvent.class, ItemStack[].class, new Getter() { + @Override + public @Nullable ItemStack[] get(InventoryDragEvent event) { + return event.getNewItems().values().toArray(new ItemStack[0]); + } + }, EventValues.TIME_NOW); + EventValues.registerEventValue(InventoryDragEvent.class, Slot[].class, new Getter() { + @Override + public @Nullable Slot[] get(InventoryDragEvent event) { + List slots = new ArrayList<>(event.getRawSlots().size()); + InventoryView view = event.getView(); + for (Integer rawSlot : event.getRawSlots()) { + Inventory inventory = view.getInventory(rawSlot); + int slot = view.convertSlot(rawSlot); + if (inventory == null) + continue; + if (inventory instanceof PlayerInventory && slot >= 36) { + slots.add(new ch.njol.skript.util.slot.EquipmentSlot(((PlayerInventory) view.getBottomInventory()).getHolder(), slot)); + } else { + slots.add(new InventorySlot(inventory, view.convertSlot(rawSlot))); + } + } + return slots.toArray(new Slot[0]); + } + }, EventValues.TIME_NOW); + EventValues.registerEventValue(InventoryDragEvent.class, ClickType.class, new Getter() { + @Override + public @Nullable ClickType get(InventoryDragEvent event) { + return event.getType() == DragType.EVEN ? ClickType.LEFT : ClickType.RIGHT; + } + }, EventValues.TIME_NOW); + EventValues.registerEventValue(InventoryDragEvent.class, Inventory[].class, new Getter() { + @Override + public @Nullable Inventory[] get(InventoryDragEvent event) { + Set inventories = new HashSet<>(2); + InventoryView view = event.getView(); + for (Integer rawSlot : event.getRawSlots()) { + inventories.add(view.getInventory(rawSlot)); + if (inventories.size() == 2) + break; + } + return inventories.toArray(new Inventory[0]); + } + }, EventValues.TIME_NOW); // PrepareAnvilEvent EventValues.registerEventValue(PrepareAnvilEvent.class, ItemStack.class, new Getter() { @Override diff --git a/src/main/java/ch/njol/skript/events/SimpleEvents.java b/src/main/java/ch/njol/skript/events/SimpleEvents.java index 1873933b05c..0206b1bf8b6 100644 --- a/src/main/java/ch/njol/skript/events/SimpleEvents.java +++ b/src/main/java/ch/njol/skript/events/SimpleEvents.java @@ -59,6 +59,7 @@ import org.bukkit.event.inventory.FurnaceBurnEvent; import org.bukkit.event.inventory.FurnaceSmeltEvent; import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.PrepareAnvilEvent; @@ -670,7 +671,7 @@ public class SimpleEvents { "Use chat format to change message format.", "Use chat recipients to edit chat recipients." ) - .examples( + .examples( "on chat:", "\tif player has permission \"owner\":", "\t\tset chat format to \"<red>[player]<light gray>: <light red>[message]\"", @@ -679,7 +680,7 @@ public class SimpleEvents { "\telse: #default message format", "\t\tset chat format to \"<orange>[player]<light gray>: <white>[message]\"" ) - .since("1.4.1"); + .since("1.4.1"); if (Skript.classExists("org.bukkit.event.world.LootGenerateEvent")) { Skript.registerEvent("Loot Generate", SimpleEvent.class, LootGenerateEvent.class, "loot generat(e|ing)") .description( @@ -709,6 +710,16 @@ public class SimpleEvents { .requiredPlugins("Paper 1.16+"); } + Skript.registerEvent("Inventory Drag", SimpleEvent.class, InventoryDragEvent.class, "inventory drag[ging]") + .description("Called when a player drags an item in their cursor across the inventory.") + .examples( + "on inventory drag:", + "\tif player's current inventory is {_gui}", + "\t\tsend \"You can't drag your items here!\" to player", + "\t\tcancel event" + ) + .since("INSERT VERSION"); + } } From d92c5290a1eed385ade78dca99c15278ed63e8d3 Mon Sep 17 00:00:00 2001 From: _tud Date: Thu, 23 Mar 2023 16:01:37 +0300 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> --- .../skript/classes/data/BukkitEventValues.java | 3 ++- .../java/ch/njol/skript/events/SimpleEvents.java | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java index 595b6526018..df42a3ad576 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java @@ -1139,7 +1139,8 @@ public Inventory get(final InventoryClickEvent e) { }, EventValues.TIME_NOW); EventValues.registerEventValue(InventoryDragEvent.class, Inventory[].class, new Getter() { @Override - public @Nullable Inventory[] get(InventoryDragEvent event) { + @Nullable + public Inventory[] get(InventoryDragEvent event) { Set inventories = new HashSet<>(2); InventoryView view = event.getView(); for (Integer rawSlot : event.getRawSlots()) { diff --git a/src/main/java/ch/njol/skript/events/SimpleEvents.java b/src/main/java/ch/njol/skript/events/SimpleEvents.java index 0206b1bf8b6..b135ee95cf1 100644 --- a/src/main/java/ch/njol/skript/events/SimpleEvents.java +++ b/src/main/java/ch/njol/skript/events/SimpleEvents.java @@ -711,14 +711,14 @@ public class SimpleEvents { } Skript.registerEvent("Inventory Drag", SimpleEvent.class, InventoryDragEvent.class, "inventory drag[ging]") - .description("Called when a player drags an item in their cursor across the inventory.") - .examples( - "on inventory drag:", - "\tif player's current inventory is {_gui}", - "\t\tsend \"You can't drag your items here!\" to player", - "\t\tcancel event" - ) - .since("INSERT VERSION"); + .description("Called when a player drags an item in their cursor across the inventory.") + .examples( + "on inventory drag:", + "\tif player's current inventory is {_gui}", + "\t\tsend \"You can't drag your items here!\" to player", + "\t\tcancel event" + ) + .since("INSERT VERSION"); } From 2044e5cdf67da490877a5a715ad829f8475d2508 Mon Sep 17 00:00:00 2001 From: _tud Date: Thu, 23 Mar 2023 17:04:55 +0300 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Ayham Al Ali <20037329+AyhamAl-Ali@users.noreply.github.com> --- src/main/java/ch/njol/skript/events/SimpleEvents.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/events/SimpleEvents.java b/src/main/java/ch/njol/skript/events/SimpleEvents.java index b135ee95cf1..d63faf7029c 100644 --- a/src/main/java/ch/njol/skript/events/SimpleEvents.java +++ b/src/main/java/ch/njol/skript/events/SimpleEvents.java @@ -714,7 +714,7 @@ public class SimpleEvents { .description("Called when a player drags an item in their cursor across the inventory.") .examples( "on inventory drag:", - "\tif player's current inventory is {_gui}", + "\tif player's current inventory is {_gui}:", "\t\tsend \"You can't drag your items here!\" to player", "\t\tcancel event" ) From a96e9fc5b17847e6b55fa1c82240b31851f83ae0 Mon Sep 17 00:00:00 2001 From: _tud Date: Fri, 23 Jun 2023 02:31:56 +0300 Subject: [PATCH 4/5] Requested Changes --- .../skript/classes/data/BukkitEventValues.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java index df42a3ad576..3e69ffe114f 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java @@ -1106,6 +1106,18 @@ public Inventory get(final InventoryClickEvent e) { return event.getWhoClicked().getWorld(); } }, EventValues.TIME_NOW); + EventValues.registerEventValue(InventoryDragEvent.class, ItemStack.class, new Getter() { + @Override + public @Nullable ItemStack get(InventoryDragEvent event) { + return event.getOldCursor(); + } + }, EventValues.TIME_PAST); + EventValues.registerEventValue(InventoryDragEvent.class, ItemStack.class, new Getter() { + @Override + public @Nullable ItemStack get(InventoryDragEvent event) { + return event.getCursor(); + } + }, EventValues.TIME_NOW); EventValues.registerEventValue(InventoryDragEvent.class, ItemStack[].class, new Getter() { @Override public @Nullable ItemStack[] get(InventoryDragEvent event) { @@ -1122,6 +1134,7 @@ public Inventory get(final InventoryClickEvent e) { int slot = view.convertSlot(rawSlot); if (inventory == null) continue; + // Not all indices point to inventory slots. Equipment, for example if (inventory instanceof PlayerInventory && slot >= 36) { slots.add(new ch.njol.skript.util.slot.EquipmentSlot(((PlayerInventory) view.getBottomInventory()).getHolder(), slot)); } else { @@ -1141,12 +1154,10 @@ public Inventory get(final InventoryClickEvent e) { @Override @Nullable public Inventory[] get(InventoryDragEvent event) { - Set inventories = new HashSet<>(2); + Set inventories = new HashSet<>(); InventoryView view = event.getView(); for (Integer rawSlot : event.getRawSlots()) { inventories.add(view.getInventory(rawSlot)); - if (inventories.size() == 2) - break; } return inventories.toArray(new Inventory[0]); } From b1adaceaa39bfb261f669d48f46e8d19aff6b65d Mon Sep 17 00:00:00 2001 From: _tud Date: Fri, 23 Jun 2023 03:13:05 +0300 Subject: [PATCH 5/5] Requested Changes Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com> --- .../classes/data/BukkitEventValues.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java index 3e69ffe114f..b628c56a887 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java @@ -1096,37 +1096,43 @@ public Inventory get(final InventoryClickEvent e) { // InventoryDragEvent EventValues.registerEventValue(InventoryDragEvent.class, Player.class, new Getter() { @Override - public @Nullable Player get(InventoryDragEvent event) { + @Nullable + public Player get(InventoryDragEvent event) { return event.getWhoClicked() instanceof Player ? (Player) event.getWhoClicked() : null; } }, EventValues.TIME_NOW); EventValues.registerEventValue(InventoryDragEvent.class, World.class, new Getter() { @Override - public @Nullable World get(InventoryDragEvent event) { + @Nullable + public World get(InventoryDragEvent event) { return event.getWhoClicked().getWorld(); } }, EventValues.TIME_NOW); EventValues.registerEventValue(InventoryDragEvent.class, ItemStack.class, new Getter() { @Override - public @Nullable ItemStack get(InventoryDragEvent event) { + @Nullable + public ItemStack get(InventoryDragEvent event) { return event.getOldCursor(); } }, EventValues.TIME_PAST); EventValues.registerEventValue(InventoryDragEvent.class, ItemStack.class, new Getter() { @Override - public @Nullable ItemStack get(InventoryDragEvent event) { + @Nullable + public ItemStack get(InventoryDragEvent event) { return event.getCursor(); } }, EventValues.TIME_NOW); EventValues.registerEventValue(InventoryDragEvent.class, ItemStack[].class, new Getter() { @Override - public @Nullable ItemStack[] get(InventoryDragEvent event) { + @Nullable + public ItemStack[] get(InventoryDragEvent event) { return event.getNewItems().values().toArray(new ItemStack[0]); } }, EventValues.TIME_NOW); EventValues.registerEventValue(InventoryDragEvent.class, Slot[].class, new Getter() { @Override - public @Nullable Slot[] get(InventoryDragEvent event) { + @Nullable + public Slot[] get(InventoryDragEvent event) { List slots = new ArrayList<>(event.getRawSlots().size()); InventoryView view = event.getView(); for (Integer rawSlot : event.getRawSlots()) { @@ -1146,7 +1152,8 @@ public Inventory get(final InventoryClickEvent e) { }, EventValues.TIME_NOW); EventValues.registerEventValue(InventoryDragEvent.class, ClickType.class, new Getter() { @Override - public @Nullable ClickType get(InventoryDragEvent event) { + @Nullable + public ClickType get(InventoryDragEvent event) { return event.getType() == DragType.EVEN ? ClickType.LEFT : ClickType.RIGHT; } }, EventValues.TIME_NOW);