4
1
mirror of https://e.coding.net/circlecloud/MiaoChat.git synced 2024-11-14 13:28:46 +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>
<groupId>pw.yumc</groupId>
<artifactId>MiaoChat</artifactId>
<version>1.3</version>
<version>1.3.1</version>
<build>
<finalName>${project.name}</finalName>
<resources>
@ -31,7 +31,6 @@
<artifactSet>
<includes>
<include>pw.yumc:YumCore</include>
<include>cn.citycraft:PluginHelper</include>
</includes>
</artifactSet>
<relocations>
@ -39,10 +38,6 @@
<pattern>pw.yumc.YumCore</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
</relocation>
<relocation>
<pattern>cn.citycraft.PluginHelper</pattern>
<shadedPattern>${project.groupId}.${project.artifactId}</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
@ -110,12 +105,6 @@
<type>jar</type>
<version>1.10.2-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.citycraft</groupId>
<artifactId>PluginHelper</artifactId>
<type>jar</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>pw.yumc</groupId>
<artifactId>YumCore</artifactId>

View File

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