Skip to content
This repository has been archived by the owner on Jul 4, 2020. It is now read-only.

! Fix take & put permission node about container #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 48 additions & 12 deletions src/main/java/ru/tehkode/modifyworld/handlers/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<HashMap<String, Object>> actionItemToCheckMappings = new ArrayList<HashMap<String, Object>>();

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<String, Object> hash = new HashMap<String, Object>(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<String, Object> hash = new HashMap<String, Object>(2);
hash.put("action", "take");
hash.put("item", itemBeingClicked);

actionItemToCheckMappings.add(hash);
}
else { // isClickOnTopInventory && !event.isShiftClick()
if (event.getCursor().getType() != Material.AIR) {
HashMap<String, Object> hash = new HashMap<String, Object>(2);
hash.put("action", "put");
hash.put("item", itemOnCursor);

String action;
ItemStack item;
actionItemToCheckMappings.add(hash);
}
if (event.getCurrentItem().getType() != Material.AIR) {
HashMap<String, Object> hash = new HashMap<String, Object>(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<String, Object> 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);
}
}
}

Expand Down