From a50e49e8c963eb11ed6b0c427f5b21d676e8fc98 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Fri, 20 Feb 2015 22:17:06 +0800 Subject: [PATCH] ! Fix `take` & `put` permission node about container - Shift click on bottom inventory now handled - Clicks on an item on top inventory with an item stack already on cursor now checks both put and take permission nodes --- .../modifyworld/handlers/PlayerListener.java | 60 +++++++++++++++---- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/src/main/java/ru/tehkode/modifyworld/handlers/PlayerListener.java b/src/main/java/ru/tehkode/modifyworld/handlers/PlayerListener.java index c86845f..1d1885c 100644 --- a/src/main/java/ru/tehkode/modifyworld/handlers/PlayerListener.java +++ b/src/main/java/ru/tehkode/modifyworld/handlers/PlayerListener.java @@ -18,6 +18,8 @@ */ package ru.tehkode.modifyworld.handlers; +import java.util.ArrayList; +import java.util.HashMap; import java.util.logging.Logger; import org.bukkit.Material; @@ -181,30 +183,64 @@ public void onItemHeldChange(PlayerItemHeldEvent event) { public void onPlayerInventoryClick(InventoryClickEvent event) { InventoryHolder holder = event.getInventory().getHolder(); + Boolean isClickOnTopInventory = event.getRawSlot() < event.getView().getTopInventory().getSize(); + Boolean isClickOnBottomInventory = !isClickOnTopInventory; + if (holder instanceof Player || // do not track inter-inventory stuff - event.getRawSlot() >= event.getView().getTopInventory().getSize() || // top inventory only + (isClickOnBottomInventory && !event.isShiftClick()) || // If non shift-click on player's inventory, don't care event.getSlotType() == InventoryType.SlotType.OUTSIDE || // do not track drop event.getSlot() == -999) { // temporary fix for bukkit bug (BUKKIT-2768) return; } - ItemStack take = event.getCurrentItem(); + ItemStack itemOnCursor = event.getCursor(); + ItemStack itemBeingClicked = event.getCurrentItem(); + + ArrayList> actionItemToCheckMappings = new ArrayList>(); + + if (isClickOnBottomInventory && event.isShiftClick()) { + // When shift click on bottom inventory, even when cursor contains an item stack + // It would ONLY move the clicked item stack to top + HashMap hash = new HashMap(2); + hash.put("action", "put"); + hash.put("item", itemBeingClicked); + + actionItemToCheckMappings.add(hash); + } else if (isClickOnTopInventory && event.isShiftClick()) { + // When shift click on top inventory, even when cursor contains an item stack + // It would ONLY move the clicked item stack to bottom + HashMap hash = new HashMap(2); + hash.put("action", "take"); + hash.put("item", itemBeingClicked); + + actionItemToCheckMappings.add(hash); + } + else { // isClickOnTopInventory && !event.isShiftClick() + if (event.getCursor().getType() != Material.AIR) { + HashMap hash = new HashMap(2); + hash.put("action", "put"); + hash.put("item", itemOnCursor); - String action; - ItemStack item; + actionItemToCheckMappings.add(hash); + } + if (event.getCurrentItem().getType() != Material.AIR) { + HashMap hash = new HashMap(2); + hash.put("action", "take"); + hash.put("item", itemBeingClicked); - if (take == null) { - action = "put"; - item = event.getCursor(); - } else { - action = "take"; - item = take; + actionItemToCheckMappings.add(hash); + } } Player player = (Player) event.getWhoClicked(); - if (permissionDenied(player, "modifyworld.items", action, item, "of", event.getInventory().getType())) { - event.setCancelled(true); + for(HashMap actionItemToCheckMapping : actionItemToCheckMappings){ + String actionToCheck = (String) actionItemToCheckMapping.get("action"); + ItemStack itemStackToCheck = (ItemStack) actionItemToCheckMapping.get("item"); + + if (permissionDenied(player, "modifyworld.items", actionToCheck, itemStackToCheck, "of", event.getInventory().getType())) { + event.setCancelled(true); + } } }