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.
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.
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){ ./.. }
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; } }
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);
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.
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.
@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.
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
You don't even need the new array. You could use Code (Text): Arrays.asList("Something here", "Something else");
create a method ? Code (Java): public ItemMeta speedpotmeta(ItemStack item) { return item.getItemMeta(); }
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; } }