Scoreboard scrolling text

Discussion in 'Plugin Development' started by ItsLeoFTW, May 10, 2014.

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

    ItsLeoFTW

    Hi,

    I was trying to use @Chinwe's Scrolling Text class, but I can't figure out how to actually put scrolling text on a scoreboard. The example that was given was for a sign, but I'm not too good with scoreboards. Can someone help?

    *if it helps, here's the scrolling text class*
    Code:java
    1. import java.util.ArrayList;
    2. import java.util.List;
    3. import org.bukkit.ChatColor;
    4.  
    5. /**
    6. * A util to scroll coloured Strings
    7. * @author Chinwe
    8. */
    9. public class Scroller
    10. {
    11. private static final char COLOUR_CHAR = '§';
    12. private int position;
    13. private List<String> list;
    14. private ChatColor colour = ChatColor.RESET;
    15.  
    16.  
    17. /**
    18.   * @param message The String to scroll
    19.   * @param width The width of the window to scroll across (i.e. 16 for signs)
    20.   * @param spaceBetween The amount of spaces between each repetition
    21.   * @param colourChar The colour code character you're using (i.e. & or §)
    22.   */
    23. public Scroller(String message, int width, int spaceBetween, char colourChar)
    24. {
    25. list = new ArrayList<String>();
    26.  
    27. // Validation
    28. // String is too short for window
    29. if (message.length() < width)
    30. {
    31. StringBuilder sb = new StringBuilder(message);
    32. while (sb.length() < width)
    33. sb.append(" ");
    34. message = sb.toString();
    35. }
    36.  
    37. // Allow for colours which add 2 to the width
    38. width -= 2;
    39.  
    40. // Invalid width/space size
    41. if (width < 1)
    42. width = 1;
    43. if (spaceBetween < 0)
    44. spaceBetween = 0;
    45.  
    46. // Change to §
    47. if (colourChar != '§')
    48. message = ChatColor.translateAlternateColorCodes(colourChar, message);
    49.  
    50.  
    51. // Add substrings
    52. for (int i = 0; i < message.length() - width; i++)
    53. list.add(message.substring(i, i + width));
    54.  
    55. // Add space between repeats
    56. StringBuilder space = new StringBuilder();
    57. for (int i = 0; i < spaceBetween; ++i)
    58. {
    59. list.add(message.substring(message.length() - width + (i > width ? width : i), message.length()) + space);
    60. if (space.length() < width)
    61. space.append(" ");
    62. }
    63.  
    64. // Wrap
    65. for (int i = 0; i < width - spaceBetween; ++i)
    66. list.add(message.substring(message.length() - width + spaceBetween + i, message.length()) + space + message.substring(0, i));
    67.  
    68. // Join up
    69. for (int i = 0; i < spaceBetween; i++)
    70. {
    71. if (i > space.length())
    72. break;
    73. list.add(space.substring(0, space.length() - i) + message.substring(0, width - (spaceBetween > width ? width : spaceBetween) + i));
    74. }
    75. }
    76.  
    77. /**
    78.   * @return Gets the next String to display
    79.   */
    80. public String next()
    81. {
    82. StringBuilder sb = getNext();
    83. if (sb.charAt(sb.length() - 1) == COLOUR_CHAR)
    84. sb.setCharAt(sb.length() - 1, ' ');
    85.  
    86. if (sb.charAt(0) == COLOUR_CHAR)
    87. {
    88. ChatColor c = ChatColor.getByChar(sb.charAt(1));
    89. if (c != null)
    90. {
    91. colour = c;
    92. sb = getNext();
    93. if (sb.charAt(0) != ' ')
    94. sb.setCharAt(0, ' ');
    95. }
    96. }
    97.  
    98. return colour + sb.toString();
    99.  
    100. }
    101.  
    102. private StringBuilder getNext()
    103. {
    104. return new StringBuilder(list.get(position++ % list.size()).substring(0));
    105. }
    106.  
    107. }


    Can someone tell me what code to put into my main class?

    Thanks!
    - ItsLeoFTW (average java coder :p)
     
  2. ItsLeoFTW I'm not 100% certain, but:
    Create a scoreboard and create a new Scroller (with whatever text).
    Create a BukkitRunnable that will loop through the scroller (next method) and set the text on the scoreboard to whatever the scroller's next method returns.
     
  3. Offline

    ItsLeoFTW

    Bump, I need help!
     
Thread Status:
Not open for further replies.

Share This Page