Is there a way to bind data to a Bukkit ItemStack?

Ok, I'm trying to attach data to Minecraft Bukkit ItemStack. I would like the object that it fell to also have it, but this is not necessary. If I cannot do this directly, is there any other way I can store a piece of data (java int, java string) with an element when moving it around the players and their inventory slots? Thanks!

EDIT: Here is a sample code.

package path.to.the.package; import org.bukkit.event.*; import org.bukkit.event.PlayerInteractEvent; import org.bukkit.plugin.java.JavaPlugin; public ExamplePlugin extends JavaPlugin { public List<ItemStack> stacks = new ArrayList<ItemStack>(); public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if(cmd.getName().equalsIgnoreCase("tester123")) { ItemStack stack = new ItemStack(272, 0, (byte)0); Player p = (Player)sender; stacks.add(stack); p.getLocation().getWorld().dropItem(player.getLocation(), stack); } return true; } @EventHandler(priority = EventPriority.HIGHEST) public void onItemStackRightClick(PlayerInteractEvent e) { Player player = e.getPlayer(); for(ItemStack item : items) { if(player.getItemInHand() == item) { //What I DO want is something like: if(item.getPluginData(this, "KEY") == "SPECIAL") //And I would have set it like: item.setPluginData(this, "KEY", "SPECIAL"); player.sendMessage("You got one of our SPECIAL stone swords!!!!"); } } } } 

I weighed this example, but it does not work when I right-click one special sword.

+4
source share
3 answers

I would use ItemStack.getItemMeta () to set the lore value:

 import java.util.ArrayList; import java.util.List; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.*; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.java.JavaPlugin; public class ExamplePlugin extends JavaPlugin { public List<ItemStack> stacks = new ArrayList<ItemStack>(); public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if(cmd.getName().equalsIgnoreCase("tester123")) { ItemStack stack = new ItemStack(272, 0, (byte)0); Player p = (Player)sender; stacks.add(stack); ItemMeta i = stack.getItemMeta(); List<String> lore = new ArrayList<String>(); lore.add("Special"); i.setLore(lore); p.getLocation().getWorld().dropItem(((Player)sender).getLocation(), stack); } return true; } @EventHandler(priority = EventPriority.HIGHEST) public void onItemStackRightClick(PlayerInteractEvent e) { Player player = e.getPlayer(); if(player.getItemInHand().getItemMeta().hasLore()) { if (player.getItemInHand().getItemMeta().getLore().get(0).equals("Special")) { player.sendMessage("You got one of our SPECIAL stone swords!!!!"); } } } } 
+5
source

There is http://jd.bukkit.org/apidocs/org/bukkit/metadata/Metadatable.html where you can store your data with a key. It looks like it already provides an interface for storing / retrieving data.

0
source

If you want to preserve the invisible data (insteed of lore), just use metadata :

Good article from Samer Alsayegh → http://sameralsayegh.com/how-to-use-metadata/

0
source

All Articles