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,16 +30,18 @@ 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 {
@ -153,7 +156,40 @@ public class PlayerListener extends ModifyworldListener {
@EventHandler(priority = EventPriority.LOW)
public void onItemHeldChange(PlayerItemHeldEvent event) {
this.checkPlayerInventory(event.getPlayer());
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 (freeSlot != 0) {
player.getInventory().setItem(freeSlot, item);
} else {
player.getWorld().dropItemNaturally(player.getLocation(), item);
}
player.getInventory().setItem(event.getNewSlot(), new ItemStack(Material.AIR));
}
this.checkPlayerInventory(player);
}
@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.have", item)) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOW)
@ -263,4 +299,14 @@ public class PlayerListener extends ModifyworldListener {
}
}
}
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