GUI Triggered by right clicking a item!

Discussion in 'Spigot Plugin Development' started by DidactCraft, May 24, 2015.

  1. Hi, does anyone know a event/code to open a inventory when right clicking a compass/clock. I have made the inventory and so far I have to execute a command to open it. But I want it to be opennable by right clicking a compass/clock. I have made a ItemStack containing the display name of the clock and meta info like lore/enchantments. And players get this item stack when they join the game.

    But I want it to be so that when they right click the item it brings up the inventory I made. Anyone know how to do this?
     
  2. PlayerInteractEvent
     
  3. Ik this, but whats the code to check for if the player has right clicked the item and it opens the inventory for the player!
     
  4. Like this? Hope it helps!
    Code (Text):
    package com.norrd.Click;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class Listen implements Listener {

        MainClass plugin;
     
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent e) {
            if(e.getPlayer().getItemInHand().equals(Material.DIAMOND)) {
                Inventory inv = plugin.getServer().createInventory(null, 9, ChatColor.RED + "Inventory Name");
                ItemStack item = new ItemStack(Material.GOLD_BLOCK);
                ItemMeta im = item.getItemMeta();
                im.setDisplayName(ChatColor.RED + "Item Name");
                item.setItemMeta(im);
                inv.setItem(1, item);
                e.getPlayer().openInventory(inv);
            }
        }
    }
    You jsut need to change the item in hand to Material.CLOCK or Material.CLOCK or just change it to a variable
     
  5. You need to check the action, otherwise it will open when you left click
     
  6. Zeryther

    Supporter

    This code will throw NullPointerExceptions..
     
  7. Just one.

    @Norrd getItemInHand() will return null - and throw a NPE (duh), - if the player isn't holding anything.
     
  8. Surely however it only takes common sense to work that out....... I have never one something on it before so yeah sorry!
     
  9. Can you please explain what exactly this is supposed to be...
    First off, your instance variable is NULL. This will fail.
    Second, why are you even doing this?..

    Just use #Bukkit.().<method>
     
  10. How can I fix the error? It works fine when I use the named item but when I use the same item it throws and error..

    PS: Sorry for bumping this thread..
     
    #10 ExWhyZed, Sep 21, 2015
    Last edited: Sep 21, 2015