Skip to content

Commit

Permalink
重构插件, 优化性能, 添加功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ApliNi committed Jan 21, 2024
1 parent 7f0433f commit ec36f7c
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 103 deletions.
24 changes: 0 additions & 24 deletions src/main/java/aplini/usetranslatednames/Cli.java

This file was deleted.

55 changes: 55 additions & 0 deletions src/main/java/aplini/usetranslatednames/Enum/Cli.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package aplini.usetranslatednames.Enum;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static aplini.usetranslatednames.Util.SEL;

public class Cli {
// 必选变量
public int inspectLength;
public String get;
public String set;
// 可选变量
public String permission;
public String displayPlace;


// 这个配置启用了哪些功能
public boolean enTransVar = false; // 翻译功能
public Matcher dataTransVar;

public boolean enWordReplace = false; // 词替换
public Matcher dataWordReplace;

public boolean enRegExpReplace = false; // 正则表达式替换


// 从 getConfig 中转换数据
public Cli setConfig(Map<?, ?> li){
// 必选变量
this.inspectLength = (int) li.get("inspectLength");
this.get = (String) li.get("get");
this.set = (String) li.get("set");
// 可选变量
this.permission = (String) SEL(li.get("permission"), "");
this.displayPlace = (String) SEL(li.get("displayPlace"), "");


// 这个配置启用了哪些功能

// 翻译功能
this.dataTransVar = Pattern.compile("_\\$(\\d+):(TranslatedName|ItemType)_").matcher(this.set);
this.enTransVar = this.dataTransVar.find();

// 词替换
this.dataWordReplace = Pattern.compile("_\\$(\\d+):Words:([^_]+)_").matcher(this.set);
this.enWordReplace = this.dataWordReplace.find();

// 正则表达式替换
this.enRegExpReplace = Pattern.compile("_\\$\\d+_").matcher(this.set).find();

return this;
}
}
6 changes: 6 additions & 0 deletions src/main/java/aplini/usetranslatednames/Enum/TransVar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package aplini.usetranslatednames.Enum;

public class TransVar {
public String transName;
public String itemType;
}
25 changes: 25 additions & 0 deletions src/main/java/aplini/usetranslatednames/Enum/Word.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package aplini.usetranslatednames.Enum;

import java.util.Map;

import static aplini.usetranslatednames.Util.SEL;

public class Word {
// 必选变量
public String get;
public String set;
// 可选变量
public String lang;


// 从 Map 中转换数据
public Word setConfig(Map<?, ?> li){
// 必选变量
this.get = (String) li.get("get");
this.set = (String) li.get("set");
// 可选变量
this.lang = (String) SEL(li.get("lang"), "");

return this;
}
}
153 changes: 100 additions & 53 deletions src/main/java/aplini/usetranslatednames/UseTranslatedNames.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aplini.usetranslatednames;

import aplini.usetranslatednames.Enum.Cli;
import aplini.usetranslatednames.Enum.Word;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerPriority;
Expand All @@ -11,29 +13,32 @@
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerLoadEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static aplini.usetranslatednames.Util.SEL;
import static aplini.usetranslatednames.Util.toTranslatedName;


public final class UseTranslatedNames extends JavaPlugin implements CommandExecutor, TabExecutor, Listener {
// 调试模式
private boolean _debug = false;
// 监听器模式
boolean listeningMode = true;
// 词配置 Map<组名.词 or 组名.语言.词, 词配置>
HashMap<String, Word> words;
// 配置文件
List<Cli> list = new ArrayList<>();

@Override
public void onEnable() {
saveDefaultConfig();
getConfig();
loadConfig();
Util.load(this);

Expand All @@ -53,23 +58,35 @@ public void onEnable() {
){
@Override
public void onPacketSending(PacketEvent event){

Player player = event.getPlayer();

// 获取消息 JSON
// String json = event.getPacket().getStrings().read(0); // 1.20.4 -
String json = event.getPacket().getChatComponents().read(0).getJson(); // 1.20.4 +
String json;
if(listeningMode){
// 1.20.4 +
json = event.getPacket().getChatComponents().read(0).getJson();
}else{
// 1.20.4 -
json = event.getPacket().getStrings().read(0);
}

if(json == null) return;

// 处理重复的消息

if(_debug){
getLogger().info("[DEBUG] [Player: "+ event.getPlayer().getName() +"] [Length: "+ json.length() +"]: -------");
getLogger().info("[DEBUG] [Player: "+ event.getPlayer().getName() +" (Lang: "+ player.getLocale() +")] [Length: "+ json.length() +"]: -------");
getLogger().info(" - [get]: "+ json);
}

// 遍历替换配置
for(Cli cli : list){
boolean ok = false;

// 如果权限不为空 且 玩家没有该权限, 则不进行替换
if(!cli.permission.isEmpty() && !player.hasPermission(cli.permission)){
continue;
}

// 防止处理过长的消息
if(json.length() > cli.inspectLength){
continue;
Expand All @@ -84,24 +101,41 @@ public void onPacketSending(PacketEvent event){
// 匹配到的完整字符串
String jsonFrame = cli.set;
if(jsonFrame.isEmpty()){ // 如果为空则仅取消发送它
continue;
ok = true;
break;
}
String oldJson = matcher.group(0);

// 处理翻译变量 _$1:ItemType_, _$1:TranslatedName_
Matcher matcher2 = Pattern.compile("_\\$(\\d+):(TranslatedName|ItemType)_").matcher(jsonFrame);
while(matcher2.find()){
String var = matcher.group(Integer.parseInt(matcher2.group(1)));
if(matcher2.group(2).equals("TranslatedName")){
jsonFrame = jsonFrame.replace(matcher2.group(), toTranslatedName(var)[1]);
}else{
jsonFrame = jsonFrame.replace(matcher2.group(), toTranslatedName(var)[0]);
if(cli.enTransVar){
cli.dataTransVar.reset();
while(cli.dataTransVar.find()){
String var = matcher.group(Integer.parseInt(cli.dataTransVar.group(1)));
if(cli.dataTransVar.group(2).equals("TranslatedName")){
jsonFrame = jsonFrame.replace(cli.dataTransVar.group(), toTranslatedName(var).transName);
}else{
jsonFrame = jsonFrame.replace(cli.dataTransVar.group(), toTranslatedName(var).itemType);
}
}
}

// 处理词替换 _$1:Words:xxx_
if(cli.enWordReplace){
cli.dataWordReplace.reset();
while(cli.dataWordReplace.find()){
String var = matcher.group(Integer.parseInt(cli.dataWordReplace.group(1)));
Word word = (Word) SEL(
words.get(cli.dataWordReplace.group(2) +"."+ player.getLocale() +"."+ var),
words.get(cli.dataWordReplace.group(2) +".."+ var));
if(word != null){
jsonFrame = jsonFrame.replace(cli.dataWordReplace.group(), word.set);
}
}
}


// 处理正则变量 _$1_
Matcher matcher3 = Pattern.compile("_\\$\\d+_").matcher(jsonFrame);
if(matcher3.find()){
if(cli.enRegExpReplace){
int matcherLength = matcher.groupCount();
for(int i = 1; i <= matcherLength; i++){
jsonFrame = jsonFrame.replace("_$" + i + "_", matcher.group(i));
Expand All @@ -111,11 +145,9 @@ public void onPacketSending(PacketEvent event){

// 替换原文本中的旧 JSON, 重新发送给玩家
jsonFrame = json.replace(oldJson, jsonFrame);
jsonFrame = jsonFrame.replaceAll("^\\{", "{\"utn\":\"utn\",");
if(_debug){
getLogger().info(" - [set]: "+ jsonFrame);
}
Player player = event.getPlayer();
// 处理显示位置
if(cli.displayPlace.equals("ACTION_BAR")){
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, ComponentSerializer.parse(jsonFrame));
Expand All @@ -132,30 +164,59 @@ public void onPacketSending(PacketEvent event){
getLogger().info("UseTranslatedNames 已启动");
}

public void loadConfig(){
list = new ArrayList<>();
for(Map<?, ?> li : getConfig().getMapList("list")){
list.add(new Cli().setConfig(li));
}
}
// 加载配置文件
public long loadConfig(){
long _startTime = System.nanoTime(); // 记录运行时间

// 检查配置版本
public void CheckConfigVersion(){
saveResource("config.yml", false);
saveResource("words.yml", false);
reloadConfig();

// 检查配置版本
if(getConfig().getInt("configVersion") != 3){
getLogger().warning("配置版本不匹配, 可能无法正常运行, 请更新或重建配置");
getLogger().warning("配置版本不匹配, 可能无法正常运行, 请更新或重建配置");
getLogger().warning("配置版本不匹配, 可能无法正常运行, 请更新或重建配置");
}
}

@EventHandler // 服务器启动完成事件
public void onServerLoad(ServerLoadEvent event) {
// 检查配置版本
CheckConfigVersion();
// 监听器模式
listeningMode = getConfig().getBoolean("dev.listeningMode");

// 处理词替换配置
words = new HashMap<>();
Map<String, Object> wordsConfig = Objects.requireNonNull(
YamlConfiguration.loadConfiguration(new File(getDataFolder(), "words.yml"))
.getConfigurationSection("words")).getValues(false);
for(String groupName : wordsConfig.keySet()){
List<?> wordList = (List<?>) wordsConfig.get(groupName);
for(Object _word : wordList){
Word word = new Word().setConfig((Map<?, ?>) _word);
// 'groupName.zh_cn.word' or 'groupName..word'
words.put(groupName +"."+ word.lang +"."+ word.get, word);
}
}

// 处理替换配置表
list = new ArrayList<>();
for(Map<?, ?> li : getConfig().getMapList("list")){
list.add(new Cli().setConfig(li));
}

return Math.round((System.nanoTime() - _startTime) / 1_000_000.0);
}

// 执行指令
@Override

@Override // 指令补全
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if(args.length == 1){
List<String> list = new ArrayList<>();
list.add("reload"); // 重载配置
list.add("debug"); // 调试模式
return list;
}
return null;
}
@Override // 执行指令
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

// 默认输出插件信息
Expand All @@ -169,33 +230,19 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

// 重载配置
else if(args[0].equals("reload")){
reloadConfig();
loadConfig();
sender.sendMessage("UseTranslatedNames 已完成重载");
CheckConfigVersion();
long time = loadConfig();
sender.sendMessage("[UTN] 已完成重载, 耗时: "+ time +" 毫秒");
return true;
}

// 调试模式
else if(args[0].equals("debug")){
_debug = ! _debug;
sender.sendMessage("UseTranslatedNames 调试模式: "+ _debug);
sender.sendMessage("[UTN] 调试模式: "+ _debug);
return true;
}

// 返回 false 时, 玩家将收到命令不存在的错误
return false;
}

// 指令补全
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if(args.length == 1){
List<String> list = new ArrayList<>();
list.add("reload"); // 重载配置
list.add("debug"); // 调试模式
return list;
}
return null;
}
}
Loading

0 comments on commit ec36f7c

Please sign in to comment.