Added "modifyworld.items.hold.<item>" permission. issue #501

This commit is contained in:
t3hk0d3 2012-07-30 00:28:15 +04:00
parent 8580b6783c
commit 70dc3386ce
2 changed files with 228 additions and 181 deletions

View File

@ -19,6 +19,7 @@
package ru.tehkode.modifyworld.handlers;
import java.util.logging.Logger;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
@ -29,238 +30,283 @@ import org.bukkit.event.block.Action;
import org.bukkit.event.enchantment.EnchantItemEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.inventory.CraftItemEvent;
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.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector;
import ru.tehkode.modifyworld.ModifyworldListener;
import ru.tehkode.modifyworld.PlayerInformer;
/**
*
* @author t3hk0d3
*/
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();
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");
}
}
Player player = event.getPlayer();
@EventHandler(priority = EventPriority.LOW)
public void onPlayerBedEnter(PlayerBedEnterEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.usebeds")) {
event.setCancelled(true);
}
}
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 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 onPlayerBedEnter(PlayerBedEnterEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.usebeds")) {
event.setCancelled(true);
}
}
@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 (permissionDenied(event.getPlayer(), "modifyworld.bucket.fill", materialName)) {
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 onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
if (event.getMessage().startsWith("/tell") && permissionDenied(event.getPlayer(), "modifyworld.chat.private")) {
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 onPlayerChat(PlayerChatEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.chat")) {
event.setCancelled(true);
}
}
if ("air".equals(materialName)) { // This should be milk
materialName = "milk";
}
@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);
}
if (permissionDenied(event.getPlayer(), "modifyworld.bucket.fill", materialName)) {
event.setCancelled(true);
}
}
this.checkPlayerInventory(event.getPlayer());
}
@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 onPlayerDropItem(PlayerDropItemEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.items.drop", event.getItemDrop().getItemStack())) {
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerChat(PlayerChatEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.chat")) {
event.setCancelled(true);
}
}
this.checkPlayerInventory(event.getPlayer());
}
@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 onItemHeldChange(PlayerItemHeldEvent event) {
this.checkPlayerInventory(event.getPlayer());
}
this.checkPlayerInventory(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);
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerDropItem(PlayerDropItemEvent event) {
if (permissionDenied(event.getPlayer(), "modifyworld.items.drop", event.getItemDrop().getItemStack())) {
event.setCancelled(true);
}
return;
}
this.checkPlayerInventory(event.getPlayer());
}
if (!event.isCancelled() && permissionDenied(event.getPlayer(), "modifyworld.interact", event.getRightClicked())) {
event.setCancelled(true);
}
}
@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 onPlayerInteract(PlayerInteractEvent event) {
Action action = event.getAction();
if (item != null && item.getType() != Material.AIR &&
permissionDenied(player, "modifyworld.items.hold", item)) {
int freeSlot = getFreeSlot(player.getInventory());
if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) { // item restriction check
this.checkPlayerInventory(event.getPlayer());
}
if (freeSlot != 0) {
player.getInventory().setItem(freeSlot, item);
} else {
player.getWorld().dropItemNaturally(player.getLocation(), item);
}
Player player = event.getPlayer();
player.getInventory().setItem(event.getNewSlot(), new ItemStack(Material.AIR));
}
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;
}
}
this.checkPlayerInventory(player);
}
if (action != Action.LEFT_CLICK_BLOCK && action != Action.RIGHT_CLICK_BLOCK && action != Action.PHYSICAL) {
return;
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInventoryEvent(InventoryClickEvent event) {
ItemStack item = event.getCursor();
if (this.checkItemUse && action != Action.PHYSICAL) {
if (permissionDenied(event.getPlayer(), "modifyworld.items.use", player.getItemInHand(), "on.block", event.getClickedBlock())) {
event.setCancelled(true);
}
if(item == null || item.getType() == Material.AIR || event.getSlotType() != InventoryType.SlotType.QUICKBAR) {
return;
}
return;
}
Player player = (Player)event.getWhoClicked();
if (!event.isCancelled() && permissionDenied(player, "modifyworld.blocks.interact", event.getClickedBlock())) {
event.setCancelled(true);
}
}
int targetSlot = player.getInventory().getHeldItemSlot();
@EventHandler(priority = EventPriority.LOW)
public void onItemEnchant(EnchantItemEvent event) {
if (permissionDenied(event.getEnchanter(), "modifyworld.items.enchant", event.getItem())) {
event.setCancelled(true);
}
}
if(event.getSlot() == targetSlot && permissionDenied(player, "modifyworld.items.have", item)) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onItemCraft(CraftItemEvent event) {
Player player = (Player) event.getWhoClicked();
@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 (permissionDenied(player, "modifyworld.items.craft", event.getRecipe().getResult())) {
event.setCancelled(true);
}
}
return;
}
@EventHandler(priority = EventPriority.LOW)
public void onFoodLevelChange(FoodLevelChangeEvent event) {
Player player = event.getEntity() instanceof Player ? (Player) event.getEntity() : null;
if (!event.isCancelled() && permissionDenied(event.getPlayer(), "modifyworld.interact", event.getRightClicked())) {
event.setCancelled(true);
}
}
if (player == null) {
return;
}
@EventHandler(priority = EventPriority.LOW)
public void onPlayerInteract(PlayerInteractEvent event) {
Action action = event.getAction();
if (_permissionDenied(player, "modifyworld.digestion")) {
event.setCancelled(true);
}
}
if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) { // item restriction check
this.checkPlayerInventory(event.getPlayer());
}
protected void checkPlayerInventory(Player player) {
if (!checkInventory) {
return;
}
Player player = event.getPlayer();
Inventory inventory = player.getInventory();
for (ItemStack stack : inventory.getContents()) {
if (stack != null && permissionDenied(player, "modifyworld.items.have", stack)) {
inventory.remove(stack);
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;
}
}
if (this.dropRestrictedItem) {
player.getWorld().dropItemNaturally(player.getLocation(), stack);
}
}
}
}
if (action != Action.LEFT_CLICK_BLOCK && action != Action.RIGHT_CLICK_BLOCK && action != Action.PHYSICAL) {
return;
}
if (this.checkItemUse && action != Action.PHYSICAL) {
if (permissionDenied(event.getPlayer(), "modifyworld.items.use", player.getItemInHand(), "on.block", event.getClickedBlock())) {
event.setCancelled(true);
}
return;
}
if (!event.isCancelled() && permissionDenied(player, "modifyworld.blocks.interact", event.getClickedBlock())) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onItemEnchant(EnchantItemEvent event) {
if (permissionDenied(event.getEnchanter(), "modifyworld.items.enchant", event.getItem())) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onItemCraft(CraftItemEvent event) {
Player player = (Player) event.getWhoClicked();
if (permissionDenied(player, "modifyworld.items.craft", event.getRecipe().getResult())) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
public void onFoodLevelChange(FoodLevelChangeEvent event) {
Player player = event.getEntity() instanceof Player ? (Player) event.getEntity() : null;
if (player == null) {
return;
}
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;
}
}

View File

@ -17,6 +17,7 @@ messages:
modifyworld.chat.private: Listener is deaf :(
modifyworld.items.have: Prohibited item &a$1&4 has been removed from your inventory.
modifyworld.items.drop: This is indecent to scatter &a$1&4 around
modifyworld.items.hold: Beware, &a$1&4 is cursed!
modifyworld.items.craft: Sorry, but &a$1&4 is too complicated
modifyworld.items.enchant: Sorry, &a$1&4 is too dangerous
modifyworld.vehicle.enter.boat: You are too heavy for this &a$1