Solved Scoreboards and BukkitRunnables

Discussion in 'Plugin Development' started by BungeeTheCookie, Dec 29, 2013.

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

    BungeeTheCookie

    Hello people of the Bukkit community! I was wondering how to make it on the display name for a scoreboard, it says, "Welcome (playername) to The Gamma Network", but instead of having it stay stationary, it scrolls to the left. So one second it may say Welcome, but later it would say "Welcome Bungeecookie to" and then later it will say "The Gamma Network". Kind of like the way Mineplex does it in the lobby. Thanks!
     
  2. Offline

    xTigerRebornx

    BungeeTheCookie Probably a repeating task that constantly changes the scoreboard to make it look like a scrolling effect, but really it is just constantly changing the scoreboard
     
  3. Offline

    BungeeTheCookie

    Example please? Thanks for giving me that idea though, i was thinking of that ;)
     
  4. Offline

    xTigerRebornx

    BungeeTheCookie Heh.....I don't spoonfeed code :p I can give some pseudo-code

    scheduleTask -> calculate scoreboard name -> set scoreboard for all online players - repeat :p
     
  5. Offline

    BungeeTheCookie

    Ok thanks. I just realized I will also need a way to split up all of the characters of a player's name and put it in a String[] so it can scroll through that in the scoreboard name too.

    EDIT: Should I make the class extend BukkitRunnable for this case?
     
  6. Offline

    thepaperboy99

  7. Offline

    BungeeTheCookie

  8. Offline

    thepaperboy99


    No problem. :)
     
  9. Offline

    BungeeTheCookie

  10. Offline

    Chinwe


    Do you mean everyone has their own name substituted into the same Scroller? If so, create a single Scroller with the player name as something such as PLAYERNAME, and then replace it before sending it to everyone: ie, something like this.
    Code:
    // in scheduler that repeatedly updates scoreboards
     
    String next = scroller.next();
     
    // while looping through players
    for (Player p : Bukkit.getOnlinePlayers())
    {
        String scroll = next.replace("PLAYERNAME", p.getName());
        // .. update scoreboard
    }
    
     
  11. Offline

    xTrollxDudex

    BungeeTheCookie
    String#toCharArray() gives you an areay of all the chars in the string, assuming that String#length() > 0. If it is null, I'm not sure but it probably returns one of the three:
    • NullPointerException
    • null
    • new char[0]
    I believe number one is the most likely.
     
  12. Offline

    BungeeTheCookie

    xTrollxDudex Chinwe
    Alright, I am going to test out my plugin with this code:
    Code:java
    1. package net.gammanetworkmc.pixelmon.scoreboard;
    2.  
    3. import net.gammanetworkmc.pixelmon.TGNPixelmon;
    4. import net.gammanetworkmc.pixelmon.api.ScrollerAPI;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.scoreboard.DisplaySlot;
    9. import org.bukkit.scoreboard.Objective;
    10. import org.bukkit.scoreboard.Scoreboard;
    11. import org.bukkit.scoreboard.ScoreboardManager;
    12.  
    13. public class ScoreBoardManager {
    14.  
    15. public ScoreBoardManager(TGNPixelmon plugin){
    16. ScoreboardManager manager = Bukkit.getScoreboardManager();
    17. Scoreboard board = manager.getNewScoreboard();
    18. Objective o;
    19. o = board.registerNewObjective("join", "dummy");
    20.  
    21. o.setDisplaySlot(DisplaySlot.SIDEBAR);
    22.  
    23. for(Player p: Bukkit.getServer().getOnlinePlayers()){
    24. ScrollerAPI scroller = new ScrollerAPI(ChatColor.BOLD + "Welcome PLAYERNAME to The Gamma Network!", 35, 5, '&');
    25. String next = scroller.next();
    26. next.replace("PLAYERNAME", p.getName());
    27. o.setDisplayName(next);
    28. p.setScoreboard(board);
    29. }
    30. }
    31. }
    32.  


    Hopefully, it works ;)
    EDIT: It doesn't show a scoreboard. Switching to a listener.

    xTrollxDudex Chinwe
    I guess I have to register an objective in order to display the scoreboard. #FeelsLikeANoob

    xTrollxDudex Chinwe thepaperboy99 xTigerRebornx
    Now every time I log in, it says "Recieved string length longer than maximum allwoed (18 > 16)"
    My code:
    PHP:
    package net.gammanetworkmc.pixelmon.scoreboard;
     
    import net.gammanetworkmc.pixelmon.api.ScrollerAPI;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.scoreboard.*;
     
    public class 
    ScoreBoardManager implements Listener {
     
        @
    EventHandler(priority EventPriority.HIGHEST)
        public 
    void showScoreBoard(PlayerJoinEvent e){
            
    ScoreboardManager manager Bukkit.getScoreboardManager();
            
    Scoreboard board manager.getNewScoreboard();
            
    Objective o;
            
    board.registerNewObjective("join""dummy");
     
            
    o.setDisplaySlot(DisplaySlot.SIDEBAR);
     
            for(
    Player pBukkit.getServer().getOnlinePlayers()){
                
    ScrollerAPI scroller = new ScrollerAPI("&lWelcome P to The Gamma Network!"325'&');
                
    String next scroller.next();
                
    next.replace("P"p.getName());
                
    o.setDisplayName(next);
                
    Score score o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN "Players Online: "));
                
    score.setScore(Bukkit.getServer().getOnlinePlayers().length);
                
    p.setScoreboard(board);
            }
        }
    }
     
  13. Offline

    xTrollxDudex

  14. Offline

    xTigerRebornx

    BungeeTheCookie Don't replace the P after you do scroller.next(), do it when define the string
    I.E. "Welcome P".replace("P", p.getName())

    And use something other then just "P", use something like %pname%, so that you don't replace every P with the name
     
  15. Offline

    BungeeTheCookie

    There is no error, it just says that.

    xTigerRebornx
    Ok, let me try that out. Thanks! :)
     
  16. Offline

    xTigerRebornx

    BungeeTheCookie If you get confused by what I meant, this is what I meant
    Code:
                ScrollerAPI scroller = new ScrollerAPI("&lWelcome P to The Gamma Network!".replaceAll("P", p.getName()), 32, 5, '&');
    
     
  17. Offline

    xTrollxDudex

    BungeeTheCookie
    Which line. Do System.out.print("debug"); and see how far it goes until it logs "BungeeTheCookie has been disconnected: disconnect.endOfStream" or something.
     
  18. Offline

    BungeeTheCookie

    Chinwe xTigerRebornx xTrollxDudex
    It's working now, but the characters in the display name are not scrolling through. It's just displaying the message. Think it's time for BukkitRunnables.
     
  19. Offline

    xTigerRebornx

    xTrollxDudex The problem is that he is replacing the "P" as it comes onto the scoreboard's title slot, and it is causing it to put an extremely large name on the scoreboard, when it can't handle that many characters

    BungeeTheCookie Test out the scrolling api with a different line of text, to make sure it actually works works

    BungeeTheCookie You are only calling the scroller.next() function once, so its not going to scroll. You need a runnable that will scroll through the scroller
     
  20. Offline

    BungeeTheCookie

    xTigerRebornx xTrollxDudex thepaperboy99 Chinwe
    PHP:
    package net.gammanetworkmc.pixelmon.scoreboard;
     
    import net.gammanetworkmc.pixelmon.TGNPixelmon;
    import net.gammanetworkmc.pixelmon.api.ScrollerAPI;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scoreboard.*;
     
    public class 
    ScoreBoardManager implements Listener {
     
    public final 
    TGNPixelmon plugin;
    public 
    ScoreBoardManager(final TGNPixelmon plugin) {this.plugin plugin;}
     
        @
    EventHandler(priority EventPriority.HIGHEST)
        public 
    void showScoreBoard(PlayerJoinEvent e){
            final 
    ScoreboardManager manager Bukkit.getScoreboardManager();
            final 
    Scoreboard board manager.getNewScoreboard();
            final 
    Objective o;
            
    board.registerNewObjective("join""dummy");
     
            
    o.setDisplaySlot(DisplaySlot.SIDEBAR);
     
            for(final 
    Player pBukkit.getServer().getOnlinePlayers()){
                new 
    BukkitRunnable(){
                    public 
    void run(){
                        
    ScrollerAPI scroller =  new ScrollerAPI("&lWelcome %P% to The Gamma Network!".replaceAll("%P%"p.getName()), 325'&');
                        
    String next scroller.next();
                        
    next.replace("%p%"p.getName());
                        
    o.setDisplayName(next);
                        
    Score score o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN "Test:"));
                        
    score.setScore(0);
                        
    p.setScoreboard(board);
                    }
                }.
    runTaskTimer(plugin0L20L);
            }
        }
    }
    Still not working. Just only displaying as a stationary message and it doesn't evens show the whole thing because it's too long. Even though I implemented a BukkitRunnable and there are no errors, it still doesn't work.

    I did. :)
     
  21. Offline

    xTigerRebornx

    BungeeTheCookie You can remove the next.replace("%p&", p.getName()), you've already done that. Add some debug in your runnable to see if its actually repeating or not
     
  22. Offline

    BungeeTheCookie

    Alright, thanks ;)

    xTigerRebornx xTrollxDudex
    It is doing it successfully and the BukkitRunnable is going through, because it repeats itself every one second, but it is not scrolling through the message??

    Please try to think of new solutions and post them if you think of them, I am going to bed and I will be back tomorrow to test out anything new you guys have come up with. Thank you for helping me this far ;)
     
  23. Offline

    xTigerRebornx

  24. Offline

    Chinwe

    Some things I can see that will give you bugs:
    • You're making a new ScrollerAPI every time for every player - you want to make only one outside the Runnable
    • You're making a new BukkitRunnable per player every time someone joins - you want to only make one task (in onEnable()) that will loop through all online players every time it runs
    • Move scroller.next() outside the loop too, you want to scroll 1, then send to all players, not scroll across once for every player
    • String#replace() returns a String, so you would have to make a new String for the modified scroller message (with %p%) inside the loop for each player
    • Try decreasing the width of the message from 32 to the maximum width of a scoreboard (16?)
     
  25. Offline

    BungeeTheCookie

    Could you provide me with an example of the new code. I am confuzzled on where to place them without giving me errors.

    EDIT: Nevermind. I think I got it ;)

    Chinwe xTrollxDudex xTigerRebornx

    Alright, here is my new code; going to test it now:
    PHP:
    package net.gammanetworkmc.pixelmon.scoreboard;
     
    import net.gammanetworkmc.pixelmon.TGNPixelmon;
    import net.gammanetworkmc.pixelmon.api.ScrollerAPI;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scoreboard.*;
     
    public class 
    ScoreBoardManager implements Listener {
     
        public final 
    TGNPixelmon plugin;
        public 
    ScoreBoardManager(final TGNPixelmon plugin) {this.plugin plugin;}
     
        @
    EventHandler(priority EventPriority.HIGHEST)
        public 
    void showScoreBoard(PlayerJoinEvent e){
            final 
    ScoreboardManager manager Bukkit.getScoreboardManager();
            final 
    Scoreboard board manager.getNewScoreboard();
            final 
    Objective o;
            
    board.registerNewObjective("join""dummy");
            
    o.setDisplaySlot(DisplaySlot.SIDEBAR);
            final 
    ScrollerAPI scroller =  new ScrollerAPI("&lWelcome %P% to The Gamma Network!"325'&');
     
            new 
    BukkitRunnable(){
                public 
    void run() {
                    
    String next scroller.next();
                    for(final 
    Player pBukkit.getServer().getOnlinePlayers()){
                        
    next.replace("%p%"p.getName());
                        
    o.setDisplayName(next);
                        
    Score score o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN "Test:"));
                        
    score.setScore(0);
                        
    p.setScoreboard(board);
                    }
                }
            }.
    runTaskTimer(plugin0L20L);
        }
    }
     
  26. Offline

    thepaperboy99

    The thing I noticed about scoreboards is that you can not set the score to 0. The way I solved this was by setting the score to 1, then to 0 again.
     
  27. Offline

    BungeeTheCookie

    Chinwe xTigerRebornx thepaperboy99 xTrollxDudex
    I found some naughty little bugs in the code.
    First off, it doesn't replace the "PLAYERNAME" with the player's name, all it says is Welcome PLAYERNAME to The Gamma Network. Also, the scoreboard is moving when the string scrolls, how do I just make it stationary? Maybe bobacadodl, NathanWolf knows?
     
  28. Offline

    NathanWolf

    I'm afraid I have no scoreboard experience... maybe one day :)
     
  29. Offline

    BungeeTheCookie

    Ok, thanks anyway :D

    Whoopsie, almost forgot
    PHP:
    public class ScoreBoardManager{
     
        public final 
    TGNPixelmon plugin;
        public 
    ScoreBoardManager(final TGNPixelmon plugin) {this.plugin plugin;}
     
        public 
    void showScoreBoard(){
            final 
    ScoreboardManager manager Bukkit.getScoreboardManager();
            final 
    Scoreboard board manager.getNewScoreboard();
            final 
    Objective o;
            
    board.registerNewObjective("join""dummy");
            
    o.setDisplaySlot(DisplaySlot.SIDEBAR);
            final 
    ScrollerAPI scroller =  new ScrollerAPI("&lWelcome PLAYERNAME to The Gamma Network!"328'&');
     
            new 
    BukkitRunnable(){
                public 
    void run() {
                    
    String next scroller.next();
                    for(final 
    Player pBukkit.getServer().getOnlinePlayers()){
                        
    next.replace("PLAYERNAME"p.getName());
                        
    o.setDisplayName(next);
                        
    Score score o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN "Test:"));
                        
    score.setScore(0);
                        
    p.setScoreboard(board);
                    }
                }
            }.
    runTaskTimer(plugin0L5L);
        }
    }
    Is my new code.
     
    NathanWolf likes this.
  30. Offline

    xTigerRebornx

    BungeeTheCookie So yeah. The reason its not replacing, is that you are replacing it in scroller.next(), which means that it will only replace when the entire PLAYERNAME word is seen on the scoreboard
     
Thread Status:
Not open for further replies.

Share This Page