feat: 使用变量参数化格式

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-09-10 18:27:32 +08:00
parent 3561b1809d
commit 55b420f11c
8 changed files with 78 additions and 159 deletions

View File

@ -1,53 +1,28 @@
package pw.yumc.MiaoChat.config; package pw.yumc.MiaoChat.config;
import java.util.LinkedList; import java.util.HashMap;
import java.util.List; import java.util.Map;
import pw.yumc.YumCore.config.FileConfig; import pw.yumc.YumCore.config.FileConfig;
public class ChatConfig { 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 FileConfig config;
private final List<ChatMessagePart> prefixs; private final Map<String, ChatMessagePart> formats;
private final List<ChatMessagePart> suffixs;
private ChatMessagePart player;
public ChatConfig(final FileConfig cfg) { public ChatConfig(final FileConfig cfg) {
config = cfg; config = cfg;
prefixs = new LinkedList<>(); formats = new HashMap<>();
suffixs = new LinkedList<>();
reload(); reload();
} }
public ChatMessagePart getPlayer() { public ChatMessagePart getFormat(final String name) {
return player; return formats.get(name);
}
public List<ChatMessagePart> getPrefixs() {
return prefixs;
}
public List<ChatMessagePart> getSuffixs() {
return suffixs;
} }
public void reload() { public void reload() {
prefixs.clear(); formats.clear();
if (config.isSet(PrefixKey)) { for (final String name : config.getKeys(false)) {
for (final String part : config.getConfigurationSection(PrefixKey).getKeys(false)) { formats.put(name, new ChatMessagePart(config.getConfigurationSection(name)));
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)));
}
} }
} }
} }

View File

@ -18,10 +18,10 @@ public class ChatMessagePart extends InjectConfigurationSection {
@Nullable @Nullable
@ConfigNode("click.type") @ConfigNode("click.type")
private String typestring; private String typestring;
private transient CLICKTYPE type = CLICKTYPE.SUGGEST;
@Nullable @Nullable
@ConfigNode("click.command") @ConfigNode("click.command")
private String command; private String command;
private transient CLICKTYPE type = CLICKTYPE.SUGGEST;
public ChatMessagePart(final ConfigurationSection config) { public ChatMessagePart(final ConfigurationSection config) {
super(config); super(config);

View File

@ -1,10 +1,13 @@
package pw.yumc.MiaoChat.config; 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.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import pw.yumc.YumCore.config.Default; import pw.yumc.YumCore.config.Default;
import pw.yumc.YumCore.config.FileConfig;
import pw.yumc.YumCore.config.InjectConfigurationSection; import pw.yumc.YumCore.config.InjectConfigurationSection;
/** /**
@ -14,17 +17,21 @@ import pw.yumc.YumCore.config.InjectConfigurationSection;
* @author * @author
*/ */
public class ChatRule extends InjectConfigurationSection { public class ChatRule extends InjectConfigurationSection {
private transient static final Pattern FORMAT_PATTERN = Pattern.compile("[#]([^#]+)[#]");
private transient String name; private transient String name;
@Default("50") @Default("50")
private Integer index; private Integer index;
@Default("MiaoChat.default")
private String permission; private String permission;
@Default("#world#player: ")
private String format;
@Default("0") @Default("0")
private Integer range; private Integer range;
@Default("false") @Default("false")
private Boolean item; private Boolean item;
@Default("&6[&b%s&6]&r") @Default("&6[&b%s&6]&r")
private String itemformat; private String itemformat;
private transient ChatConfig formats; private transient LinkedList<String> formats;
public ChatRule(final String name, final ConfigurationSection config) { public ChatRule(final String name, final ConfigurationSection config) {
super(config); super(config);
@ -32,14 +39,22 @@ public class ChatRule extends InjectConfigurationSection {
if (permission == null) { if (permission == null) {
permission = String.format("MiaoChat.%s", name); 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) { public boolean check(final Player player) {
return player.hasPermission(permission); return player.hasPermission(permission);
} }
public ChatConfig getFormats() { public String getFormat() {
return format;
}
public LinkedList<String> getFormats() {
return formats; return formats;
} }

View File

@ -20,8 +20,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import pw.yumc.MiaoChat.MiaoChat; 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.MiaoChat.config.ChatRule;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P; 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) { private void handleFormat(final Player p, final Tellraw tr, final ChatRule cr) {
final ChatConfig cc = cr.getFormats(); final LinkedList<String> formats = cr.getFormats();
for (final ChatMessagePart cmp : cc.getPrefixs()) { for (final String format : formats) {
cmp.then(tr, p);
}
cc.getPlayer().then(tr, p);
for (final ChatMessagePart cmp : cc.getSuffixs()) {
cmp.then(tr, p);
} }
} }

View File

@ -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% 我需要你的帮助!'
#同样可以添加多个

View File

@ -11,6 +11,8 @@ Formats:
permission: 'MiaoChat.default' permission: 'MiaoChat.default'
#范围(0为无限制) #范围(0为无限制)
range: 0 range: 0
#聊天格式(#xxx会调用format.yml下的对应格式)
format: '#world#player: '
#解析物品(%i=>手上物品,%0-9快捷栏对应物品) #解析物品(%i=>手上物品,%0-9快捷栏对应物品)
item: true item: true
#物品解析规则 #物品解析规则
@ -21,6 +23,8 @@ Formats:
index: 49 index: 49
#权限 #权限
permission: 'MiaoChat.admin' permission: 'MiaoChat.admin'
#聊天格式(#xxx会调用format.yml下的对应格式)
format: '#admin#world#player#help: '
#范围(0为无限制) #范围(0为无限制)
range: 0 range: 0
#解析物品(%i=>手上物品,%0-9快捷栏对应物品) #解析物品(%i=>手上物品,%0-9快捷栏对应物品)

View File

@ -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:

View File

@ -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% 我需要你的帮助!'