Solved Inventory Click

Discussion in 'Plugin Development' started by Scorpyon04, Jul 23, 2017.

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

    Scorpyon04

    Im making a plugin that has lots on different Invetories and i need a way to check if the open inv is the players or one of the ones in my plugin.
     
  2. So you need to check if your Inventory is the Inventory that is created by your plugin?
     
  3. Offline

    Scorpyon04

    @Minecraft1o1 i need to check if the inventory being clicked in is one from my plugin
     
    Last edited: Jul 23, 2017
  4. You can use the Inventory name as a check if the inventory is from your plugin.

    So you use InventoryOpenEvent and check if the name of the inventory is same as the name of the inventory of your plugin. Now, that isn't safe because if someone names the chest that way your event will pass that.

    How do your Inventories get opened? You have a command or what?
     
  5. Offline

    Caderape2

    @Scorpyon04 You can use InventoryHolder for manage many inventories.
     
  6. Offline

    dumbasPL

    just name your inventory and use this:
    Code:
        @EventHandler
        public void onClick(InventoryClickEvent e){
             if(e.getInventory() == null) return;
             if(e.getCurrentItem() == null) return;  //optional (detect if player clicked item)
             if(e.getCurrentItem().getType().equals(Material.AIR)) return;  //optional (detect if player clicked item)
             if(e.getInventory().getTitle() !=null && (e.getInventory().getTitle().equals("your title"){
                 //do stuf
             }
         }
    
     
  7. Offline

    Scorpyon04

    @Minecraft1o1 i have a command to open inventories, i have about 20 different inventories in my plugin and i need a easy way to check if the inventory is not mine

    @dumbasPL that didnt work

    @Caderape2 i have never used a inventory holder
     
    Last edited: Jul 23, 2017
  8. Offline

    Caderape2

    @Scorpyon04 A small example of how inventoryHolder works.
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.inventory.InventoryOpenEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.InventoryHolder;
    
    public abstract class CustomInventory implements InventoryHolder {
    
      
        private final Inventory inventory;
      
      
        public CustomInventory(int row, String title) {
            this.inventory = Bukkit.createInventory(this, row * 9, title);
        }
      
      
        @Override
        public Inventory getInventory() {
            return this.inventory;
        }
    
      
      
        public abstract void onInventoryOpen(InventoryOpenEvent e);
      
        public abstract void onInventoryClick(InventoryClickEvent e);
      
        public abstract void onInventoryClose(InventoryCloseEvent e);  
    }
    You create a class by inventory that extends CutomInventory. Then a class for listen to event.

    Code:
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.inventory.InventoryOpenEvent;
    
    public class InventoryManagement implements Listener {
    
      
      
        @EventHandler
        public void onOpen(InventoryOpenEvent e) {
            if (e.getInventory().getHolder() instanceof CustomInventory)
                ((CustomInventory) e.getInventory().getHolder()).onInventoryOpen(e);
        }
      
      
        @EventHandler
        public void onclick(InventoryClickEvent e) {
            if (e.getInventory().getHolder() instanceof CustomInventory)
                ((CustomInventory) e.getInventory().getHolder()).onInventoryClick(e);
        }
      
      
        @EventHandler
        public void onClose(InventoryCloseEvent e) {
            if (e.getInventory().getHolder() instanceof CustomInventory)
                ((CustomInventory) e.getInventory().getHolder()).onInventoryClose(e);
        }
    }
    This line will check if it's one of your inventory : e.getInventory() instanceof CustomInventory
    And it will call the method linked to the inventory
     
    Last edited: Jul 24, 2017
  9. Offline

    Scorpyon04

    I Understand now ill try it
     
    Last edited: Jul 24, 2017
  10. Offline

    Caderape2

    @Scorpyon04 Taht depends what you need. If you want the abstract methods for run differents codes depend of the inventory clicked, yea.
    Or if you just want to check if it's one of your inventory, you can remove the abstract method if they are useless, and create 20 instance of a single class.
     
  11. Offline

    Scorpyon04

    You said that i need a class that extends custominventory and one for a listener. could i make it in one?
     
    Last edited: Jul 24, 2017
  12. Offline

    Caderape2

    @Scorpyon04 You also can add a identifier for each inventory. Then in the listener class, you can compare inventory with the identifier and run different code.
     
  13. Offline

    Scorpyon04

    Ok thanks do i have to register any events or anything??
     
    Last edited: Jul 24, 2017
  14. Offline

    Caderape2

    @Scorpyon04 yea, the listener class need to be register of course
     
  15. Offline

    Scorpyon04

    ok thanks it worked
     
Thread Status:
Not open for further replies.

Share This Page