feat: 新增 getItemName 方法
This commit is contained in:
@@ -187,6 +187,60 @@ public final class CoreBukkitUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取物品名称
|
||||
* <p>
|
||||
* 当物品拥有 displayName 时,返回 displayName 的普通文本
|
||||
* <p>
|
||||
* 否则返回物品的材质名称
|
||||
*
|
||||
* @param stack 物品
|
||||
* @return 物品名称
|
||||
*/
|
||||
public static String getItemName(@Nullable ItemStack stack) {
|
||||
if (isEmptyItemStack(stack)) {
|
||||
return "AIR";
|
||||
}
|
||||
ItemMeta meta = stack.getItemMeta();
|
||||
if (meta != null) {
|
||||
if (meta.hasDisplayName()) {
|
||||
return meta.getDisplayName();
|
||||
}
|
||||
}
|
||||
return stack.getType().name();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取物品名称的 Component
|
||||
* <p>
|
||||
* 当物品拥有 displayName 时,返回 displayName 的普通文本
|
||||
* <p>
|
||||
* 否则返回翻译文本,以显示客户端的不同语言中物品的原版名称
|
||||
* <p>
|
||||
* 当物品为 null 时,返回 block.minecraft.air 翻译文本
|
||||
*
|
||||
* @param stack 物品
|
||||
* @return 物品名称的 Component
|
||||
*/
|
||||
@NotNull
|
||||
public static Component getItemNameComponent(@Nullable ItemStack stack) {
|
||||
if (isEmptyItemStack(stack)) {
|
||||
return Component.translatable("block.minecraft.air");
|
||||
}
|
||||
ItemMeta meta = stack.getItemMeta();
|
||||
if (meta != null) {
|
||||
if (meta.hasDisplayName()) {
|
||||
return Component.text(meta.getDisplayName());
|
||||
}
|
||||
}
|
||||
Material type = stack.getType();
|
||||
if (type.isBlock()) {
|
||||
return Component.translatable("block.minecraft." + type.name().toLowerCase());
|
||||
} else {
|
||||
return Component.translatable("item.minecraft." + type.name().toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 config 中加载 DisplayMessage 实例
|
||||
*
|
||||
@@ -270,36 +324,6 @@ public final class CoreBukkitUtils {
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取物品名称的 Component
|
||||
* <p>
|
||||
* 当物品拥有 displayName 时,返回 displayName 的普通文本
|
||||
* <p>
|
||||
* 否则返回翻译文本,以显示客户端的不同语言中物品的原版名称
|
||||
* <p>
|
||||
* 当物品为 null 时,返回 block.minecraft.air 翻译文本
|
||||
*
|
||||
* @param stack 物品
|
||||
* @return 物品名称的 Component
|
||||
*/
|
||||
public static Component getItemNameComponent(@Nullable ItemStack stack) {
|
||||
if (isEmptyItemStack(stack)) {
|
||||
return Component.translatable("block.minecraft.air");
|
||||
}
|
||||
ItemMeta meta = stack.getItemMeta();
|
||||
if (meta != null) {
|
||||
if (meta.hasDisplayName()) {
|
||||
return Component.text(meta.getDisplayName());
|
||||
}
|
||||
}
|
||||
Material type = stack.getType();
|
||||
if (type.isBlock()) {
|
||||
return Component.translatable("block.minecraft." + type.name().toLowerCase());
|
||||
} else {
|
||||
return Component.translatable("item.minecraft." + type.name().toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static YamlConfiguration getPluginConfig(@NotNull Plugin plugin, @NotNull String filename) {
|
||||
File file = new File(plugin.getDataFolder(), filename);
|
||||
|
Reference in New Issue
Block a user