Help - Scoreboard Names As Score

Discussion in 'Plugin Development' started by martin0leung, Oct 25, 2014.

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

    martin0leung

    Is there a way to make a scoreboard name that has the score in it like:
    [​IMG] ( :p that is my Hypixel Arena Brawl Stats, don't judge me plz :D )

    The number on the right is just the sequence then the left number is the score, how do you do it?
    This is the score name code I got right now :
    Code:java
    1. Score kills = stats.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.RED + "" + ChatColor.BOLD + "Total Kills:" + ChatColor.AQUA + ChatColor.BOLD + TotalKills));
    2. kills.setScore(0);

    The TotalKills is a hashmap I made which does its job well, but How do I get it to change the scoreboard?
     
  2. Offline

    tcvs

    martin0leung First have you set the scoreboard to display? If you could show us all your code that will help.
     
  3. Offline

    eyamaz

    Moved to Dev.
     
  4. Offline

    TheCodingCat

    if you are using per-player-scoreboards which I'm sure you are, remember to put the
    player.setScoreboard(YourScoreboard);
     
  5. Offline

    JjPwN1

    This is how I do it:


    Code:java
    1. Scoreboard board = Utils.getScoreboardManager().getNewScoreboard();
    2. Objective obj = board.registerNewObjective("board", "dummy");
    3. obj.setDisplayName(gold + player.getName());
    4. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    5. List<Score> scores = Arrays.asList(obj.getScore("Wallet"),
    6. obj.getScore("$" + (int)this.getWalletBalance()),
    7. obj.getScore("Bank"),
    8. obj.getScore("$" + (int)this.getBankBalance()));
    9. int order = scores.size();
    10. for(Score s : scores){
    11. s.setScore(order);
    12. order--;
    13. }
    14. player.setScoreboard(board);
     
  6. Offline

    martin0leung

    Ok, sorry! This is my scoreboard code:

    Privates:
    Code:java
    1. private Map<OfflinePlayer, Score> TotalKills = new HashMap<OfflinePlayer, Score>();
    2. private org.bukkit.scoreboard.Scoreboard board;


    onEnable()
    Code:java
    1. @Override
    2. public void onEnable() {
    3. Bukkit.getServer().getPluginManager().registerEvents(this,this);
    4.  
    5. setupScoreboard();
    6. }


    setupScoreboard()
    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void setupScoreboard() {
    3. board = Bukkit.getScoreboardManager().getNewScoreboard();
    4.  
    5.  
    6. Objective stats = board.registerNewObjective("Thronch", "dummy");
    7. stats.setDisplaySlot(DisplaySlot.SIDEBAR);
    8. stats.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Thronch Arena " + ChatColor.AQUA + ChatColor.BOLD + "[ALPHA]");
    9.  
    10. Score kills = stats.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.RED + "" + ChatColor.BOLD + "Total Kills:" + ChatColor.AQUA + ChatColor.BOLD + TotalKills));
    11. kills.setScore(0);
    12. }


    On Player Join:
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent e) {
    3. Player p = e.getPlayer();
    4. if (!p.hasPlayedBefore()) {
    5. p.setScoreboard(board);
    6. KillsHash(p);
    7. }
    8. }


    KillsHash:
    Code:java
    1. public void KillsHash(Player player) {
    2. OfflinePlayer p = player.getPlayer();
    3. if (!TotalKills.containsKey(p)) {
    4. TotalKills.get(p).setScore(0);
    5. }
    6. }


    On Player Death:
    Code:java
    1. @EventHandler
    2. public void OnPlayerDeath(PlayerDeathEvent e) {
    3. Player killer = e.getEntity().getKiller();
    4. if (killer instanceof Player) {
    5. addKillsToHash(killer);
    6. Score killerKills = TotalKills.get(killer.getName());
    7. killer.sendMessage(ChatColor.BOLD + "" + killerKills);
    8. }
    9. }


    Add Kills To Hash:
    Code:java
    1. public void addKillsToHash(Player player) {
    2. TotalKills.get(player.getPlayer()).setScore(TotalKills.get(player.getPlayer()).getScore() + 1);
    3. }


    Hope that helps you a little bit about understanding what I did.

    PS: It doesn't update the scores anymore... the killer.sendMessage() doesn't do anything, it just puts errors to the server console. It is something wrong with the TotalKills.get(p).setScore(0) How should I do it? there must be another way.
     
  7. Offline

    martin0leung

    Bump... Guys how do I set a HashMap with <OfflinePlayer, Score> and put the Score as a int? like TotalKills.put(aplayername, the number); ? I want the number to be a number, how do I do it? Please tell me...
     
  8. Offline

    martin0leung

    bump! Please find out a way to do it! i'm desperate
     
  9. Offline

    mythbusterma

  10. Offline

    OffLuffy

    As mythbusterma said (but with a bit more details):

    You can use numbers in HashMaps, but not the primitive data types. So if you want to put an 'int' in a HashMap, put 'Integer' instead. Likewise for 'long' and 'Long', etc. HashMap<OfflinePlayer, Integer> should work for ya. You could also create a 'Score' class and store an int in it, but that's probably counter-productive if that's all the class is for.
     
  11. Offline

    martin0leung

    mythbusterma

    In my understanding, I can't do most of my stuff using a integer, I must use a score, so that is why I am using it because it comes from the bukkit scoreboard.
     
  12. Offline

    OffLuffy

    You can nest HashMaps inside one another if you need to. I'm not sure if it's good practice, but you can do something like:
    Code:java
    1. HashMap<OfflinePlayer, HashMap<Score, Integer>> scores;

    And access the Integer via :
    Code:java
    1. Integer i = scores.get(OfflinePlayer).get(Score) ;

    Or you can have two HashMaps, one with <OfflinePlayer, Score> and another with <OfflinePlayer, Integer>, and access either one as you need to using the same player object.

    Not sure if that helps you. If you can fetch the Score from the Scoreboard itself via the OfflinePlayer, maybe you can get away with not storing the Score in the HashMap at all, but rather the Integer, and just get the Score from the Scoreboard.
     
  13. Offline

    martin0leung

    OffLuffy

    Sorry because i'm still new to bukkit... What do I do to change my code and what I should implement to make that even possible? :p

    Edit: And I am trying to set the score of the hashmap to be a integer...
     
  14. Offline

    martin0leung

    Bump?...

    BUMP! Please!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page