From 461e9985376a6ac06d18100b245b010f4dfaf3a1 Mon Sep 17 00:00:00 2001 From: cerevisiae Date: Wed, 1 Dec 2010 21:25:45 -0600 Subject: [PATCH] Fixing words too long to fit on a single line. --- vMinecraftAnnouncements.java | 8 +- vMinecraftChat.java | 138 +++++++++++++++++++++++------------ vMinecraftCommands.java | 10 +-- vMinecraftListener.java | 2 +- 4 files changed, 102 insertions(+), 56 deletions(-) diff --git a/vMinecraftAnnouncements.java b/vMinecraftAnnouncements.java index c93db1b96..9b78b7fa7 100644 --- a/vMinecraftAnnouncements.java +++ b/vMinecraftAnnouncements.java @@ -26,24 +26,24 @@ public class vMinecraftAnnouncements { if(split[0].equalsIgnoreCase("/kick")) { Player playerTarget = etc.getServer().matchPlayer(split[1]); 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")) { Player playerTarget = etc.getServer().matchPlayer(split[1]); 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")) { Player playerTarget = etc.getServer().matchPlayer(split[1]); 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.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()); } } } diff --git a/vMinecraftChat.java b/vMinecraftChat.java index 75d3f2148..e0b19b786 100644 --- a/vMinecraftChat.java +++ b/vMinecraftChat.java @@ -16,10 +16,10 @@ public class vMinecraftChat { //Output: None //Use: Outputs a message to everybody //===================================================================== - public static void gmsg(String msg){ - for (Player p : etc.getServer().getPlayerList()) { - if (p != null) { - sendMessage(p, msg); + public static void gmsg(Player sender, String msg){ + for (Player receiver : etc.getServer().getPlayerList()) { + if (receiver != null) { + sendMessage(sender, receiver, msg); } } } @@ -30,10 +30,10 @@ public class vMinecraftChat { //Output: None //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)); for(String out : message) - player.sendMessage(out + " "); + receiver.sendMessage(out + " "); } //===================================================================== @@ -45,38 +45,49 @@ public class vMinecraftChat { //===================================================================== public static String[] wordWrap(String msg){ //Split each word apart - String[] split = msg.split(" "); + ArrayList split = new ArrayList(); + for(String in : msg.split(" ")) + split.add(in); //Create an arraylist for the output ArrayList out = new ArrayList(); - //While i is less than the length of the array of words - int i = 0; - while(i < split.length){ + while(!split.isEmpty()){ int len = 0; - int j = i; - + + //Create an arraylist to hold individual words + ArrayList words = new ArrayList(); + //Loop through the words finding their length and increasing //j, the end point for the sub string - while(len <= 300 && i < split.length) + while(len <= 316 && !split.isEmpty()) { - len += msgLength(split[i]) + 4; - if( len <= 300) - i++; + int wordLength = msgLength(split.get(0)) + 4; + + //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. - 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 - String[] tempout = new String[out.size()]; - out.toArray(tempout); - return tempout; + return out.toArray(new String[out.size()]); } //===================================================================== @@ -91,25 +102,60 @@ public class vMinecraftChat { //and their following color codes for(int x = 0; x 0) + length += len; + else 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; } + + //===================================================================== + //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 @@ -271,7 +317,7 @@ public class vMinecraftChat { //And if p is an admin or has access to adminchat send message 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); //Output the message - gmsg(playerName + Colors.LightGreen + message); + gmsg(player, playerName + Colors.LightGreen + message); return true; } return false; @@ -323,7 +369,7 @@ public class vMinecraftChat { log.log(Level.INFO, "<"+player.getName()+"> "+message); //Output the message - gmsg(playerName + Colors.Red + message); + gmsg(player, playerName + Colors.Red + message); return true; } return false; @@ -347,7 +393,7 @@ public class vMinecraftChat { log.log(Level.INFO, "<"+player.getName()+"> "+message); //Output the message - gmsg(playerName + message); + gmsg(player, playerName + message); //Loop through the string finding the color codes and inserting them return true; @@ -363,7 +409,7 @@ public class vMinecraftChat { //===================================================================== public static boolean emote(Player player, String message) { - gmsg("* " + getName(player) + " " + Colors.White + message); + gmsg(player, "* " + getName(player) + " " + Colors.White + message); return true; } @@ -391,7 +437,7 @@ public class vMinecraftChat { for(int x = 0; x< msg.length(); x++) { //If the char is a ^ or � - if(msg.charAt(x) == '^') + if(msg.charAt(x) == '^' || msg.charAt(x) == Colors.White.charAt(0)) { if(x != msg.length() - 1) { diff --git a/vMinecraftCommands.java b/vMinecraftCommands.java index 9dbd788e9..a57f40744 100644 --- a/vMinecraftCommands.java +++ b/vMinecraftCommands.java @@ -113,7 +113,7 @@ public class vMinecraftCommands{ playerTarget.sendMessage(Colors.Blue + "You have been healed by " + vMinecraftChat.getName(player)); } 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; @@ -318,7 +318,7 @@ public class vMinecraftCommands{ log.log(Level.INFO, player.getName()+" fabulously said \""+ str+"\""); //Prepend the player name and cut into lines. - vMinecraftChat.gmsg(playerName + vMinecraftChat.rainbow(str)); + vMinecraftChat.gmsg(player, playerName + vMinecraftChat.rainbow(str)); return EXIT_SUCCESS; } @@ -405,7 +405,7 @@ public class vMinecraftCommands{ int maxPlayers = server.getInt("max-players"); //Output the player list - vMinecraftChat.sendMessage(player, Colors.Rose + "Player List (" + vMinecraftChat.sendMessage(player, player, Colors.Rose + "Player List (" + count + "/" + maxPlayers +"): " + tempList); return EXIT_SUCCESS; @@ -429,7 +429,7 @@ public class vMinecraftCommands{ player.sendMessage(Colors.Rose + "Usage is /say [message]"); } //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_FAIL; @@ -454,7 +454,7 @@ public class vMinecraftCommands{ //If the player isn't invulnerable kill them if (!vMinecraftSettings.getInstance().isEzModo(playerTarget.getName())) { playerTarget.setHealth(0); - vMinecraftChat.gmsg(vMinecraftChat.getName(player) + vMinecraftChat.gmsg(player, vMinecraftChat.getName(player) + Colors.LightBlue + " has slain " + vMinecraftChat.getName(playerTarget)); //Otherwise output error to the user diff --git a/vMinecraftListener.java b/vMinecraftListener.java index 6faeb1aaa..0318de5ae 100644 --- a/vMinecraftListener.java +++ b/vMinecraftListener.java @@ -86,7 +86,7 @@ public class vMinecraftListener extends PluginListener { } 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; }