1.15.2 getting the title of a inventory

Discussion in 'Spigot Plugin Development' started by Phoenix852, Mar 26, 2020 at 4:36 PM.

  1. I wanted to create custom inventory GUIs. I made it but... There is always a idiot problem out there, I am trying to close the inventory as soon as the player clicks an item and also want to set it to cancelled, I know how to do this but when I do setCancelled(true); it also blocks players to move items on their own inventory. I searched the docs but found no information. Thanks for the help.
     
  2. Close the inventory in the InventoryClickEvent delayed by one tick using a BukkitScheduler/BukkitRunnable.
     
  3. Use InventoryView#getTitle(). You can get a handle to the view using InventoryEvent#getView()
     
    • Agree Agree x 2
  4. Try
    Code (Java):
    if(event.getRawSlot() < yourCustomInventorySize) event.setCancelled(true);
    That way only clicks inside your custom inventory are cancelled.
     
    • Informative Informative x 1
  5. Code (Java):


    Player p = (Player) e.getWhoClicker();

    if(!e.getInventory().getName().contains("YourTitle")) return;
    if(e.getCurrentItem().getType() == Material.AIR || e.getCurrentItem().getType() == null) return;
    int slot = e.getRawSlot();

    if(slot < YourSlot && slot > -1 && e.getInventory().getName() == "YourTitle")
    {
    //your code;
    }else
    {
    e.setCancelled(true);
    }
     
     
  6. getTitle() method doesn't exists. It was before but it doesn't exist anymore.
     
    • Optimistic Optimistic x 1
  7. It seems like getInventory().getName() doesn't exist. I guess e.getInventory().getType().name() will work but I am confused as there are two methods name() and getDefaultTitle(). I am confused on which one to use.
     
  8. What's wrong with e.getView().getTitle()?
     
  9. getView() is not a method :(
     
    • Optimistic Optimistic x 1
  10. What event are you using?
     
  11. InventoryClickEvent, I am listening to Inventory clicks.
     
  12. md_5

    Administrator Developer

  13. Assuming you upgraded from 1.8 to 1.15, then indeed the getTitle() method on inventories was removed.
    Also assuming you are really using the 1.15 API, and you are working with any kind of inventory event, then to 100% there is a method getView(), since all of them inherit it from the super interface InventoryEvent as mentioned by @md_5 .

    Do not confuse inventory.getType().getDefaultTitle() with the actual title of an inventory, as this will only give you the default title defined in the enum of the InventoryType that the inventory was created with ("Chest" or "Furnace" for example). Same goes for the inventory.getType().name(), you will only get the literal name of the InventoryType enum ("CHEST" or "FURNACE" for example).

    You would only have some struggle to get the title without having access to the inventory view and only handling a raw inventory, which (except for me again) most people never do anyways.
     
  14. md_5

    Administrator Developer

    Which is a subclass of InventoryEvent