diff --git a/lib/PlaceholderAPI.jar b/lib/PlaceholderAPI.jar deleted file mode 100644 index 87bd3ab..0000000 Binary files a/lib/PlaceholderAPI.jar and /dev/null differ diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java index 24ba714..6a8da2f 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatConfig.java @@ -1,12 +1,13 @@ package pw.yumc.MiaoChat.config; +import java.util.*; + import org.bukkit.entity.Player; + import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.config.FileConfig; -import java.util.*; - /** * * @since 2016年9月9日 下午4:40:50 @@ -52,8 +53,8 @@ public class ChatConfig { return null; } - public ChatMessagePart getFormat(String name) { - return formats.get(name); + public Map getFormats() { + return formats; } public void load() { diff --git a/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java b/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java index 84c91ad..1544f5b 100644 --- a/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java +++ b/src/main/java/pw/yumc/MiaoChat/config/ChatRule.java @@ -8,8 +8,12 @@ import org.bukkit.ChatColor; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; +import me.clip.placeholderapi.PlaceholderAPI; +import pw.yumc.MiaoChat.MiaoChat; +import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.config.annotation.Default; import pw.yumc.YumCore.config.inject.InjectConfigurationSection; +import pw.yumc.YumCore.tellraw.Tellraw; /** * 聊天规则 @@ -18,6 +22,8 @@ import pw.yumc.YumCore.config.inject.InjectConfigurationSection; * @author 喵♂呜 */ public class ChatRule extends InjectConfigurationSection { + private transient static MiaoChat plugin = P.getPlugin(); + private transient static ChatConfig cc = plugin.getChatConfig(); private transient static Pattern FORMAT_PATTERN = Pattern.compile("[\\[]([^\\[\\]]+)[]]"); private transient String name; @Default("50") @@ -88,6 +94,21 @@ public class ChatRule extends InjectConfigurationSection { return item; } + public Tellraw create(Player p) { + Tellraw tr = Tellraw.create(); + for (String format : formats) { + ChatMessagePart cmp = cc.getFormats().get(format); + if (cmp != null) { + // Log.d("解析格式: %s", format); + cmp.then(tr, p); + } else { + // Log.d("追加文本: %s", format); + tr.then(PlaceholderAPI.setPlaceholders(p, format)); + } + } + return tr; + } + private void load() { formats = new LinkedList<>(); Matcher m = FORMAT_PATTERN.matcher(format); diff --git a/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java b/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java index 1b71e08..3cfe297 100644 --- a/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java +++ b/src/main/java/pw/yumc/MiaoChat/listeners/ChatListener.java @@ -15,11 +15,9 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.inventory.ItemStack; -import me.clip.placeholderapi.PlaceholderAPI; import pw.yumc.MiaoChat.MiaoChat; import pw.yumc.MiaoChat.MiaoMessage; 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; @@ -53,30 +51,12 @@ public class ChatListener implements Listener { return; } e.setCancelled(true); - handleChat(p, Tellraw.create(), cr, e.getMessage()); + handleChat(p, cr, e.getMessage()); } - private void handleChat(Player p, Tellraw tr, ChatRule cr, String message) { + private void handleChat(Player p, ChatRule cr, String message) { // Log.d("玩家: %s 使用 %s 规则 解析 %s", p.getName(), cr.getName(), message); - handleFormat(p, tr, cr); - handleTellraw(p, tr, cr, message); - handleSend(p, tr, cr.getRange()); - } - - private String handleFormat(Player p, Tellraw tr, ChatRule cr) { - LinkedList formats = cr.getFormats(); - // Log.d("处理前缀信息..."); - for (String format : formats) { - ChatMessagePart cmp = cc.getFormat(format); - if (cmp != null) { - // Log.d("解析格式: %s", format); - cmp.then(tr, p); - } else { - // Log.d("追加文本: %s", format); - tr.then(PlaceholderAPI.setPlaceholders(p, format)); - } - } - return ChatColor.getLastColors(formats.isEmpty() ? "§r" : formats.getLast()); + handleSend(p, handleTellraw(p, cr.create(p), cr, message), cr.getRange()); } private LinkedList handleMessage(LinkedList il, String message) { @@ -121,12 +101,16 @@ public class ChatListener implements Listener { } private void handleSend(Player p, Tellraw tr, int range) { - Collection plist; + List plist = new ArrayList<>(); if (range != 0) { - plist = p.getNearbyEntities(range, range, range); - tr.send(p); + for (Entity ent : p.getNearbyEntities(range, range, range)) { + if (ent instanceof Player) { + plist.add((Player) ent); + } + } + plist.add(p); } else { - plist = C.Player.getOnlinePlayers(); + plist.addAll(C.Player.getOnlinePlayers()); if (cc.isBungeeCord()) { byte[] mm = MiaoMessage.encode(tr.toJsonString()); if (mm == null) { @@ -136,29 +120,28 @@ public class ChatListener implements Listener { } } } - for (Entity ne : plist) { - // 此处必须进行强制转换 老版本服务器的Entity没有getName() - if (ne instanceof Player && !offList.contains(((Player) ne).getName())) { - tr.send(ne); + for (Player player : plist) { + if (!offList.contains(player.getName())) { + tr.send(player); } } Bukkit.getConsoleSender().sendMessage(tr.toOldMessageFormat()); } - private void handleTellraw(Player player, Tellraw tr, ChatRule cr, String message) { - if (message.isEmpty()) { return; } + private Tellraw handleTellraw(Player player, Tellraw tr, ChatRule cr, String message) { + if (message.isEmpty()) { return tr; } if (player.hasPermission("MiaoChat.color")) { message = ChatColor.translateAlternateColorCodes('&', message); } if (!cr.isItem()) { tr.then(message); - return; + return tr; } LinkedList il = handlePattern(message); // 如果返回null说明存在相同的物品 if (il == null) { Log.sender(player, "§c不允许展示相同的物品!"); - return; + return tr; } LinkedList ml = handleMessage(il, message); // Log.d("处理Tellraw格式..."); @@ -177,5 +160,6 @@ public class ChatListener implements Listener { tr.then(cr.getLastColor() + mm); } } + return tr; } }