How can I create objectives for each player?

Discussion in 'Plugin Development' started by NonameSL, Feb 15, 2014.

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

    NonameSL

    I am trying to create objectives for each players - that each player would see the same objective in slot sidebar but with a different displayname, lets take Player1 and Player2 for example:
    I want them both to see the scoreboard of OfflinePlayer Time that has 5 points, but I want the title of Player1s objective will show the displayname of Player1s Dislpay and the title of Player2s objective will show the displayname of Player2s Display, at the same time. How can I do that?

    (If my question is unclear, here is some pictures of what I want the 2 objectives of Player1s and Player2s to be:)
    Player1s Screen : [​IMG]
    Player2s Screen: [​IMG]
     
  2. Offline

    FlareLine

    I'm not sure, but I don't think you can have different scoreboards for each player, as the scoreboard is Server referenced.
     
  3. Offline

    bars96

  4. Offline

    NonameSL

    I tried this code for creating different Scoreboards + Objectives but t is still noy working.
    Code:java
    1. public void setScoreboard(Player p){
    2. Objective o;
    3. Scoreboard board = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
    4. if(board.getObjective(p.getName())!=null){
    5. throw new IllegalArgumentException(p.getName()+" is already a registered player.");
    6. }else{
    7. o = board.registerNewObjective(p.getName(), "dummy");
    8. if(DisguiseCraft.getAPI().getDisguise(p)!=null){
    9. String disguise = Material.getMaterial(DisguiseCraft.getAPI().getDisguise(p).getBlockID()).toString();
    10. String s = "";
    11. for(String str : disguise.split("_")){
    12. s += capitalize(str)+" ";
    13. }
    14. o.setDisplayName(ChatColor.BLUE+s);
    15. }else{
    16. o.setDisplayName(ChatColor.BLUE+"No Disguise.");
    17. }
    18. o.setDisplaySlot(DisplaySlot.SIDEBAR);
    19. o.getScore(p).setScore(1);
    20. boards.add(board);
    21. }
    22. }
    23. public void onEnable() {
    24. for(Player p :Bukkit.getOnlinePlayers()){
    25. setScoreboard(p);
    26. Bukkit.getServer().getLogger().info(p.getName()+" had his scoreboard registered.");
    27. }
    28. }

    There is no error but I cant see the scoreboard.
     
  5. Offline

    FlareLine

    Did you use /scoreboard objectives setdisplay?
    Nevermind.
    Edit: For a scoreboard not to show up, it means that noone has a score on it.
     
  6. Offline

    Panjab

    NonameSL

    Your wrong code is between the lines 23 - 28.
    You are setting the scoreboard for every player who is online, but while your plugin is starting up - which is defenitely at the server startup, where no player is able to join - there is no player online so no one will see his personal scoreboard.

    You have to move your setScoreboard(p); usage to a PlayerJoinEvent, where you do something like this:
    --> Remember: You have to use a static method (public static void setScoreboard()) or an instance of your main class!

    Code:java
    1.  
    2. @EventHandler
    3. public void onScoreboardSet(PlayerJoinEvent event) {
    4. <instance>.setScoreboard(event.getPlayer());
    5. }
     
    Vinsanity likes this.
  7. Offline

    mastermustard

    You can. Plugins such as mcmmo do it
     
  8. Offline

    FlareLine

  9. Offline

    NonameSL

    Panjab , this dosen't seem to work, it does nothing. What can I do?
     
  10. Offline

    FlareLine

  11. Offline

    NonameSL

    Mcmmo can do it? Where in code and how?

    NEVERMIND, This code seemed to work.
    Code:java
    1. public void setScoreboard(Player player){
    2. ScoreboardManager manager = Bukkit.getScoreboardManager();
    3. Scoreboard board;
    4. if(omap.get(player)==null){
    5. board = manager.getNewScoreboard();
    6. }else{
    7. board = omap.get(player);
    8. }
    9. Objective objective = board.registerNewObjective("objective", "dummy");
    10. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    11. String disguise = "No disguise";;
    12. if(DisguiseCraft.getAPI().getDisguise(player)!=null){
    13. String s = "";
    14. String currentd = Material.getMaterial(DisguiseCraft.getAPI().getDisguise(player).getBlockID()).toString();
    15. for(String str : currentd.split("_")){
    16. s+=" "+str;
    17. }
    18. disguise = s+" ";
    19. }
    20. objective.setDisplayName(ChatColor.GOLD+disguise);
    21. objective.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE+"Time:")).setScore(150);
    22. player.setScoreboard(board);
    23.  
    24. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  12. create an object that will store, the players name, scoreboard and scores. You could store them in a list , map or arraylist which ever approach you wish to use. With that use the setters and getters in your object class to set the values for each player and send them there own scoreboard thats unique to them. Its very doable :)
     
Thread Status:
Not open for further replies.

Share This Page