InventoryClickEvent isn't being cancled

Discussion in 'Plugin Development' started by HelGod, Mar 17, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    HelGod

    Code:
    public class Main extends JavaPlugin implements Listener {
     
        public static Inventory shopInventory;
     
        public void onEnable() {
            shopInventory = Bukkit.createInventory(null, 9, "Welcome To The Shop");
            System.out.print("ShopInventory has been enabled");
            getServer().getPluginManager().registerEvents(this, this);
        }
     
        public void onDisable() {
            System.out.print("ShopInventory has been disabled");
        }
     
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
            if (label.equalsIgnoreCase("shop")) {
                List<String> ls = new ArrayList<String>();
                ls.add(ChatColor.GREEN + "(Hero Item)");
                ls.add(ChatColor.GOLD + "200 Gold");
                List<String> villan = new ArrayList<String>();
                villan.add(ChatColor.DARK_RED + "(Villian Item)");
                villan.add(ChatColor.AQUA + "200 Diamonds");
                if (sender instanceof Player) {
                    ((Player)sender).openInventory(shopInventory);
                    shopInventory.clear();
                    shopInventory.addItem(setName(
                            new ItemStack(Material.SLIME_BALL), ChatColor.DARK_RED
                                    + "Slime Grenade", ls));
                    shopInventory.addItem(setName(new ItemStack(
                            Material.GOLDEN_APPLE), ChatColor.DARK_PURPLE
                            + "Jester's Apple", villan));
                } else if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "GameName" + ChatColor.GRAY + "]" + ChatColor.DARK_RED + "You must be a player");
                    return true;
                }
            }
            return false;
        }
     
        private ItemStack setName(ItemStack is, String name, List<String> lore) {
            ItemMeta im = is.getItemMeta();
            if (name != null)
                im.setDisplayName(name);
            if (lore != null)
                im.setLore(lore);
            is.setItemMeta(im);
            return is;
        }
     
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if (event.getInventory() == shopInventory) {
                if (event.getCurrentItem().getType() == Material.SLIME_BALL) {
                    event.setCancelled(true);
                    event.getWhoClicked().getInventory()
                            .addItem(new ItemStack(Material.SLIME_BALL));
                }
            }
        }
    }
     
     
    
    This is my code and when i click the slimeball the event isn't being canceled and i can take it out of the shopInventory. Does anyone know what i did wrong?
     
  2. Offline

    LudicrousYoshi

    maybe try replacing
    event.setCancelled(true);
    with
    event.setResult(DENY);
     
  3. Offline

    HelGod

    I tried event.setResult(Result.DENY); instead of event.setResult(DENY);
     
  4. setCanceled() actually sets Result.DENY, see the source code.

    The problem is in your inventory comparison, I doubt that works.
    You should compare title and size instead.
     
  5. Offline

    HelGod

    How would i got about comparing title and size?

    Edit: I Fixed it, that you digi. I compared it with the inventory size instead :D you are a great help
     
Thread Status:
Not open for further replies.

Share This Page