location of an inventory

Discussion in 'Plugin Development' started by triangalo, Dec 6, 2016.

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

    triangalo

    I am trying to create an if statement about if a player clicks on an item in an inventory that is not the 3rd item, the event cancels

    here's what I have, but doesn't work, can you guys send suggestions ?
    Code:
    @EventHandler
         public void onInventoryClick(InventoryClickEvent event) {
    
         if (event.getInventory().getItem(2).getType() == event.getCurrentItem() .getType()) {
              return;
         } else {
              event.setCancelled(true);
              return;
         }
    }
     
  2. Offline

    Zombie_Striker

    @triangalo
    Try using this instead
    Code:
    if(event.getSlot != 3)
    //cancel
     
  3. Offline

    renaud444

    Try this.
    Check to see if it was the third slot, like you did, and if it is, simply return. Except try it without the "else { ... }" block:

    PHP:
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (
    event.getInventory().getItem(2).getType() == event.getCurrentItem() .getType()) 
            return;
        
    event.setCancelled(true);
    }
     
  4. Offline

    Zombie_Striker

    @renaud444
    No. That will run for a lot of other items. All you need is for the material to be the same in order to have this return.

    @triangalo
    Just use my method and check if the slot is equal to "2" (the third slot)
     
  5. Offline

    renaud444

    @Zombie_Striker
    My bad, I meant:

    Code:java
    1. @EventHandlerpublic void onInventoryClick(InventoryClickEvent event) {
    2. if (event.getSlot() == 2)
    3. return;
    4. event.setCancelled(true);
    5. }
     
  6. Offline

    triangalo

    @renaud444
    yeah the .getslot() makes more sense
    let me try it out
    ah, 1 more thing, why don't I need the return; under the event.setcancel?

    well the event doesn't seem to get triggered, i can still pick up items from the inventory
    just to make sure
    i registered the events here
    Code:
    public void registerEvents() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new EventListener(), this);;
        }
     
    Last edited: Dec 7, 2016
  7. Offline

    Jan108

    @triangalo if your write the return it will do the same as when you don't do it, because the method ends there and a return statement ends the method too.

    Get other events in the listener triggered?
     
Thread Status:
Not open for further replies.

Share This Page