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

Tom picture Tom · Jan 15, 2013 · Viewed 13k times · Source

Ok, I am trying to attach data to a Minecraft Bukkit ItemStack. I would like it so the entity that it drops would also have it, but that is optional. If I can not do this directly, is there some other way I can keep a piece of data (java int, java string) with the item as it moves through players and their inventory slots? Thanks!

EDIT: Here's a code example.

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 whipped up this example but it does not work when I right click with the one special sword.

Answer

Mark Lalor picture Mark Lalor · Jan 21, 2013

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!!!!");
          }
      }
  }
}