From f43af0b3a1586f959fef190e35e1edaa027ec6f2 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 12 Sep 2016 01:42:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=201.1=E7=89=88=E6=9C=AC=E5=8F=91=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- pom.xml | 2 +- src/main/java/pw/yumc/MiaoChat/MiaoChat.java | 20 +++--- .../pw/yumc/MiaoChat/config/ChatConfig.java | 60 +++++++++++++++-- .../pw/yumc/MiaoChat/config/ChatRule.java | 29 +++++++-- .../java/pw/yumc/MiaoChat/config/Config.java | 64 ------------------- .../yumc/MiaoChat/listeners/ChatListener.java | 27 ++++---- src/main/resources/config.yml | 10 +-- src/main/resources/format.yml | 4 +- 8 files changed, 114 insertions(+), 102 deletions(-) delete mode 100644 src/main/java/pw/yumc/MiaoChat/config/Config.java diff --git a/pom.xml b/pom.xml index 912f471..f25553d 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 pw.yumc MiaoChat - 1.0 + 1.1 ${project.name} diff --git a/src/main/java/pw/yumc/MiaoChat/MiaoChat.java b/src/main/java/pw/yumc/MiaoChat/MiaoChat.java index 1730a2c..c214dc2 100644 --- a/src/main/java/pw/yumc/MiaoChat/MiaoChat.java +++ b/src/main/java/pw/yumc/MiaoChat/MiaoChat.java @@ -1,9 +1,11 @@ package pw.yumc.MiaoChat; +import org.bukkit.Material; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; -import pw.yumc.MiaoChat.config.Config; +import pw.yumc.MiaoChat.config.ChatConfig; import pw.yumc.MiaoChat.listeners.ChatListener; import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.commands.CommandArgument; @@ -12,20 +14,21 @@ import pw.yumc.YumCore.commands.CommandManager; import pw.yumc.YumCore.commands.annotation.Cmd; import pw.yumc.YumCore.commands.annotation.Help; import pw.yumc.YumCore.config.FileConfig; +import pw.yumc.YumCore.misc.L10N; public class MiaoChat extends JavaPlugin implements CommandExecutor { private FileConfig cfg; - private Config config; + private ChatConfig chatConfig; + + public ChatConfig getChatConfig() { + return chatConfig; + } @Override public FileConfiguration getConfig() { return cfg; } - public Config getConfigExt() { - return config; - } - @Cmd(permission = "MiaoChat.toggle") @Help("关闭聊天功能") public void off(final CommandArgument e) { @@ -44,19 +47,20 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor { public void onEnable() { new ChatListener(); new CommandManager("MiaoChat", this); + L10N.getItemFullName(new ItemStack(Material.DIAMOND_SWORD)); } @Override public void onLoad() { cfg = new FileConfig(); - config = new Config(); + chatConfig = new ChatConfig(); } @Cmd(permission = "MiaoChat.reload") @Help("重载配置文件") public void reload(final CommandArgument e) { cfg.reload(); - config.reload(); + chatConfig.reload(); Log.toSender(e.getSender(), "§a配置文件已重载!"); } } diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java index 3cdf2ea..48d3086 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java @@ -1,28 +1,78 @@ package pw.yumc.MiaoChat.config; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; +import org.bukkit.entity.Player; + +import pw.yumc.YumCore.bukkit.Log; +import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.config.FileConfig; +/** + * + * @since 2016年9月9日 下午4:40:50 + * @author 喵♂呜 + */ public class ChatConfig { - private final FileConfig config; + private static final String F = "Formats"; private final Map formats; + private final RuleComparator rulecomp; + private final List rules; + private final FileConfig config; + private final FileConfig format; - public ChatConfig(final FileConfig cfg) { - config = cfg; + public ChatConfig() { + config = P.getConfig(); + format = new FileConfig("format.yml"); + rulecomp = new RuleComparator(); formats = new HashMap<>(); + rules = new LinkedList<>(); reload(); } + /** + * 获得玩家可用的消息处理 + * + * @param player + * @return {@link ChatConfig} + */ + public ChatRule getChatRule(final Player player) { + for (final ChatRule cr : rules) { + Log.debug(cr.getName()); + if (cr.check(player)) { + return cr; + } + } + return null; + } + public ChatMessagePart getFormat(final String name) { return formats.get(name); } public void reload() { formats.clear(); - for (final String name : config.getKeys(false)) { - formats.put(name, new ChatMessagePart(config.getConfigurationSection(name))); + for (final String name : format.getKeys(false)) { + formats.put(name, new ChatMessagePart(format.getConfigurationSection(name))); + } + rules.clear(); + if (config.isSet(F)) { + for (final String rule : config.getConfigurationSection(F).getKeys(false)) { + rules.add(new ChatRule(rule, config.getConfigurationSection(F + "." + rule))); + } + } + Collections.sort(rules, rulecomp); + } + + private class RuleComparator implements Comparator { + @Override + public int compare(final ChatRule o1, final ChatRule o2) { + return o1.getIndex().compareTo(o2.getIndex()); } } } diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java b/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java index dd16cc2..f6fd54e 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java @@ -17,13 +17,13 @@ import pw.yumc.YumCore.config.InjectConfigurationSection; * @author 喵♂呜 */ public class ChatRule extends InjectConfigurationSection { - private transient static final Pattern FORMAT_PATTERN = Pattern.compile("[#]([^#]+)[#]"); + 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: ") + @Default("#world##player#: ") private String format; @Default("0") private Integer range; @@ -40,10 +40,7 @@ public class ChatRule extends InjectConfigurationSection { permission = String.format("MiaoChat.%s", name); } formats = new LinkedList<>(); - final Matcher m = FORMAT_PATTERN.matcher(format); - while (m.find()) { - formats.add(m.group(1)); - } + load(); } public boolean check(final Player player) { @@ -85,4 +82,24 @@ public class ChatRule extends InjectConfigurationSection { public boolean isItem() { return item; } + + private void load() { + final Matcher m = FORMAT_PATTERN.matcher(format); + final LinkedList temp = new LinkedList<>(); + while (m.find()) { + temp.add(m.group(1)); + } + String tempvar = format; + if (!temp.isEmpty()) { + for (final String var : temp) { + final String[] args = tempvar.split("\\[" + var + "\\]", 2); + formats.add(args[0]); + formats.add(var); + tempvar = args[1]; + } + if (!tempvar.isEmpty()) { + formats.add(tempvar); + } + } + } } diff --git a/src/main/java/pw/yumc/MiaoChat/config/Config.java b/src/main/java/pw/yumc/MiaoChat/config/Config.java deleted file mode 100644 index f226186..0000000 --- a/src/main/java/pw/yumc/MiaoChat/config/Config.java +++ /dev/null @@ -1,64 +0,0 @@ -package pw.yumc.MiaoChat.config; - -import java.util.Collections; -import java.util.Comparator; -import java.util.LinkedList; -import java.util.List; - -import org.bukkit.entity.Player; - -import pw.yumc.YumCore.bukkit.Log; -import pw.yumc.YumCore.bukkit.P; -import pw.yumc.YumCore.config.FileConfig; - -/** - * - * @since 2016年9月9日 下午4:40:50 - * @author 喵♂呜 - */ -public class Config { - private static final String F = "Formats"; - private final RuleComparator rulecomp; - private final List rules; - private final FileConfig config; - - public Config() { - config = P.getConfig(); - rulecomp = new RuleComparator(); - rules = new LinkedList<>(); - reload(); - } - - /** - * 获得玩家可用的消息处理 - * - * @param player - * @return {@link ChatConfig} - */ - public ChatRule getChatRule(final Player player) { - for (final ChatRule cr : rules) { - Log.debug(cr.getName()); - if (cr.check(player)) { - return cr; - } - } - return null; - } - - public void reload() { - rules.clear(); - if (config.isSet(F)) { - for (final String rule : config.getConfigurationSection(F).getKeys(false)) { - rules.add(new ChatRule(rule, config.getConfigurationSection(F + "." + rule))); - } - } - Collections.sort(rules, rulecomp); - } - - private class RuleComparator implements Comparator { - @Override - public int compare(final ChatRule o1, final ChatRule o2) { - return o1.getIndex().compareTo(o2.getIndex()); - } - } -} diff --git a/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java b/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java index 21e94f0..1cf7bcd 100644 --- a/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java +++ b/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java @@ -20,19 +20,23 @@ 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; import pw.yumc.YumCore.bukkit.compatible.C; +import pw.yumc.YumCore.misc.L10N; import pw.yumc.YumCore.statistic.Statistics; import pw.yumc.YumCore.tellraw.Tellraw; import pw.yumc.YumCore.update.SubscribeTask; public class ChatListener implements Listener { public static Set offList = new HashSet<>(); - static final Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)"); + private static final Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)"); - MiaoChat plugin = P.getPlugin(); + private final MiaoChat plugin = P.getPlugin(); + private final ChatConfig cc = plugin.getChatConfig(); public ChatListener() { Bukkit.getPluginManager().registerEvents(this, P.instance); @@ -43,7 +47,7 @@ public class ChatListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onChat(final AsyncPlayerChatEvent e) { final Player p = e.getPlayer(); - final ChatRule cr = plugin.getConfigExt().getChatRule(e.getPlayer()); + final ChatRule cr = cc.getChatRule(e.getPlayer()); if (cr == null) { return; } @@ -61,7 +65,12 @@ public class ChatListener implements Listener { private void handleFormat(final Player p, final Tellraw tr, final ChatRule cr) { final LinkedList formats = cr.getFormats(); for (final String format : formats) { - + final ChatMessagePart cmp = cc.getFormat(format); + if (cmp != null) { + cmp.then(tr, p); + } else { + tr.then(format); + } } } @@ -103,6 +112,7 @@ public class ChatListener implements Listener { Collection plist = Collections.emptyList(); if (range != 0) { plist = p.getNearbyEntities(range, range, range); + tr.send(p); } else { plist = C.Player.getOnlinePlayers(); } @@ -130,15 +140,10 @@ public class ChatListener implements Listener { while (!ml.isEmpty()) { final String mm = ml.removeFirst(); if (il.contains(mm)) { - ItemStack is = null; final char k = mm.charAt(1); - if (k == 'i') { - is = player.getItemInHand(); - } else { - is = player.getInventory().getItem(k - '0' - 1); - } + final ItemStack is = k == 'i' ? player.getItemInHand() : player.getInventory().getItem(k - '0' - 1); if (is != null && is.getType() != Material.AIR) { - tr.then(String.format(ChatColor.translateAlternateColorCodes('&', cr.getItemformat()), is.hasItemMeta() && is.getItemMeta().hasDisplayName() ? is.getItemMeta().getDisplayName() : is.getType().name())); + tr.then(String.format(ChatColor.translateAlternateColorCodes('&', cr.getItemformat()), L10N.getItemName(is))); tr.item(is); } } else { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 0f206d7..c806635 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,5 +1,5 @@ #配置文件版本号 请勿修改 -Version: 1.0 +Version: 1.1 #格式列表 Formats: @@ -11,8 +11,8 @@ Formats: permission: 'MiaoChat.default' #范围(0为无限制) range: 0 - #聊天格式(#xxx会调用format.yml下的对应格式) - format: '#world#player: ' + #聊天格式([xxx]会调用format.yml下的对应格式) + format: '[world][player]: ' #解析物品(%i=>手上物品,%0-9快捷栏对应物品) item: true #物品解析规则 @@ -23,8 +23,8 @@ Formats: index: 49 #权限 permission: 'MiaoChat.admin' - #聊天格式(#xxx会调用format.yml下的对应格式) - format: '#admin#world#player#help: ' + #聊天格式([xxx]会调用format.yml下的对应格式) + format: '[admin][world][player][help]: ' #范围(0为无限制) range: 0 #解析物品(%i=>手上物品,%0-9快捷栏对应物品) diff --git a/src/main/resources/format.yml b/src/main/resources/format.yml index 47b9965..31446cc 100644 --- a/src/main/resources/format.yml +++ b/src/main/resources/format.yml @@ -19,7 +19,7 @@ world: #命令或网址 支持PAPI command: '/tpa %player_name%' player: - text: '&b%player_name%: ' + text: '&b%player_name%' tip: - '&6玩家名称: &b%player_name%' - '&6玩家等级: &a%player_level%' @@ -34,7 +34,7 @@ player: admin: text: '&6[&c管理员&6]' help: - text: '&4[求助]&r ' + text: '&4[求助]' tip: - '点击求助OP' click: