Fixing words too long to fit on a single line.

This commit is contained in:
cerevisiae 2010-12-01 21:25:45 -06:00
parent 61a8781f62
commit 461e998537
4 changed files with 102 additions and 56 deletions

View File

@ -26,24 +26,24 @@ public class vMinecraftAnnouncements {
if(split[0].equalsIgnoreCase("/kick")) { if(split[0].equalsIgnoreCase("/kick")) {
Player playerTarget = etc.getServer().matchPlayer(split[1]); Player playerTarget = etc.getServer().matchPlayer(split[1]);
if (playerTarget != null && !playerTarget.hasControlOver(player)) { if (playerTarget != null && !playerTarget.hasControlOver(player)) {
vMinecraftChat.gmsg(player.getColor()+player.getName()+Colors.Blue+" has kicked "+Colors.Red+playerTarget.getColor()+playerTarget.getName()); vMinecraftChat.gmsg(player, player.getColor()+player.getName()+Colors.Blue+" has kicked "+Colors.Red+playerTarget.getColor()+playerTarget.getName());
} }
} }
if(split[0].equalsIgnoreCase("/ban")) { if(split[0].equalsIgnoreCase("/ban")) {
Player playerTarget = etc.getServer().matchPlayer(split[1]); Player playerTarget = etc.getServer().matchPlayer(split[1]);
if (playerTarget != null && !playerTarget.hasControlOver(player)) { if (playerTarget != null && !playerTarget.hasControlOver(player)) {
vMinecraftChat.gmsg(player.getColor()+player.getName()+Colors.Blue+" has banned "+Colors.Red+playerTarget.getColor()+playerTarget.getName()); vMinecraftChat.gmsg(player, player.getColor()+player.getName()+Colors.Blue+" has banned "+Colors.Red+playerTarget.getColor()+playerTarget.getName());
} }
} }
if(split[0].equalsIgnoreCase("/ipban")) { if(split[0].equalsIgnoreCase("/ipban")) {
Player playerTarget = etc.getServer().matchPlayer(split[1]); Player playerTarget = etc.getServer().matchPlayer(split[1]);
if (playerTarget != null && !playerTarget.hasControlOver(player)) { if (playerTarget != null && !playerTarget.hasControlOver(player)) {
vMinecraftChat.gmsg(player.getColor()+player.getName()+Colors.Blue+" has IP banned "+Colors.Red+playerTarget.getColor()+playerTarget.getName()); vMinecraftChat.gmsg(player, player.getColor()+player.getName()+Colors.Blue+" has IP banned "+Colors.Red+playerTarget.getColor()+playerTarget.getName());
} }
} }
if(split[0].equalsIgnoreCase("/time")) { if(split[0].equalsIgnoreCase("/time")) {
if (split.length <= 2) { if (split.length <= 2) {
vMinecraftChat.gmsg(Colors.Blue+"Time changes thanks to "+player.getColor()+player.getName()); vMinecraftChat.gmsg(player, Colors.Blue+"Time changes thanks to "+player.getColor()+player.getName());
} }
} }
} }

View File

@ -16,10 +16,10 @@ public class vMinecraftChat {
//Output: None //Output: None
//Use: Outputs a message to everybody //Use: Outputs a message to everybody
//===================================================================== //=====================================================================
public static void gmsg(String msg){ public static void gmsg(Player sender, String msg){
for (Player p : etc.getServer().getPlayerList()) { for (Player receiver : etc.getServer().getPlayerList()) {
if (p != null) { if (receiver != null) {
sendMessage(p, msg); sendMessage(sender, receiver, msg);
} }
} }
} }
@ -30,10 +30,10 @@ public class vMinecraftChat {
//Output: None //Output: None
//Use: Outputs a message to everybody //Use: Outputs a message to everybody
//===================================================================== //=====================================================================
public static void sendMessage(Player player, String msg){ public static void sendMessage(Player sender, Player receiver, String msg){
String[] message = applyColors(wordWrap(msg)); String[] message = applyColors(wordWrap(msg));
for(String out : message) for(String out : message)
player.sendMessage(out + " "); receiver.sendMessage(out + " ");
} }
//===================================================================== //=====================================================================
@ -45,38 +45,49 @@ public class vMinecraftChat {
//===================================================================== //=====================================================================
public static String[] wordWrap(String msg){ public static String[] wordWrap(String msg){
//Split each word apart //Split each word apart
String[] split = msg.split(" "); ArrayList<String> split = new ArrayList<String>();
for(String in : msg.split(" "))
split.add(in);
//Create an arraylist for the output //Create an arraylist for the output
ArrayList<String> out = new ArrayList<String>(); ArrayList<String> out = new ArrayList<String>();
//While i is less than the length of the array of words //While i is less than the length of the array of words
int i = 0; while(!split.isEmpty()){
while(i < split.length){
int len = 0; int len = 0;
int j = i;
//Create an arraylist to hold individual words
ArrayList<String> words = new ArrayList<String>();
//Loop through the words finding their length and increasing //Loop through the words finding their length and increasing
//j, the end point for the sub string //j, the end point for the sub string
while(len <= 300 && i < split.length) while(len <= 316 && !split.isEmpty())
{ {
len += msgLength(split[i]) + 4; int wordLength = msgLength(split.get(0)) + 4;
if( len <= 300)
i++; //If a word is too long for a line
if(wordLength > 316)
{
String[] tempArray = wordCut(len, split.remove(0));
words.add(tempArray[0]);
split.add(tempArray[1]);
log.log(Level.INFO, tempArray[0]);
log.log(Level.INFO, tempArray[1]);
}
//If the word is not too long to fit
len += wordLength;
log.log(Level.INFO, String.valueOf(len));
if( len < 316)
words.add(split.remove(0));
} }
//Copy the words in the selection into a new array
String[] temp = new String[i - j];
System.arraycopy(split, j, temp, 0, i - j);
//Merge them and add them to the output array. //Merge them and add them to the output array.
out.add( etc.combineSplit(0, temp, " ") ); log.log(Level.INFO, etc.combineSplit(0,
words.toArray(new String[out.size()]), " "));
out.add( etc.combineSplit(0,
words.toArray(new String[out.size()]), " ") );
} }
//Convert to an array and return //Convert to an array and return
String[] tempout = new String[out.size()]; return out.toArray(new String[out.size()]);
out.toArray(tempout);
return tempout;
} }
//===================================================================== //=====================================================================
@ -91,25 +102,60 @@ public class vMinecraftChat {
//and their following color codes //and their following color codes
for(int x = 0; x<str.length(); x++) for(int x = 0; x<str.length(); x++)
{ {
if(str.charAt(x) == Colors.White.charAt(0)) int len = charLength(str.charAt(x));
if( len > 0)
length += len;
else
x++; x++;
else if("i;,.:|!".indexOf(str.charAt(x)) != -1)
length+=2;
else if("l'".indexOf(str.charAt(x)) != -1)
length+=3;
else if("tI[]".indexOf(str.charAt(x)) != -1)
length+=4;
else if("kf{}<>\"*()".indexOf(str.charAt(x)) != -1)
length+=5;
else if("hequcbrownxjmpsvazydgTHEQUCKBROWNFXJMPSVLAZYDG1234567890#\\/?$%-=_+&".indexOf(str.charAt(x)) != -1)
length+=6;
else if("@~".indexOf(str.charAt(x)) != -1)
length+=7;
else if(str.charAt(x)==' ')
length+=4;
} }
return length; return length;
} }
//=====================================================================
//Function: wordCut
//Input: String str: The string to find the length of
//Output: String[]: The cut up word
//Use: Cuts apart a word that is too long to fit on one line
//=====================================================================
private static String[] wordCut(int lengthBefore, String str){
int length = lengthBefore;
//Loop through all the characters, skipping any color characters
//and their following color codes
String[] output = new String[2];
int x = 0;
while(length < 316 && x < str.length())
{
int len = charLength(str.charAt(x));
if( len > 0)
length += len;
x++;
}
//Add the substring to the output after cutting it
output[0] = str.substring(0, x);
//Add the last of the string to the output.
output[1] = str.substring(x);
return output;
}
private static int charLength(char x)
{
if("i;,.:|!".indexOf(x) != -1)
return 2;
else if("l'".indexOf(x) != -1)
return 3;
else if("tI[]".indexOf(x) != -1)
return 4;
else if("kf{}<>\"*()".indexOf(x) != -1)
return 5;
else if("hequcbrownxjmpsvazydgTHEQUCKBROWNFXJMPSVLAZYDG1234567890#\\/?$%-=_+&".indexOf(x) != -1)
return 6;
else if("@~".indexOf(x) != -1)
return 7;
else if(x==' ')
return 4;
else
return -1;
}
//===================================================================== //=====================================================================
//Function: rainbow //Function: rainbow
@ -271,7 +317,7 @@ public class vMinecraftChat {
//And if p is an admin or has access to adminchat send message //And if p is an admin or has access to adminchat send message
if (p.isAdmin() || (p.canUseCommand("/adminchat"))) { if (p.isAdmin() || (p.canUseCommand("/adminchat"))) {
sendMessage(p, adminchat + message); sendMessage(player, p, adminchat + message);
} }
} }
} }
@ -301,7 +347,7 @@ public class vMinecraftChat {
log.log(Level.INFO, "<"+player.getName()+"> " +message); log.log(Level.INFO, "<"+player.getName()+"> " +message);
//Output the message //Output the message
gmsg(playerName + Colors.LightGreen + message); gmsg(player, playerName + Colors.LightGreen + message);
return true; return true;
} }
return false; return false;
@ -323,7 +369,7 @@ public class vMinecraftChat {
log.log(Level.INFO, "<"+player.getName()+"> "+message); log.log(Level.INFO, "<"+player.getName()+"> "+message);
//Output the message //Output the message
gmsg(playerName + Colors.Red + message); gmsg(player, playerName + Colors.Red + message);
return true; return true;
} }
return false; return false;
@ -347,7 +393,7 @@ public class vMinecraftChat {
log.log(Level.INFO, "<"+player.getName()+"> "+message); log.log(Level.INFO, "<"+player.getName()+"> "+message);
//Output the message //Output the message
gmsg(playerName + message); gmsg(player, playerName + message);
//Loop through the string finding the color codes and inserting them //Loop through the string finding the color codes and inserting them
return true; return true;
@ -363,7 +409,7 @@ public class vMinecraftChat {
//===================================================================== //=====================================================================
public static boolean emote(Player player, String message) public static boolean emote(Player player, String message)
{ {
gmsg("* " + getName(player) + " " + Colors.White + message); gmsg(player, "* " + getName(player) + " " + Colors.White + message);
return true; return true;
} }
@ -391,7 +437,7 @@ public class vMinecraftChat {
for(int x = 0; x< msg.length(); x++) for(int x = 0; x< msg.length(); x++)
{ {
//If the char is a ^ or <EFBFBD> //If the char is a ^ or <EFBFBD>
if(msg.charAt(x) == '^') if(msg.charAt(x) == '^' || msg.charAt(x) == Colors.White.charAt(0))
{ {
if(x != msg.length() - 1) if(x != msg.length() - 1)
{ {

View File

@ -113,7 +113,7 @@ public class vMinecraftCommands{
playerTarget.sendMessage(Colors.Blue + "You have been healed by " + vMinecraftChat.getName(player)); playerTarget.sendMessage(Colors.Blue + "You have been healed by " + vMinecraftChat.getName(player));
} }
else if (playerTarget == null){ else if (playerTarget == null){
vMinecraftChat.gmsg(Colors.Rose + "Couldn't find that player"); player.sendMessage(Colors.Rose + "Couldn't find that player");
} }
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -318,7 +318,7 @@ public class vMinecraftCommands{
log.log(Level.INFO, player.getName()+" fabulously said \""+ str+"\""); log.log(Level.INFO, player.getName()+" fabulously said \""+ str+"\"");
//Prepend the player name and cut into lines. //Prepend the player name and cut into lines.
vMinecraftChat.gmsg(playerName + vMinecraftChat.rainbow(str)); vMinecraftChat.gmsg(player, playerName + vMinecraftChat.rainbow(str));
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -405,7 +405,7 @@ public class vMinecraftCommands{
int maxPlayers = server.getInt("max-players"); int maxPlayers = server.getInt("max-players");
//Output the player list //Output the player list
vMinecraftChat.sendMessage(player, Colors.Rose + "Player List (" vMinecraftChat.sendMessage(player, player, Colors.Rose + "Player List ("
+ count + "/" + maxPlayers +"): " + tempList); + count + "/" + maxPlayers +"): " + tempList);
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -429,7 +429,7 @@ public class vMinecraftCommands{
player.sendMessage(Colors.Rose + "Usage is /say [message]"); player.sendMessage(Colors.Rose + "Usage is /say [message]");
} }
//Display the message globally //Display the message globally
vMinecraftChat.gmsg(Colors.Yellow + etc.combineSplit(0, args, " ")); vMinecraftChat.gmsg(player, Colors.Yellow + etc.combineSplit(0, args, " "));
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
return EXIT_FAIL; return EXIT_FAIL;
@ -454,7 +454,7 @@ public class vMinecraftCommands{
//If the player isn't invulnerable kill them //If the player isn't invulnerable kill them
if (!vMinecraftSettings.getInstance().isEzModo(playerTarget.getName())) { if (!vMinecraftSettings.getInstance().isEzModo(playerTarget.getName())) {
playerTarget.setHealth(0); playerTarget.setHealth(0);
vMinecraftChat.gmsg(vMinecraftChat.getName(player) vMinecraftChat.gmsg(player, vMinecraftChat.getName(player)
+ Colors.LightBlue + " has slain " + Colors.LightBlue + " has slain "
+ vMinecraftChat.getName(playerTarget)); + vMinecraftChat.getName(playerTarget));
//Otherwise output error to the user //Otherwise output error to the user

View File

@ -86,7 +86,7 @@ public class vMinecraftListener extends PluginListener {
} }
else if (vMinecraftSettings.getInstance().globalmessages() && player.getHealth() < 1) { else if (vMinecraftSettings.getInstance().globalmessages() && player.getHealth() < 1) {
vMinecraftChat.gmsg(Colors.Gray + player.getName() + " " + vMinecraftSettings.randomDeathMsg()); vMinecraftChat.gmsg(player, Colors.Gray + player.getName() + " " + vMinecraftSettings.randomDeathMsg());
} }
return false; return false;
} }