Solved InventoryClickEvent setcancelled.

Discussion in 'Plugin Development' started by DarkRangerMC, Aug 20, 2014.

Thread Status:
Not open for further replies.
  1. I want it so that when I pick something from my custom inventory... you can't drop it. And the only way I can find is closing the inventory but youtube videos tell me that doesn't have to be the case.
    When I select something from my custom inventory, it duplicates, so I can drag it into my normal inventory but there's still one in the custom one.
    So that will make infinit items.

    Here is my event handler
    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent event){
    3. Player player = (Player) event.getWhoClicked();
    4. if (event.getInventory().getName().equals(shopinventory.getName())){
    5. event.setCancelled(true);
    6. }else{
    7. player.sendMessage("test2");
    8. }
    9. if (event.getCurrentItem() == null){
    10. return;
    11. }
    12. if (event.getCurrentItem().getType() == Material.PAPER){
    13. player.sendMessage("Test");
    14. }
    15. }


    This is my command:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
    2. Player player = (Player) sender;
    3. if (label.equalsIgnoreCase("shop")){
    4. player.openInventory(shopinventory);
    5. shopinventory.addItem(new ItemStack(Material.PAPER, 1));
    6.  
    7. }
    8. return false;
    9.  
    10. }


    And this is where I create the custom inventory:
    Code:java
    1. public void onEnable(){
    2. PluginManager manager = this.getServer().getPluginManager();
    3. manager.registerEvents(this, this);
    4. shopinventory = Bukkit.createInventory(null, 9, ChatColor.RED + "Shop");
    5. shopinventoryab = Bukkit.createInventory(null, 9, ChatColor.RED + "Shop - Abilities");
    6. }


    I would like to see some help.
     
  2. Offline

    Totom3

    DarkRangerMC Use .equals() to compare ItemStacks and Inventories. Or just compare their names/display names for better performance
     
    The Fancy Whale likes this.
  3. Offline

    The Fancy Whale

    What isn't working? Error? Maybe add some debug messages?
     
  4. Changed topic...
    everything works except the event cancelling.
    Anyone could help?
     
  5. Offline

    Cycryl

    DarkRangerMC
    make sure you initialize the shopinventory
    also maybe try == instead of .equals() when comparing strings
     

  6. That stuff works... When I click something in another inv, it shows test2, when it's in the custom inv, it shows nothing. So it's not that part.
     
  7. Offline

    Totom3

    Cycryl No. Never use == on String. Never.

    DarkRangerMC I had the same problem once, my solution was to refresh the inventory. So you simply close the inventory, then open it up again.
     
  8. Totom3 That is for ghostitems, but this wasn't... I fixed it by myself now :)
     
Thread Status:
Not open for further replies.

Share This Page