Scrolling Objective

Discussion in 'Plugin Development' started by CrazyGuy3000, Apr 6, 2014.

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

    CrazyGuy3000

    Using this code here,
    Code:
    static ScoreboardManager manager;
        static Scoreboard scoreboard;
        static Objective objective;
       
        @Override
        public void onEnable(){
            manager = Bukkit.getScoreboardManager();
            scoreboard = manager.getNewScoreboard();
            objective = scoreboard.registerNewObjective("Sideboard", "dummy");
     
            objective.setDisplayName("Testing");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
           
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
                public void run(){
                   
                    Score test = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "" + ChatColor.BOLD + "^ plz"));
                    test.setScore(0);
                   
                    Scroller scroller = new Scroller("&oThis text should be scrolling", 10, 5, '&');
                   
                    Score scrollerscore = objective.getScore(Bukkit.getOfflinePlayer(scroller.next()));
                    scrollerscore.setScore(1);   
                }
               
            }, 20L, 20L);
           
            getCommand("arcade").setExecutor(new ArcadeCommand());
        }
    My Scroller wont scroll Chinwe

    The Scroller.java
    Show Spoiler

    Code:
    public class Scroller {
        private static final char COLOUR_CHAR = '§';
        private int position;
        private List<String> line;
        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) {
            line = 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++)
                line.add(message.substring(i, i + width));
     
            // Add space between repeats
            StringBuilder space = new StringBuilder();
            for (int i = 0; i < spaceBetween; ++i) {
                line.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)
                line.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;
                line.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();
     
        }
       
        public String old() {
            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(line.get(position++ % line.size())
                    .substring(0));
        }
    
     
  2. Offline

    Chinwe

    You want to make the Scroller outside of the scheduler, so you only have one instance that you're calling .next() on, otherwise you're making a new Scroller each time.
     
  3. Offline

    CrazyGuy3000

    ...derp...

    EDIT:
    Chinwe How can I get the previous string from it so I can board.resetScores? :?

    Chinwe
    Sorry if its annoying that I tagged you in twice, not sure if the other one worked or not...

    Chinwe ?

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

    Chinwe

    You mean the original scroller string, "&oThis text should be scrolling"? You can save it as a variable which you pass into the Scroller constructor and then use later?
     
  5. Offline

    CrazyGuy3000

    No I mean as in the text before it changes and goes into a new string. Like so I can save it and then it resets the scores so that string is no longer visible on the scoreboard but the text with the next set of letters will show.
     
Thread Status:
Not open for further replies.

Share This Page