Scoreboard Scrolling Title

Discussion in 'Plugin Development' started by Drkmaster83, Jan 6, 2014.

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

    Drkmaster83

    I really feel like I should already be able to do this, but I know that I won't figure it out on my own. What I'm trying to do is create a half-second thread that makes text change color and appear to shift 1 down as a String. I'll post the code I currently have, but it throws an StringIndexOutOfBoundsException: 32 in the console when it reaches the end.
    Code:
    plug.getServer().getScheduler().runTaskTimer(plug, new Runnable() {
    int i = 0;
    String prefix = "c";
    @Override
    public void run() {
    for(Player player : plug.getServer().getOnlinePlayers()) {
    //BossBarUtils.displayTextBar("\u00A77Welcome to \u00A73\u00A7lBlock\u00A74\u00A7lRealms\u00A77!", player);
    score = manager.getNewScoreboard();
    objective = score.registerNewObjective("Hub", "dummy");
    String displayName = "Welcome, " + player.getName() + "!          ";
    objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    if(i + 10 >= displayName.length() + 2) i = 0;
    objective.setDisplayName("\u00A7" + prefix + "\u00A7l" + moveXDown(displayName, i));
    if(prefix.equals("c")) prefix = "6";
    else if(prefix.equals("a")) prefix = "b";
    else if(prefix.equals("b")) prefix = "9";
    else if(prefix.equals("9")) prefix = "1";
    else if(prefix.equals("1")) prefix = "5";
    else if(prefix.equals("5")) prefix = "d";
    else if(prefix.equals("d")) prefix = "c";
    else if(prefix.equals("6")) prefix = "e";
    else if(prefix.equals("e")) prefix = "a";
    objective.getScore(plug.getServer().getOfflinePlayer("\u00A7aPlayers:")).setScore(plug.getServer().getOnlinePlayers().length);
    player.setScoreboard(score);
    }
    i++;
    }
    }, 0L, 10L);
     
    public String moveXDown(String s, int x) {
    if(x < 0) x = 0;
    return s.substring(x, x + 10);
    }
    
    The timer is started upon onEnable().

    Edit: The error points to the return statement of moveXDown().
     
  2. Offline

    nuclearmissile

    You have a pretty complex method of making the prefixes cycle repeatedly. Though this is probably not going to help your error, may I suggest using a Queue to store those? That way you can easily just take the one off the end and put it at the front, without stacks of code like that, not to mention making it expandable.
     
  3. Offline

    Drkmaster83

    Are you perhaps talking about making an iterator and making the prefix iterator.next() of an Array of the letters?
     
  4. Offline

    nuclearmissile

    Drkmaster83 No, I'm talking about using a Queue data structure. It's a specialized sort of structure with a first in first out method of storage. Check it out, it has many good uses, and I think he may find it useful for shortening his code, especially if he increases the number of scrolling characters.
     
  5. Offline

    Drkmaster83

  6. Offline

    Drkmaster83

  7. Offline

    Maurdekye

    You need to describe your problem better, your code is very messy.
     
  8. Offline

    DrJava

    Here you go.

    Code:
    /**
    * A util to scroll coloured Strings
    * @author Chinwe
    */
    public class Scroller
    {
        private static final char COLOUR_CHAR = '§';
        private int position;
        private List<String> list;
        private ChatColor colour = ChatColor.RESET;
     
     
        /**
        * @param message      The String to scroll
        * @param width        The width of the window to scroll across (i.e. 16 for signs)
        * @param spaceBetween The amount of spaces between each repetition
        * @param colourChar  The colour code character you're using (i.e. & or §)
        */
        public Scroller(String message, int width, int spaceBetween, char colourChar)
        {
            list = new ArrayList<String>();
     
            // Validation
            // String is too short for window
            if (message.length() < width)
            {
                StringBuilder sb = new StringBuilder(message);
                while (sb.length() < width)
                    sb.append(" ");
                message = sb.toString();
            }
     
            // Allow for colours which add 2 to the width
            width -= 2;
     
            // Invalid width/space size
            if (width < 1)
                width = 1;
            if (spaceBetween < 0)
                spaceBetween = 0;
     
            // Change to §
            if (colourChar != '§')
                message = ChatColor.translateAlternateColorCodes(colourChar, message);
     
     
            // Add substrings
            for (int i = 0; i < message.length() - width; i++)
                list.add(message.substring(i, i + width));
     
            // Add space between repeats
            StringBuilder space = new StringBuilder();
            for (int i = 0; i < spaceBetween; ++i)
            {
                list.add(message.substring(message.length() - width + (i > width ? width : i), message.length()) + space);
                if (space.length() < width)
                    space.append(" ");
            }
     
            // Wrap
            for (int i = 0; i < width - spaceBetween; ++i)
                list.add(message.substring(message.length() - width + spaceBetween + i, message.length()) + space + message.substring(0, i));
     
            // Join up
            for (int i = 0; i < spaceBetween; i++)
            {
                if (i > space.length())
                    break;
                list.add(space.substring(0, space.length() - i) + message.substring(0, width - (spaceBetween > width ? width : spaceBetween) + i));
            }
        }
     
        /**
        * @return Gets the next String to display
        */
        public String next()
        {
            StringBuilder sb = getNext();
            if (sb.charAt(sb.length() - 1) == COLOUR_CHAR)
                sb.setCharAt(sb.length() - 1, ' ');
     
            if (sb.charAt(0) == COLOUR_CHAR)
            {
                ChatColor c = ChatColor.getByChar(sb.charAt(1));
                if (c != null)
                {
                    colour = c;
                    sb = getNext();
                    if (sb.charAt(0) != ' ')
                        sb.setCharAt(0, ' ');
                }
            }
     
            return colour + sb.toString();
     
        }
     
        private StringBuilder getNext()
        {
            return new StringBuilder(list.get(position++ % list.size()).substring(0));
        }
     
    }
    
    Code:
    Scroller scroller = new Scroller("&aThis is a scrolling string!", 10, 5, '&');
    
     
Thread Status:
Not open for further replies.

Share This Page