Gave the ability to use color colors and macros in greeting and farewell messages.

This commit is contained in:
sk89q 2011-06-26 21:47:26 -07:00
parent b90334c8a8
commit 110838715b
3 changed files with 70 additions and 6 deletions

View File

@ -22,13 +22,10 @@
import java.util.List;
import com.sk89q.worldedit.blocks.BlockType;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector;
@ -184,4 +181,42 @@ public static void findFreePosition(Player player) {
y++;
}
}
/**
* Replace color macros in a string. The macros are in the form of `[char]
* where char represents the color. R is for red, Y is for yellow,
* G is for green, C is for cyan, B is for blue, and P is for purple.
* The uppercase versions of those are the darker shades, while the
* lowercase versions are the lighter shades. For white, it's 'w', and
* 0-2 are black, dark grey, and grey, respectively.
*
* @param str
* @return color-coded string
*/
public static String replaceColorMacros(String str) {
str = str.replace("&r", ChatColor.RED.toString());
str = str.replace("&R", ChatColor.DARK_RED.toString());
str = str.replace("&y", ChatColor.YELLOW.toString());
str = str.replace("&Y", ChatColor.GOLD.toString());
str = str.replace("&g", ChatColor.GREEN.toString());
str = str.replace("&G", ChatColor.DARK_GREEN.toString());
str = str.replace("&c", ChatColor.AQUA.toString());
str = str.replace("&C", ChatColor.DARK_AQUA.toString());
str = str.replace("&b", ChatColor.BLUE.toString());
str = str.replace("&B", ChatColor.DARK_BLUE.toString());
str = str.replace("&p", ChatColor.LIGHT_PURPLE.toString());
str = str.replace("&P", ChatColor.DARK_PURPLE.toString());
str = str.replace("&0", ChatColor.BLACK.toString());
str = str.replace("&1", ChatColor.DARK_GRAY.toString());
str = str.replace("&2", ChatColor.GRAY.toString());
str = str.replace("&w", ChatColor.WHITE.toString());
return str;
}
}

View File

@ -326,12 +326,16 @@ public void onPlayerMove(PlayerMoveEvent event) {
if (state.lastFarewell != null && (farewell == null
|| !state.lastFarewell.equals(farewell))) {
player.sendMessage(ChatColor.AQUA + " ** " + state.lastFarewell);
String replacedFarewell = plugin.replaceMacros(
player, BukkitUtil.replaceColorMacros(state.lastFarewell));
player.sendMessage(ChatColor.AQUA + " ** " + replacedFarewell);
}
if (greeting != null && (state.lastGreeting == null
|| !state.lastGreeting.equals(greeting))) {
player.sendMessage(ChatColor.AQUA + " ** " + greeting);
String replacedGreeting = plugin.replaceMacros(
player, BukkitUtil.replaceColorMacros(greeting));
player.sendMessage(ChatColor.AQUA + " ** " + replacedGreeting);
}
if ((notifyLeave == null || !notifyLeave)

View File

@ -735,4 +735,29 @@ public boolean canBuild(Player player, Location loc) {
public boolean canBuild(Player player, Block block) {
return getGlobalRegionManager().canBuild(player, block);
}
/**
* Replace macros in the text.
*
* @param sender
* @param message
* @return
*/
public String replaceMacros(CommandSender sender, String message) {
Player[] online = getServer().getOnlinePlayers();
message = message.replace("%name%", toName(sender));
message = message.replace("%id%", toUniqueName(sender));
if (sender instanceof Player) {
Player player = (Player) sender;
World world = player.getWorld();
message = message.replace("%world%", world.getName());
message = message.replace("%online%", String.valueOf(online.length));
message = message.replace("%health%", String.valueOf(player.getHealth()));
}
return message;
}
}