有没有办法将数据附加到Bukkit ItemStack?

时间:2013-01-15 12:50:22

标签: java module minecraft bukkit

好的,我正在尝试将数据附加到Minecraft Bukkit ItemStack。我想它,所以它丢弃的实体也会有它,但这是可选的。如果我不能直接这样做,是否有其他方式我可以保留一个数据(java int,java string)与项目,因为它通过玩家和他们的库存插槽?谢谢!

编辑:这是一个代码示例。

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

我掀起了这个例子,但是当我用一把特殊的剑右击时它不起作用。

3 个答案:

答案 0 :(得分:5)

我会使用ItemStack.getItemMeta()来设置传说值:

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

答案 1 :(得分:0)

您可以使用http://jd.bukkit.org/apidocs/org/bukkit/metadata/Metadatable.html以键值方式存储数据。看起来它已经提供了存储/检索数据的接口。

答案 2 :(得分:0)

如果你想存储不可见数据(传说绝杀),只需使用gradle.properties

来自Samer Alsayegh的好文章 - &gt; http://sameralsayegh.com/how-to-use-metadata/