获取 ItemStack in bukkit 的"Custom id"



首先我想为我糟糕的英语道歉。所以,我正在开发一个经济插件,只是为了学习,在这个插件中,我正在创建一个标志商店。一切都很顺利,但当我在尝试我的商店时,我被迷住了物品,例如,因为我不能只获得id:

ItemStack item = player.getItemInHand();
item.getTypeId();

这只返回真实物品的id,没有结界。有人可以告诉我是否有一个方法,我可以设置一些自定义id,在物品的爱,例如,我可以使用以后做一个标识店?

编辑:

现在可以很好地保存项目数据,但仍然给出错误。我现在用的是:

public static void saveItem(Player player){
ItemStack item = player.getItemInHand();
//config.createSection("eco.sell.aFirstItem");
ConfigurationSection configToEdit = config.createSection("eco.sell.aFirstItem");
configToEdit.set("type", item.getType().name());
configToEdit.set("amount", item.getAmount());
if(item.hasItemMeta()){
ItemMeta meta = item.getItemMeta();
configToEdit.set("meta.name", meta.getDisplayName());
List<String> enchants = new ArrayList<>();
meta.getEnchants().forEach((en, lvl) -> enchants.add(en.getName() + ":" + lvl));
configToEdit.set("meta.enchant", enchants);
save();
}
}

所以,为了恢复数据,我使用:

public static void restoreData(Player player){
//config.getConfigurationSection("eco.sell.aFirstItem");
ConfigurationSection configToEdit = config.createSection("eco.sell.aFirstItem");
ItemStack item = new ItemStack(Material.valueOf(configToEdit.getString("type")), configToEdit.getInt("amount")); 
if(configToEdit.contains("meta")) {
player.sendMessage("Contains( Meta )");
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(configToEdit.getString("meta.name"));
List<String> enchants = configToEdit.getStringList("meta.enchants"); 
enchants.forEach(line -> {
String[] split = line.split(":"); 
item.addEnchantment(Enchantment.getByName(split[0]), Integer.parseInt(split[1]));
});
}
player.getInventory().addItem(item);
player.updateInventory();
}

你不能有一个唯一的ID。

但是有两种方法:

  1. 存储完整对象。例如,创建一个json,其中包含物品id,物品名称,魔咒…然后将其存储为您想要保存的id。

例如,我们将保存一个项目并恢复它。我们将使用spigot的默认配置。

保存项目

Configuration objConfig = plugin.getConfig().createSection("eco.sell.aFirstItem"); // create new section in plugin config
// now we will add all content from item to config
objConfig.set("type", item.getType().name()); // item type
objConfig.set("amount", item.getAmount()); // amount of item
if(item.hasMetadata()) {
ItemMeta meta = item.getItemMeta();
objConfig.set("meta.name", meta.getDisplayName()); // item name
List<String> enchants = new ArrayList<>(); // create hashmap that will be saved
meta.getEnchants().forEach((en, lvl) -> enchants.add(en.name() + ":" + lvl));
objConfig.set("meta.enchants", enchants); // all items enchants
}
plugin.saveConfig();
现在,我们将从配置中恢复数据:
Configuration objConfig = plugin.getConfig().getConfigurationSection("eco.sell.aFirstItem"); // retrieve section
ItemStack item = new ItemStack(Material.valueOf(objConfig.getString("type")), objConfig.getInt("amount")); // here we have an item that we can edit
if(objConfig.contains("meta")) {
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(objConfig.getString("meta.name"));
List<String> enchants = objConfig.getStringList("meta.enchants"); // get all enchants
enchants.forEach(line -> {
String[] split = line.split(":"); // split enchantName:enchantLevel
item.addEnchant(Enchantement.valueOf(split[0]), Integer.parse(split[1])); // get spigot objects
});
}

警告:少数行不能工作,因为我没有测试这段代码,但这就是它应该如何工作。

  1. 创建一个具有ID的项目列表,然后在需要的地方引用该ID。你只需要在两者之间建立链接(例如SQL数据库工作)。

如果你总是在销售相同的商品,第二种方法很有用。我认为就你的情况而言,第一个解决方案更好。

最新更新