Sending Messages with WordWrap [TUTORIAL]

Discussion in 'Resources' started by dreadiscool, Apr 22, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    dreadiscool

    Hi guys,

    This was always a stickler for me. I used to just debug / test the plugin until I got the text to be formatted the way I wanted (by just using sendMessage() for each line.) Then I decided I would stand up against this bad coding practice D: I created a method that allows you to send a long string of text with wordwrap. If a word is too long, it snaps to the next line, making your job much easier :D

    Code:
    // assume 'name' is the name of the player ._.
    public void sendWrapped(String msg, ChatColor cc) {
    String[] msgSplit = msg.split(" ");
    String built = "";
    for (int i = 0; i < msgSplit.length; i++) {
    if ((built + " " + msgSplit[i]).length() > 60 || msgSplit[i].contains("\n")) {
    Bukkit.getPlayer(name).sendMessage(cc + built);
    built = "";
    }
    built += " " + msgSplit[i];
    built = built.trim();
    }
    if (built != "")
    Bukkit.getPlayer(name).sendMessage(cc + built);
    }
    
     
    Mitsugaru and Tauryuu like this.
  2. Offline

    Tauryuu

    Amazing :D
     
  3. Offline

    dreadiscool

    Thanks :)
     
  4. Offline

    desht

Thread Status:
Not open for further replies.

Share This Page