InventoryClickEvent with item meta

Discussion in 'Spigot Plugin Development' started by DecisionsYT, May 26, 2016.

  1. Hey!

    So, I'm creating a plugin with an inventory (GUI)

    Obviously, I need an InventoryClickEvent

    So, what I'm going to see what Item they clicked, because all of the items in the GUI are the same (Empty Bottle) I need to check the meta.

    So whats the best way to check the meta. Because when doing
    Code (Text):
    if (e.getCurrentItem().getItemMeta() == speedpotmeta) {
    It wants me to create "speedpotmeta" even though it's in the command section of the plugin.
     
  2. If speedpotmeta is not a global variable, you can't access it from another method. So if you're defining it in onCommand(), you're onInventoryClick() method can't see it and won't be able to use it.
     
  3. Depends what you're doing with the item meta when you check it.
     
  4. create a item stack for the item e.g.

    Itemstack speedPot = new ItemStack(....)

    then on inventory click even you can do

    if(e.getCurrentItem() == speedPot){
    ./..
    }
     
  5. I know that. That's why I'm un to sure on how to fix it
     
  6. Show us some more code please?
     
  7. Code (Text):

        @EventHandler
          public void onClick(InventoryClickEvent e) {
           
            if ((e.getWhoClicked() instanceof Player)) {
               
              if (e.getCurrentItem().getItemMeta() == speedpotmeta) {
             
                 
                 
              }
            }
          }
             
                 
        Inventory inv = Bukkit.getServer().createInventory(null, getConfig().getInt("GUI-Slots"), ChatColor.translateAlternateColorCodes('&', getConfig().getString("GUI-Name")));
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] a) {
            if (cmd.getName().equalsIgnoreCase("Potions")) {
                Player player = (Player) sender;
               
            player.openInventory(inv);
               
               
            ItemStack awkwardpot = new ItemStack(Material.POTION, 1);
            ItemMeta awkwardpotmeta = awkwardpot.getItemMeta();
            awkwardpotmeta.setDisplayName("§3Awkward Potion");
            awkwardpotmeta.setLore(Arrays.asList(new String[] { "§bNo Effects" }));
            awkwardpot.setItemMeta(awkwardpotmeta);
           
            ItemStack regenpot = new ItemStack(Material.POTION, 1);
            ItemMeta regenpotmeta = regenpot.getItemMeta();
            regenpotmeta.setDisplayName("§3Regeneration Potion");
            regenpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Regeneration II" }));
            regenpotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            regenpot.setItemMeta(regenpotmeta);
           
            ItemStack speedpot = new ItemStack(Material.POTION, 1);
            ItemMeta speedpotmeta = speedpot.getItemMeta();
            speedpotmeta.setDisplayName("§3Speed Potion");
            speedpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Speed II" }));
            speedpotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            speedpot.setItemMeta(speedpotmeta);
           
            ItemStack firerespot = new ItemStack(Material.POTION, 1);
            ItemMeta fireresmeta = firerespot.getItemMeta();
            fireresmeta.setDisplayName("§3Fire Resistance Potion");
            fireresmeta.setLore(Arrays.asList(new String[] { "§bApplies Fire Resistance II" }));
            fireresmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            firerespot.setItemMeta(fireresmeta);
           
            ItemStack poisonpot = new ItemStack(Material.POTION, 1);
            ItemMeta poisonpotmeta = poisonpot.getItemMeta();
            poisonpotmeta.setDisplayName("§3Poison Potion");
            poisonpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Poison II" }));
            poisonpotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            poisonpot.setItemMeta(poisonpotmeta);
           
            ItemStack nightvisionpot = new ItemStack(Material.POTION, 1);
            ItemMeta nightvisionpotmeta = nightvisionpot.getItemMeta();
            nightvisionpotmeta.setDisplayName("§3NightVision Potion");
            nightvisionpotmeta.setLore(Arrays.asList(new String[] { "§bApplies NightVision I" }));
            nightvisionpotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            nightvisionpot.setItemMeta(nightvisionpotmeta);
           
            ItemStack weaknesspot = new ItemStack(Material.POTION, 1);
            ItemMeta weaknesspotmeta = weaknesspot.getItemMeta();
            weaknesspotmeta.setDisplayName("§3Weakness Potion");
            weaknesspotmeta.setLore(Arrays.asList(new String[] { "§bApplies Weakness I" }));
            weaknesspotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            weaknesspot.setItemMeta(weaknesspotmeta);
           
            ItemStack strengthpot = new ItemStack(Material.POTION, 1);
            ItemMeta strengthpotmeta = strengthpot.getItemMeta();
            strengthpotmeta.setDisplayName("§3Strength Potion");
            strengthpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Strength II" }));
            strengthpotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            strengthpot.setItemMeta(strengthpotmeta);
           
            ItemStack slownesspot = new ItemStack(Material.POTION, 1);
            ItemMeta slownesspotmeta = slownesspot.getItemMeta();
            slownesspotmeta.setDisplayName("§3Slowness Potion");
            slownesspotmeta.setLore(Arrays.asList(new String[] { "§bApplies Slowness I" }));
            slownesspotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            slownesspot.setItemMeta(slownesspotmeta);
           
            ItemStack jumpboostpot = new ItemStack(Material.POTION, 1);
            ItemMeta jumpboostpotmeta = jumpboostpot.getItemMeta();
            jumpboostpotmeta.setDisplayName("§3JumpBoost Potion");
            jumpboostpotmeta.setLore(Arrays.asList(new String[] { "§bApplies JumpBoost II" }));
            jumpboostpotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            jumpboostpot.setItemMeta(jumpboostpotmeta);
           
            ItemStack waterbreathingpot = new ItemStack(Material.POTION, 1);
            ItemMeta waterbreathingpotmeta = waterbreathingpot.getItemMeta();
            waterbreathingpotmeta.setDisplayName("§3Water Breathing Potion");
            waterbreathingpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Water Breathing I" }));
            waterbreathingpotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            waterbreathingpot.setItemMeta(waterbreathingpotmeta);
           
            ItemStack invispot = new ItemStack(Material.POTION, 1);
            ItemMeta invispotmeta = invispot.getItemMeta();
            invispotmeta.setDisplayName("§3Invisibility Potion");
            invispotmeta.setLore(Arrays.asList(new String[] { "§bApplies Invisibility I" }));
            invispotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            invispot.setItemMeta(invispotmeta);
           
            ItemStack clear = new ItemStack(Material.BARRIER, 1);
            ItemMeta clearmeta = clear.getItemMeta();
            clearmeta.setDisplayName("§3Clear Potion Effects");
            clearmeta.setLore(Arrays.asList(new String[] { "§bClears All Effects" }));
            clear.setItemMeta(clearmeta);
           
           
              inv.setItem(1, awkwardpot);
              inv.setItem(3, regenpot);
              inv.setItem(5, speedpot);
              inv.setItem(7, firerespot);
              inv.setItem(9, poisonpot);
              inv.setItem(11, nightvisionpot);
              inv.setItem(13, clear);
              inv.setItem(15, weaknesspot);
              inv.setItem(17, strengthpot);
              inv.setItem(19, slownesspot);
              inv.setItem(21, jumpboostpot);
              inv.setItem(23, waterbreathingpot);
              inv.setItem(25, invispot);
           
            }
            return true;
        }
    }
     
     
  8. I've been doing that.

    Code (Text):
    ItemStack speedpot = new ItemStack(Material.POTION, 1);
            ItemMeta speedpotmeta = speedpot.getItemMeta();
            speedpotmeta.setDisplayName("§3Speed Potion");
            speedpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Speed II" }));
            speedpotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
            speedpot.setItemMeta(speedpotmeta);
     
  9. Looking at it like you have your code right now, you would probably have to define the speedpot itemstack again in your listener. There are so many things that can be done better in your code though.
     
  10. You're generating those same items every time someone executes the command. Why? Declare the ItemStacks as fields, use them as necessary in your command class. Why are you using == instead of #equals? Your basic Java seems to be flawed, I'd go back and revisit the basics and or learn them if you haven't already before trying to continue with the Spigot API.
     
    • Agree Agree x 1
  11. You could make those global variables, your code will still work.
     
  12. @Msrules123 is right. Also things like these contain redundant code:
    Code (Text):
            speedpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Speed II" }));
            speedpotmeta.setLore(Arrays.asList(new String[] { "§bLasts For 5 Minutes" }));
    Here you could just do this:
    Code (Text):
    speedpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Speed II", "Second line" }));
    In the first snippet you would only get the second line in the lore as you are overwriting it the second time.
     
  13. If they are all the same items then u can just use getSlot() to see what item they clicked, so if(event.getSlot() == 3)
    //do stuff
     
  14. :eek:
     
  15. You don't even need the new array. You could use
    Code (Text):

    Arrays.asList("Something here", "Something else");
     
     
  16. create a method ?

    Code (Java):
    public ItemMeta speedpotmeta(ItemStack item) {
      return item.getItemMeta();
    }
     
    • Optimistic Optimistic x 1
  17. Okay so.

    I changed some stuff about and this doesn't even work.

    Code (Text):
    @EventHandler
             public void onInvClick(InventoryClickEvent e) {
                 Player player = (Player) e.getWhoClicked();
               
                 if (e.getCurrentItem().getType() == Material.BARRIER) {
                     player.closeInventory();
                     player.sendMessage("Hi");
                   
                 }
             }
    FULL CODE:

    Code (Text):
    package me.DecisionsYT;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
       
        ItemStack clear = new ItemStack(Material.BARRIER, 1);{
        ItemMeta clearmeta = clear.getItemMeta();
        clearmeta.setDisplayName("§3Clear Potion Effects");
        clearmeta.setLore(Arrays.asList(new String[] { "§bClears All Effects" }));
        clear.setItemMeta(clearmeta);
        }
       
       
        public void onEnable() {
            saveDefaultConfig();
        }
       
        public void onDisable() {
           
        }
       
             @EventHandler
             public void onInvClick(InventoryClickEvent e) {
                 Player player = (Player) e.getWhoClicked();
                 
                 if (e.getCurrentItem().getType() == Material.BARRIER) {
                     player.closeInventory();
                     player.sendMessage("Hi");
                     
                 }
             }
             
             
             
                 
        Inventory inv = Bukkit.getServer().createInventory(null, getConfig().getInt("GUI-Slots"), ChatColor.translateAlternateColorCodes('&', getConfig().getString("GUI-Name")));
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] a) {
            if (cmd.getName().equalsIgnoreCase("Potions")) {
                Player player = (Player) sender;
               
            player.openInventory(inv);
               
               
            ItemStack awkwardpot = new ItemStack(Material.POTION, 1);
            ItemMeta awkwardpotmeta = awkwardpot.getItemMeta();
            awkwardpotmeta.setDisplayName("§3Awkward Potion");
            awkwardpotmeta.setLore(Arrays.asList(new String[] { "§bNo Effects" }));
            awkwardpot.setItemMeta(awkwardpotmeta);
           
            ItemStack regenpot = new ItemStack(Material.POTION, 1);
            ItemMeta regenpotmeta = regenpot.getItemMeta();
            regenpotmeta.setDisplayName("§3Regeneration Potion");
            regenpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Regeneration II", "§bLasts For 5 Minutes" }));
            regenpot.setItemMeta(regenpotmeta);
           
            ItemStack speedpot = new ItemStack(Material.POTION, 1);
            ItemMeta speedpotmeta = speedpot.getItemMeta();
            speedpotmeta.setDisplayName("§3Speed Potion");
            speedpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Speed II", "§bLasts For 5 Minutes" }));
            speedpot.setItemMeta(speedpotmeta);
           
            ItemStack firerespot = new ItemStack(Material.POTION, 1);
            ItemMeta fireresmeta = firerespot.getItemMeta();
            fireresmeta.setDisplayName("§3Fire Resistance Potion");
            fireresmeta.setLore(Arrays.asList(new String[] { "§bApplies Fire Resistance II", "§bLasts For 5 Minutes" }));
            firerespot.setItemMeta(fireresmeta);
           
            ItemStack poisonpot = new ItemStack(Material.POTION, 1);
            ItemMeta poisonpotmeta = poisonpot.getItemMeta();
            poisonpotmeta.setDisplayName("§3Poison Potion");
            poisonpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Poison II", "§bLasts For 5 Minutes" }));
            poisonpot.setItemMeta(poisonpotmeta);
           
            ItemStack nightvisionpot = new ItemStack(Material.POTION, 1);
            ItemMeta nightvisionpotmeta = nightvisionpot.getItemMeta();
            nightvisionpotmeta.setDisplayName("§3NightVision Potion");
            nightvisionpotmeta.setLore(Arrays.asList(new String[] { "§bApplies NightVision I", "§bLasts For 5 Minutes" }));
            nightvisionpot.setItemMeta(nightvisionpotmeta);
           
            ItemStack weaknesspot = new ItemStack(Material.POTION, 1);
            ItemMeta weaknesspotmeta = weaknesspot.getItemMeta();
            weaknesspotmeta.setDisplayName("§3Weakness Potion");
            weaknesspotmeta.setLore(Arrays.asList(new String[] { "§bApplies Weakness I", "§bLasts For 5 Minutes" }));
            weaknesspot.setItemMeta(weaknesspotmeta);
           
            ItemStack strengthpot = new ItemStack(Material.POTION, 1);
            ItemMeta strengthpotmeta = strengthpot.getItemMeta();
            strengthpotmeta.setDisplayName("§3Strength Potion");
            strengthpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Strength II", "§bLasts For 5 Minutes" }));
            strengthpot.setItemMeta(strengthpotmeta);
           
            ItemStack slownesspot = new ItemStack(Material.POTION, 1);
            ItemMeta slownesspotmeta = slownesspot.getItemMeta();
            slownesspotmeta.setDisplayName("§3Slowness Potion");
            slownesspotmeta.setLore(Arrays.asList(new String[] { "§bApplies Slowness I", "§bLasts For 5 Minutes" }));
            slownesspot.setItemMeta(slownesspotmeta);
           
            ItemStack jumpboostpot = new ItemStack(Material.POTION, 1);
            ItemMeta jumpboostpotmeta = jumpboostpot.getItemMeta();
            jumpboostpotmeta.setDisplayName("§3JumpBoost Potion");
            jumpboostpotmeta.setLore(Arrays.asList(new String[] { "§bApplies JumpBoost II", "§bLasts For 5 Minutes" }));
            jumpboostpot.setItemMeta(jumpboostpotmeta);
           
            ItemStack waterbreathingpot = new ItemStack(Material.POTION, 1);
            ItemMeta waterbreathingpotmeta = waterbreathingpot.getItemMeta();
            waterbreathingpotmeta.setDisplayName("§3Water Breathing Potion");
            waterbreathingpotmeta.setLore(Arrays.asList(new String[] { "§bApplies Water Breathing I", "§bLasts For 5 Minutes" }));
            waterbreathingpot.setItemMeta(waterbreathingpotmeta);
           
            ItemStack invispot = new ItemStack(Material.POTION, 1);
            ItemMeta invispotmeta = invispot.getItemMeta();
            invispotmeta.setDisplayName("§3Invisibility Potion");
            invispotmeta.setLore(Arrays.asList(new String[] { "§bApplies Invisibility I", "§bLasts For 5 Minutes" }));
            invispot.setItemMeta(invispotmeta);
           
           
              inv.setItem(1, awkwardpot);
              inv.setItem(3, regenpot);
              inv.setItem(5, speedpot);
              inv.setItem(7, firerespot);
              inv.setItem(9, poisonpot);
              inv.setItem(11, nightvisionpot);
              inv.setItem(13, clear);
              inv.setItem(15, weaknesspot);
              inv.setItem(17, strengthpot);
              inv.setItem(19, slownesspot);
              inv.setItem(21, jumpboostpot);
              inv.setItem(23, waterbreathingpot);
              inv.setItem(25, invispot);
           
            }
            return true;
        }
    }
     
     
  18. You did not register the events
     
  19. You need to register your listener class or the events will never be passed through the methods.
     
  20. Wow.. I'm so stupid


    Sent from my iPod touch using Tapatalk