本帖最后由 mohuang521 于 2019-5-21 15:31 编辑
public static ItemStack StrengthenWeapon(ItemStack weapon) {
String weaponName = weapon.getItemMeta().getDisplayName();
System.err.println(weaponName.substring(weaponName.length()-2,weaponName.lastIndexOf("]")));
String prefix = weaponName.substring(0,weaponName.length()-2);
Integer count = Integer.parseInt(weaponName.substring(weaponName.length()-2,weaponName.lastIndexOf("]")));
System.err.println(count);
count = count+1;
String newName = prefix+count+"]";
System.err.println(newName);
weapon.getItemMeta().setDisplayName(newName);
List<String> weaponLore = new ArrayList<>();
for(String lore : weapon.getItemMeta().getLore()) {
//改变武器lore
weaponLore.add(RegexUtil.weaponUtil(lore));
}
weapon.getItemMeta().setLore(weaponLore);
System.err.println(weapon.getItemMeta().getDisplayName());
return weapon;
}
这是代码 请问为啥我明明重新用setDisplayName设置了物品的新名字,但是return之前的输出发现其实名字根本没有更改
看文档说getItemMeta是获取物品副本,但是想直接修改原本物品的数据值应该如何操作
public static ItemStack StrengthenWeapon(ItemStack weapon) {
String weaponName = weapon.getItemMeta().getDisplayName();
System.err.println(weaponName.substring(weaponName.length()-2,weaponName.lastIndexOf("]")));
String prefix = weaponName.substring(0,weaponName.length()-2);
Integer count = Integer.parseInt(weaponName.substring(weaponName.length()-2,weaponName.lastIndexOf("]")));
System.err.println(count);
count = count+1;
String newName = prefix+count+"]";
System.err.println(newName);
weapon.getItemMeta().setDisplayName(newName);
List<String> weaponLore = new ArrayList<>();
for(String lore : weapon.getItemMeta().getLore()) {
//改变武器lore
weaponLore.add(RegexUtil.weaponUtil(lore));
}
weapon.getItemMeta().setLore(weaponLore);
System.err.println(weapon.getItemMeta().getDisplayName());
return weapon;
}
这是代码 请问为啥我明明重新用setDisplayName设置了物品的新名字,但是return之前的输出发现其实名字根本没有更改
看文档说getItemMeta是获取物品副本,但是想直接修改原本物品的数据值应该如何操作
修改完 ItemMeta 需要重新设置到 ItemStack 物品栈对象中。
复制代码
- ItemMeta meta = weapon.getItemMeta(); // 先获取物品属性
- meta.setDisplayName("名称"); // 设置物品名
- meta.setLore(Arrays.asList("标签")); // 设置标签
- meta.... // 设置其他属性
- weapon.setItemMeta(meta); // 最后再设置到物品栈中即可