Solved Anything wrong with this code?

Discussion in 'Plugin Development' started by UNC00KED, Dec 22, 2015.

Thread Status:
Not open for further replies.
  1. Having this problem with Scoreboards. I simply want to create a scoreboard that holds a list of the players in a world. So it updates when they join a world. Is there anything wrong with this code?

    Hashmap holding scoreboards:
    Code:
    Map<String, Scoreboard> scoreboards = new HashMap<String, Scoreboard>();

    This function runs onEnable:
    Code:
    void start() {
    
            for (World world : Bukkit.getServer().getWorlds()) {
               
                Scoreboard sb = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
                Objective obj = sb.registerNewObjective(world.getName(), world.getName() + "sb");
                obj.setDisplayName(world.getName());
                obj.setDisplaySlot(DisplaySlot.SIDEBAR);
                final Score objScore1 = sb.getObjective(world.getName()).getScore("");
                objScore1.setScore(2);
                final Score objScore2 = sb.getObjective(world.getName()).getScore(ChatColor.BOLD + "Players:");
                objScore2.setScore(1);
               
                scoreboards.put(world.getName(), sb);
               
            }
           
    }

    Code:
    @EventHandler
    public void worldChangeEvent (PlayerChangedWorldEvent event) {
    
            OreRunner.plugin.scoreboards.get(fromWorld).resetScores(player.getName());
            Scoreboard sb = OreRunner.plugin.scoreboards.get(toWorld);
            player.setScoreboard(sb);
            int score = OreRunner.plugin.playerScore.get(player.getName());
            final Score objScore = sb.getObjective(toWorld).getScore(player.getName());
            objScore.setScore(score);
    
    }

    To test it, I joined the sever then had another person join the server. I joined world x then I had the other player join world x. Everything worked perfectly, both players showed up on the scoreboard. Then what I did was have the other player join world y. That's when the problem occurred. Everybody's scoreboard was deleted, for lack of a better word, and all scoreboards went away.

    This code looks perfectly fine to me. It's gotta be something simple I guess that I'm missing.
     
  2. Offline

    mythbusterma

    @UNC00KED

    Well that's because you create a scoreboard for every loaded world. Don't do that.
     
  3. How else would you suggest doing it then?
     
  4. Share one scoreboard across all worlds?
     
  5. That wouldn't work because I need a different scoreboard for each world because there will be a different amount of people in each world.
     
  6. Ah, I should have read your post. Looks fine here from my phone... Post your resetScores method please?
     
  7. Stand by... I think I may have fixed it...

    Yep, it's fixed! Turned out to be another plugin that was causing the issue, a plugin that I actually made. The other plugin had one simple line of code in the PlayerChangedWorldEvent that caused all these issues:
    Code:
    player.getScoreboard().clearSlot(DisplaySlot.SIDEBAR);
    Thanks for the help guys!


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 22, 2015
  8. Haha xD that's why we first run plugins on clean setups!!
     
Thread Status:
Not open for further replies.

Share This Page