fix: 修复聊天颜色错误

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-10-16 22:16:47 +08:00
parent cabce51520
commit 0366ad42cf
7 changed files with 83 additions and 77 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>MiaoChat</artifactId> <artifactId>MiaoChat</artifactId>
<version>1.3.2</version> <version>1.3.3</version>
<build> <build>
<finalName>${project.name}</finalName> <finalName>${project.name}</finalName>
<resources> <resources>
@ -60,6 +60,7 @@
</goals> </goals>
<configuration> <configuration>
<options> <options>
<option>-repackageclasses \ʼ.ʽ.ʾ.${project.artifactId}</option>
<option>-keep class ${project.groupId}.${project.artifactId}.${project.artifactId}</option> <option>-keep class ${project.groupId}.${project.artifactId}.${project.artifactId}</option>
</options> </options>
<libs> <libs>
@ -109,7 +110,7 @@
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId> <artifactId>YumCore</artifactId>
<type>jar</type> <type>jar</type>
<version>1.0</version> <version>1.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>

View File

@ -16,9 +16,6 @@
# -----保留所有属性 # -----保留所有属性
-keepattributes ** -keepattributes **
# -----替换包名
-repackageclasses \ʼ.ʽ.ʾ
# -----公共数据不混淆----- # -----公共数据不混淆-----
-keep class cn.citycraft.CommonData.** {*;} -keep class cn.citycraft.CommonData.** {*;}
@ -37,13 +34,13 @@
} }
# -----保护配置注入不被清理----- # -----保护配置注入不被清理-----
-keepclassmembers class * extends **.config.Inject** { -keepclassmembers class * extends **.config**Inject** {
<fields>; <fields>;
} }
# -----保护注解命令方法不被清理----- # -----保护注解命令方法不被清理-----
-keepclassmembers class **.commands.annotation.** { <methods>; } -keepclassmembers class **.commands.annotation.** { <methods>; }
-keepclassmembers class * implements **.commands.CommandExecutor { <methods>; } -keepclassmembers class * implements **.commands**CommandExecutor { <methods>; }
# -----保护注解NotProguard标记----- # -----保护注解NotProguard标记-----
-keep class **.NotProguard -keep class **.NotProguard

View File

@ -1,6 +1,7 @@
package pw.yumc.MiaoChat; package pw.yumc.MiaoChat;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -8,11 +9,10 @@ import org.bukkit.plugin.java.JavaPlugin;
import pw.yumc.MiaoChat.config.ChatConfig; import pw.yumc.MiaoChat.config.ChatConfig;
import pw.yumc.MiaoChat.listeners.ChatListener; import pw.yumc.MiaoChat.listeners.ChatListener;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.commands.CommandArgument;
import pw.yumc.YumCore.commands.CommandExecutor;
import pw.yumc.YumCore.commands.CommandManager; import pw.yumc.YumCore.commands.CommandManager;
import pw.yumc.YumCore.commands.annotation.Cmd; import pw.yumc.YumCore.commands.annotation.Cmd;
import pw.yumc.YumCore.commands.annotation.Help; import pw.yumc.YumCore.commands.annotation.Help;
import pw.yumc.YumCore.commands.interfaces.CommandExecutor;
import pw.yumc.YumCore.config.FileConfig; import pw.yumc.YumCore.config.FileConfig;
import pw.yumc.YumCore.global.L10N; import pw.yumc.YumCore.global.L10N;
@ -31,16 +31,16 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor {
@Cmd(permission = "MiaoChat.toggle") @Cmd(permission = "MiaoChat.toggle")
@Help("关闭聊天功能") @Help("关闭聊天功能")
public void off(final CommandArgument e) { public void off(CommandSender sender) {
ChatListener.offList.add(e.getSender().getName()); ChatListener.offList.add(sender.getName());
Log.toSender(e.getSender(), "§c聊天功能已关闭!"); Log.toSender(sender, "§c聊天功能已关闭!");
} }
@Cmd(permission = "MiaoChat.toggle") @Cmd(permission = "MiaoChat.toggle")
@Help("开启聊天功能") @Help("开启聊天功能")
public void on(final CommandArgument e) { public void on(CommandSender sender) {
ChatListener.offList.remove(e.getSender().getName()); ChatListener.offList.remove(sender.getName());
Log.toSender(e.getSender(), "§a聊天功能已开启!"); Log.toSender(sender, "§a聊天功能已开启!");
} }
@Override @Override
@ -58,9 +58,9 @@ public class MiaoChat extends JavaPlugin implements CommandExecutor {
@Cmd(permission = "MiaoChat.reload") @Cmd(permission = "MiaoChat.reload")
@Help("重载配置文件") @Help("重载配置文件")
public void reload(final CommandArgument e) { public void reload(CommandSender sender) {
cfg.reload(); cfg.reload();
chatConfig.reload(); chatConfig.reload();
Log.toSender(e.getSender(), "§a配置文件已重载!"); Log.toSender(sender, "§a配置文件已重载!");
} }
} }

View File

@ -18,12 +18,12 @@ import pw.yumc.YumCore.config.FileConfig;
* @author * @author
*/ */
public class ChatConfig { public class ChatConfig {
private static final String F = "Formats"; private static String F = "Formats";
private final Map<String, ChatMessagePart> formats; private Map<String, ChatMessagePart> formats;
private final RuleComparator rulecomp; private RuleComparator rulecomp;
private final LinkedList<ChatRule> rules; private LinkedList<ChatRule> rules;
private final FileConfig config; private FileConfig config;
private final FileConfig format; private FileConfig format;
public ChatConfig() { public ChatConfig() {
config = P.getConfig(); config = P.getConfig();
@ -40,8 +40,8 @@ public class ChatConfig {
* @param player * @param player
* @return {@link ChatConfig} * @return {@link ChatConfig}
*/ */
public ChatRule getChatRule(final Player player) { public ChatRule getChatRule(Player player) {
for (final ChatRule cr : rules) { for (ChatRule cr : rules) {
Log.debug(cr.getName()); Log.debug(cr.getName());
if (cr.check(player)) { if (cr.check(player)) {
return cr; return cr;
@ -50,19 +50,19 @@ public class ChatConfig {
return null; return null;
} }
public ChatMessagePart getFormat(final String name) { public ChatMessagePart getFormat(String name) {
return formats.get(name); return formats.get(name);
} }
public void load() { public void load() {
formats.clear(); formats.clear();
for (final String name : format.getKeys(false)) { for (String name : format.getKeys(false)) {
formats.put(name, new ChatMessagePart(format.getConfigurationSection(name))); formats.put(name, new ChatMessagePart(format.getConfigurationSection(name)));
Log.d("载入聊天格式: %s", name); Log.d("载入聊天格式: %s", name);
} }
rules.clear(); rules.clear();
if (config.isSet(F)) { if (config.isSet(F)) {
for (final String rule : config.getConfigurationSection(F).getKeys(false)) { for (String rule : config.getConfigurationSection(F).getKeys(false)) {
rules.add(new ChatRule(rule, config.getConfigurationSection(F + "." + rule))); rules.add(new ChatRule(rule, config.getConfigurationSection(F + "." + rule)));
Log.d("载入聊天规则: %s => \"%s\"", rule, rules.getLast().getFormat()); Log.d("载入聊天规则: %s => \"%s\"", rule, rules.getLast().getFormat());
} }
@ -77,7 +77,7 @@ public class ChatConfig {
private class RuleComparator implements Comparator<ChatRule> { private class RuleComparator implements Comparator<ChatRule> {
@Override @Override
public int compare(final ChatRule o1, final ChatRule o2) { public int compare(ChatRule o1, ChatRule o2) {
return o1.getIndex().compareTo(o2.getIndex()); return o1.getIndex().compareTo(o2.getIndex());
} }
} }

View File

@ -6,9 +6,9 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import pw.yumc.YumCore.config.ConfigNode; import pw.yumc.YumCore.config.annotation.ConfigNode;
import pw.yumc.YumCore.config.InjectConfigurationSection; import pw.yumc.YumCore.config.annotation.Nullable;
import pw.yumc.YumCore.config.Nullable; import pw.yumc.YumCore.config.inject.InjectConfigurationSection;
import pw.yumc.YumCore.tellraw.Tellraw; import pw.yumc.YumCore.tellraw.Tellraw;
public class ChatMessagePart extends InjectConfigurationSection { public class ChatMessagePart extends InjectConfigurationSection {
@ -23,20 +23,20 @@ public class ChatMessagePart extends InjectConfigurationSection {
private String command; private String command;
private transient CLICKTYPE type = CLICKTYPE.SUGGEST; private transient CLICKTYPE type = CLICKTYPE.SUGGEST;
public ChatMessagePart(final ConfigurationSection config) { public ChatMessagePart(ConfigurationSection config) {
super(config); super(config);
if (typestring != null) { if (typestring != null) {
type = CLICKTYPE.valueOf(typestring); type = CLICKTYPE.valueOf(typestring);
} }
} }
public Tellraw then(final Tellraw tr, final Player p) { public Tellraw then(Tellraw tr, Player p) {
tr.then(f(p, text)); tr.then(f(p, text));
if (tip != null && !tip.isEmpty()) { if (tip != null && !tip.isEmpty()) {
tr.tip(f(p, tip)); tr.tip(f(p, tip));
} }
if (command != null && !command.isEmpty()) { if (command != null && !command.isEmpty()) {
final String tc = f(p, command); String tc = f(p, command);
switch (type) { switch (type) {
case COMMAND: case COMMAND:
tr.command(tc); tr.command(tc);
@ -49,17 +49,16 @@ public class ChatMessagePart extends InjectConfigurationSection {
break; break;
default: default:
break; break;
} }
} }
return tr; return tr;
} }
private List<String> f(final Player player, final List<String> text) { private List<String> f(Player player, List<String> text) {
return PlaceholderAPI.setPlaceholders(player, text); return PlaceholderAPI.setPlaceholders(player, text);
} }
private String f(final Player player, final String text) { private String f(Player player, String text) {
return PlaceholderAPI.setPlaceholders(player, text); return PlaceholderAPI.setPlaceholders(player, text);
} }
} }

View File

@ -4,11 +4,12 @@ import java.util.LinkedList;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
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 pw.yumc.YumCore.config.Default; import pw.yumc.YumCore.config.annotation.Default;
import pw.yumc.YumCore.config.InjectConfigurationSection; import pw.yumc.YumCore.config.inject.InjectConfigurationSection;
/** /**
* 聊天规则 * 聊天规则
@ -17,7 +18,7 @@ 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 static Pattern FORMAT_PATTERN = Pattern.compile("[\\[]([^\\[\\]]+)[\\]]");
private transient String name; private transient String name;
@Default("50") @Default("50")
private Integer index; private Integer index;
@ -32,8 +33,9 @@ public class ChatRule extends InjectConfigurationSection {
@Default("&6[&b%s&6]&r") @Default("&6[&b%s&6]&r")
private String itemformat; private String itemformat;
private transient LinkedList<String> formats; private transient LinkedList<String> formats;
private transient String lastColor;
public ChatRule(final String name, final ConfigurationSection config) { public ChatRule(String name, ConfigurationSection config) {
super(config); super(config);
this.name = name; this.name = name;
if (permission == null) { if (permission == null) {
@ -41,9 +43,10 @@ public class ChatRule extends InjectConfigurationSection {
} }
formats = new LinkedList<>(); formats = new LinkedList<>();
load(); load();
lastColor = ChatColor.getLastColors(ChatColor.translateAlternateColorCodes('&', formats.getLast()));
} }
public boolean check(final Player player) { public boolean check(Player player) {
return player.hasPermission(permission); return player.hasPermission(permission);
} }
@ -67,6 +70,10 @@ public class ChatRule extends InjectConfigurationSection {
return itemformat; return itemformat;
} }
public String getLastColor() {
return lastColor;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -84,15 +91,15 @@ public class ChatRule extends InjectConfigurationSection {
} }
private void load() { private void load() {
final Matcher m = FORMAT_PATTERN.matcher(format); Matcher m = FORMAT_PATTERN.matcher(format);
final LinkedList<String> temp = new LinkedList<>(); LinkedList<String> temp = new LinkedList<>();
while (m.find()) { while (m.find()) {
temp.add(m.group(1)); temp.add(m.group(1));
} }
String tempvar = format; String tempvar = format;
if (!temp.isEmpty()) { if (!temp.isEmpty()) {
for (final String var : temp) { for (String var : temp) {
final String[] args = tempvar.split("\\[" + var + "\\]", 2); String[] args = tempvar.split("\\[" + var + "\\]", 2);
if (!"".equals(args[0])) { if (!"".equals(args[0])) {
formats.add(args[0]); formats.add(args[0]);
} }

View File

@ -34,10 +34,10 @@ import pw.yumc.YumCore.update.SubscribeTask;
public class ChatListener implements Listener { public class ChatListener implements Listener {
public static Set<String> offList = new HashSet<>(); public static Set<String> offList = new HashSet<>();
private static final Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)"); private static Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)");
private final MiaoChat plugin = P.getPlugin(); private MiaoChat plugin = P.getPlugin();
private final ChatConfig cc = plugin.getChatConfig(); private ChatConfig cc = plugin.getChatConfig();
public ChatListener() { public ChatListener() {
Bukkit.getPluginManager().registerEvents(this, P.instance); Bukkit.getPluginManager().registerEvents(this, P.instance);
@ -46,30 +46,30 @@ public class ChatListener implements Listener {
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onChat(final AsyncPlayerChatEvent e) { public void onChat(AsyncPlayerChatEvent e) {
final Player p = e.getPlayer(); Player p = e.getPlayer();
final ChatRule cr = cc.getChatRule(e.getPlayer()); ChatRule cr = cc.getChatRule(e.getPlayer());
if (cr == null) { if (cr == null) {
// Log.d("玩家: %s 未发现可用ChatRule!", p.getName()); // Log.d("玩家: %s 未发现可用ChatRule!", p.getName());
return; return;
} }
e.setCancelled(true); e.setCancelled(true);
final Tellraw tr = Tellraw.create(); Tellraw tr = Tellraw.create();
handleChat(p, tr, cr, e.getMessage()); handleChat(p, tr, cr, e.getMessage());
} }
private void handleChat(final Player p, final Tellraw tr, final ChatRule cr, final String message) { private void handleChat(Player p, Tellraw tr, 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); handleFormat(p, tr, cr);
handleTellraw(p, tr, cr, message); handleTellraw(p, tr, cr, message);
handleSend(p, tr, cr.getRange()); handleSend(p, tr, cr.getRange());
} }
private void handleFormat(final Player p, final Tellraw tr, final ChatRule cr) { private String handleFormat(Player p, Tellraw tr, ChatRule cr) {
final LinkedList<String> formats = cr.getFormats(); LinkedList<String> formats = cr.getFormats();
// Log.d("处理前缀信息..."); // Log.d("处理前缀信息...");
for (final String format : formats) { for (String format : formats) {
final ChatMessagePart cmp = cc.getFormat(format); ChatMessagePart cmp = cc.getFormat(format);
if (cmp != null) { if (cmp != null) {
// Log.d("解析格式: %s", format); // Log.d("解析格式: %s", format);
cmp.then(tr, p); cmp.then(tr, p);
@ -78,14 +78,15 @@ public class ChatListener implements Listener {
tr.then(PlaceholderAPI.setPlaceholders(p, format)); tr.then(PlaceholderAPI.setPlaceholders(p, format));
} }
} }
return ChatColor.getLastColors(formats.getLast());
} }
private LinkedList<String> handleMessage(final LinkedList<String> il, String message) { private LinkedList<String> handleMessage(LinkedList<String> il, String message) {
final LinkedList<String> mlist = new LinkedList<>(); LinkedList<String> mlist = new LinkedList<>();
// Log.d("处理聊天信息..."); // Log.d("处理聊天信息...");
if (!il.isEmpty()) { if (!il.isEmpty()) {
for (final String k : il) { for (String k : il) {
final String[] args = message.split(k, 2); String[] args = message.split(k, 2);
if (!args[0].isEmpty()) { if (!args[0].isEmpty()) {
// Log.d("追加文本: %s", args[0]); // Log.d("追加文本: %s", args[0]);
mlist.add(args[0]); mlist.add(args[0]);
@ -102,13 +103,13 @@ public class ChatListener implements Listener {
return mlist; return mlist;
} }
private LinkedList<String> handlePattern(final String message) { private LinkedList<String> handlePattern(String message) {
final Matcher m = ITEM_PATTERN.matcher(message); Matcher m = ITEM_PATTERN.matcher(message);
final Set<String> temp = new HashSet<>(); Set<String> temp = new HashSet<>();
final LinkedList<String> ilist = new LinkedList<>(); LinkedList<String> ilist = new LinkedList<>();
// Log.d("处理聊天物品信息..."); // Log.d("处理聊天物品信息...");
while (m.find()) { while (m.find()) {
final String key = m.group(0); String key = m.group(0);
if (key.length() == 2) { if (key.length() == 2) {
if (temp.add(key)) { if (temp.add(key)) {
// Log.d("解析物品关键词: %s", key); // Log.d("解析物品关键词: %s", key);
@ -121,7 +122,7 @@ public class ChatListener implements Listener {
return ilist; return ilist;
} }
private void handleSend(final Player p, final Tellraw tr, final int range) { private void handleSend(Player p, Tellraw tr, int range) {
Collection<? extends Entity> plist = Collections.emptyList(); Collection<? extends Entity> plist = Collections.emptyList();
if (range != 0) { if (range != 0) {
plist = p.getNearbyEntities(range, range, range); plist = p.getNearbyEntities(range, range, range);
@ -129,7 +130,7 @@ public class ChatListener implements Listener {
} else { } else {
plist = C.Player.getOnlinePlayers(); plist = C.Player.getOnlinePlayers();
} }
for (final Entity ne : plist) { for (Entity ne : plist) {
if (ne instanceof Player && !offList.contains(((Player) ne).getName())) { if (ne instanceof Player && !offList.contains(((Player) ne).getName())) {
tr.send(ne); tr.send(ne);
} }
@ -137,7 +138,7 @@ public class ChatListener implements Listener {
Bukkit.getConsoleSender().sendMessage(tr.toOldMessageFormat()); Bukkit.getConsoleSender().sendMessage(tr.toOldMessageFormat());
} }
private void handleTellraw(final Player player, final Tellraw tr, final ChatRule cr, String message) { private void handleTellraw(Player player, Tellraw tr, ChatRule cr, String message) {
if (message.isEmpty()) { if (message.isEmpty()) {
return; return;
} }
@ -148,18 +149,19 @@ public class ChatListener implements Listener {
tr.then(message); tr.then(message);
return; return;
} }
final LinkedList<String> il = handlePattern(message); LinkedList<String> il = handlePattern(message);
// 如果返回null说明存在相同的物品
if (il == null) { if (il == null) {
Log.toSender(player, "§c不允许展示相同的物品!"); Log.toSender(player, "§c不允许展示相同的物品!");
return; return;
} }
final LinkedList<String> ml = handleMessage(il, message); LinkedList<String> ml = handleMessage(il, message);
// Log.d("处理Tellraw格式..."); // Log.d("处理Tellraw格式...");
while (!ml.isEmpty()) { while (!ml.isEmpty()) {
final String mm = ml.removeFirst(); String mm = ml.removeFirst();
if (il.contains(mm)) { if (il.contains(mm)) {
final char k = mm.charAt(1); char k = mm.charAt(1);
final ItemStack is = k == 'i' ? player.getItemInHand() : player.getInventory().getItem(k - '0' - 1); ItemStack is = k == 'i' ? player.getItemInHand() : player.getInventory().getItem(k - '0' - 1);
if (is != null && is.getType() != Material.AIR) { if (is != null && is.getType() != Material.AIR) {
// Log.d("处理物品: %s", mm); // Log.d("处理物品: %s", mm);
tr.then(String.format(ChatColor.translateAlternateColorCodes('&', cr.getItemformat()), L10N.getName(is))); tr.then(String.format(ChatColor.translateAlternateColorCodes('&', cr.getItemformat()), L10N.getName(is)));
@ -167,7 +169,7 @@ public class ChatListener implements Listener {
} }
} else { } else {
// Log.d("追加聊天: %s", mm); // Log.d("追加聊天: %s", mm);
tr.then(mm); tr.then(cr.getLastColor() + mm);
} }
} }
} }