Show player money on scoreboard!

Discussion in 'Plugin Development' started by kevin3220, Jul 29, 2013.

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

    kevin3220

    Hello,


    How to show players money on scoreboard ;( ?



    Thank you.
     
  2. Offline

    Nicster189

    I would like to now this to if anyone knows a way!
     
  3. kevin3220
    You should give (alot) more information.

    - What money?
    - What plugin (if any) holds this money?
    - How does it hold it?
    etc.

    Anyways, chasechocolate (tagging because I can't copy links for some reason) made an excellent tutorial on how to use scoreboards.
     
  4. Offline

    chasechocolate

  5. Offline

    etaxi341

  6. Offline

    kevin3220

    @chasechocolate etaxi341
    Code:java
    1. package pl.plugin;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.scoreboard.DisplaySlot;
    9. import org.bukkit.scoreboard.Objective;
    10. import org.bukkit.scoreboard.Score;
    11. import org.bukkit.scoreboard.Scoreboard;
    12. import org.bukkit.scoreboard.ScoreboardManager;
    13.  
    14.  
    15.  
    16. public class kaka{
    17.  
    18. public static HashMap<String, Scoreboard> UsedScoreboard = new HashMap<String, Scoreboard>();
    19. public static HashMap<String, Score> levelscore = new HashMap<String, Score>();
    20. public static HashMap<String, Score> moneyscore = new HashMap<String, Score>();
    21.  
    22. public static void create(Player p, String BoardName, String BoardDisplayName){
    23. ScoreboardManager manager = Bukkit.getScoreboardManager();
    24. Scoreboard board = manager.getNewScoreboard();
    25.  
    26. Objective objective = board.registerNewObjective(BoardName, "dummy");
    27.  
    28. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    29. objective.setDisplayName(BoardDisplayName);
    30.  
    31. Score level = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Level" + ChatColor.GRAY + ":"));
    32. Score money = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Money" + ChatColor.GRAY + ":"));
    33.  
    34. level.setScore(0);
    35. money.setScore(0);
    36.  
    37. levelscore.put(p.getName(), level);
    38. moneyscore.put(p.getName(), money);
    39.  
    40. UsedScoreboard.put(p.getName(), board);
    41. }
    42.  
    43. public static void update(Player p){
    44.  
    45. Score level = levelscore.get(p.getName());
    46. Score money = moneyscore.get(p.getName());
    47.  
    48. level.setScore(PlayerDataListener.getLevel(p.getName()));
    49. money.setScore(PlayerDataListener.getMoney(p));
    50.  
    51. }
    52.  
    53. public static void set(Player p){
    54.  
    55. p.setScoreboard(UsedScoreboard.get(p.getName()));
    56.  
    57. }}



    ive got errors @ 48,49 lines. and should i make a brand new class and put this all into it?
     
  7. Offline

    etaxi341

    kevin3220 Yeah this is a Part out of my Plugin. You need to replace the PlayerDataListener.getLevel(p.getName())) with an Integer
     
  8. Offline

    kevin3220

    where should i put this code xD ? onenable() ?
     
  9. Offline

    etaxi341

  10. Offline

    kevin3220

    No i mean code? Where should i put code?
     
  11. Offline

    etaxi341

    kevin3220 Make a new Class for this Methods and call them in the PlayerJoinEvent. To update them call them in the onEnable about every 5 Seconds.
     
  12. Offline

    kevin3220

    Should i make parameters null?
    Code:
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        create(null,null,null);
        update(null);
       
    }
     
    
     
  13. Offline

    etaxi341

    kevin3220 No use it like this:
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent ev){
    3. Player p = ev.getPlayer();
    4. String pname = p.getName().toLowerCase();
    5.  
    6. if (!playerScoreboards.containsKey(pname))
    7. create(p, pname, ChatColor.BLUE + "Player Statistic");
    8.  
    9. update(p);
    10. set(p);
    11.  
    12. }

    And your Methods should look like this:
    Code:java
    1. public static HashMap<String, Scoreboard> playerScoreboards = new HashMap<String, Scoreboard>();
    2.  
    3. public static HashMap<String, Score> moneyscore = new HashMap<String, Score>();
    4.  
    5.  
    6. public static void create(Player p, String BoardName, String BoardDisplayName){
    7. String pname = p.getName().toLowerCase();
    8.  
    9. ScoreboardManager manager = Bukkit.getScoreboardManager();
    10. Scoreboard board = manager.getNewScoreboard();
    11.  
    12. Objective objective = board.registerNewObjective(BoardName, "dummy");
    13.  
    14. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    15. objective.setDisplayName(BoardDisplayName);
    16.  
    17. Score money = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Money" + ChatColor.GRAY + ":"));
    18.  
    19. money.setScore(0); // On Create just set the Integer of Money to 0
    20.  
    21. moneyscore.put(pname, money);
    22.  
    23. playerScoreboards.put(pname, board);
    24. }
    25.  
    26. public static void update(Player p){
    27. String pname = p.getName().toLowerCase();
    28.  
    29. Score money = moneyscore.get(pname);
    30.  
    31. money.setScore(100); //In this case everytime you Update the Scoreboard hist Money in the Scoreboard will be set to 100. (Replace the 100 with a Method of you to get the Players Money.
    32.  
    33. }
    34.  
    35. public static void set(Player p){
    36.  
    37. String pname = p.getName().toLowerCase();
    38. p.setScoreboard(playerScoreboards.get(pname));
    39.  
    40. }
     
  14. Offline

    kevin3220

    Do i need to register Events @ my mainclass?
     
  15. Offline

    etaxi341

    kevin3220 You mean the Join Listener? Yes you need to register it.
     
  16. Offline

    kevin3220

    What a about others? like create and update? Need registering too?
     
  17. Offline

    etaxi341

    kevin3220 Create is in the Player Join Event I've sent you. You only need to update the scoreboard maybe about every 5 Seconds by using a Scheduler in the onEnable
     
  18. Offline

    Xinito


    How would the scheduler code look like?
     
  19. Offline

    etaxi341

    Xinito This is how my Scheduler looks: (ScoreboardCreator is the Class where my Methods are)
    Code:java
    1. //REFRESH SCOREBOARD EVERY 5 SECONDS
    2. Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.instanz, new Runnable() {
    3. @Override
    4. public void run() {
    5. for (Player p:Bukkit.getOnlinePlayers()){
    6.  
    7. if (!ScoreboardCreator.playerScoreboards.containsKey(p.getName()))
    8. ScoreboardCreator.create(p, p.getName(), ChatColor.Blue + "Player Statistic");
    9.  
    10. ScoreboardCreator.update(p);
    11. ScoreboardCreator.set(p);
    12. }
    13. }
    14. },0L,100L);
     
  20. Offline

    kevin3220

    Dude, i love you xD Thank you, got it work! Really thanks!
     
  21. Offline

    etaxi341

    Vinsanity and kevin3220 like this.
Thread Status:
Not open for further replies.

Share This Page