From 1b79a38ca57c59b0fec236e4fe0b3e2a35643ff4 Mon Sep 17 00:00:00 2001 From: _tud Date: Fri, 23 Jun 2023 11:25:58 +0300 Subject: [PATCH] Add InventoryDragEvent (#5474) (cherry picked from commit 3dd5394e53e247face129d726be2d5e709e90fd4) --- .../classes/data/BukkitEventValues.java | 82 +++++++++++++++++++ .../ch/njol/skript/events/SimpleEvents.java | 15 +++- 2 files changed, 95 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..b628c56a887 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,82 @@ public Inventory get(final InventoryClickEvent e) { return e.getClickedInventory(); } }, 0); + // InventoryDragEvent + EventValues.registerEventValue(InventoryDragEvent.class, Player.class, new Getter() { + @Override + @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 + @Nullable + public World get(InventoryDragEvent event) { + return event.getWhoClicked().getWorld(); + } + }, EventValues.TIME_NOW); + EventValues.registerEventValue(InventoryDragEvent.class, ItemStack.class, new Getter() { + @Override + @Nullable + public ItemStack get(InventoryDragEvent event) { + return event.getOldCursor(); + } + }, EventValues.TIME_PAST); + EventValues.registerEventValue(InventoryDragEvent.class, ItemStack.class, new Getter() { + @Override + @Nullable + public ItemStack get(InventoryDragEvent event) { + return event.getCursor(); + } + }, EventValues.TIME_NOW); + EventValues.registerEventValue(InventoryDragEvent.class, ItemStack[].class, new Getter() { + @Override + @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 + @Nullable + public 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; + // 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 { + 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 + @Nullable + public 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 + @Nullable + public Inventory[] get(InventoryDragEvent event) { + Set inventories = new HashSet<>(); + InventoryView view = event.getView(); + for (Integer rawSlot : event.getRawSlots()) { + inventories.add(view.getInventory(rawSlot)); + } + 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 6e1aa2b7004..bcb301489e1 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"); + } }