Solved [HARD] Splitting on \n

Discussion in 'Plugin Development' started by Bammerbom, Jun 8, 2014.

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

    Bammerbom

    I was trying to split my message on a "\n" but failed. I spent 2 hours of searching, tried much and nothing worked.
    Code of three of my tries:
    Code:java
    1. String message = "Test\nAnother test\nAnother another test";
    2. log(message.split("\n").length + ", " + message.split("\\n").length + ", " + message.split(System.getProperty("line.separator")).length);

    Returning: 1, 1, 1 while it needs to return 3, Any Ideas?
     
  2. Offline

    MCMatters

    Try echoing the msg 3 times
     
  3. Offline

    fireblast709

    Code:java
    1. System.out.println("test\ntest\ntest\ntest".split("\n").length);
    Output:
    Code:
    4
    Your machine must be doing something weird.
     
    Bammerbom likes this.
  4. Offline

    MCMatters

  5. Offline

    Bammerbom

    fireblast709 I think so.

    MCMatters fireblast709 fireblast709
    I tried it again.
    The post above was an example. I thougt I was just doing something wrong.
    Looking at this I think it is something with my Config getter r.mes or something with ChatColors.
    Result:
    Code:java
    1. String message = r.mes("List.List").replaceAll("%Online", i + "").replaceAll("%Max", Bukkit.getMaxPlayers() + "").replaceAll("%List", online.toString());
    2. log(message);
    3. log(message.split("\n").length + "");

    Returning:
    There are 0/20 players online: \nDefault: (In Colors)
    1
    Code:java
    1. String anothertest = "There are 0/20 players online: \nDefault:";
    2. r.log(anothertest.split("\n").length + "");

    Returning:
    2

    Config getter:
    Config:
    Code:java
    1. List: '@1There are @2%Online@1/@2%Max @1players online: \n@2%List'

    Getter:
    Code:java
    1. public static String mes(String padMessage){
    2. YamlConfiguration lang = null;
    3. if(UltimateFileLoader.LANGf == null){
    4. return null;
    5. }
    6. lang = YamlConfiguration.loadConfiguration(UltimateFileLoader.LANGf);
    7. if(lang.get(padMessage) == null){
    8. return null;
    9. }
    10.  
    11. if(lang.get(padMessage) != null){
    12. String try1 = default1 + ChatColor.translateAlternateColorCodes('&', lang.getString(padMessage).replaceAll("@1", default1 + "").replaceAll("@2", default2 + ""));
    13. return try1;
    14. }
    15. lang = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "Messages/EN.yml"));
    16. if(lang.get(padMessage) != null){
    17. String try2 = default1 + ChatColor.translateAlternateColorCodes('&', lang.getString(padMessage).replaceAll("@1", default1 + "").replaceAll("@2", default2 + ""));
    18. return try2;
    19. }
    20. return null;
    21.  
    22. }

    Information about getter:
    - UltimateFileLoader.LANGf returns a File.
    - @1 @2, etc are standard chatcolors in my plugin.
    - default1, default2 are just ChatColor.BLUE and ChatColor.AQUA

    Bammerbom Any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  6. Offline

    fireblast709

    Bammerbom YAML doesn't like newlines, most likely.
     
    Bammerbom likes this.
  7. Offline

    Bammerbom

    fireblast709
    It is returning the message correctly.
    But with some more testing I found this:
    The newlines are NOT executing (EDIT: I mean they are being showed in console as \n) , so YAML is probarly returning the string like "\\n"
    With this I used .split("\\\\n") and the thread is SOLVED.

    That I didnt think about that... -_-
     
  8. Offline

    Garris0n

    It takes regex. I don't know regex, but you might've needed to do Pattern.quote("\n") instead. I'm pretty sure the extra escaping is from regex and not YAML.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page