Seperate scoreboard per user.

Discussion in 'Plugin Development' started by Mindfulhacker, Oct 12, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Mindfulhacker

    Hi there!

    I'm currently working on a plugin for a server. The plugin will take variables such as money and place them in a scoreboard. However, I need this scoreboard to be different for each user. So each user has their own money value.

    I've seen the idea of hashmaps floating around, but do you have any ideas??

    Code:java
    1.  
    2. private Scoreboard sb;
    3.  
    4. public void setupScoreboard(){
    5.  
    6. sb = Bukkit.getScoreboardManager().getNewScoreboard();
    7.  
    8. Objective ServerName = sb.registerNewObjective("ServerName", "dummy");
    9. ServerName.setDisplayName(ChatColor.GOLD + "Server Name");
    10. ServerName.setDisplaySlot(DisplaySlot.SIDEBAR);
    11. Score test = ServerName.getScore(Bukkit.getOfflinePlayer("Money"));
    12. test.setScore(1000);
    13. }
    14.  
    15. @Override
    16. public void onEnable(){
    17. getServer().getPluginManager().registerEvents(this,this);
    18. setupScoreboard();
    19. }
    20.  
    21. public Scoreboard getScoreboard(){
    22. return this.sb;
    23. }
    24.  
    25. @EventHandler
    26. public void onJoin(PlayerJoinEvent event){
    27. Player player = event.getPlayer();
    28. player.setScoreboard(getScoreboard());
    29. }
     
  2. Offline

    fireblast709

    Mindfulhacker Create a new Scoreboard each time a Player joins
     
  3. Offline

    Mindfulhacker

    fireblast709

    Ok, as you suggested, I modified the code.
    Now, whenever the task is run, the player's stats show for others too.

    Code:java
    1. package net.mindfulhacker.testing;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import net.milkbowl.vault.economy.Economy;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.PlayerJoinEvent;
    13. import org.bukkit.plugin.RegisteredServiceProvider;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.scoreboard.DisplaySlot;
    16. import org.bukkit.scoreboard.Objective;
    17. import org.bukkit.scoreboard.Score;
    18. import org.bukkit.scoreboard.Scoreboard;
    19.  
    20. public class PluginMain extends JavaPlugin implements Listener {
    21. // ScoreboardManager manager = Bukkit.getScoreboardManager();
    22. // public void onEnable(){
    23. // Player player = event.getPlayer();
    24. // Scoreboard board = manager.getNewScoreboard();
    25. // Objective objective = board.registerNewObjective("test", "dummy");
    26. // objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    27. // objective.setDisplayName("Testing");
    28. // Score test = objective.getScore(Bukkit.getOfflinePlayer("TestName"));
    29. // test.setScore(1);
    30.  
    31. // }
    32.  
    33. public static Economy econ = null;
    34. Scoreboard sb;
    35. private static final Logger log = Logger.getLogger("Minecraft");
    36. static Objective ServerName;
    37. static Score test;
    38. static Score money;
    39.  
    40.  
    41.  
    42. public void setupScoreboard(){
    43.  
    44. sb = Bukkit.getScoreboardManager().getNewScoreboard();
    45. int ServerPlayers = this.getServer().getOnlinePlayers().length;
    46. ServerName = sb.registerNewObjective("ServerName", "dummy");
    47. ServerName.setDisplayName(ChatColor.BLUE + "Server Name");
    48. ServerName.setDisplaySlot(DisplaySlot.SIDEBAR);
    49. test = ServerName.getScore(Bukkit.getOfflinePlayer("Online:"));
    50. money = ServerName.getScore(Bukkit.getOfflinePlayer("Money: $"));
    51.  
    52. test.setScore(ServerPlayers);
    53. }
    54.  
    55. private boolean setupEconomy() {
    56. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    57. return false;
    58. }
    59. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    60. if (rsp == null) {
    61. return false;
    62. }
    63. econ = rsp.getProvider();
    64. return econ != null;
    65. }
    66. @Override
    67. public void onEnable(){
    68. getServer().getPluginManager().registerEvents(this,this);
    69. setupScoreboard();
    70.  
    71. if (!setupEconomy() ) {
    72. log.info(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    73. getServer().getPluginManager().disablePlugin(this);
    74. return;
    75. }
    76. }
    77.  
    78. public Scoreboard getScoreboard(){
    79. return this.sb;
    80. }
    81.  
    82. @EventHandler
    83. public void onJoin(PlayerJoinEvent event){
    84. Player player = event.getPlayer();
    85. setupScoreboard();
    86. player.setScoreboard(getScoreboard());
    87. money.setScore((int) Math.round(econ.getBalance(player)));
    88. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    89. @Override public void run() {
    90. for(Player p : Bukkit.getServer().getOnlinePlayers()) {
    91. p.setScoreboard(getScoreboard());
    92. money.setScore((int) Math.round(econ.getBalance(p)));
    93. }
    94.  
    95. }
    96. }, 0, 20);
    97. }
    98.  
    99. }
    100.  
     
  4. Offline

    fireblast709

    Mindfulhacker create one task in onEnable (and remove the task scheduling from your PlayerJoinEvent handler), and inside the loop, get the Scoreboard the player is viewing (getScoreboard()), get the score from that scoreboard and update it.
     
  5. Offline

    Mindfulhacker

    fireblast709 Same thing happens...

    Code:java
    1. package net.mindfulhacker.testing;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import net.milkbowl.vault.economy.Economy;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.PlayerJoinEvent;
    13. import org.bukkit.plugin.RegisteredServiceProvider;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.scoreboard.DisplaySlot;
    16. import org.bukkit.scoreboard.Objective;
    17. import org.bukkit.scoreboard.Score;
    18. import org.bukkit.scoreboard.Scoreboard;
    19.  
    20. public class PluginMain extends JavaPlugin implements Listener {
    21. // ScoreboardManager manager = Bukkit.getScoreboardManager();
    22. // public void onEnable(){
    23. // Player player = event.getPlayer();
    24. // Scoreboard board = manager.getNewScoreboard();
    25. // Objective objective = board.registerNewObjective("test", "dummy");
    26. // objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    27. // objective.setDisplayName("Testing");
    28. // Score test = objective.getScore(Bukkit.getOfflinePlayer("TestName"));
    29. // test.setScore(1);
    30.  
    31. // }
    32.  
    33. public static Economy econ = null;
    34. Scoreboard sb;
    35. private static final Logger log = Logger.getLogger("Minecraft");
    36. static Objective ServerName;
    37. static Score test;
    38. static Score money;
    39. static Score players;
    40. static Score plymoney;
    41.  
    42. public void setupScoreboard(){
    43.  
    44. sb = Bukkit.getScoreboardManager().getNewScoreboard();
    45. int ServerPlayers = this.getServer().getOnlinePlayers().length;
    46. ServerName = sb.registerNewObjective("ServerName", "dummy");
    47. ServerName.setDisplayName(" " +ChatColor.BOLD + " " + ChatColor.DARK_PURPLE + "Warped " + ChatColor.AQUA + "Prison");
    48. ServerName.setDisplaySlot(DisplaySlot.SIDEBAR);
    49. test = ServerName.getScore(Bukkit.getOfflinePlayer(ChatColor.BOLD + "" + ChatColor.GRAY + "Online:"));
    50. players = ServerName.getScore(Bukkit.getOfflinePlayer(ChatColor.BOLD + "" + ServerPlayers));
    51. money = ServerName.getScore(Bukkit.getOfflinePlayer(ChatColor.BOLD + "" + ChatColor.GRAY + "Money: $"));
    52.  
    53.  
    54. money.setScore(8);
    55. players.setScore(9);
    56. test.setScore(10);
    57. }
    58.  
    59. private boolean setupEconomy() {
    60. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    61. return false;
    62. }
    63. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    64. if (rsp == null) {
    65. return false;
    66. }
    67. econ = rsp.getProvider();
    68. return econ != null;
    69. }
    70. @Override
    71. public void onEnable(){
    72. getServer().getPluginManager().registerEvents(this,this);
    73. setupScoreboard();
    74. if (!setupEconomy() ) {
    75. log.info(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    76. getServer().getPluginManager().disablePlugin(this);
    77. return;
    78. }
    79. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    80. @Override public void run() {
    81. for(Player p : Bukkit.getServer().getOnlinePlayers()) {
    82. plymoney = ServerName.getScore(Bukkit.getOfflinePlayer("" + (int) Math.round(econ.getBalance(p))));
    83. plymoney.setScore(7);
    84. p.setScoreboard(getScoreboard());
    85. }
    86.  
    87. }
    88. }, 0, 20);
    89. }
    90.  
    91. public Scoreboard getScoreboard(){
    92. return this.sb;
    93. }
    94.  
    95. @EventHandler
    96. public void onJoin(PlayerJoinEvent event){
    97. Player player = event.getPlayer();
    98. setupScoreboard();
    99. player.setScoreboard(getScoreboard());
    100. }
    101.  
    102. }
    103.  


    Any help? Need this urgently

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  6. Offline

    fireblast709

  7. Offline

    Mindfulhacker

    fireblast709 Nothing changes.
    Now, whenever I update the money of a player, it creates another fake player.
     
  8. Offline

    Mindfulhacker

    Anyone?

    Empire92

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  9. Offline

    Empire92

    Since you tagged me, you can take a look at what I've done in my scoreboard plugin.
    https://github.com/F9Alejandro/SideBar2/tree/master/src/com/empcraft/sidebar
    https://github.com/F9Alejandro/SideBar2/blob/master/src/com/empcraft/sidebar/SideBar.java

    It basically keeps track of the offline player so when it changes, it removes the old one so that you don't get any duplicate entries.

    The reason I had to do this is that if you wipe the scoreboard every time, you get this funky flickering of the scoreboard which can get quite annoying.
     
Thread Status:
Not open for further replies.

Share This Page