How can I create a scoreboard that shows multiple user-specific stats?

Discussion in 'Plugin Development' started by SaxSalute, Mar 22, 2014.

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

    SaxSalute

    I have been trying to come up with a solution to this all day, but the Bukkit Scoreboard implementation is incredibly dense. I recognize that since scoreboards are designed to show one stat about lots of players to lots of players, some trickery has to be used to make it show a lot of stats about one player to one player. I'm trying to get something similar to what is shown on a server like KitPvP where the objective display name is your IGN and the stats are numbers that pertain to you and only you. The only thought I had was to use HashMap<OfflinePlayer, Scoreboard>, HashMap<OfflinePlayer, Objective>, and HashMap<OfflinePlayer, ArrayList<OfflinePlayer, Score>> to keep specific scoreboards, objectives, and scores for each player, but that failed catastrophically in testing. How can I do something like the KitPvP Scoreboard?
     
  2. Offline

    drtshock

    Paste some code you've done so we have something to go off of.
     
    Garris0n likes this.
  3. Offline

    SaxSalute

    This is just one of many broken attempts, the one I mentioned above. I haven't managed anything that works to any degree. I think I just have a fundamental misunderstanding about how scoreboards handle individual instances.
    Code:java
    1. lobbyBoards.put(player, Bukkit.getServer().getScoreboardManager().getNewScoreboard());
    2.  
    3. o.put(player, lobbyBoards.get(player).registerNewObjective("test", "dummy"));
    4. o.get(player).setDisplaySlot(DisplaySlot.SIDEBAR);
    5. player.setScoreboard(lobbyBoards.get(player));
    6. o.get(player).setDisplayName("Arena Stats");
    7. Score score = o.get(player).getScore(plugin.getServer().getOfflinePlayer(playerName));
    8. score.setScore(playerName.length());
     
  4. I am having the same problem SaxSalute.. :(
     
  5. Offline

    SaxSalute

    I actually came up with a solution. This class can be used to easily build player specific scoreboards.

    Code:java
    1. package bourg.austin.VersusArena.Interface;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.OfflinePlayer;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.scoreboard.DisplaySlot;
    10. import org.bukkit.scoreboard.Objective;
    11. import org.bukkit.scoreboard.Score;
    12. import org.bukkit.scoreboard.Scoreboard;
    13.  
    14. public class DisplayBoard
    15. {
    16. private Player player;
    17.  
    18. private Scoreboard board;
    19. private Objective o;
    20. private HashMap<OfflinePlayer, Score> values;
    21.  
    22. private ChatColor scoreColor, headerColor;
    23.  
    24. public DisplayBoard(Player player, String title, ChatColor scoreColor, ChatColor headerColor)
    25. {
    26. this.player = player;
    27. this.scoreColor = scoreColor;
    28. this.headerColor = headerColor;
    29.  
    30. board = Bukkit.getScoreboardManager().getNewScoreboard();
    31. o = board.registerNewObjective(title, "dummy");
    32. o.setDisplayName(title);
    33. o.setDisplaySlot(DisplaySlot.SIDEBAR);
    34.  
    35. values = new HashMap<OfflinePlayer, Score>();
    36. }
    37.  
    38. private void put(String text)
    39. {
    40. String displayText = text;
    41. //Add spaces to the end
    42. while (displayText.length() < 16)
    43. displayText += " ";
    44.  
    45. //Loop and remove spaces for duplicates
    46. while (true)
    47. {
    48. if (!values.keySet().contains(Bukkit.getOfflinePlayer(displayText)))
    49. break;
    50.  
    51. displayText = displayText.substring(0, displayText.length() - 1);
    52.  
    53. if (displayText.length() < (text).length())
    54. return;
    55. }
    56.  
    57. values.put(Bukkit.getOfflinePlayer(displayText), o.getScore(Bukkit.getOfflinePlayer(displayText)));
    58. values.get(Bukkit.getOfflinePlayer(displayText)).setScore(0);
    59. for (OfflinePlayer p : values.keySet())
    60. values.get(p).setScore(values.get(p).getScore() + 1);
    61. }
    62.  
    63. public void putField(String name, int value)
    64. {
    65. put(name + scoreColor + value);
    66. }
    67.  
    68. public void putHeader(String text)
    69. {
    70. put(headerColor + text);
    71. }
    72.  
    73. public void putSpace()
    74. {
    75. put("");
    76. }
    77.  
    78. public void display()
    79. {
    80. player.setScoreboard(board);
    81. }
    82. }
     
  6. Offline

    Garris0n

    SaxSalute If you're going to be storing the player inside the class, be careful about how you're storing the class.
     
  7. Offline

    SaxSalute

    The scoreboard is disposed of at logout if that's what you're thinking.
     
  8. Offline

    Mr360zack

    how i currently do it:
    Code:java
    1. void setScoreboard(Player p) {
    2. board = Bukkit.getScoreboardManager().getNewScoreboard();
    3. o = board.registerNewObjective(p.getName().toString(), "dummy");
    4. o.setDisplaySlot(DisplaySlot.SIDEBAR);
    5. o.setDisplayName(ChatColor.BOLD+"ยง6"+p.getName());
    6. Score kills = board.getObjective(p.getName().toString()).getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW+"Kills"));
    7. Score deaths = board.getObjective(p.getName().toString()).getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW+"Deaths"));
    8. Score killstreak = board.getObjective(p.getName().toString()).getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW+"Killstreak"));
    9. Score credits = board.getObjective(p.getName().toString()).getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.YELLOW+"Credits"));
    10.  
    11. kills.setScore(69);
    12. deaths.setScore(69);
    13. killstreak.setScore(69);
    14. credits.setScore(69);
    15.  
    16. //////////////////////////////////////////////////////
    17. kills.setScore(StatsManager.getStats(p.getUniqueId().toString()).getKills());
    18. deaths.setScore(StatsManager.getStats(p.getUniqueId().toString()).getDeaths());
    19. killstreak.setScore(StatsManager.getStats(p.getUniqueId().toString()).getKillstreak());
    20. credits.setScore(StatsManager.getStats(p.getUniqueId().toString()).getCredits());
    21. //////////////////////////////////////////////////////
    22. p.setScoreboard(board);
    23.  
    24. }
     
Thread Status:
Not open for further replies.

Share This Page