1.15.2 Open GUI on right click item

Discussion in 'Spigot Plugin Development' started by seniorjerry, Mar 25, 2020.

  1. Hey, I'm trying to make basically a backpack plugin and want a GUI to open when a player right-clicks a specific item. After testing I can get a GUI to show up when I a player types in a command but not when I'm right-clicking an item. What am I doing wrong? After exporting the code I hold an item name Small Backpack and right click but nothing happens.

    Code (Java):

     @EventHandler
        public void openBackpack(PlayerInteractEvent e)
        {
                if(e.getAction().equals(Action.RIGHT_CLICK_AIR) ||  e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
                {
                    if( e.getPlayer().getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals("Small Backpack"))
                    {
                        Player p = e.getPlayer();
                        Inventory gui = Bukkit.createInventory(p, 9, "Small Backpack");
                        ItemStack item1 = new ItemStack(Material.RED_SAND, 1);
                        ItemMeta item1_meta = item1.getItemMeta();
                        item1_meta.setDisplayName("Test");
                        item1.setItemMeta(item1_meta);
                   
                        gui.addItem(item1);
                        p.openInventory(gui);
                    }
                }
            }
     
  2. I would use e.getItem() to determine which item the player is right clicking. If thay doesn't work, double check whether the name is really " Small backpack" or if there are things like colours.

    Edit: You might want to replace .equals with ==.
     
  3. This will not fix his issue.

    The code works fine, with major issues, but if it's called "Small Backpack" it works perfectly fine. I advise you to add a check to make sure the item != null so it doesn't spam console with an error. I also suggest not creating an inventory every time someone opens a "backpack" but I assume it's just for testing right now.
     
  4. Just tried that it didn't work. When I put the code to create the GUI outside the check item if statement it creates a GUI whenever I right click so I know that part works. However inside the if statement nothing happens when I right click with a shulker shell named Small Backpack
     
  5. Fixed the issue by changing .equals to .equalsIgnoreCase
     
    • Winner Winner x 1
  6. Did you implement anything I said? You will be having errors if you haven't.