Score duplicating?

Discussion in 'Spigot Plugin Development' started by avighnash, May 14, 2016.

Thread Status:
Not open for further replies.
  1. Hello everyone! When trying to make a scoreboard, I made a score that is used to show the online players:

    Code (Text):
    core players = obj.getScore(
    //this is iside a tasktimer              
    ChatColor.WHITE + "Players: " + ChatColor.GREEN + Bukkit.getServer().getOnlinePlayers().size());
    but whenever someone new joins the server, a new line appears that duplicates the score showed above but with the new player count. What should I do to fix this? Thanks!
     
  2. create a new scoreboard for every player, then update it not set it.
     
  3. Would this work?
    Code (Text):
    @EventHandler
        public void onJoin(PlayerJoinEvent e) {

            this.obj.getScore(
                    ChatColor.WHITE + "Players: " + ChatColor.GREEN + Bukkit.getServer().getOnlinePlayers().size() + 1);

        }

        @EventHandler
        public void onLeave(PlayerQuitEvent e) {

            int i = Bukkit.getServer().getOnlinePlayers().size();

            this.obj.getScore(ChatColor.WHITE + "Players: " + ChatColor.GREEN + i--);

        }
     
  4. Try it :p
     
  5. It doesn't :(
     
  6. Looking back at the code, you aren't actually setting the line of the score. Currently, you are just getting the score, but not assigning it to any place on the actual scoreboard. You're also adding the score every time the player joins the server. Take it out of the event to prevent this.

    Score getScore(String entry)
    Gets an entry's Score for an Objective on this Scoreboard.

    void setScore(int score)
    Sets the current score.
     
  7. What i do is just have it on a runnable(The whole scoreboard)
     
  8. What does that have to do with the score duplicating every time a player joins?
     
  9. I know what ur talking about, it makes a score right under the previous score, What I think might work is removing and adding the score again inside the tasktimer, but instead I create a new scoreboard every 10 seconds but it does flicker, and takes space :/ But its the only solution that ive read and seen out there that works. But maybe this is not what u are talking about. :p
     
  10. This is what I am talking about. Is there any other way to show the amount of players on the server?
     
  11. Do it for the join and quit events. You will need to get the score based on the scoreboards current player count, not the new player count of the server. i.e.
    obj.getScore(oldText + oldPlayerCount).setScore(newText + newPlayerCount);
     
  12. Could someone join my server quickly to see if it is working?
    Thanks!

    ip: play.universalpvp.us
     
  13. NVM tested it, doesn't work.
     
  14. Can we see your current code?
     
  15. My whole class:

    Code (Text):
    package us.universalpvp.te.scoreboard;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;

    import net.md_5.bungee.api.ChatColor;
    import us.universalpvp.te.MainClass;

    public class HubScoreboard {
        private Objective obj;

        public HubScoreboard() {

        }

        @SuppressWarnings("deprecation")
        public void hubScoreboard() {

            obj = MainClass.getInstance().sb.registerNewObjective("main", "dummy");

            obj.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&b&lUniversalPvP"));
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);

            String s = ChatColor.translateAlternateColorCodes('&', "&e&luniversalpvp.us");

            String rankName = " ";

            Score rank = obj.getScore("Rank: " + rankName);
            rank.setScore(9);

            Score space1 = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.RED.toString()));
            space1.setScore(8);

            Score players = obj.getScore(
                    ChatColor.WHITE + "Players: " + ChatColor.GREEN + Bukkit.getServer().getOnlinePlayers().size());

            players.setScore(7);

            Score space2 = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW.toString()));
            space2.setScore(6);

            Score line1 = obj.getScore("---------------");
            line1.setScore(5);

            Score ip = obj.getScore(s);
            ip.setScore(4);

            Score space3 = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN.toString()));
            space3.setScore(3);

        }

        public static void update() {

            new BukkitRunnable() {

                @Override
                public void run() {

                    for (Player players : Bukkit.getOnlinePlayers()) {
                        players.setScoreboard(MainClass.getInstance().sb);
                    }

                }
            }.runTaskTimer(MainClass.getInstance(), 0L, 10L);

        }

        @EventHandler
        public void onJoin(PlayerJoinEvent e) {

            Score players = this.obj.getScore(
                    ChatColor.WHITE + "Players: " + ChatColor.GREEN + Bukkit.getServer().getOnlinePlayers().size() + 1);

            players.setScore(7);

        }

        @EventHandler
        public void onLeave(PlayerQuitEvent e) {

            int i = Bukkit.getServer().getOnlinePlayers().size();

            Score players = this.obj.getScore(ChatColor.WHITE + "Players: " + ChatColor.GREEN + i--);

            players.setScore(7);

        }
    }
     
     
  16. In the onJoin:
    getScore(ChatColor.WHITE + "Players: " + ChatColor.GREEN + Bukkit.getServer().getOnlinePlayers().size() - 1)
    setScore(ChatColor.WHITE + "Players: " + ChatColor.GREEN + Bukkit.getServer().getOnlinePlayers().size());

    and for the onLeave I am not sure if getOnlinePlayers() still contains the player that is leaving or not, you will have to test this yourself.
     
  17. Shouldn't it be + 1?
     
  18. And for the setScore, it is underlined red because it is a string, not a integer..
     
  19. its -1 cus ur getting rid of it and adding it back :p
     
  20. Oh yeah, but then that is underlined because the operator "-" is undefined for the types String and Int
     
Thread Status:
Not open for further replies.