From cd3ec3dbfaabee2088b3ee900b2bc90b90639d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Fri, 12 Jul 2019 00:33:37 +0800 Subject: [PATCH] Update Items.java --- .../io/izzel/taboolib/util/item/Items.java | 67 +++++++++++++------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/src/main/scala/io/izzel/taboolib/util/item/Items.java b/src/main/scala/io/izzel/taboolib/util/item/Items.java index 1a3a444..aa305c8 100644 --- a/src/main/scala/io/izzel/taboolib/util/item/Items.java +++ b/src/main/scala/io/izzel/taboolib/util/item/Items.java @@ -1,8 +1,9 @@ package io.izzel.taboolib.util.item; +import com.google.common.collect.ImmutableMap; import io.izzel.taboolib.Version; -import io.izzel.taboolib.module.locale.TLocale; import io.izzel.taboolib.module.lite.SimpleI18n; +import io.izzel.taboolib.module.locale.TLocale; import io.izzel.taboolib.module.nms.NMS; import io.izzel.taboolib.module.nms.nbt.NBTBase; import io.izzel.taboolib.module.nms.nbt.NBTCompound; @@ -24,6 +25,7 @@ import org.bukkit.potion.PotionEffectType; import org.bukkit.util.NumberConversions; import java.util.List; +import java.util.Map; /** * @Author 坏黑 @@ -67,6 +69,22 @@ public class Items { } } + public static ItemFlag asItemFlag(String flag) { + try { + return ItemFlag.valueOf(flag); + } catch (Exception e) { + return null; + } + } + + public static Color asColor(String color) { + try { + return Color.fromBGR(Integer.valueOf(color.split("-")[0]), Integer.valueOf(color.split("-")[1]), Integer.valueOf(color.split("-")[2])); + } catch (Exception e) { + return Color.fromBGR(0, 0, 0); + } + } + public static Enchantment asEnchantment(String enchant) { try { Enchantment enchantment = Enchantment.getByName(enchant); @@ -85,22 +103,6 @@ public class Items { } } - public static ItemFlag asItemFlag(String flag) { - try { - return ItemFlag.valueOf(flag); - } catch (Exception e) { - return null; - } - } - - public static Color asColor(String color) { - try { - return Color.fromBGR(Integer.valueOf(color.split("-")[0]), Integer.valueOf(color.split("-")[1]), Integer.valueOf(color.split("-")[2])); - } catch (Exception e) { - return Color.fromBGR(0, 0, 0); - } - } - public static String asAttribute(String name) { switch (name.toLowerCase()) { case "damage": @@ -122,12 +124,37 @@ public class Items { } } + public static ItemStack replaceName(ItemStack item, String nameOld, String nameNew) { + return replaceLore(item, ImmutableMap.of(nameOld, nameNew)); + } + public static ItemStack replaceLore(ItemStack item, String loreOld, String loreNew) { + return replaceLore(item, ImmutableMap.of(loreOld, loreNew)); + } + + public static ItemStack replaceName(ItemStack item, Map map) { + if (hasName(item)) { + ItemMeta meta = item.getItemMeta(); + String name = meta.getDisplayName(); + for (Map.Entry entry : map.entrySet()) { + name = name.replace(entry.getKey(), entry.getValue()); + } + meta.setDisplayName(name); + item.setItemMeta(meta); + } + return item; + } + + public static ItemStack replaceLore(ItemStack item, Map map) { if (hasLore(item)) { ItemMeta meta = item.getItemMeta(); List lore = meta.getLore(); for (int i = 0; i < lore.size(); i++) { - lore.set(i, lore.get(i).replace(loreOld, loreNew)); + String line = lore.get(i); + for (Map.Entry entry : map.entrySet()) { + line = line.replace(entry.getKey(), entry.getValue()); + } + lore.set(i, line); } meta.setLore(lore); item.setItemMeta(meta); @@ -182,11 +209,11 @@ public class Items { ItemMeta meta = item.getItemMeta(); // 展示名 if (section.contains("name")) { - meta.setDisplayName(section.getString("name")); + meta.setDisplayName(TLocale.Translate.setColored(section.getString("name"))); } // 描述 if (section.contains("lore")) { - meta.setLore(section.getStringList("lore")); + meta.setLore(TLocale.Translate.setColored(section.getStringList("lore"))); } // 附魔 if (section.contains("enchant")) {