[UTIL] Colour-Persistent String Wrapping

Discussion in 'Resources' started by Chinwe, Dec 29, 2013.

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


    Hey there,

    Using WordUtils.wrap() (from org.apache.commons.lang), colour codes aren't kept, so
    wrapping "&aThis is a long green message, &bfollowed by an &3aqua &b one, &cand ends with a &4red &cone!" would do this when sending it to a player:

    I noticed there is a double space between 'aqua' and 'one' D:

    With this util, you can send the exact same message while preserving the colours:


    String str: The string to wrap
    char colourCode: The colour code character to use, such as '&' or '§'
    int lineLength: The line length to base around - not all lines will be this length exactly
    boolean wrapLongWords: Whether long words should be cut up to fit the lines

    To show what wrapLongWords does, imagine your string was "&7Thisis&8averylongword&bsuchlength&3wow":


    Example Usage
    // the string to wrap
    String s = "&aThis is a long green message, &bfollowed by an &3aqua&b one, &cand ends with a &4red &cone!";
    String wrapped = Utils.wrapString(s, '&', 25, false);
    // send it to the player
    // colour codes are changed to §, so you can send the raw output

    Find the gist here!

    As usual, please leave any comments and (constructive) critisism below!
    rbrick, Yonas, chasechocolate and 5 others like this.
  2. Offline



    You haved earned one [diamondore].
  3. Chinwe
    I hate to bump a fairly old thread, but this doesn't seem to be working for me. This is how I try to use it
    1. ItemStack air = ItemHelper.createItemStack(new ItemStack(Material.PISTON_MOVING_PIECE), ChatColor.GRAY + "Empty", "&6Nothing to see here.");
    and here is the createItemStack method
    1. public static ItemStack createItemStack(ItemStack stack, String name, String lore) {
    2. ItemMeta meta = stack.getItemMeta();
    4. meta.setDisplayName(name);
    5. meta.setLore(Arrays.asList(StringWrapper.wrapString(lore, '&', 20, false)));
    7. stack.setItemMeta(meta);
    8. return stack;
    9. }
    the display name and lore is working, but instead of splitting the line when told to, it just adds an odd looking character in between the words.
    Show Spoiler

    Any clue where I have gone wrong?
  4. Offline


    Lore might not like new lines, you could try spliting up the wrapped string around \\n, and using that as the List for the lore?
  5. Offline


    I love you. :) [diamond]
  6. Offline


    I understand that this is almost a year grave-dig, but I really want more people to see the absolute amazingness of this utility that this beautiful person has made.
    Chinwe likes this.
Thread Status:
Not open for further replies.

Share This Page