Solved player.openInventory() ?

Discussion in 'Plugin Development' started by ItsMas_, Jan 2, 2016.

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

    ItsMas_

    I am making a plugin, and I tried to make an inventory menu, and I was told to use player.openInventory(inventory) , but that does not seem to work, and the code is not recognised as something that can be done?

    Is there a different way I can make a player open an inventory?
     
  2. Offline

    Ruptur

    @ItsMas_
    Are you sure because it does work.
    What are you trying to do, post a snippet of what you have atm
     
  3. Offline

    mine-care

    Answering your question, yes there is but it involves quite a bit of NMS and is pointless since it is what is done more or less by openInventory(Inventory param).

    Now, in order to solve your overall problem, can you please show us your code that isnt working and we can spend some time on it trying to identify what is wrong with it.

    You mean there is an error?
     
  4. Offline

    Gorbit99

    This is the skeleton of an inventory creation:
    Code:
    Inventory inv = Bukkit.CreateInventory("Args here");
    //Set the items here if you want
    Player#openInventory(inv);
    Are you sure you are doing something like this?
     
  5. Offline

    ItsMas_

    EDIT: I have managed to fix it, but there is still an error:
    For some reason this code isn't working:
    Code:
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e){
    Player clicker = (Player) e.getWhoClicked();
    ItemStack clicked = e.getCurrentItem();
    Inventory inventory = e.getInventory();
    if (inventory.getName().equals(myInventory.getName())){
         if (clicked.getType() == Material.BARRIER){
             Bukkit.broadcastMessage(ChatColor.RED + "Chat was silenced by " + clicker.getName());
             e.setCancelled(true);
             clicker.closeInventory();
             }
         }
    }
    
    When I click on the barrier, nothing happens, and it lets me take it from the inventory. It seems like that part of the code is not activating
     
    Last edited: Jan 2, 2016
  6. Offline

    Gorbit99

    I don't think the player's inventory has a title (btw, use Inventory#getTitle() instead). Tried using if (e.getInventory() instanceof PlayerInventory)?

    Edit: misunderstood your problem
    If you opened an inventory before, try using clicker.getOpenInventory().getTopInventory() or something like that
     
  7. Offline

    ItsMas_

    There is a new problem, please read my post above :)
     
    Last edited: Jan 2, 2016
  8. Offline

    Gorbit99

    Have you tried using getTitle instead?
     
  9. Offline

    ItsMas_

    No, how would I use that?

    Also what did I do wrong in my code two posts ago that is stopping it from working?
     
  10. Offline

    Gorbit99

    getTitle returns the name of the inventory, so change getName to getTitle
     
  11. Offline

    ItsMas_

    Okay, I will try it, thanks
     
  12. Offline

    Gorbit99

    Also, try debugging by writing a sysout after each line with 1,2,3,4,5... incrementing by 1 with each sysout and then check, where the code fails.
    BTW: you don't do a null check, so if you click on an empty slot, you'll get an error
     
  13. Offline

    ItsMas_

    Added a null check, and changed to getTitle(), still didn't work.

    I did a sysout debug, and it didn't print anything out. The only conclusion could be that the onInventoryClick event is not being recognised when I click on the item.
     
  14. Offline

    timtower Administrator Administrator Moderator

    @ItsMas_ Did you register the event?
     
  15. Offline

    boomboompower

    Code:
    if (inventory.getName().equals(myInventory.getName())) {
         e.setCancelled(true);
         if (clicked.getType() == Material.BARRIER){
             Bukkit.broadcastMessage(ChatColor.RED + "Chat was silenced by " + clicker.getName());
             clicker.closeInventory();
             }
         }
    }
    @ItsMas_ This isn't to fix it, just a better way to cancel the event of taking the item from the inventory.
     
  16. Offline

    mine-care

    Why not inventory.equals(myInventory) instead?
     
  17. Offline

    ItsMas_

    Solved. I stupidly forgot to register the event. Thanks for all your help everyone.
     
Thread Status:
Not open for further replies.

Share This Page