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!
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--); }
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.
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.
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);
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); } }
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.
Oh yeah, but then that is underlined because the operator "-" is undefined for the types String and Int