Skip to content

Commit

Permalink
feat: 自动更新玩家背包的护甲与武器 Lore 信息(在玩家进服与reload时) (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcayear authored Sep 8, 2024
1 parent 42d612a commit c5f535a
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>RcRPG.RcRPGMain</groupId>
<artifactId>RcRPG</artifactId>
<version>1.1.5-MOT</version>
<version>1.1.6-MOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/RcRPG/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/RcRPG/RPG/Armour.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -291,7 +297,7 @@ public static void setStone(Player player, Item item, LinkedList<Stone> 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) {
Expand Down Expand Up @@ -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<String> lore = (ArrayList<String>) armour.getLoreList().clone();
Expand All @@ -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);
}
Expand All @@ -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<String, Object> attr) {
this.attr = attr;
setItemAttrConfig(attr);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/RcRPG/RPG/Suit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)));
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/RcRPG/RPG/Weapon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -288,7 +293,7 @@ public static void setStone(Player player, Item item, LinkedList<Stone> 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) {
Expand All @@ -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;
}
Expand All @@ -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<String> lore;
Expand All @@ -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);
}
Expand All @@ -339,6 +347,10 @@ public static Item setWeaponLore(Item item) {
return item;
}

public static Item setWeaponLore(Item item) {
return setWeaponLore(item, LangCode.en_US);
}

/**
* 仅作为属性分类的标识
*
Expand Down
59 changes: 50 additions & 9 deletions src/main/java/RcRPG/RcRPGMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +39,9 @@ public class RcRPGMain extends PluginBase implements Listener {

public static LangCode serverLangCode;

@Getter
public GemTemplateConfig gemTemplateConfig;

public Config ornamentConfig;

/**
Expand Down Expand Up @@ -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"));
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
}

}
1 change: 1 addition & 0 deletions src/main/java/RcRPG/command/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit c5f535a

Please sign in to comment.