From 55b420f11cbd60f0c9b35840a2f483d4c5239367 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sat, 10 Sep 2016 18:27:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=8C=96=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../pw/yumc/MiaoChat/config/ChatConfig.java | 43 +++----------- .../yumc/MiaoChat/config/ChatMessagePart.java | 2 +- .../pw/yumc/MiaoChat/config/ChatRule.java | 23 ++++++-- .../yumc/MiaoChat/listeners/ChatListener.java | 12 +--- src/main/resources/admin.yml | 56 ------------------- src/main/resources/config.yml | 4 ++ src/main/resources/default.yml | 55 ------------------ src/main/resources/format.yml | 42 ++++++++++++++ 8 files changed, 78 insertions(+), 159 deletions(-) delete mode 100644 src/main/resources/admin.yml delete mode 100644 src/main/resources/default.yml create mode 100644 src/main/resources/format.yml diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java index 500d74e..3cdf2ea 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java @@ -1,53 +1,28 @@ package pw.yumc.MiaoChat.config; -import java.util.LinkedList; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import pw.yumc.YumCore.config.FileConfig; public class ChatConfig { - private static String PrefixKey = "Format.Prefix"; - private static String PlayerKey = "Format.Player"; - private static String SuffixKey = "Format.Suffix"; private final FileConfig config; - private final List prefixs; - private final List suffixs; - private ChatMessagePart player; + private final Map formats; public ChatConfig(final FileConfig cfg) { config = cfg; - prefixs = new LinkedList<>(); - suffixs = new LinkedList<>(); + formats = new HashMap<>(); reload(); } - public ChatMessagePart getPlayer() { - return player; - } - - public List getPrefixs() { - return prefixs; - } - - public List getSuffixs() { - return suffixs; + public ChatMessagePart getFormat(final String name) { + return formats.get(name); } public void reload() { - prefixs.clear(); - if (config.isSet(PrefixKey)) { - for (final String part : config.getConfigurationSection(PrefixKey).getKeys(false)) { - prefixs.add(new ChatMessagePart(config.getConfigurationSection(PrefixKey + "." + part))); - } - } - if (config.isSet(PlayerKey)) { - player = new ChatMessagePart(config.getConfigurationSection(PlayerKey)); - } - suffixs.clear(); - if (config.isSet(SuffixKey)) { - for (final String part : config.getConfigurationSection(SuffixKey).getKeys(false)) { - suffixs.add(new ChatMessagePart(config.getConfigurationSection(SuffixKey + "." + part))); - } + formats.clear(); + for (final String name : config.getKeys(false)) { + formats.put(name, new ChatMessagePart(config.getConfigurationSection(name))); } } } diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatMessagePart.java b/src/main/java/pw/yumc/MiaoChat/config/ChatMessagePart.java index 54835d0..3a56292 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatMessagePart.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatMessagePart.java @@ -18,10 +18,10 @@ public class ChatMessagePart extends InjectConfigurationSection { @Nullable @ConfigNode("click.type") private String typestring; - private transient CLICKTYPE type = CLICKTYPE.SUGGEST; @Nullable @ConfigNode("click.command") private String command; + private transient CLICKTYPE type = CLICKTYPE.SUGGEST; public ChatMessagePart(final ConfigurationSection config) { super(config); diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java b/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java index 45ea097..dd16cc2 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java @@ -1,10 +1,13 @@ package pw.yumc.MiaoChat.config; +import java.util.LinkedList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import pw.yumc.YumCore.config.Default; -import pw.yumc.YumCore.config.FileConfig; import pw.yumc.YumCore.config.InjectConfigurationSection; /** @@ -14,17 +17,21 @@ import pw.yumc.YumCore.config.InjectConfigurationSection; * @author 喵♂呜 */ public class ChatRule extends InjectConfigurationSection { + private transient static final Pattern FORMAT_PATTERN = Pattern.compile("[#]([^#]+)[#]"); private transient String name; @Default("50") private Integer index; + @Default("MiaoChat.default") private String permission; + @Default("#world#player: ") + private String format; @Default("0") private Integer range; @Default("false") private Boolean item; @Default("&6[&b%s&6]&r") private String itemformat; - private transient ChatConfig formats; + private transient LinkedList formats; public ChatRule(final String name, final ConfigurationSection config) { super(config); @@ -32,14 +39,22 @@ public class ChatRule extends InjectConfigurationSection { if (permission == null) { permission = String.format("MiaoChat.%s", name); } - formats = new ChatConfig(new FileConfig(name + ".yml")); + formats = new LinkedList<>(); + final Matcher m = FORMAT_PATTERN.matcher(format); + while (m.find()) { + formats.add(m.group(1)); + } } public boolean check(final Player player) { return player.hasPermission(permission); } - public ChatConfig getFormats() { + public String getFormat() { + return format; + } + + public LinkedList getFormats() { return formats; } diff --git a/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java b/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java index b067b1c..21e94f0 100644 --- a/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java +++ b/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java @@ -20,8 +20,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.inventory.ItemStack; import pw.yumc.MiaoChat.MiaoChat; -import pw.yumc.MiaoChat.config.ChatConfig; -import pw.yumc.MiaoChat.config.ChatMessagePart; import pw.yumc.MiaoChat.config.ChatRule; import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; @@ -61,13 +59,9 @@ public class ChatListener implements Listener { } private void handleFormat(final Player p, final Tellraw tr, final ChatRule cr) { - final ChatConfig cc = cr.getFormats(); - for (final ChatMessagePart cmp : cc.getPrefixs()) { - cmp.then(tr, p); - } - cc.getPlayer().then(tr, p); - for (final ChatMessagePart cmp : cc.getSuffixs()) { - cmp.then(tr, p); + final LinkedList formats = cr.getFormats(); + for (final String format : formats) { + } } diff --git a/src/main/resources/admin.yml b/src/main/resources/admin.yml deleted file mode 100644 index 942d823..0000000 --- a/src/main/resources/admin.yml +++ /dev/null @@ -1,56 +0,0 @@ -#格式化配置 -Format: - #前缀配置 于玩家名称前 - Prefix: - 0: - text: '&6[&c管理员&6]' - #顺序 - 1: - #文本 支持PAPI变量 - text: '&6[&a%player_world%&6]' - #悬浮提示 支持PAPI - tip: - - '&6当前所在位置:' - - '&6世界: &d%player_world%' - - '&6坐标: &aX:%player_x% Y: %player_y% Z: %player_z%' - - '' - - '&c点击即可TP我!' - #点击操作 - click: - #操作类型: [COMMAND,SUGGEST,OPENURL] - #COMMAND代表执行命令 - #SUGGEST代表命令补全 - #OPENURL代表打开网址 - type: 'COMMAND' - #命令或网址 支持PAPI - command: '/tpa %player_name%' -# 可以添加多个 -# 2: -# #文本 支持PAPI变量 -# text: '&6[&a%player_world%&6]' -# #悬浮提示 支持PAPI -# tip: [] - #玩家名称配置 - Player: - text: '&b%player_name%' - tip: - - '&6玩家名称: &b%player_name%' - - '&6玩家等级: &a%player_level%' - - '&6玩家血量: &c%player_health%' - - '&6玩家饥饿: &d%player_food_level%' - - '&6游戏模式: &4%player_gamemode%' - - '' - - '&c点击与我聊天' - click: - type: 'SUGGEST' - command: '/tell %player_name%' - #后缀配置 于玩家名称后 - Suffix: - 1: - text: '&4[求助]&r ' - tip: - - '点击求助OP' - click: - type: 'COMMAND' - command: '管理员@%player_name% 我需要你的帮助!' - #同样可以添加多个 \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 24bb756..0f206d7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -11,6 +11,8 @@ Formats: permission: 'MiaoChat.default' #范围(0为无限制) range: 0 + #聊天格式(#xxx会调用format.yml下的对应格式) + format: '#world#player: ' #解析物品(%i=>手上物品,%0-9快捷栏对应物品) item: true #物品解析规则 @@ -21,6 +23,8 @@ Formats: index: 49 #权限 permission: 'MiaoChat.admin' + #聊天格式(#xxx会调用format.yml下的对应格式) + format: '#admin#world#player#help: ' #范围(0为无限制) range: 0 #解析物品(%i=>手上物品,%0-9快捷栏对应物品) diff --git a/src/main/resources/default.yml b/src/main/resources/default.yml deleted file mode 100644 index b26d75e..0000000 --- a/src/main/resources/default.yml +++ /dev/null @@ -1,55 +0,0 @@ -#格式化配置 -Format: - #前缀配置 于玩家名称前 - Prefix: - #顺序 - 1: - #文本 支持PAPI变量 - text: '&6[&a%player_world%&6]' - #悬浮提示 支持PAPI - tip: - - '&6当前所在位置:' - - '&6世界: &d%player_world%' - - '&6坐标: &aX:%player_x% Y: %player_y% Z: %player_z%' - - '' - - '&c点击即可TP我!' - #点击操作 - click: - #操作类型: [COMMAND,SUGGEST,OPENURL] - #COMMAND代表执行命令 - #SUGGEST代表命令补全 - #OPENURL代表打开网址 - type: 'COMMAND' - #命令或网址 支持PAPI - command: '/tpa %player_name%' -# 可以添加多个 -# 2: -# #文本 支持PAPI变量 -# text: '&6[&a%player_world%&6]' -# #悬浮提示 支持PAPI -# tip: [] - #玩家名称配置 - Player: - text: '&b%player_name% ' - tip: - - '&6玩家名称: &b%player_name%' - - '&6玩家等级: &a%player_level%' - - '&6玩家血量: &c%player_health%' - - '&6玩家饥饿: &d%player_food_level%' - - '&6游戏模式: &4%player_gamemode%' - - '' - - '&c点击与我聊天' - click: - type: 'SUGGEST' - command: '/tell %player_name%' -#后缀配置 于玩家名称后 由于一般没人用 我注释掉了 -# Suffix: -# 1: -# text: '[变量1]' -# tip: -# - '变量1提示' -# click: -# type: 'COMMAND' -# command: 'say 命令执行' -# 同样可以添加多个 -# 2: \ No newline at end of file diff --git a/src/main/resources/format.yml b/src/main/resources/format.yml new file mode 100644 index 0000000..47b9965 --- /dev/null +++ b/src/main/resources/format.yml @@ -0,0 +1,42 @@ +#当前文件为定义格式的基础文件 +world: + #文本 支持PAPI变量 + text: '&6[&a%player_world%&6]' + #悬浮提示 支持PAPI + tip: + - '&6当前所在位置:' + - '&6世界: &d%player_world%' + - '&6坐标: &aX:%player_x% Y: %player_y% Z: %player_z%' + - '' + - '&c点击即可TP我!' + #点击操作 + click: + #操作类型: [COMMAND,SUGGEST,OPENURL] + #COMMAND代表执行命令 + #SUGGEST代表命令补全 + #OPENURL代表打开网址 + type: 'COMMAND' + #命令或网址 支持PAPI + command: '/tpa %player_name%' +player: + text: '&b%player_name%: ' + tip: + - '&6玩家名称: &b%player_name%' + - '&6玩家等级: &a%player_level%' + - '&6玩家血量: &c%player_health%' + - '&6玩家饥饿: &d%player_food_level%' + - '&6游戏模式: &4%player_gamemode%' + - '' + - '&c点击与我聊天' + click: + type: 'SUGGEST' + command: '/tell %player_name%' +admin: + text: '&6[&c管理员&6]' +help: + text: '&4[求助]&r ' + tip: + - '点击求助OP' + click: + type: 'COMMAND' + command: '管理员@%player_name% 我需要你的帮助!' \ No newline at end of file