feat: 优化代码

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-12-09 20:32:03 +08:00
parent e85f69e03a
commit 938bc2d03a
4 changed files with 45 additions and 39 deletions

Binary file not shown.

View File

@ -1,12 +1,13 @@
package pw.yumc.MiaoChat.config; package pw.yumc.MiaoChat.config;
import java.util.*;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P; import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.config.FileConfig; import pw.yumc.YumCore.config.FileConfig;
import java.util.*;
/** /**
* *
* @since 2016年9月9日 下午4:40:50 * @since 2016年9月9日 下午4:40:50
@ -52,8 +53,8 @@ public class ChatConfig {
return null; return null;
} }
public ChatMessagePart getFormat(String name) { public Map<String, ChatMessagePart> getFormats() {
return formats.get(name); return formats;
} }
public void load() { public void load() {

View File

@ -8,8 +8,12 @@ import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; 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.annotation.Default;
import pw.yumc.YumCore.config.inject.InjectConfigurationSection; 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 * @author
*/ */
public class ChatRule extends InjectConfigurationSection { 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 static Pattern FORMAT_PATTERN = Pattern.compile("[\\[]([^\\[\\]]+)[]]");
private transient String name; private transient String name;
@Default("50") @Default("50")
@ -88,6 +94,21 @@ public class ChatRule extends InjectConfigurationSection {
return item; 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() { private void load() {
formats = new LinkedList<>(); formats = new LinkedList<>();
Matcher m = FORMAT_PATTERN.matcher(format); Matcher m = FORMAT_PATTERN.matcher(format);

View File

@ -15,11 +15,9 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import me.clip.placeholderapi.PlaceholderAPI;
import pw.yumc.MiaoChat.MiaoChat; import pw.yumc.MiaoChat.MiaoChat;
import pw.yumc.MiaoChat.MiaoMessage; import pw.yumc.MiaoChat.MiaoMessage;
import pw.yumc.MiaoChat.config.ChatConfig; 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;
@ -53,30 +51,12 @@ public class ChatListener implements Listener {
return; return;
} }
e.setCancelled(true); 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); // Log.d("玩家: %s 使用 %s 规则 解析 %s", p.getName(), cr.getName(), message);
handleFormat(p, tr, cr); handleSend(p, handleTellraw(p, cr.create(p), cr, message), cr.getRange());
handleTellraw(p, tr, cr, message);
handleSend(p, tr, cr.getRange());
}
private String handleFormat(Player p, Tellraw tr, ChatRule cr) {
LinkedList<String> 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());
} }
private LinkedList<String> handleMessage(LinkedList<String> il, String message) { private LinkedList<String> handleMessage(LinkedList<String> il, String message) {
@ -121,12 +101,16 @@ public class ChatListener implements Listener {
} }
private void handleSend(Player p, Tellraw tr, int range) { private void handleSend(Player p, Tellraw tr, int range) {
Collection<? extends Entity> plist; List<Player> plist = new ArrayList<>();
if (range != 0) { if (range != 0) {
plist = p.getNearbyEntities(range, range, range); for (Entity ent : p.getNearbyEntities(range, range, range)) {
tr.send(p); if (ent instanceof Player) {
plist.add((Player) ent);
}
}
plist.add(p);
} else { } else {
plist = C.Player.getOnlinePlayers(); plist.addAll(C.Player.getOnlinePlayers());
if (cc.isBungeeCord()) { if (cc.isBungeeCord()) {
byte[] mm = MiaoMessage.encode(tr.toJsonString()); byte[] mm = MiaoMessage.encode(tr.toJsonString());
if (mm == null) { if (mm == null) {
@ -136,29 +120,28 @@ public class ChatListener implements Listener {
} }
} }
} }
for (Entity ne : plist) { for (Player player : plist) {
// 此处必须进行强制转换 老版本服务器的Entity没有getName() if (!offList.contains(player.getName())) {
if (ne instanceof Player && !offList.contains(((Player) ne).getName())) { tr.send(player);
tr.send(ne);
} }
} }
Bukkit.getConsoleSender().sendMessage(tr.toOldMessageFormat()); Bukkit.getConsoleSender().sendMessage(tr.toOldMessageFormat());
} }
private void handleTellraw(Player player, Tellraw tr, ChatRule cr, String message) { private Tellraw handleTellraw(Player player, Tellraw tr, ChatRule cr, String message) {
if (message.isEmpty()) { return; } if (message.isEmpty()) { return tr; }
if (player.hasPermission("MiaoChat.color")) { if (player.hasPermission("MiaoChat.color")) {
message = ChatColor.translateAlternateColorCodes('&', message); message = ChatColor.translateAlternateColorCodes('&', message);
} }
if (!cr.isItem()) { if (!cr.isItem()) {
tr.then(message); tr.then(message);
return; return tr;
} }
LinkedList<String> il = handlePattern(message); LinkedList<String> il = handlePattern(message);
// 如果返回null说明存在相同的物品 // 如果返回null说明存在相同的物品
if (il == null) { if (il == null) {
Log.sender(player, "§c不允许展示相同的物品!"); Log.sender(player, "§c不允许展示相同的物品!");
return; return tr;
} }
LinkedList<String> ml = handleMessage(il, message); LinkedList<String> ml = handleMessage(il, message);
// Log.d("处理Tellraw格式..."); // Log.d("处理Tellraw格式...");
@ -177,5 +160,6 @@ public class ChatListener implements Listener {
tr.then(cr.getLastColor() + mm); tr.then(cr.getLastColor() + mm);
} }
} }
return tr;
} }
} }