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;
@ -46,267 +47,300 @@ import ru.tehkode.modifyworld.PlayerInformer;
*/
public class PlayerListener extends ModifyworldListener {
protected boolean checkInventory = false;
protected boolean dropRestrictedItem = false;
protected boolean checkInventory = false;
protected boolean dropRestrictedItem = false;
public PlayerListener(Plugin plugin, ConfigurationSection config, PlayerInformer informer) {
super(plugin, config, informer);
public PlayerListener(Plugin plugin, ConfigurationSection config, PlayerInformer informer) {
super(plugin, config, informer);
this.checkInventory = config.getBoolean("item-restrictions", this.checkInventory);
this.dropRestrictedItem = config.getBoolean("drop-restricted-item", this.dropRestrictedItem);
this.checkInventory = config.getBoolean("item-restrictions", this.checkInventory);
this.dropRestrictedItem = config.getBoolean("drop-restricted-item", this.dropRestrictedItem);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerSneak(PlayerToggleSneakEvent event) {
Player player = event.getPlayer();
@EventHandler(priority = EventPriority.LOW)
public void onPlayerSneak(PlayerToggleSneakEvent event) {
Player player = event.getPlayer();
if (event.isSneaking() && _permissionDenied(player, "modifyworld.sneak")) {
event.setCancelled(true);
event.getPlayer().setSneaking(false);
}
}
if (event.isSneaking() && _permissionDenied(player, "modifyworld.sneak")) {
event.setCancelled(true);
event.getPlayer().setSneaking(false);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerSprint(PlayerToggleSprintEvent event) {
Player player = event.getPlayer();
@EventHandler(priority = EventPriority.LOW)
public void onPlayerSprint(PlayerToggleSprintEvent event) {
Player player = event.getPlayer();
if (event.isSprinting() && _permissionDenied(player, "modifyworld.sprint")) {
event.setCancelled(true);
event.getPlayer().setSprinting(false);
}
}
if (event.isSprinting() && _permissionDenied(player, "modifyworld.sprint")) {
event.setCancelled(true);
event.getPlayer().setSprinting(false);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerLogin(PlayerLoginEvent event) {
if (!enableWhitelist) {
return;
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerLogin(PlayerLoginEvent event) {
if (!enableWhitelist) {
return;
}
Player player = event.getPlayer();
Player player = event.getPlayer();
if (_permissionDenied(player, "modifyworld.login")) {
// String whiteListMessage = user.getOption("kick-message", worldName, this.whitelistKickMessage);
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, informer.getMessage(player, "modifyworld.login"));
Logger.getLogger("Minecraft").info("Player \"" + player.getName() + "\" were kicked by Modifyworld - lack of 'modifyworld.login' permission");
}
}
if (_permissionDenied(player, "modifyworld.login")) {
// String whiteListMessage = user.getOption("kick-message", worldName, this.whitelistKickMessage);
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, informer.getMessage(player, "modifyworld.login"));
Logger.getLogger("Minecraft").info("Player \"" + player.getName() + "\" were kicked by Modifyworld - lack of 'modifyworld.login' permission");
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerBedEnter(PlayerBedEnterEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.usebeds")) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerBedEnter(PlayerBedEnterEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.usebeds")) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
String bucketName = event.getBucket().toString().toLowerCase().replace("_bucket", ""); // WATER_BUCKET -> water
if (permissionDenied(event.getPlayer(), "modifyworld.bucket.empty", bucketName)) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerBucketEmpty(PlayerBucketEmptyEvent event) {
String bucketName = event.getBucket().toString().toLowerCase().replace("_bucket", ""); // WATER_BUCKET -> water
if (permissionDenied(event.getPlayer(), "modifyworld.bucket.empty", bucketName)) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerBucketFill(PlayerBucketFillEvent event) {
String materialName = event.getBlockClicked().getType().toString().toLowerCase().replace("stationary_", ""); // STATIONARY_WATER -> water
@EventHandler(priority = EventPriority.LOW)
public void onPlayerBucketFill(PlayerBucketFillEvent event) {
String materialName = event.getBlockClicked().getType().toString().toLowerCase().replace("stationary_", ""); // STATIONARY_WATER -> water
if ("air".equals(materialName)) { // This should be milk
materialName = "milk";
}
if ("air".equals(materialName)) { // This should be milk
materialName = "milk";
}
if (permissionDenied(event.getPlayer(), "modifyworld.bucket.fill", materialName)) {
event.setCancelled(true);
}
}
if (permissionDenied(event.getPlayer(), "modifyworld.bucket.fill", materialName)) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
if (event.getMessage().startsWith("/tell") && permissionDenied(event.getPlayer(), "modifyworld.chat.private")) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
if (event.getMessage().startsWith("/tell") && permissionDenied(event.getPlayer(), "modifyworld.chat.private")) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerChat(PlayerChatEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.chat")) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerChat(PlayerChatEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.chat")) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
// No inform to avoid spam
if (_permissionDenied(event.getPlayer(), "modifyworld.items.pickup", event.getItem().getItemStack())) {
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
// No inform to avoid spam
if (_permissionDenied(event.getPlayer(), "modifyworld.items.pickup", event.getItem().getItemStack())) {
event.setCancelled(true);
}
this.checkPlayerInventory(event.getPlayer());
}
this.checkPlayerInventory(event.getPlayer());
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerDropItem(PlayerDropItemEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.items.drop", event.getItemDrop().getItemStack())) {
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerDropItem(PlayerDropItemEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.items.drop", event.getItemDrop().getItemStack())) {
event.setCancelled(true);
}
this.checkPlayerInventory(event.getPlayer());
}
this.checkPlayerInventory(event.getPlayer());
}
@EventHandler(priority = EventPriority.LOW)
public void onItemHeldChange(PlayerItemHeldEvent event) {
Player player = event.getPlayer();
ItemStack item = player.getInventory().getItem(event.getNewSlot());
@EventHandler(priority = EventPriority.LOW)
public void onItemHeldChange(PlayerItemHeldEvent event) {
Player player = event.getPlayer();
ItemStack item = player.getInventory().getItem(event.getNewSlot());
if (item != null && item.getType() != Material.AIR &&
permissionDenied(player, "modifyworld.items.hold", item)) {
int freeSlot = getFreeSlot(player.getInventory());
if (item != null && item.getType() != Material.AIR &&
permissionDenied(player, "modifyworld.items.hold", item)) {
int freeSlot = getFreeSlot(player.getInventory());
if (freeSlot != 0) {
player.getInventory().setItem(freeSlot, item);
} else {
player.getWorld().dropItemNaturally(player.getLocation(), item);
}
if (freeSlot != 0) {
player.getInventory().setItem(freeSlot, item);
} else {
player.getWorld().dropItemNaturally(player.getLocation(), item);
}
player.getInventory().setItem(event.getNewSlot(), new ItemStack(Material.AIR));
}
player.getInventory().setItem(event.getNewSlot(), new ItemStack(Material.AIR));
}
this.checkPlayerInventory(player);
}
this.checkPlayerInventory(player);
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInventoryEvent(InventoryClickEvent event) {
ItemStack item = event.getCursor();
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInventoryClick(InventoryClickEvent event) {
InventoryHolder holder = event.getInventory().getHolder();
if(item == null || item.getType() == Material.AIR || event.getSlotType() != InventoryType.SlotType.QUICKBAR) {
return;
}
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;
}
int targetSlot = player.getInventory().getHeldItemSlot();
ItemStack take = holder.getInventory().getItem(event.getSlot());
if(event.getSlot() == targetSlot && permissionDenied(player, "modifyworld.items.hold", item)) {
event.setCancelled(true);
}
}
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 onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (this.checkItemUse) {
if (permissionDenied(event.getPlayer(), "modifyworld.items.use", event.getPlayer().getItemInHand(), "on.entity", event.getRightClicked())) {
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInventoryEvent(InventoryClickEvent event) {
ItemStack item = event.getCursor();
return;
}
if (item == null || item.getType() == Material.AIR || event.getSlotType() != InventoryType.SlotType.QUICKBAR) {
return;
}
if (!event.isCancelled() && permissionDenied(event.getPlayer(), "modifyworld.interact", event.getRightClicked())) {
event.setCancelled(true);
}
}
Player player = (Player) event.getWhoClicked();
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInteract(PlayerInteractEvent event) {
Action action = event.getAction();
int targetSlot = player.getInventory().getHeldItemSlot();
if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) { // item restriction check
this.checkPlayerInventory(event.getPlayer());
}
if (event.getSlot() == targetSlot && permissionDenied(player, "modifyworld.items.hold", item)) {
event.setCancelled(true);
}
}
Player player = event.getPlayer();
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (this.checkItemUse) {
if (permissionDenied(event.getPlayer(), "modifyworld.items.use", event.getPlayer().getItemInHand(), "on.entity", event.getRightClicked())) {
event.setCancelled(true);
}
if (action == Action.RIGHT_CLICK_AIR) { //RIGHT_CLICK_AIR is cancelled by default.
switch (player.getItemInHand().getType()) {
case POTION: //Only check splash potions.
if ((player.getItemInHand().getDurability() & 0x4000) != 0x4000) {
break;
}
case EGG:
case SNOW_BALL:
case EXP_BOTTLE:
if (permissionDenied(player, "modifyworld.items.throw", player.getItemInHand())) {
event.setUseItemInHand(Result.DENY);
//Denying a potion works fine, but the client needs to be updated because it already reduced the item.
if (player.getItemInHand().getType() == Material.POTION) {
event.getPlayer().updateInventory();
}
}
return;
}
}
return;
}
if (action != Action.LEFT_CLICK_BLOCK && action != Action.RIGHT_CLICK_BLOCK && action != Action.PHYSICAL) {
return;
}
if (!event.isCancelled() && permissionDenied(event.getPlayer(), "modifyworld.interact", event.getRightClicked())) {
event.setCancelled(true);
}
}
if (this.checkItemUse && action != Action.PHYSICAL) {
if (permissionDenied(event.getPlayer(), "modifyworld.items.use", player.getItemInHand(), "on.block", event.getClickedBlock())) {
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInteract(PlayerInteractEvent event) {
Action action = event.getAction();
return;
}
if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) { // item restriction check
this.checkPlayerInventory(event.getPlayer());
}
if (!event.isCancelled() && permissionDenied(player, "modifyworld.blocks.interact", event.getClickedBlock())) {
event.setCancelled(true);
}
}
Player player = event.getPlayer();
@EventHandler(priority = EventPriority.LOW)
public void onItemEnchant(EnchantItemEvent event) {
if (permissionDenied(event.getEnchanter(), "modifyworld.items.enchant", event.getItem())) {
event.setCancelled(true);
}
}
if (action == Action.RIGHT_CLICK_AIR) { //RIGHT_CLICK_AIR is cancelled by default.
switch (player.getItemInHand().getType()) {
case POTION: //Only check splash potions.
if ((player.getItemInHand().getDurability() & 0x4000) != 0x4000) {
break;
}
case EGG:
case SNOW_BALL:
case EXP_BOTTLE:
if (permissionDenied(player, "modifyworld.items.throw", player.getItemInHand())) {
event.setUseItemInHand(Result.DENY);
//Denying a potion works fine, but the client needs to be updated because it already reduced the item.
if (player.getItemInHand().getType() == Material.POTION) {
event.getPlayer().updateInventory();
}
}
return;
}
}
@EventHandler(priority = EventPriority.LOW)
public void onItemCraft(CraftItemEvent event) {
Player player = (Player) event.getWhoClicked();
if (action != Action.LEFT_CLICK_BLOCK && action != Action.RIGHT_CLICK_BLOCK && action != Action.PHYSICAL) {
return;
}
if (permissionDenied(player, "modifyworld.items.craft", event.getRecipe().getResult())) {
event.setCancelled(true);
}
}
if (this.checkItemUse && action != Action.PHYSICAL) {
if (permissionDenied(event.getPlayer(), "modifyworld.items.use", player.getItemInHand(), "on.block", event.getClickedBlock())) {
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.LOW)
public void onFoodLevelChange(FoodLevelChangeEvent event) {
Player player = event.getEntity() instanceof Player ? (Player) event.getEntity() : null;
return;
}
if (player == null) {
return;
}
if (!event.isCancelled() && permissionDenied(player, "modifyworld.blocks.interact", event.getClickedBlock())) {
event.setCancelled(true);
}
}
if (_permissionDenied(player, "modifyworld.digestion")) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onItemEnchant(EnchantItemEvent event) {
if (permissionDenied(event.getEnchanter(), "modifyworld.items.enchant", event.getItem())) {
event.setCancelled(true);
}
}
protected void checkPlayerInventory(Player player) {
if (!checkInventory) {
return;
}
@EventHandler(priority = EventPriority.LOW)
public void onItemCraft(CraftItemEvent event) {
Player player = (Player) event.getWhoClicked();
Inventory inventory = player.getInventory();
for (ItemStack stack : inventory.getContents()) {
if (stack != null && permissionDenied(player, "modifyworld.items.have", stack)) {
inventory.remove(stack);
if (permissionDenied(player, "modifyworld.items.craft", event.getRecipe().getResult())) {
event.setCancelled(true);
}
}
if (this.dropRestrictedItem) {
player.getWorld().dropItemNaturally(player.getLocation(), stack);
}
}
}
}
@EventHandler(priority = EventPriority.LOW)
public void onFoodLevelChange(FoodLevelChangeEvent event) {
Player player = event.getEntity() instanceof Player ? (Player) event.getEntity() : null;
private int getFreeSlot(Inventory inventory) {
for (int i = 9; i <= 35; i++) {
if (inventory.getItem(i) == null || inventory.getItem(i).getType() == Material.AIR) {
return i;
}
}
if (player == null) {
return;
}
return 0;
}
if (_permissionDenied(player, "modifyworld.digestion")) {
event.setCancelled(true);
}
}
protected void checkPlayerInventory(Player player) {
if (!checkInventory) {
return;
}
Inventory inventory = player.getInventory();
for (ItemStack stack : inventory.getContents()) {
if (stack != null && permissionDenied(player, "modifyworld.items.have", stack)) {
inventory.remove(stack);
if (this.dropRestrictedItem) {
player.getWorld().dropItemNaturally(player.getLocation(), stack);
}
}
}
}
private int getFreeSlot(Inventory inventory) {
for (int i = 9; i <= 35; i++) {
if (inventory.getItem(i) == null || inventory.getItem(i).getType() == Material.AIR) {
return i;
}
}
return 0;
}
}