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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14338147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:55:23  来源:igfitidea点击:

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

javamoduleminecraftbukkit

提问by Tom

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!

好的,我正在尝试将数据附加到 Minecraft Bukkit ItemStack。我想要它,所以它掉落的实体也会拥有它,但这是可选的。如果我不能直接执行此操作,是否有其他方法可以在该项目通过玩家及其库存槽时保留一条数据(java int、java string)?谢谢!

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.

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

回答by Mark Lalor

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

我会使用 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!!!!");
          }
      }
  }
}

回答by cringe

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

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

回答by Alexandre Daubricourt

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

如果你想存储不可见的数据(而不是传说),只需使用metadata

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

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