Solved Check if a specific inventory is closed

Discussion in 'Plugin Development' started by max12345, Jan 25, 2018.

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

    max12345

    Hi there,

    I tried this with InventoryCloseEvent, but that's for ALL inventories.

    So: How can i check if it's the inventory inv and not the normal player inventory?

    Thanks!
     
  2. Offline

    Zombie_Striker

    @max12345
    You can try:
    1. Checking if the name of the inventory that was closed is equal to the player's inventory.
    2. Checking if the amount of inventory slots for the inventory is not divisible by 9 (since there are 4 extra slots for armor)
    3. Checking if the inventory instance is an instanceof PlayerInventory
    4. You can check if the inventory is the same instance as the player's inventory.
     
  3. Offline

    ipodtouch0218

    Wouldn't the player's inventory have +5 slots now because of the offhand?
     
  4. Offline

    max12345

    Do you have a code example for this?

    I've tried this:

    @EventHandler
    public void onInventoryClose(InventoryCloseEvent e) {
    Player p = (Player) e.getPlayer();
    if(!(instanceinventory instanceof Player)) {

    }

    But thats of course not working :(
     
  5. Offline

    Machine Maker

    Well this line is wrong. Zombie_Striker said to check of the inventory is an instance of PlayerInventory.

    Code:
    if (e.getInventory() instanceof PlayerInventory)
     
    max12345 likes this.
  6. Offline

    max12345

    Hi,
    I've tried this but it spams the console (https://pastebin.com/ndemNvEt) when i close the teleporter and when i open my normal player inventory and close this, he opens the teleporter...

    Here's the code again:
    @EventHandler
    public void onInventoryClose(InventoryCloseEvent e) {
    Player p = (Player) e.getPlayer();
    if(!(e.getInventory() instanceof Player)) {

    NavigatorItems.NavItems(p);

    }
     
  7. Offline

    Machine Maker

    So again e.getInventory() is never an instance of Player. It will be however be an instance of PlayerInventory if the inventory being closed is a players.
     
  8. Offline

    max12345

    When I understood it right: Whats "better" than e.getInventory?
     
  9. Offline

    Jan108

    e.getInventory() is ok when you check for a PlayerInventory.
    If you want to check for a Player, you should use e.getInventory().getHolder().
     
  10. Offline

    Machine Maker

    @Jan108 ‘s suggestion works too. You can either check if the holder is a player or if the inventory is a PlayerInventory.
     
  11. Offline

    Colinus999

    I would recommend this:
    Code:
    InventoryView view = e.getView();
    Inventory top = e.getTop(), // Should be the open Inventory or null
        bottom = e.getBottom(); // Should be the Player’s Inventory but better check if it is instanceof PlayerInventory
    InventoryView works like this:
    ——————————
    Top Inventory
    ——————————
    Bottom Inventory
    ——————————
     
  12. Offline

    max12345

    So now i check for instance of PlayerInventory but when i close the PlayerInventory it opens the teleporter.

    Code:
    @EventHandler
    public void onInventoryClose(InventoryCloseEvent e) {
    Player p = (Player) e.getPlayer();
    if(!(e.getInventory() instanceof PlayerInventory)) {

    NavigatorItems.NavItems(p);

    }


    }
     
  13. Guys why don't you use the identifier of the GUI themself.
    if you create a gui you must fill in 3 parameters: (holder) (size) (name).
    Code:
    Bukkit.createInventory(InventoryId.getInstance(), 54, "gui");
    And if you create a custom inventory holder you can identify it by everything.
    That works fine for me :)

    preview:

    Code:
    this.inv = Bukkit.createInventory(InventoryId.getInstance(), 54, Chat.color("&9&l" + player.getName() + "'s &d&lItems"));
    Code:
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.InventoryHolder;
    
    public class InventoryId implements InventoryHolder {
    
        public static InventoryId instance;
    
    
        public static InventoryId getInstance() {
            if(instance == null) {
                instance = new InventoryId();
            }
            return instance;
        }
    
    
        @Override
        public Inventory getInventory() {
            return null;
        }
    }
    Code:
        @EventHandler
        public void inv(InventoryCloseEvent e) {
            if(e.getInventory().getHolder() == InventoryId.getInstance()) {
                Bukkit.broadcastMessage("hey");
            }
        }
    You need to make the instance static because the system will compare the instances and if you create a gui with that existing instance it will always return true on compare.
     
    Last edited: Jan 28, 2018
    max12345 likes this.
  14. Offline

    max12345

    THANKS!
     
  15. No problem :)
     
Thread Status:
Not open for further replies.

Share This Page