Implemented inventory permission checks - "modifyworld.items.put/take.<item>.of.<container>"

This commit is contained in:
t3hk0d3 2012-08-12 01:38:41 +04:00
parent e2db122b8a
commit afba9cdbcd
2 changed files with 246 additions and 209 deletions

View File

@ -20,6 +20,7 @@ package ru.tehkode.modifyworld;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.*;
import org.bukkit.event.Listener;
@ -153,6 +154,8 @@ public abstract class ModifyworldListener implements Listener {
protected String getObjectPermission(Object obj) {
if (obj instanceof Entity) {
return (getEntityName((Entity) obj));
} else if (obj instanceof BlockState) {
return (getBlockPermission(((BlockState)obj).getBlock()));
} else if (obj instanceof ItemStack) {
return (getItemPermission((ItemStack) obj));
} else if (obj instanceof Material) {

View File

@ -34,6 +34,7 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.*;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.Plugin;
@ -176,18 +177,51 @@ public class PlayerListener extends ModifyworldListener {
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInventoryEvent(InventoryClickEvent event) {
ItemStack item = event.getCursor();
public void onPlayerInventoryClick(InventoryClickEvent event) {
InventoryHolder holder = event.getInventory().getHolder();
if(item == null || item.getType() == Material.AIR || event.getSlotType() != InventoryType.SlotType.QUICKBAR) {
if (holder instanceof Player) { // do not track inter-inventory stuff
return;
}
Player player = (Player)event.getWhoClicked();
if (event.getRawSlot() >= event.getView().getTopInventory().getSize()) { // top inventory only
return;
}
ItemStack take = holder.getInventory().getItem(event.getSlot());
String action;
String op;
ItemStack item;
if(take == null) {
action = "put";
item = event.getCursor();
} else {
action = "take";
item = take;
}
Player player = (Player) event.getWhoClicked();
if(permissionDenied(player, "modifyworld.items", action, item, "of", holder)) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInventoryEvent(InventoryClickEvent event) {
ItemStack item = event.getCursor();
if (item == null || item.getType() == Material.AIR || event.getSlotType() != InventoryType.SlotType.QUICKBAR) {
return;
}
Player player = (Player) event.getWhoClicked();
int targetSlot = player.getInventory().getHeldItemSlot();
if(event.getSlot() == targetSlot && permissionDenied(player, "modifyworld.items.hold", item)) {
if (event.getSlot() == targetSlot && permissionDenied(player, "modifyworld.items.hold", item)) {
event.setCancelled(true);
}
}