Solved How to disable taking Items from ChestInventory

Discussion in 'Plugin Development' started by OverDodo, Jan 27, 2017.

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

    OverDodo

    Hello,
    I just started creating an Admin-Tool by using a ChestInventory.
    My Problem is: I can´t add functions to the Items, I added to the Inventory and you can take the Items out of the "Chest".

    Here is my Code for Creating and adding an Item. What do I have to add?

    Inventory inv = p.getServer().createInventory(null, 27, "AdminTool");
    inv.setItem(0, new ItemStack(Material.APPLE));
     
  2. Offline

    JanTuck

    You would have to listen on the Event called InventoryClickEvent

    Sendt fra min ALE-L21 med Tapatalk
     
  3. Offline

    OverDodo

    Yes, I already tried that. But I don´t know how to only cancel the event, if the ChestInventory with the Name "AdminTool" is open. Can you give me an explanation for that?
     
  4. Offline

    OTF Catastrophe

    PHP:
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e)
    {

        if (
    e.getInventory().getDisplayName().contains("AdminTools"))
        {

            
    e.setCancelled(true);

        }

    ]
    Something like this I believe would cancel the click event with the AdminTools gui. Check for the item that the player is clicking and get it's display name, type, id, or even it's lore and if it's equal to that, then have the player do what you want.

    EDIT: Replaced .equals with .contains because it's much easier to check for names that way.
     
  5. Offline

    OverDodo

    Ah I got it now. It works now. But could you guys tell me, how I can let it check, which Item the Player Clicks? My Tool still has no function, because I don´t get it to work
     
    Last edited: Jan 27, 2017
  6. Offline

    OTF Catastrophe

    PHP:
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e)
    {

        if (
    e.getInventory().getDisplayName().contains("AdminTools"))
        {

            
    e.setCancelled(true);

            if (
    e.getCurrentItem().getItemMeta().getDisplayName().contains("InsertItemName"))
            {

                
    //This is where you put what the player does

            
    }

        }

    ]
    This checks for the displayname of the item that you clicked. Theres other methods of this like getting the type or type id of the item being clicked but most GUI plugins have custom names for the items that the GUI has in it so this is the one I use 99% of the time.
     
  7. Offline

    JanTuck

    @OverDodo
    If you are using @OTF Catastrophe's way you should add some check onto it.

    Check if item is null, then check if item has ItemMeta, and then check if the ItemMeta has a displayname.
     
Thread Status:
Not open for further replies.

Share This Page