Tom
Tom

Reputation: 856

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

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.

Upvotes: 3

Views: 13993

Answers (3)

Alexandre Daubricourt
Alexandre Daubricourt

Reputation: 4933

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

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

Upvotes: 0

Mark Lalor
Mark Lalor

Reputation: 7907

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

Upvotes: 5

cringe
cringe

Reputation: 14010

There is the http://jd.bukkit.org/apidocs/org/bukkit/metadata/Metadatable.html where you can store your data in a key-value way. Looks like it already provides an interface to store/retrieve data.

Upvotes: 0

Related Questions