From 335b04d01a1db7eaf0d5e30710937c461471c63d Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sat, 10 Sep 2016 03:17:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E7=89=A9=E5=93=81?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=20=E6=B7=BB=E5=8A=A0=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/java/pw/yumc/MiaoChat/MiaoChat.java | 18 ++- .../pw/yumc/MiaoChat/config/ChatConfig.java | 5 +- .../yumc/MiaoChat/config/ChatMessagePart.java | 4 + .../pw/yumc/MiaoChat/config/ChatRule.java | 52 ++++++--- .../java/pw/yumc/MiaoChat/config/Config.java | 39 ++++++- .../yumc/MiaoChat/listeners/ChatListener.java | 104 +++++++++++++++++- src/main/resources/admin.yml | 55 +++++++++ src/main/resources/config.yml | 12 +- src/main/resources/plugin.yml | 6 + 9 files changed, 264 insertions(+), 31 deletions(-) create mode 100644 src/main/resources/admin.yml diff --git a/src/main/java/pw/yumc/MiaoChat/MiaoChat.java b/src/main/java/pw/yumc/MiaoChat/MiaoChat.java index f1362d9..b4cb596 100644 --- a/src/main/java/pw/yumc/MiaoChat/MiaoChat.java +++ b/src/main/java/pw/yumc/MiaoChat/MiaoChat.java @@ -5,6 +5,7 @@ import org.bukkit.plugin.java.JavaPlugin; import pw.yumc.MiaoChat.config.Config; import pw.yumc.MiaoChat.listeners.ChatListener; +import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.commands.CommandArgument; import pw.yumc.YumCore.commands.CommandExecutor; import pw.yumc.YumCore.commands.CommandManager; @@ -25,6 +26,20 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor { return config; } + @Cmd(permission = "MiaoChat.toggle") + @Help("关闭聊天功能") + public void off(final CommandArgument e) { + ChatListener.offList.remove(e.getSender().getName()); + Log.toSender(e.getSender(), "§c聊天功能已关闭!"); + } + + @Cmd(permission = "MiaoChat.toggle") + @Help("开启聊天功能") + public void on(final CommandArgument e) { + ChatListener.offList.add(e.getSender().getName()); + Log.toSender(e.getSender(), "§a聊天功能已开启!"); + } + @Override public void onEnable() { new ChatListener(); @@ -34,6 +49,7 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor { @Override public void onLoad() { cfg = new FileConfig(); + config = new Config(); } @Cmd(permission = "MiaoChat.reload") @@ -41,6 +57,6 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor { public void reload(final CommandArgument e) { cfg.reload(); config.reload(); - e.getSender().sendMessage("§a配置文件已重载!"); + 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 0b9e610..500d74e 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java @@ -7,6 +7,7 @@ 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; @@ -39,7 +40,9 @@ public class ChatConfig { prefixs.add(new ChatMessagePart(config.getConfigurationSection(PrefixKey + "." + part))); } } - player = new ChatMessagePart(config.getConfigurationSection("Format.Player")); + 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)) { diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatMessagePart.java b/src/main/java/pw/yumc/MiaoChat/config/ChatMessagePart.java index 98701cd..7f14944 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatMessagePart.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatMessagePart.java @@ -8,14 +8,18 @@ import org.bukkit.entity.Player; import me.clip.placeholderapi.PlaceholderAPI; import pw.yumc.YumCore.config.ConfigNode; import pw.yumc.YumCore.config.InjectConfigurationSection; +import pw.yumc.YumCore.config.Nullable; import pw.yumc.YumCore.tellraw.Tellraw; public class ChatMessagePart extends InjectConfigurationSection { private String text; + @Nullable private List tip; + @Nullable @ConfigNode("click.type") private String typestring; private transient CLICKTYPE type = CLICKTYPE.SUGGEST; + @Nullable @ConfigNode("click.command") private String command; diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java b/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java index 0b53fa5..beb8d8b 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java @@ -1,7 +1,10 @@ package pw.yumc.MiaoChat.config; 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; /** @@ -11,41 +14,60 @@ import pw.yumc.YumCore.config.InjectConfigurationSection; * @author 喵♂呜 */ public class ChatRule extends InjectConfigurationSection { + private transient String name; + @Default("50") private Integer index; private String permission; + @Default("0") private Integer range; - private boolean item; + @Default("false") + private Boolean item; + @Default("&6[%s&6]&r") + private String itemformat; + private transient ChatConfig formats; - public ChatRule(final ConfigurationSection config) { + public ChatRule(final String name, final ConfigurationSection config) { super(config); + this.name = name; + if (permission == null) { + permission = String.format("MiaoChat.%s", name); + } + formats = new ChatConfig(new FileConfig(name + ".yml")); + } + + public boolean check(final Player player) { + return player.hasPermission(permission); + } + + public ChatConfig getFormats() { + return formats; } - /** - * @return the index - */ public Integer getIndex() { return index; } - /** - * @return the permission - */ + public Boolean getItem() { + return item; + } + + public String getItemformat() { + return itemformat; + } + + public String getName() { + return name; + } + public String getPermission() { return permission; } - /** - * @return the range - */ public Integer getRange() { return range; } - /** - * @return the item - */ public boolean isItem() { return item; } - } diff --git a/src/main/java/pw/yumc/MiaoChat/config/Config.java b/src/main/java/pw/yumc/MiaoChat/config/Config.java index 9b41879..f226186 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/Config.java +++ b/src/main/java/pw/yumc/MiaoChat/config/Config.java @@ -1,12 +1,15 @@ package pw.yumc.MiaoChat.config; -import java.util.HashMap; -import java.util.Map; +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; import org.bukkit.entity.Player; -import cn.citycraft.PluginHelper.config.FileConfig; +import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; +import pw.yumc.YumCore.config.FileConfig; /** * @@ -14,10 +17,15 @@ import pw.yumc.YumCore.bukkit.P; * @author 喵♂呜 */ public class Config { - FileConfig config = P.getConfig(); - Map formats = new HashMap<>(); + 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(); } @@ -27,11 +35,30 @@ public class Config { * @param player * @return {@link ChatConfig} */ - public ChatConfig getChatConfig(final Player player) { + 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 cb5bf87..d775522 100644 --- a/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java +++ b/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java @@ -1,21 +1,38 @@ package pw.yumc.MiaoChat.listeners; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; 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.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]?)"); + MiaoChat plugin = P.getPlugin(); public ChatListener() { @@ -27,10 +44,11 @@ public class ChatListener implements Listener { @EventHandler public void onChat(final AsyncPlayerChatEvent e) { final Player p = e.getPlayer(); - final ChatConfig cc = plugin.getConfigExt().getChatConfig(e.getPlayer()); - if (cc == null) { + final ChatRule cr = plugin.getConfigExt().getChatRule(e.getPlayer()); + if (cr == null) { return; } + final ChatConfig cc = cr.getFormats(); e.setCancelled(true); final String msg = e.getMessage(); final Tellraw tr = Tellraw.create(); @@ -41,7 +59,85 @@ public class ChatListener implements Listener { for (final ChatMessagePart cmp : cc.getSuffixs()) { cmp.then(tr, p); } - tr.then(ChatColor.translateAlternateColorCodes('&', msg)); - tr.broadcast(); + final String message = ChatColor.translateAlternateColorCodes('&', msg); + if (!message.isEmpty() && cr.isItem()) { + if (!handlerTellraw(p, tr, message, cr.getItemformat())) { + Log.toSender(p, "§c不允许展示相同的物品!"); + return; + } + } + final int range = cr.getRange(); + Collection plist = Collections.emptyList(); + if (range != 0) { + plist = p.getNearbyEntities(range, range, range); + } else { + plist = C.Player.getOnlinePlayers(); + } + for (final Entity ne : plist) { + if (ne instanceof Player && !offList.contains(ne.getName())) { + tr.send(ne); + } + } + } + + private LinkedList handlerMessage(final LinkedList il, String message) { + final LinkedList mlist = new LinkedList<>(); + if (!il.isEmpty()) { + for (final String k : il) { + final String[] args = message.split(k, 2); + mlist.add(args[0]); + mlist.add(k); + message = args[1]; + } + } + if (!message.isEmpty()) { + mlist.add(message); + } + return mlist; + } + + private LinkedList handlerPattern(final String message) { + final Matcher m = ITEM_PATTERN.matcher(message); + final Set temp = new HashSet<>(); + final LinkedList ilist = new LinkedList<>(); + while (m.find()) { + final String key = m.group(0); + if (key.length() == 2) { + if (temp.add(key)) { + ilist.add(key); + } else { + return null; + } + } + } + return ilist; + } + + private boolean handlerTellraw(final Player player, final Tellraw tr, final String message, final String itemformat) { + final LinkedList il = handlerPattern(message); + if (il == null) { + return false; + } + final LinkedList ml = handlerMessage(il, message); + 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); + } + if (is != null && is.getType() != Material.AIR) { + tr.then(String.format(ChatColor.translateAlternateColorCodes('&', itemformat), + is.hasItemMeta() && is.getItemMeta().hasDisplayName() ? is.getItemMeta().getDisplayName() : is.getType().name())); + tr.item(is); + } + } else { + tr.then(mm); + } + } + return true; } } diff --git a/src/main/resources/admin.yml b/src/main/resources/admin.yml new file mode 100644 index 0000000..781bf21 --- /dev/null +++ b/src/main/resources/admin.yml @@ -0,0 +1,55 @@ +#格式化配置 +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] + #COMMAND代表执行命令 + #SUGGEST代表命令补全 + 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 d7d5e8d..24bb756 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -11,15 +11,19 @@ Formats: permission: 'MiaoChat.default' #范围(0为无限制) range: 0 - #解析物品(%i%=>手上物品,%0-9%快捷栏对应物品) + #解析物品(%i=>手上物品,%0-9快捷栏对应物品) item: true + #物品解析规则 + itemformat: '&6[&b%s&6]&r' #格式名称 对应当前文件夹下的admin.yml admin: #优先级(将按照从小到大依次检测 比如 1-50 优先检测 1 符合则显示 不符合 检测 2 ...) index: 49 #权限 - permission: 'MiaoChat.reload' + permission: 'MiaoChat.admin' #范围(0为无限制) range: 0 - #解析物品(%i%=>手上物品,%0-9%快捷栏对应物品) - item: true \ No newline at end of file + #解析物品(%i=>手上物品,%0-9快捷栏对应物品) + item: true + #物品解析规则 + itemformat: '&6[&b%s&6]&r' \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 532a562..ef156fc 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -19,6 +19,12 @@ permissions: ${project.artifactId}.default: description: 默认格式权限! default: true + ${project.artifactId}.admin: + description: 管理员格式权限! + default: op + ${project.artifactId}.toggle: + description: 开启关闭聊天! + default: true ${project.artifactId}.reload: description: 重新载入插件! default: op \ No newline at end of file