Convert string to neat lore

Discussion in 'Plugin Development' started by Hex_27, Jul 15, 2015.

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

    Hex_27

    When adding lores to items, the text can go on forever, and the box will stretch with it. I need a method to neatly split a string into an arraylist (or set) to add neatly into a lore.
     
  2. Offline

    xJayD

    Arrays.asList("line 1", "line 2", "line 3");
     
  3. Offline

    Hex_27

    @xJayD It's a custom string that people can change, so I need to detect words and put them in each line.
     
  4. Offline

    teej107

    @Hex_27 split the string into words and then count each character in each word. When the total char count becomes higher than a certain size, start on the next lore line and reset the total char count.
     
  5. Offline

    Hex_27

    @teej107 Current Code:
    Code:
        public void addLore(String line, ArrayList<String> lore, ChatColor cc){
           
           
            String[] split = line.split(" ");
            String oneline = cc + "";
            for(String s : split){
            if(oneline.split(" ").length < 8){   
                oneline += s + " ";
            }else{
                lore.add(oneline);
                oneline = cc + "";
            }
            }
           
           
        }
    String "line": Weapons Masters are ruthless and skillful, effectively decimating all that dares to challenge him. Attacks deal bonus damage.


    Result: Weapons Masters are ruthless and skillful, effectively decimating all that dares to challenge him. Attacks deal bonus
     
  6. @Hex_27
    I once created this:
    Code:
    
        public static String[] split(String s, int length) {
            if(s.length() <= length) return new String[] {s};
            String[] split = new String[s.length() / length + 1];
            Arrays.fill(split, "");
            StringBuilder sb = new StringBuilder();
            char[] c = s.toCharArray();
            int index = 0;
            for(int i=0;i<c.length;i++) {
                if(i != 0 && i % length == 0) {
                    split[index++] = sb.toString();
                    sb = new StringBuilder();
                }
                sb.append(c[i]);
            }
            split[index] = sb.toString();
            return split;
        }
     
  7. Offline

    teej107

    is there a problem?
     
  8. Offline

    Hex_27

    @teej107 Yes, the last word disappears
     
  9. Offline

    Hex_27

  10. Offline

    Hex_27

  11. @Hex_27
    *cough cough*
     
  12. Offline

    Hex_27

    Last edited by a moderator: Jun 12, 2016
  13. @Hex_27
    Try this, untested, but should work:
    Code:
        public static String[] split(String s, int length) {
            if (s.length() <= length)return new String[] { s };
            String[] split = new String[s.length() / length + 1];
            Arrays.fill(split, "");
            StringBuilder sb = new StringBuilder();
            boolean nextLine = false;
            char[] c = s.toCharArray();
            int index = 0;
            for (int i = 0; i < c.length; i++) {
                if (i != 0 && i % length == 0) {
                    nextLine = true;
                }
                if(nextLine && c[i] == ' ') {
                    nextLine = false;
                    split[index++] = sb.toString();
                    sb = new StringBuilder();
                    continue;
                }
                sb.append(c[i]);
            }
            split[index] = sb.toString();
            return split;
        }
     
  14. Offline

    Hex_27

    @megamichiel Thanks, it works great now, but is there a way to make a new line each time "|" is detected?
     
  15. @Hex_27
    You could use the code I stated above, but replace the if statement at line 14 with if(nextLine && (c == ' ' || c == '|'))
     
Thread Status:
Not open for further replies.

Share This Page