4
1
mirror of https://e.coding.net/circlecloud/MiaoChat.git synced 2024-12-22 19:18:47 +00:00

fix: 修复值存在特殊字符时错误的问题

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-09-14 14:21:14 +08:00
parent 232ba7f95d
commit 6d7660f96c
2 changed files with 28 additions and 20 deletions

13
pom.xml
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</version> <version>1.3.1</version>
<build> <build>
<finalName>${project.name}</finalName> <finalName>${project.name}</finalName>
<resources> <resources>
@ -31,7 +31,6 @@
<artifactSet> <artifactSet>
<includes> <includes>
<include>pw.yumc:YumCore</include> <include>pw.yumc:YumCore</include>
<include>cn.citycraft:PluginHelper</include>
</includes> </includes>
</artifactSet> </artifactSet>
<relocations> <relocations>
@ -39,10 +38,6 @@
<pattern>pw.yumc.YumCore</pattern> <pattern>pw.yumc.YumCore</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern> <shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>cn.citycraft.PluginHelper</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
</relocation>
</relocations> </relocations>
</configuration> </configuration>
<executions> <executions>
@ -110,12 +105,6 @@
<type>jar</type> <type>jar</type>
<version>1.10.2-R0.1-SNAPSHOT</version> <version>1.10.2-R0.1-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>cn.citycraft</groupId>
<artifactId>PluginHelper</artifactId>
<type>jar</type>
<version>1.0</version>
</dependency>
<dependency> <dependency>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId> <artifactId>YumCore</artifactId>

View File

@ -49,26 +49,31 @@ public class ChatListener implements Listener {
final Player p = e.getPlayer(); final Player p = e.getPlayer();
final ChatRule cr = cc.getChatRule(e.getPlayer()); final ChatRule cr = cc.getChatRule(e.getPlayer());
if (cr == null) { if (cr == null) {
// Log.d("玩家: %s 未发现可用ChatRule!", p.getName());
return; return;
} }
e.setCancelled(true); e.setCancelled(true);
final Tellraw tr = Tellraw.create(); final Tellraw tr = Tellraw.create();
handleChat(p, tr, cr, e); handleChat(p, tr, cr, e.getMessage());
} }
private void handleChat(final Player p, final Tellraw tr, final ChatRule cr, final AsyncPlayerChatEvent e) { private void handleChat(final Player p, final Tellraw tr, final ChatRule cr, final String message) {
// Log.d("玩家: %s 使用 %s 规则 解析 %s", p.getName(), cr.getName(), message);
handleFormat(p, tr, cr); handleFormat(p, tr, cr);
handleTellraw(p, tr, cr, e.getMessage()); handleTellraw(p, tr, cr, message);
handleSend(p, tr, cr); handleSend(p, tr, cr.getRange());
} }
private void handleFormat(final Player p, final Tellraw tr, final ChatRule cr) { private void handleFormat(final Player p, final Tellraw tr, final ChatRule cr) {
final LinkedList<String> formats = cr.getFormats(); final LinkedList<String> formats = cr.getFormats();
// Log.d("处理前缀信息...");
for (final String format : formats) { for (final String format : formats) {
final ChatMessagePart cmp = cc.getFormat(format); final ChatMessagePart cmp = cc.getFormat(format);
if (cmp != null) { if (cmp != null) {
// Log.d("解析格式: %s", format);
cmp.then(tr, p); cmp.then(tr, p);
} else { } else {
// Log.d("追加文本: %s", format);
tr.then(format); tr.then(format);
} }
} }
@ -76,15 +81,21 @@ public class ChatListener implements Listener {
private LinkedList<String> handleMessage(final LinkedList<String> il, String message) { private LinkedList<String> handleMessage(final LinkedList<String> il, String message) {
final LinkedList<String> mlist = new LinkedList<>(); final LinkedList<String> mlist = new LinkedList<>();
// Log.d("处理聊天信息...");
if (!il.isEmpty()) { if (!il.isEmpty()) {
for (final String k : il) { for (final String k : il) {
final String[] args = message.split(k, 2); final String[] args = message.split(k, 2);
mlist.add(args[0]); if (!args[0].isEmpty()) {
// Log.d("追加文本: %s", args[0]);
mlist.add(args[0]);
}
// Log.d("解析物品: %s", args[0]);
mlist.add(k); mlist.add(k);
message = args[1]; message = args[1];
} }
} }
if (!message.isEmpty()) { if (!message.isEmpty()) {
// Log.d("追加文本: %s", message);
mlist.add(message); mlist.add(message);
} }
return mlist; return mlist;
@ -94,10 +105,12 @@ public class ChatListener implements Listener {
final Matcher m = ITEM_PATTERN.matcher(message); final Matcher m = ITEM_PATTERN.matcher(message);
final Set<String> temp = new HashSet<>(); final Set<String> temp = new HashSet<>();
final LinkedList<String> ilist = new LinkedList<>(); final LinkedList<String> ilist = new LinkedList<>();
// Log.d("处理聊天物品信息...");
while (m.find()) { while (m.find()) {
final String key = m.group(0); final 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);
ilist.add(key); ilist.add(key);
} else { } else {
return null; return null;
@ -107,8 +120,7 @@ public class ChatListener implements Listener {
return ilist; return ilist;
} }
private void handleSend(final Player p, final Tellraw tr, final ChatRule cr) { private void handleSend(final Player p, final Tellraw tr, final int range) {
final int range = cr.getRange();
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);
@ -125,28 +137,35 @@ public class ChatListener implements Listener {
} }
private void handleTellraw(final Player player, final Tellraw tr, final ChatRule cr, String message) { private void handleTellraw(final Player player, final Tellraw tr, final ChatRule cr, String message) {
if (message.isEmpty() || !cr.isItem()) { if (message.isEmpty()) {
return; return;
} }
if (player.hasPermission("MiaoChat.color")) { if (player.hasPermission("MiaoChat.color")) {
message = ChatColor.translateAlternateColorCodes('&', message); message = ChatColor.translateAlternateColorCodes('&', message);
} }
if (!cr.isItem()) {
tr.then(message);
return;
}
final LinkedList<String> il = handlePattern(message); final LinkedList<String> il = handlePattern(message);
if (il == null) { if (il == null) {
Log.toSender(player, "§c不允许展示相同的物品!"); Log.toSender(player, "§c不允许展示相同的物品!");
return; return;
} }
final LinkedList<String> ml = handleMessage(il, message); final LinkedList<String> ml = handleMessage(il, message);
// Log.d("处理Tellraw格式...");
while (!ml.isEmpty()) { while (!ml.isEmpty()) {
final String mm = ml.removeFirst(); final String mm = ml.removeFirst();
if (il.contains(mm)) { if (il.contains(mm)) {
final char k = mm.charAt(1); final char k = mm.charAt(1);
final ItemStack is = k == 'i' ? player.getItemInHand() : 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) { if (is != null && is.getType() != Material.AIR) {
// Log.d("处理物品: %s", mm);
tr.then(String.format(ChatColor.translateAlternateColorCodes('&', cr.getItemformat()), L10N.getItemName(is))); tr.then(String.format(ChatColor.translateAlternateColorCodes('&', cr.getItemformat()), L10N.getItemName(is)));
tr.item(is); tr.item(is);
} }
} else { } else {
// Log.d("追加聊天: %s", mm);
tr.then(mm); tr.then(mm);
} }
} }