Solved InventoryClickEvent

Discussion in 'Spigot Plugin Development' started by HazmatDemon, Jul 6, 2018.

Thread Status:
Not open for further replies.
  1. Hello there,
    I have this in my class:
    Code (Text):

     @EventHandler(priority = EventPriority.HIGHEST)
           public void onInventory(InventoryClickEvent event) {
           ItemStack clicked = event.getCurrentItem();
           Inventory inventory = event.getInventory();
           Player player = (Player) event.getWhoClicked();
           if (inventory.getName().equals(Schemat.getName())) {
               if(clicked.getType().equals(greenwool)){
                   event.setCancelled(false);
                   if(inventory.contains(dirt, 64) && inventory.contains(gravel, 64)){
                   player.getInventory().removeItem(new ItemStack(dirt));
                   player.getInventory().removeItem(new ItemStack(gravel));
                   player.getInventory().addItem(new ItemStack(sand));
                   player.getInventory().addItem(new ItemStack(sand1));
                   }else{
                       event.setCancelled(true);
                   }
               }else{
                   event.setCancelled(true);
           }
           }
         }
     
    I want it to check if clicked item is a light green wool and if yes give the player sand and take 64 dirt and gravel.
    In this class I also have a method where I made GUI for this.
    Code (Text):

    ItemStack greenwool = new ItemStack(Material.WOOL, 1, (short) 5);{
           ItemMeta im = greenwool.getItemMeta();
           im.setDisplayName("Accept");
           greenwool.setItemMeta(im);
           Schemat.setItem(42, greenwool);
        }
    public static Inventory Schemat = Bukkit.createInventory(null, 54, "Schemat");
         static {
    ItemStack darkglass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 15); {
                 ItemMeta Meta = darkglass.getItemMeta();
                 Meta.setDisplayName("*");
                 darkglass.setItemMeta(Meta);
             Schemat.setItem(0, darkglass);
    }
    }
     
    But unfortunately when i click green wool in my gui nothing happens.
    Only GUI is showing up with items.
    Any ideas how to fix this?
    I am using Java 8 latest built, spigot 1.12 and java ecplise luna.

    oh and i am using this to prevent players from taking any item from GUI:
    Code (Text):

    @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
       try{
              ItemStack clicked = event.getCurrentItem();
              Inventory inventory = event.getInventory();
         if (inventory.getName().equals(Schemat.getName())) {
            if(!(clicked.getType().equals(Material.AIR))){  
                 event.setCancelled(true);        
              }else{
                 event.setCancelled(false);
              }
       }
       }catch(Exception e){
         
       }
    }
     
    I really didn't want to make this thread because i thought i can do it on my own but i just can't.
    Please help.o_O
     
  2. Hello, i am sorry but your code is a bit messed up. Anyways, check in the second InventoryClickEvent you are showing us because you check if the clicked item is not air, you cancel the event.

    Code (Text):
            if(!(clicked.getType().equals(Material.AIR))){
                 event.setCancelled(true);      
     
  3. Because i don't want players to take items which are not air from GUI.
     
  4. In the code i showed you, you check if clicked item is not air, cancel.

    Meaning, whatever they click, if it's not air, it won't work.
     
  5. FrostedSnowman

    Resource Staff

    [​IMG]
     
    • Agree Agree x 1
    • Funny Funny x 1
  6. But when change it items are still in inventory so nothing changed ;<
     
  7. You are telling us that you're issue is that when you click an item in the GUI it doesn't work. Am i correct?

    Right now, what you do is allowing only AIR to be clicked and do an action, nothing else.
     
  8. I just want to click wool which should take items from inventory, add add some items to inventory and also player cant take anything from gui.
     
  9. Like i said, check what i wrote above, this solves your issue.
     
  10. OK problem solved when i deleted:
    Code (Text):

     if(inventory.contains(dirt, 64) && inventory.contains(gravel, 64)){
     
    I will try fix this line somehow ;<
    Thanks everyone for help :)
     
  11. PLEASE do not surround the entire method in a try catch statement.. Learn to read stacktraces and figure out what's wrong with your code through them. The errors are there for a reason, not to be annoying and rub in your face "lol, you have to see this now".
     
    • Useful Useful x 1
Thread Status:
Not open for further replies.