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

View File

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