Scoreboard Error [Solved]

Discussion in 'Spigot Plugin Development' started by gartenzaun, May 25, 2015.

  1. Hello,

    i'm currently working on a pvp stats scoreboard, but it always throws me an error:

    Here's my code:

    Code (Text):
    package gartenzaun;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;

    public class ScoreboardStats implements Listener {
       
        int sched;
       
        public ScoreboardStats(PeaceBuild peacebuild) {
            peacebuild.getServer().getPluginManager().registerEvents(this, peacebuild);
        }
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerJoin(final PlayerJoinEvent event) {
            if(!Bukkit.getScheduler().isCurrentlyRunning(sched)) {
                sched = Bukkit.getScheduler().scheduleAsyncRepeatingTask(PeaceBuild.plugin, new Runnable() {
                    @Override
                    public void run() {
                        updateSocreboard(event.getPlayer());
                    }
                }, 20, 20);
            }
        }
       
        @SuppressWarnings("deprecation")
        public void updateSocreboard(Player p) {
            Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
            Objective obj = board.registerNewObjective("", "");
           
            obj.setDisplayName("PeaceBuild");
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
           
            int kills = 1;
            int deaths = 2;
           
            Score five = obj.getScore(Bukkit.getOfflinePlayer("Kills"));
            Score four = obj.getScore(Bukkit.getOfflinePlayer("" + kills));
            Score three = obj.getScore(Bukkit.getOfflinePlayer("Deaths"));
            Score two = obj.getScore(Bukkit.getOfflinePlayer(" " + deaths));
            Score one = obj.getScore(Bukkit.getOfflinePlayer("=========="));
            Score zero = obj.getScore(Bukkit.getOfflinePlayer("=========="));
           
            five.setScore(5);
            four.setScore(4);
            three.setScore(3);
            two.setScore(2);
            one.setScore(1);
            zero.setScore(0);
           
            p.setScoreboard(board);
        }  
    }
     

    Here's the error:

    Code (Text):
    [08:59:30 WARN]: org.apache.commons.lang.UnhandledException: Plugin PeaceCraftPeaceBuild v0.1 generated an exception while executing task 875
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.IllegalStateException: Asynchronous scoreboard creation!
            at org.spigotmc.AsyncCatcher.catchOp(AsyncCatcher.java:14)
            at org.bukkit.craftbukkit.v1_8_R2.scoreboard.CraftScoreboardManager.getNewScoreboard(CraftScoreboardManager.java:45)
            at org.bukkit.craftbukkit.v1_8_R2.scoreboard.CraftScoreboardManager.getNewScoreboard(CraftScoreboardManager.java:1)
            at ihgnids.ScoreboardStats.updateSocreboard(ScoreboardStats.java:36)
            at ihgnids.ScoreboardStats$1.run(ScoreboardStats.java:28)
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftTask.run(CraftTask.java:71)
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
            ... 3 more
     

    Did I do something wrong?
     
  2. Read the error.
    You can't make scoreboards async.
     
  3. Oh sry, changed line 24 to sched = Bukkit.getScheduler().scheduleSyncRepeatingTask(PeaceBuild.plugin, new Runnable() {.

    The error is now gone, but it still doesn't show me any scoreboard.
     
  4. how ???
     
  5. Probably:

    - Objective obj = board.registerNewObjective("", "");
    +Objective obj = board.registerNewObjective("someobjective", "dummy");
     
  6. Yep, that's exactly what I made :D
     
  7. Ich schau mir das morgen mal genauer an. Trotzdem ist spigotmc englisch :D
     
  8. xD
     
  9. Hmm, i dont see where it fails... You could try out the TextScoreboard class i use in my plugins (self written). This is 100% tested and works like a charm:

    Code (Text):
    package net.friwi.util.scoreboard;

    import org.bukkit.Bukkit;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;

    public class TextScoreboard {
        public static void createScoreboard(Player p, String title, String[] text){
            //Create new Scoreboard
           
            ScoreboardManager mgr = Bukkit.getScoreboardManager();
            Scoreboard s = mgr.getNewScoreboard();
         
            //Create objective


            Objective obj = s.registerNewObjective("sb", "dummy");
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
            obj.setDisplayName(title);
         
         
            //Add lines
         
            int value = text.length;
            for(String x : text){
                String pre = "";
                for(char c : (value+"").toCharArray()){
                    pre+="§"+c;
                }
                pre+="§f";
                x=pre+x;
                if(x.length()<=16){
                    obj.getScore(x).setScore(value);
                }else{
                    Team team = s.registerNewTeam("line"+value);
                    String prefix = x.substring(0, 16);
                    String name = x.substring(16, x.length());
                    String suffix = "";
                    if(name.length()>16){
                        name = name.substring(0, 16);
                        suffix = x.substring(32, x.length());
                        if(suffix.length()>16)suffix = suffix.substring(0, 16);
                    }
                    team.setPrefix(prefix);
                    team.setSuffix(suffix);
                    OfflinePlayer op = Bukkit.getOfflinePlayer(name);
                    team.addPlayer(op);
                    obj.getScore(op).setScore(value);
                }
             
             
                value--;
            }
         
            //Set the Scoreboard for the Player
         
            p.setScoreboard(s);
        }
    }
     
    When you want to edit the scoreboard content just call this function again. The old scoreboard will be garbage collected.
     
  10. thanks :D