Optimise StringHelper#parseColors, fix an NPE

This commit is contained in:
fullwall 2012-11-16 23:42:52 +08:00
parent 6e9593c4c3
commit 0d56317bc0
2 changed files with 17 additions and 10 deletions

View File

@ -972,9 +972,10 @@ public class NPCCommands {
"tphere", "tph", "move" }, min = 1, max = 1, permission = "npc.tphere")
public void tphere(CommandContext args, Player player, NPC npc) {
// Spawn the NPC if it isn't spawned to prevent NPEs
if (!npc.isSpawned())
npc.spawn(npc.getTrait(CurrentLocation.class).getLocation());
npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND);
if (!npc.isSpawned()) {
npc.spawn(player.getLocation());
} else
npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND);
Messaging.sendTr(player, Messages.NPC_TELEPORTED, npc.getName());
}

View File

@ -1,5 +1,7 @@
package net.citizensnpcs.util;
import java.util.regex.Pattern;
import net.citizensnpcs.Settings.Setting;
import org.bukkit.ChatColor;
@ -60,13 +62,8 @@ public class StringHelper {
return p[n];
}
public static String parseColors(Object string) {
String parsed = string.toString();
for (ChatColor color : ChatColor.values()) {
parsed = parsed.replace("<" + color.getChar() + ">", color.toString());
}
parsed = ChatColor.translateAlternateColorCodes('&', parsed);
return parsed;
public static String parseColors(String parsed) {
return COLOR_MATCHER.matcher(parsed).replaceAll(ChatColor.COLOR_CHAR + "$1");
}
public static String wrap(Object string) {
@ -85,4 +82,13 @@ public class StringHelper {
String highlight = Setting.HIGHLIGHT_COLOUR.asString();
return highlight + "=====[ " + string.toString() + highlight + " ]=====";
}
private static Pattern COLOR_MATCHER;
static {
String colors = "";
for (ChatColor color : ChatColor.values())
colors += color.getChar();
COLOR_MATCHER = Pattern.compile("[&<]([COLORS])[>]?".replace("COLORS", colors),
Pattern.CASE_INSENSITIVE);
}
}