From 00d85d81e5104ae6a269df5c08cf573774c2dc4f Mon Sep 17 00:00:00 2001 From: HoverEpic Date: Tue, 31 Jan 2017 19:17:12 +0100 Subject: [PATCH] add Left/Right click handler in custom inventories (menus) --- .../bungeeutil/item/ItemStack.java | 124 +++++++++++------- .../packetlib/handler/MainPacketHandler.java | 3 +- 2 files changed, 75 insertions(+), 52 deletions(-) diff --git a/src/main/java/dev/wolveringer/bungeeutil/item/ItemStack.java b/src/main/java/dev/wolveringer/bungeeutil/item/ItemStack.java index d3e9e1b..17e696d 100755 --- a/src/main/java/dev/wolveringer/bungeeutil/item/ItemStack.java +++ b/src/main/java/dev/wolveringer/bungeeutil/item/ItemStack.java @@ -5,85 +5,107 @@ public abstract class ItemStack extends Item { - public static class Click { - private Player player; - private int slot; - private Inventory inventory; - private int mode; - private boolean cancel = true; - private Item item; - private boolean sync; + public enum ClickType + { + RIGHT, + LEFT; + } + + public static class Click{ + private Player player; + private int slot; + private Inventory inventory; + private ClickType clickType = null; + private int mode; + private boolean cancel = true; + private Item item; + private boolean sync; public Click(Player p, int slot, Inventory inv,Item ci, int mode,boolean sync) { - this.player = p; - this.slot = slot; - this.inventory = inv; - this.mode = mode; - this.item = ci; - this.sync = sync; - } + this.player = p; + this.slot = slot; + this.inventory = inv; + this.mode = mode; + this.item = ci; + this.sync = sync; + } + + public Click(Player p, int slot, Inventory inv, Item ci, int mode, ClickType clickType, boolean sync){ + this.player = p; + this.slot = slot; + this.inventory = inv; + this.mode = mode; + this.clickType = clickType; + this.item = ci; + this.sync = sync; + } public Inventory getInventory() { - return this.inventory; - } + return this.inventory; + } public Item getItem() { - return this.item; - } + return this.item; + } public int getMode() { - return this.mode; - } - + return this.mode; + } + + public ClickType getClickType() + { + return this.clickType; + } + public Player getPlayer() { - return this.player; - } + return this.player; + } public int getSlot() { - return this.slot; - } + return this.slot; + } - @Deprecated + @Deprecated public boolean isCancelled() { - return this.cancel; - } + return this.cancel; + } public boolean isSyncHandle() { - return this.sync; - } + return this.sync; + } - @Deprecated + @Deprecated public void setCancelled(boolean b) { - this.cancel = b; - } - } + this.cancel = b; + } + } public static enum InteractType { - RIGHT_CLICK, - LEFT_CLICK; - } + RIGHT_CLICK, + LEFT_CLICK; + } - @SuppressWarnings("deprecation") + @SuppressWarnings("deprecation") public ItemStack(int type, int amount, short damage) { - super(type, amount, damage); - } + super(type, amount, damage); + } public ItemStack(Item stack) throws IllegalArgumentException { - super(stack); - } + super(stack); + } public ItemStack(Material type) { - super(type); - } + super(type); + } public ItemStack(Material type, int amount) { - super(type, amount); - } + super(type, amount); + } public ItemStack(Material type, int amount, short damage) { - super(type, amount, damage); - } - public abstract void click(Click click); + super(type, amount, damage); + } + public abstract void click(Click click); public void onInteract(Player player,InteractType type){} -} \ No newline at end of file + } diff --git a/src/main/java/dev/wolveringer/bungeeutil/packetlib/handler/MainPacketHandler.java b/src/main/java/dev/wolveringer/bungeeutil/packetlib/handler/MainPacketHandler.java index 2766452..0b622cb 100755 --- a/src/main/java/dev/wolveringer/bungeeutil/packetlib/handler/MainPacketHandler.java +++ b/src/main/java/dev/wolveringer/bungeeutil/packetlib/handler/MainPacketHandler.java @@ -298,7 +298,8 @@ else if(button == 2){ player.updateInventory(); if (player.getInventoryView().isClickable()){ boolean sync = ((CraftItemMeta)is.getItemMeta()).isClickSync() || Configuration.isSyncInventoryClickActive(); - handleItemClick(player,is,new Click(player, pl.getSlot(), player.getInventoryView(), pl.getItem(), pl.getMode(), sync),sync,false); + ClickType clickType = button == 0 ? ClickType.LEFT : ClickType.RIGHT; + handleItemClick(player,is,new Click(player, pl.getSlot(), player.getInventoryView(), pl.getItem(), pl.getMode(), clickType, sync),sync,false); } Profiler.packet_handle.stop("handleWindowClick"); e.setCancelled(true);