From c5f535abc9d62ef015d83172f1c9db547e7f6e74 Mon Sep 17 00:00:00 2001 From: Mcayear Date: Sun, 8 Sep 2024 11:11:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=8E=A9=E5=AE=B6=E8=83=8C=E5=8C=85=E7=9A=84=E6=8A=A4=E7=94=B2?= =?UTF-8?q?=E4=B8=8E=E6=AD=A6=E5=99=A8=20Lore=20=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=EF=BC=88=E5=9C=A8=E7=8E=A9=E5=AE=B6=E8=BF=9B=E6=9C=8D=E4=B8=8E?= =?UTF-8?q?reload=E6=97=B6=EF=BC=89=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- src/main/java/RcRPG/Events.java | 11 +- src/main/java/RcRPG/RPG/Armour.java | 21 +++- src/main/java/RcRPG/RPG/Suit.java | 3 +- src/main/java/RcRPG/RPG/Weapon.java | 22 +++- src/main/java/RcRPG/RcRPGMain.java | 59 ++++++++-- src/main/java/RcRPG/command/Commands.java | 1 + .../java/RcRPG/config/GemTemplateConfig.java | 110 ++++++++++++++++++ src/main/java/RcRPG/config/MainConfig.java | 4 +- src/main/resources/Weapon/Weapon.yml | 1 + .../{DismantlePlan.yml => dismantlePlan.yml} | 0 src/main/resources/gemTemplate.yml | 20 ++++ src/main/resources/plugin.yml | 2 +- .../resources/{SuitPlan.yml => suitPlan.yml} | 0 14 files changed, 229 insertions(+), 27 deletions(-) create mode 100644 src/main/java/RcRPG/config/GemTemplateConfig.java rename src/main/resources/{DismantlePlan.yml => dismantlePlan.yml} (100%) create mode 100644 src/main/resources/gemTemplate.yml rename src/main/resources/{SuitPlan.yml => suitPlan.yml} (100%) diff --git a/pom.xml b/pom.xml index 2fa8fd7..d6105dd 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ RcRPG.RcRPGMain RcRPG - 1.1.5-MOT + 1.1.6-MOT UTF-8 diff --git a/src/main/java/RcRPG/Events.java b/src/main/java/RcRPG/Events.java index 7f4f336..f099a7e 100644 --- a/src/main/java/RcRPG/Events.java +++ b/src/main/java/RcRPG/Events.java @@ -636,16 +636,21 @@ public void chatEvent(PlayerChatEvent event){ } @EventHandler(priority = EventPriority.LOWEST) - public void joinEvent(PlayerPreLoginEvent event){ + public void joinEvent(PlayerLocallyInitializedEvent event){ + RcRPGMain.updateItemLore(event.getPlayer());// 更新玩家背包物品 + } + + @EventHandler(priority = EventPriority.LOWEST) + public void preJoinEvent(PlayerPreLoginEvent event){ Player player = event.getPlayer(); String name = player.getName(); - PlayerAttr.setPlayerAttr(player); + PlayerAttr.setPlayerAttr(player);// 设置玩家属性 File file = new File(RcRPGMain.getInstance().getDataFolder()+"/Players/"+name+".yml"); if(!file.exists()) { RcRPGMain.getInstance().saveResource("Players/Player.yml","/Players/"+name+".yml",false); - Config config = new Config(RcRPGMain.getInstance().getPlayerFile()+"/"+name+".yml"); + Config config = new Config(RcRPGMain.getInstance().getPlayerFile()+File.separator + name+".yml"); config.set("名称",name); config.set("公会", MainConfig.getInitialGuild()); config.set("称号", MainConfig.getInitialPrefix()); diff --git a/src/main/java/RcRPG/RPG/Armour.java b/src/main/java/RcRPG/RPG/Armour.java index 47c36c9..f32c7e5 100644 --- a/src/main/java/RcRPG/RPG/Armour.java +++ b/src/main/java/RcRPG/RPG/Armour.java @@ -5,6 +5,7 @@ import RcRPG.RcRPGMain; import cn.nukkit.Player; import cn.nukkit.item.Item; +import cn.nukkit.lang.LangCode; import cn.nukkit.nbt.tag.CompoundTag; import cn.nukkit.nbt.tag.ListTag; import cn.nukkit.nbt.tag.StringTag; @@ -192,7 +193,7 @@ public static boolean delArmourConfig(String name) { return false; } - public static Item getItem(String name, int count) { + public static Item getItem(String name, int count, LangCode langCode) { Armour armour = RcRPGMain.loadArmour.get(name); Item item = armour.getItem(); item.setCount(count); @@ -207,8 +208,13 @@ public static Item getItem(String name, int count) { item.setNamedTag(tag); item.setCustomName(armour.getShowName()); - Armour.setArmourLore(item); + Armour.setArmourLore(item, langCode); return item; + + } + + public static Item getItem(String name, int count) { + return getItem(name, count, LangCode.en_US); } public static boolean giveArmour(Player player, String name, int count) { @@ -291,7 +297,7 @@ public static void setStone(Player player, Item item, LinkedList list) { CompoundTag tag = item.getNamedTag(); tag.putList(stoneList); item.setNamedTag(tag); - player.getInventory().setItemInHand(Armour.setArmourLore(item)); + player.getInventory().setItemInHand(Armour.setArmourLore(item, player.getLanguageCode())); } public static int getStoneHealth(Item item) { @@ -333,7 +339,7 @@ public static int getStoneReDamage(Item item) { return 0; } - public static Item setArmourLore(Item item) { + public static Item setArmourLore(Item item, LangCode langCode) { if (Armour.isArmour(item)) { Armour armour = RcRPGMain.loadArmour.get(item.getNamedTag().getString("name")); ArrayList lore = (ArrayList) armour.getLoreList().clone(); @@ -346,6 +352,9 @@ public static Item setArmourLore(Item item) { s = s.replace("@stoneDamage", String.valueOf(Armour.getStoneDamage(item))); if (s.contains("@stoneReDamage")) s = s.replace("@stoneReDamage", String.valueOf(Armour.getStoneReDamage(item))); + if (s.contains("@gemLore")) + s = s.replace("@gemLore", RcRPGMain.getInstance().getGemTemplateConfig().getTemplateText(langCode, item.getNamedTag(), armour.getStone(), armour.getStoneList())); + s = armour.replaceAttrTemplate(s);// 替换属性的值 lore.set(i, s); } @@ -354,6 +363,10 @@ public static Item setArmourLore(Item item) { return item; } + public static Item setArmourLore(Item item) { + return setArmourLore(item, LangCode.en_US); + } + public void setAttr(Map attr) { this.attr = attr; setItemAttrConfig(attr); diff --git a/src/main/java/RcRPG/RPG/Suit.java b/src/main/java/RcRPG/RPG/Suit.java index 1f9fb8b..4855e54 100644 --- a/src/main/java/RcRPG/RPG/Suit.java +++ b/src/main/java/RcRPG/RPG/Suit.java @@ -6,6 +6,7 @@ import cn.nukkit.utils.ConfigSection; import lombok.Getter; +import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -16,7 +17,7 @@ public class Suit { public static void init() { suitInfo.clear(); - Config cfg = new Config(RcRPGMain.getInstance().getDataFolder() + "/SuitPlan.yml"); + Config cfg = new Config(RcRPGMain.getInstance().getDataFolder() + File.separator + "suitPlan.yml"); cfg.getAll().keySet().forEach(suitName -> { RcRPGMain.getInstance().getLogger().info("suitName: "+suitName); suitInfo.put(suitName, new SuitConfig(cfg.getSection(suitName))); diff --git a/src/main/java/RcRPG/RPG/Weapon.java b/src/main/java/RcRPG/RPG/Weapon.java index 3639a41..2b110f4 100644 --- a/src/main/java/RcRPG/RPG/Weapon.java +++ b/src/main/java/RcRPG/RPG/Weapon.java @@ -5,6 +5,7 @@ import RcRPG.RcRPGMain; import cn.nukkit.Player; import cn.nukkit.item.Item; +import cn.nukkit.lang.LangCode; import cn.nukkit.nbt.tag.CompoundTag; import cn.nukkit.nbt.tag.ListTag; import cn.nukkit.nbt.tag.StringTag; @@ -187,7 +188,7 @@ public static boolean delWeaponConfig(String name) { return false; } - public static Item getItem(String name, int count) { + public static Item getItem(String name, int count, LangCode langCode) { Weapon weapon = RcRPGMain.loadWeapon.get(name); Item item = weapon.getItem(); item.setCount(count); @@ -204,10 +205,14 @@ public static Item getItem(String name, int count) { tag.putList(stoneList); item.setNamedTag(tag); item.setCustomName(weapon.getShowName()); - Weapon.setWeaponLore(item); + Weapon.setWeaponLore(item, langCode); return item; } + public static Item getItem(String name, int count) { + return getItem(name, count, LangCode.en_US); + } + public static boolean giveWeapon(Player player, String name, int count) { if (!RcRPGMain.loadWeapon.containsKey(name)) { return false; @@ -288,7 +293,7 @@ public static void setStone(Player player, Item item, LinkedList list) { CompoundTag tag = item.getNamedTag(); tag.putList(stoneList); item.setNamedTag(tag); - player.getInventory().setItemInHand(Weapon.setWeaponLore(item)); + player.getInventory().setItemInHand(Weapon.setWeaponLore(item, player.getLanguageCode())); } public static int getStoneDamage(Item item) { @@ -297,7 +302,7 @@ public static int getStoneDamage(Item item) { int damage = 0; for (Stone stone : list) { if (stone == null) continue; - damage += stone.getItemAttr("PVE攻击力"); + damage += (int) stone.getItemAttr("PVE攻击力"); } return damage; } @@ -317,7 +322,7 @@ public static int getStoneReDamage(Item item) { return 0; } - public static Item setWeaponLore(Item item) { + public static Item setWeaponLore(Item item, LangCode langCode) { if (Weapon.isWeapon(item)) { Weapon weapon = RcRPGMain.loadWeapon.get(item.getNamedTag().getString("name")); ArrayList lore; @@ -331,6 +336,9 @@ public static Item setWeaponLore(Item item) { s = s.replace("@stoneDamage", String.valueOf(Weapon.getStoneDamage(item))); if (s.contains("@stoneReDamage")) s = s.replace("@stoneReDamage", String.valueOf(Weapon.getStoneReDamage(item))); + if (s.contains("@gemLore")) + s = s.replace("@gemLore", RcRPGMain.getInstance().getGemTemplateConfig().getTemplateText(langCode, item.getNamedTag(), weapon.getStone(), weapon.getStoneList())); + s = weapon.replaceAttrTemplate(s);// 替换属性的值 lore.set(i, s); } @@ -339,6 +347,10 @@ public static Item setWeaponLore(Item item) { return item; } + public static Item setWeaponLore(Item item) { + return setWeaponLore(item, LangCode.en_US); + } + /** * 仅作为属性分类的标识 * diff --git a/src/main/java/RcRPG/RcRPGMain.java b/src/main/java/RcRPG/RcRPGMain.java index 657d73f..a8a8fc6 100644 --- a/src/main/java/RcRPG/RcRPGMain.java +++ b/src/main/java/RcRPG/RcRPGMain.java @@ -3,16 +3,20 @@ import RcRPG.RPG.*; import RcRPG.RPG.forging.ForgingPaper; import RcRPG.RPG.forging.ForgingStone; -import RcRPG.task.BoxTimeTask; -import RcRPG.task.PlayerAttrUpdateTask; -import RcRPG.task.PlayerTipTask; import RcRPG.command.Commands; +import RcRPG.config.GemTemplateConfig; import RcRPG.config.MainConfig; import RcRPG.floatingtext.TextEntity; +import RcRPG.task.BoxTimeTask; +import RcRPG.task.PlayerAttrUpdateTask; +import RcRPG.task.PlayerTipTask; import RcRPG.tips.TipsVariables; +import cn.nukkit.Player; import cn.nukkit.Server; import cn.nukkit.entity.Entity; import cn.nukkit.event.Listener; +import cn.nukkit.inventory.PlayerInventory; +import cn.nukkit.item.Item; import cn.nukkit.lang.LangCode; import cn.nukkit.lang.PluginI18n; import cn.nukkit.lang.PluginI18nManager; @@ -35,6 +39,9 @@ public class RcRPGMain extends PluginBase implements Listener { public static LangCode serverLangCode; + @Getter + public GemTemplateConfig gemTemplateConfig; + public Config ornamentConfig; /** @@ -86,7 +93,7 @@ public void onEnable() { this.getServer().getScheduler().scheduleRepeatingTask(new PlayerTipTask(this), 20); } this.getServer().getScheduler().scheduleRepeatingTask(new BoxTimeTask(this), 20); - //this.getServer().getScheduler().scheduleRepeatingTask(new loadHealth(this), 10); + // this.getServer().getScheduler().scheduleRepeatingTask(new loadHealth(this), 10); this.getServer().getScheduler().scheduleRepeatingTask(new PlayerAttrUpdateTask(this), 20); this.getServer().getPluginManager().addPermission(new Permission("plugin.rcrpg", "rcrpg 命令权限", "true")); @@ -108,21 +115,24 @@ public void onEnable() { public void init() { MainConfig.init(); - this.saveResource("OrnamentConfig.yml", "/OrnamentConfig.yml", false); + this.saveResource("OrnamentConfig.yml", "OrnamentConfig.yml", false); ornamentConfig = new Config(this.getDataFolder() + File.separator + "OrnamentConfig.yml"); - this.saveResource("DismantlePlan.yml", "/DismantlePlan.yml", false); - dismantleConfig = new Config(this.getDataFolder() + File.separator + "DismantlePlan.yml"); + this.saveResource("dismantlePlan.yml", "dismantlePlan.yml", false); + dismantleConfig = new Config(this.getDataFolder() + File.separator + "dismantlePlan.yml"); - this.saveResource("SuitPlan.yml", "/SuitPlan.yml", false); + this.saveResource("suitPlan.yml", "suitPlan.yml", false); Suit.init(); + this.saveResource("gemTemplate.yml", "gemTemplate.yml", false); + gemTemplateConfig = new GemTemplateConfig(new Config(this.getDataFolder() + File.separator + "gemTemplate.yml")); + this.getLogger().info("开始读取武器信息"); loadWeapon.clear(); for (String name : Handle.getDefaultFiles("Weapon")) { Weapon weapon = null; try { - weapon = Weapon.loadWeapon(name, new Config(this.getDataFolder() + File.separator + "Weapon/" + name + ".yml", Config.YAML)); + weapon = Weapon.loadWeapon(name, new Config(this.getDataFolder() + File.separator + "Weapon" + File.separator + name + ".yml", Config.YAML)); } catch (Exception e) { throw new RuntimeException(e); } @@ -330,4 +340,35 @@ public void initServerLangCode() { } } + public static void updateItemLore(Player player) { + if (player == null) { + return; + } + + PlayerInventory bag = player.getInventory(); + + for (int num = 0; num < bag.getSize(); num++) { + Item item = bag.getItem(num); + + if (item.isNull() || !item.hasCompoundTag()) { + continue; + } + + Item newItem; + if (Weapon.isWeapon(item)) { + newItem = Weapon.setWeaponLore(item.clone(), player.getLanguageCode()); + } else if (Armour.isArmour(item)) { + newItem = Armour.setArmourLore(item.clone(), player.getLanguageCode()); + } else { + continue; + } + + if (!item.equals(newItem)) { + newItem.setCount(item.count); + bag.remove(item); + bag.setItem(num, newItem); + } + } + } + } \ No newline at end of file diff --git a/src/main/java/RcRPG/command/Commands.java b/src/main/java/RcRPG/command/Commands.java index e22689f..80bff7b 100644 --- a/src/main/java/RcRPG/command/Commands.java +++ b/src/main/java/RcRPG/command/Commands.java @@ -258,6 +258,7 @@ public boolean execute(CommandSender sender, String label, String[] args) { return false; } RcRPGMain.getInstance().init(); + Server.getInstance().getOnlinePlayers().values().forEach(RcRPGMain::updateItemLore); sender.sendMessage(TextFormat.GREEN + i18n.tr(langCode, "rcrpg.commands.reloaded")); return true; } diff --git a/src/main/java/RcRPG/config/GemTemplateConfig.java b/src/main/java/RcRPG/config/GemTemplateConfig.java new file mode 100644 index 0000000..1fdd66e --- /dev/null +++ b/src/main/java/RcRPG/config/GemTemplateConfig.java @@ -0,0 +1,110 @@ +package RcRPG.config; + +import cn.nukkit.lang.LangCode; +import cn.nukkit.nbt.tag.CompoundTag; +import cn.nukkit.nbt.tag.ListTag; +import cn.nukkit.nbt.tag.StringTag; +import cn.nukkit.utils.Config; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import static RcRPG.RcRPGMain.loadStone; + +@Getter +public class GemTemplateConfig { + + private final Map>> MULTI_LANGUAGE = new HashMap<>(); + + public GemTemplateConfig(Config cfg) { + // 遍历所有语言代码 (en_US, zh_CN 等) + for (String langKey : cfg.getKeys(false)) { + LangCode langCode = LangCode.valueOf(langKey); // 假设 LangCode 枚举与语言代码匹配 + Map> gemTemplates = new HashMap<>(); + + // 获取该语言的所有宝石类型配置 (default, 攻击宝石, 生命宝石 等) + Map langSection = cfg.getSection(langKey); + for (String gemType : langSection.keySet()) { + Map states = new HashMap<>(); + + // 获取具体的`insertable`和`embedded`值 + String insertable = cfg.getString(langKey + "." + gemType + ".insertable"); + String embedded = cfg.getString(langKey + "." + gemType + ".embedded"); + + // 存储这些值到 states 中 + states.put("insertable", insertable); + states.put("embedded", embedded); + + // 将 states 存储到 gemTemplates 中 + gemTemplates.put(gemType, states); + } + + // 将语言代码与其模板数据对应,存入 MULTI_LANGUAGE + MULTI_LANGUAGE.put(langCode, gemTemplates); + } + } + + /** + * 根据语言代码和宝石类型获取模板信息,带有回退机制。 + * + * @param langCode 语言代码 + * @param gemType 宝石类型 + * @param state 状态 (insertable 或 embedded) + * @return 模板字符串 + */ + public String templateTr(LangCode langCode, String gemType, String state) { + // 尝试获取指定语言的宝石配置 + Map> langData = MULTI_LANGUAGE.getOrDefault(langCode, MULTI_LANGUAGE.get(LangCode.en_US)); + + // 尝试获取指定的宝石类型,如果没有则回退到 default + Map gemData = langData.getOrDefault(gemType, langData.get("default")); + + // 获取具体的状态 (insertable 或 embedded) 的值 + return gemData.get(state); + } + + /** + * 根据语言代码和宝石类型获取模板信息,带有回退机制。 + * + * @param langCode 语言代码 + * @param nbt 物品nbt + * @param stoneMax 最大宝石数量 + * @param stoneSlots 宝石槽位列表 + * @return 模板字符串 + */ + public String getTemplateText(LangCode langCode, CompoundTag nbt, int stoneMax, ArrayList stoneSlots) { + ListTag stoneList = (ListTag) nbt.getList("stone"); + ArrayList templateList = new ArrayList<>(); // 用于存储每个模板字符串 + + for (int i = 0; i < stoneMax; i++) { + String template; + if (stoneList.isEmpty() || i >= stoneList.size()) { + if (i < stoneSlots.size()) { + template = templateTr(langCode, stoneSlots.get(i), "insertable") + .replace("@stoneType", stoneSlots.get(i)); + } else { + template = templateTr(langCode, "default", "insertable") + .replace("@stoneType", stoneSlots.get(i)); + } + } else { + String stoneName = stoneList.get(i).parseValue(); + if (stoneName.isEmpty()) { + template = templateTr(langCode, stoneSlots.get(i), "insertable") + .replace("@stoneType", stoneSlots.get(i)); + } else if (i < stoneSlots.size()) { + template = templateTr(langCode, stoneSlots.get(i), "embedded") + .replace("@stoneName", loadStone.get(stoneName).getShowName()); + } else { + template = templateTr(langCode, "default", "embedded") + .replace("@stoneName", loadStone.get(stoneName).getShowName()); + } + } + templateList.add(template); // 将模板字符串添加到列表中 + } + + return String.join("\n", templateList); + } + +} diff --git a/src/main/java/RcRPG/config/MainConfig.java b/src/main/java/RcRPG/config/MainConfig.java index bc55709..da090c5 100644 --- a/src/main/java/RcRPG/config/MainConfig.java +++ b/src/main/java/RcRPG/config/MainConfig.java @@ -60,9 +60,7 @@ public class MainConfig { public static List attrDisplayPercent = new ArrayList<>(); public static void init() { - if (!new File(RcRPGMain.getInstance().getDataFolder(), "config.yml").exists()) { - RcRPGMain.getInstance().saveResource("config.yml"); - } + RcRPGMain.getInstance().saveResource("config.yml"); config = new Config(new File(RcRPGMain.getInstance().getDataFolder(), "config.yml"), Config.YAML); // 读取配置文件中的值 diff --git a/src/main/resources/Weapon/Weapon.yml b/src/main/resources/Weapon/Weapon.yml index 972439b..a9c4fa7 100644 --- a/src/main/resources/Weapon/Weapon.yml +++ b/src/main/resources/Weapon/Weapon.yml @@ -38,6 +38,7 @@ - §r§6◈§7PVP攻击§6◈§a {{PVP攻击力}} - §r§6◈§7吸血倍率§6◈§a {{吸血倍率}} - §r§6◈§f═§7╞════════════╡§f═ + - @gemLore 攻击者药水效果: [] 受击者药水效果: [] 群体药水效果: [] diff --git a/src/main/resources/DismantlePlan.yml b/src/main/resources/dismantlePlan.yml similarity index 100% rename from src/main/resources/DismantlePlan.yml rename to src/main/resources/dismantlePlan.yml diff --git a/src/main/resources/gemTemplate.yml b/src/main/resources/gemTemplate.yml new file mode 100644 index 0000000..94c2fa1 --- /dev/null +++ b/src/main/resources/gemTemplate.yml @@ -0,0 +1,20 @@ +en_US: + default: + insertable: §aInsertable<§7@stoneType§a> + embedded: "@stoneName" + 攻击宝石: + insertable: §aInsertable<§c@stoneType§a> + embedded: "@stoneName" + 生命宝石: + insertable: §aInsertable<§a§l@stoneType§r§a> + embedded: "@stoneName" +zh_CN: + default: + insertable: §a可镶嵌<§7@stoneType§a> + embedded: "@stoneName" + 攻击宝石: + insertable: §a可镶嵌<§c@stoneType§a> + embedded: "@stoneName" + 生命宝石: + insertable: §a可镶嵌<§a§l@stoneType§r§a> + embedded: "@stoneName" diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index d68a25b..bdb1805 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: RcRPG main: RcRPG.RcRPGMain -version: 1.1.5 +version: 1.1.6 author: 若尘 authors: ["若尘", "Mcayear"] api: 1.0.8 diff --git a/src/main/resources/SuitPlan.yml b/src/main/resources/suitPlan.yml similarity index 100% rename from src/main/resources/SuitPlan.yml rename to src/main/resources/suitPlan.yml