Scoreboard automatically reset after player quits

Discussion in 'Spigot Plugin Development' started by AnujGR, May 26, 2015.

  1. Hey guys! I have a really annoying problem. I created a scoreboard in my code for my RPG plugin. The problem is, even though the scoreboard works and all, whenever a player quits the server and joins again, the scoreboard is completely gone and scores are reset. All the scores are gone, even though I'm not resetting the scores in my code. Help?
  2. Just with a eventjoin, set the scoreboard again. For the 'scores are gone' thing, you can make a hashmap on quit en get it onjoin and place it in your scoreboard.
  3. Main.class code (part of it, because this is technically the only part where the scoreboard comes into play):

    Code (Text):

    public void onEnable(){
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
           public void run() {
             Collection<? extends Player> playerlist = Bukkit.getServer().getOnlinePlayers();
             for(Player p : playerlist){
               if(p != null){
                 if(playerlist != null){
                   if(p.getScoreboard() != null && p.getScoreboard().getObjective("Mage XP") != null){
                     if(p.getScoreboard().getObjective("Mage XP").getScore(p.getName()).getScore() >= 20 && p.getScoreboard().getObjective("Mage XP").getScore(p.getName()).getScore() < 30){
                       if(p.getScoreboard().getObjective("Statistics").getScore(ChatColor.DARK_GREEN + "Mage Level: ").getScore() == 1){
                         p.getScoreboard().getObjective("Statistics").getScore(ChatColor.DARK_GREEN + "Mage Level: ").setScore(p.getScoreboard().getObjective("Statistics").getScore(ChatColor.DARK_GREEN + "Mage Level: ").getScore() + 1);
                         p.sendMessage(ChatColor.AQUA + "You have levelled up as a " + ChatColor.LIGHT_PURPLE + "Mage" + ChatColor.AQUA + "! Your " + ChatColor.LIGHT_PURPLE + "Mage Level" + ChatColor.AQUA + " is now 1.");
         }, 300L, 20L);
       public void onPlayerJoin(PlayerJoinEvent e){
         if(e.getPlayer() != null){
           scoreboardsys = new ScoreboardSys();
             e.getPlayer().sendMessage(ChatColor.GREEN + "Welcome back to the server, " + ChatColor.DARK_GREEN + e.getPlayer().getName() + ChatColor.GREEN + "!");
             e.getPlayer().sendMessage(ChatColor.GREEN + "Welcome to the server, " + ChatColor.DARK_GREEN + e.getPlayer().getName() + ChatColor.GREEN + "!");
             e.getPlayer().getScoreboard().getObjective("Mage Lvl").getScore(e.getPlayer().getName()).setScore(1);
             e.getPlayer().getScoreboard().getObjective("Knight Lvl").getScore(e.getPlayer().getName()).setScore(1);
    ScoreboardSys.class (the full thing, this class is important):

    Code (Text):

    package org.plugin.osgarnioth;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Bukkit;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;

    public class ScoreboardSys {
       public Scoreboard board;
       public Objective magelevel;
       public Objective mageXP;
       public Objective knightlevel;
       public Objective knightXP;
       public Objective stats;
       public ScoreboardSys(){
         ScoreboardManager manager = Bukkit.getServer().getScoreboardManager();
         board = manager.getNewScoreboard();
         magelevel = board.registerNewObjective("Mage Lvl", "dummy");
         magelevel.setDisplayName(ChatColor.LIGHT_PURPLE + "Mage Lvl");
         mageXP = board.registerNewObjective("Mage XP", "dummy");
         knightlevel = board.registerNewObjective("Knight Lvl", "dummy");
         knightlevel.setDisplayName(ChatColor.LIGHT_PURPLE + "Knight Lvl");
         knightXP = board.registerNewObjective("Knight XP", "dummy");
         stats = board.registerNewObjective("Statistics", "dummy");
         stats.setDisplayName(ChatColor.GREEN + "Statistics");
    [EDIT] I copied the code wrongly. It's alright now.
  4. I thought HashMaps are deleted after the plugin/server is closed.
  5. You're creating your scoreboard a special way.. I would suggest making your scoreboard with a Util or learn some more about scoreboards.
  6. Nah, this way works. All that matters is why are the scores being reset? I don't understand that.
  7. If you are using the server console I may be able to help. I use this for my kitpvp plugin.
  8. I'm not using a server host. I'm hosting an offline non-portforwarded server on my local network.
  9. Maybe because of that ;)

  10. Not that. I mean running commands from the server console.
    Code (Text):
    Bukkit.getServer().dispatchCommand(Bukkit.getServerConsole(), "<command>");
  11. There are a couple reasons you are not getting the result you want. First:

    In your onJoin event, you check if the player has a scoreboard. And if they don't, you create a new one. They will never have a scoreboard because you left the server.


    You need to store your data somewhere referencing the player in some way. Then, when the player logs back in, you just populate a scoreboard with those values.

    Instead of looping through the players and actually using the scoreboard as a reference, you should set the values in a map or list, and then display those values with the scoreboard.
  12. Ok just asking.
  13. @Fiwi I think I get what you're trying to convey :p I'm setting the player's scoreboards to be a new, blank one every time they join XD I'm so silly
    • Funny Funny x 1
  14. Thats the long explanation to my post above :)
    • Like Like x 1
  15. @ismooch HashMaps, Lists and ArrayLists are deleted after the program is terminated. This means that when the server is closed, all maps and lists are deleted.
  16. You only pointed out they were going to get a new scoreboard on join, it still does not solve that the only reference for the values are the scoreboard itself. The reference for the scoreboard that the player is using is gone the minute the player disconnects. No real way around not giving the player a new scoreboard, just have to find a different way to show the values.
    • Agree Agree x 1
  17. I'll probably find a way out of this problem. Maybe I'll save the scores in a file or something. Any other suggestions? I get my mistake now :p

  18. Then store it. Use a config and store the player's values there. And you think scoreboards are persistent across server restarts? because they are not.
    • Agree Agree x 1