Solved Scoreboards and BukkitRunnables

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

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

    BungeeTheCookie

    So how do I fix that?
     
  2. Offline

    xTigerRebornx

  3. Offline

    bobacadodl

    Code:
    public void showScoreBoard(Player p){
            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.getName()+"to The Gamma Network!", 32, 8, '&');
            new BukkitRunnable(){
                public void run() {
                    String next = scroller.next();
                    o.setDisplayName(next);
                    Score score = o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Test:"));
                    score.setScore(1);
                    p.setScoreboard(board);
                }
            }.runTaskTimer(plugin, 0L, 5L);
        }
    That should work
     
  4. Offline

    xTigerRebornx

    BungeeTheCookie A problem i've noticed. You are setting the width of the scrolling message to 32 each time, so its not going to fit at all. You need to set it to 16, as thats the max for scoreboards(I believe)
     
  5. Offline

    BungeeTheCookie

    It works having it at 32, it's just moving, but I'll set it to 16 and see if it still moves.

    Ok, I am going to try that with a playerjoinevent and see if that works.

    I need to show it for every player. If I use a PlayerJoinEvent, and a reload happens...

    xTigerRebornx
    It still is moving regardless of the size. I set it to 25. It doesn't matter how large the message is, because then the scoreboard just expands.

    Chinwe
    Wouldn't
    PHP:
    scroller.toString()
    work?
     
  6. Offline

    xTigerRebornx

    BungeeTheCookie What bobacadodl said works perfectly fine. And if you want to handle reloads, you can just put this in your onEnable()
    Code:
    for(Player p : Bukkit.getOnlinePlayers()){
      showScoreBoard(p);
    }
    Shouldn't ever reload tho...doesn't benefit the server any
     
  7. Offline

    bobacadodl

    If you want to optimize my solution, you should probably store every player's scroller object in a hashmap so that you only need to use one bukkitrunnable, but yeah. Other than that my solution works
     
  8. Offline

    BungeeTheCookie

    bobacadodl
    It doesn't even show the scoreboard now. Debugger time.

    bobacadodl
    xTigerRebornx
    None of the code is being executed?????
     
  9. Offline

    bobacadodl

    What do you mean?...
     
  10. Offline

    BungeeTheCookie

    None of the code is being executed. The scoreboard isn't showing.

    chasechocolate Cirno Assist

    bobacadodl
    Do you know why the code isn't being executed?
     
  11. Offline

    bobacadodl

    BungeeTheCookie
    Post your code...
    You probably arent registering the listener or something
     
  12. Offline

    BungeeTheCookie


    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.scheduler.BukkitRunnable;
    import org.bukkit.scoreboard.*;
     
    public class 
    ScoreBoardManager{
     
        public final 
    TGNPixelmon plugin;
        public 
    ScoreBoardManager(final TGNPixelmon plugin) {this.plugin plugin;}
     
     
        public 
    void showScoreBoard(final Player p){
            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.getName()+ " to The Gamma Network!"2512'&');
            
    Bukkit.getServer().getLogger().info("Registers a new scoreboard.");
     
            new 
    BukkitRunnable(){
     
                public 
    void run() {
                    
    Bukkit.getServer().getLogger().info("Instantiates a new Bukkit Runnable");
                    
    String next scroller.next();
                    
    o.setDisplayName(next);
                    
    Score score o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN "Test:"));
                    
    score.setScore(0);
                    
    p.setScoreboard(board);
                    
    Bukkit.getServer().getLogger().info("Displays scoreboard.");
                }
            }.
    runTaskTimer(plugin0L5L);
        }
    }
    PHP:
    package net.gammanetworkmc.pixelmon;
     
    import net.gammanetworkmc.pixelmon.configuration.Configuration;
    import net.gammanetworkmc.pixelmon.scoreboard.ScoreBoardManager;
    import net.gammanetworkmc.pixelmon.util.CommandHandler;
    import net.gammanetworkmc.pixelmon.util.EventHandler;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import java.io.File;
     
    public final class 
    TGNPixelmon extends JavaPlugin {
     
    public static 
    TGNPixelmon plugin;
     
        public 
    void onEnable(){
            
    Bukkit.getServer().getLogger().info("[TGN] Pixelmon has been enabled!");
            
    Bukkit.getServer().getLogger().info("[TGN] The developer of this plugin is Bungeecookie.");
            
    File changeLogFile = new File(getDataFolder(), "changelog.txt");
            if(
    changeLogFile.exists()){
                
    changeLogFile.delete();
            }
            new 
    CommandHandler(this);
            new 
    EventHandler(this);
            new 
    Configuration(this);
            for(
    Player p Bukkit.getServer().getOnlinePlayers()){
                new 
    ScoreBoardManager(this).showScoreBoard(p);
            }
        }
        public 
    void onDisable(){
            
    Bukkit.getServer().getLogger().info("[TGN] Pixelmon has been disabled!");
            
    Bukkit.getServer().getLogger().info("[TGN] Thank you for using this plugin, have a nice day!");
        }
    }
     
  13. Offline

    xTrollxDudex

  14. Offline

    BungeeTheCookie

    Thats what I did in my ScoreBoardManager class.
     
  15. Offline

    xTrollxDudex

    I missed that D:
     
  16. Offline

    BungeeTheCookie

    Any other things that could cause this? otherwise I'll have to revert back to my old method.
     
  17. Offline

    bobacadodl

    Do showScoreboard() on player join...
     
  18. Offline

    BungeeTheCookie

    Let me try that out.

    xTrollxDudex bobacadodl
    It works when I join! But, when I disconnect it throws this error:
    PHP:
    2013-12-30 14:12:38 [WARNING] [TGN-PixelmonTask #2 for TGN-Pixelmon vGamma Release 1.0.8 generated an exception
    java.lang.IllegalStateExceptionCannot set scoreboard for invalid CraftPlayer
        at org
    .bukkit.craftbukkit.v1_6_R3.entity.CraftPlayer.setScoreboard(CraftPlayer.java:1012)
        
    at net.gammanetworkmc.pixelmon.scoreboard.ScoreBoardManager$1.run(ScoreBoardManager.java:35)
        
    at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftTask.run(CraftTask.java:58)
        
    at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
        
    at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:825)
        
    at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:327)
        
    at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:780)
        
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:662)
        
    at net.minecraft.server.ThreadMinecraftServer.run(ThreadMinecraftServer.java:16)
     
  19. Offline

    xTrollxDudex

    BungeeTheCookie
    After you leave, the task keeps running. Cancel the runnable for the player when he leaves
     
  20. Offline

    BungeeTheCookie

    Would that be using a PlayerQuitEvent, or just checking if the player is not null in the runnable?
     
  21. Offline

    xTrollxDudex

    BungeeTheCookie
    Try both but I would definitely do the second since it's simplest
     
  22. Offline

    BungeeTheCookie

    Same error.
    Code:
    PHP:
    ackage 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;}
     
        public 
    void showScoreBoard(final Player p){
            if(
    != null){
                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.getName() + " to The Gamma Network!"2512'&');
     
                new 
    BukkitRunnable(){
                    public 
    void run() {
                        if(
    != null){
                            
    String next scroller.next();
                            
    o.setDisplayName(next);
                            
    Score score o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN "Test:"));
                            
    score.setScore(0);
                            
    p.setScoreboard(board);
                        }
                    }
                }.
    runTaskTimer(plugin0L5L);
            }
        }
     
        @
    EventHandler(priority EventPriority.HIGHEST)
        public 
    void displayScoreBoard(PlayerJoinEvent e){
            
    showScoreBoard(e.getPlayer());
        }
    }
    bobacadodl

    bobacadodl EcMiner
    Any idea why it is still throwing the error when I check if the player is null?
     
  23. Offline

    EcMiner

    I'll try and debug your code tomorrow, i will try to provide you an answer tomorrow morning (West Europa time) if it's mot yet solved
     
  24. Offline

    BungeeTheCookie

    Ok, thanks. Even though I'd preferably like this solved now. :)
     
  25. Offline

    EcMiner

    Yeah i'm sorry i can't help you earlier, i'm not really close to a pc right now... But maybe someone else is able to help you right now
     
  26. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Please do not just tag large groups of users. Thanks! :)
     
    Chinwe and bobacadodl like this.
  27. Offline

    BungeeTheCookie

    I'm sorry mbaxter :(.I just wanted to tag people to see if they knew how to solve it. I am getting far with it and it is almost closed to being solved. Do you know why the error is being produced by the BukkitRunnable when a player disconnects? I have checked to see if the player is null, but it still is giving me an invalid CraftPlayer for the BukkitRunnable.
     
  28. Offline

    EcMiner

    BungeeTheCookie What exactly doesn't work? I am not getting any errors when i join or quit...

    EDIT: I see the error now, when i kick myself it will keep generating errors...
     
  29. Offline

    BungeeTheCookie

    It happens when you disconnect, it throws an invalid CraftPlayer error.
     
  30. Offline

    EcMiner

    BungeeTheCookie I think I've found a solution for your problem. Basicly what i do is do a try catch statement and whenever it catches an error it will cancel the task.

    Code:java
    1. public void showScoreBoard(final Player p) {
    2. if (p != null) {
    3. final ScoreboardManager manager = Bukkit.getScoreboardManager();
    4. final Scoreboard board = manager.getNewScoreboard();
    5. final Objective o;
    6. o = board.registerNewObjective("join", "dummy");
    7. o.setDisplaySlot(DisplaySlot.SIDEBAR);
    8. final Scroller scroller = new Scroller("&lWelcome " + p.getName() + " to The Gamma Network!", 16, 12, '&');
    9.  
    10. new BukkitRunnable() {
    11. public void run() {
    12. try {
    13. if (p != null) {
    14. String next = scroller.next();
    15. o.setDisplayName(next);
    16. Score score = o.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Test:"));
    17. score.setScore(0);
    18. p.setScoreboard(board);
    19. }
    20. } catch (Exception e) {
    21. this.cancel();
    22. }
    23. }
    24. }.runTaskTimer(Main.plugin, 0L, 5L);
    25. }
    26. }


    I hope this helps you out. :)
     
Thread Status:
Not open for further replies.

Share This Page