3

好的,我正在尝试将数据附加到 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!!!!");
      }
    }
  }
}

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

4

3 回答 3

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!!!!");
          }
      }
  }
}
于 2013-01-21T02:01:22.310 回答
0

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

于 2013-01-15T13:04:03.727 回答
0

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

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

于 2017-01-23T19:04:03.820 回答