How do we make Per-Player Scoreboards?

Discussion in 'Plugin Development' started by gabrielmaennl555, Apr 28, 2014.

Thread Status:
Not open for further replies.
  1. Hello!
    i was wondering how would i make per-player scoreboards? like a scoreboard that shows their players Health and Enemy Health and some other stats like Mana or something
    anyway how do i do that?
     
  2. gabrielmaennl555 You need to create multiple scoreboards. One for each player. And then set the player's scoreboard to their personal scoreboard.
     
  3. AdamQpzm
    Ok but do I need to create like ALOT of scoreboards? Because a lot of players will eventually join or is there a way to like create multiple with 1 code and add Evryone online to it?
     
  4. gabrielmaennl555 You won't need to create each one by hand. Just make some sort of method that will create a scoreboard for a player, with a player parameter.
     
  5. AdamQpzm oh ok Phew lol
    How would I create a scoreboard with a player parameter?
    I've never done scoreboards before
     
  6. gabrielmaennl555 Here is a tutorial for creating scoreboards. It might be a little out of date, but it should be fine.
     
  7. @AdamOpzm
    I'm not sure it says how to make per player scoreboards on it :( I can't find them
     
  8. gabrielmaennl555 Well make some code of how you want your scoreboard to work (like with the health that you mentioned) and I'll tell you what it needs. :) Also, my name has a Q not an O, just click the "tahg" link at the bottom of the post of the user you wish to tag ;)
     
  9. AdamQpzm
    Ok ;)
    I'm trying to make a scoreboard that will display on the SideBar
    And it will say Health: and EnemyHP:
    The Health will display the users health and the EnemyHP will show the health of a player or mob you Just hit
    :D
     
  10. gabrielmaennl555 Following the tutorial, do you have any idea how to do that (even if not per player)
     
  11. I know how to do it well I know how to do a normal scoreboard in coding but got no idear for Per Player or how to set the players health next to Health:
    I have some idear of how to do the EnemyHealth: like id use the EntityDamageEvent where it will set the hits health to that
    But I'm still not sure XD
     
  12. Offline

    Mr360zack

    pass them into a method, create an objective of the players name, and set the scoreboard
     
  13. Offline

    leet4044

    You can do something like this (note: this is old)
    Code:java
    1. public void setupScoreboard(final Player player) {
    2. Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
    3. final Objective objective = scoreboard.registerNewObjective("test", "dummy");
    4.  
    5. objective.setDisplayName("NameOFScoreboardHere");
    6. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    7.  
    8. player.setScoreboard(scoreboard);
    9. updateScoreboard(player);
    10. }
    11.  
    12. public static void updateScoreboard(Player player) {
    13. org.bukkit.scoreboard.Scoreboard scoreboard = player.getScoreboard();
    14. Objective objective = scoreboard.getObjective("test");
    15.  
    16. online = Bukkit.getOnlinePlayers().length;
    17.  
    18. objective.getScore(Bukkit.getOfflinePlayer("§d§lOnline")).setScore(9);
    19. objective.getScore(Bukkit.getOfflinePlayer("" + online)).setScore(8);
    20. objective.getScore(Bukkit.getOfflinePlayer("")).setScore(7);
    21. objective.getScore(Bukkit.getOfflinePlayer("§a§lRank")).setScore(6);
    22. objective.getScore(Bukkit.getOfflinePlayer("§cComing Soon")).setScore(5);
    23. objective.getScore(Bukkit.getOfflinePlayer(" ")).setScore(4);
    24. objective.getScore(Bukkit.getOfflinePlayer("§e§lCoins")).setScore(3);
    25. objective.getScore(Bukkit.getOfflinePlayer("Unavailable")).setScore(2);
    26. objective.getScore(Bukkit.getOfflinePlayer("----------------")).setScore(1);
    27. }


    Then to set the scoreboard for the player do
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e) {
    3. setupScoreboard(e.getPlayer());
    4. //And if you need to update teh score for everyone else do
    5. for (Player players : Bukkit.getOnlinePlayers()) {
    6. updateScoreboard(players);
    7. }
    8. }
     
  14. It won't work D;


    Code:java
    1. package SPPlugin;
    2.  
    3.  
    4.  
    5. import java.util.ArrayList;
    6.  
    7. import java.util.List;
    8.  
    9. import java.util.logging.Logger;
    10.  
    11.  
    12.  
    13. import org.bukkit.Bukkit;
    14.  
    15. import org.bukkit.ChatColor;
    16.  
    17. import org.bukkit.Material;
    18.  
    19. import org.bukkit.command.Command;
    20.  
    21. import org.bukkit.command.CommandSender;
    22.  
    23. import org.bukkit.entity.Player;
    24.  
    25. import org.bukkit.event.EventHandler;
    26.  
    27. import org.bukkit.event.Listener;
    28.  
    29. import org.bukkit.event.player.PlayerJoinEvent;
    30.  
    31. import org.bukkit.inventory.ItemStack;
    32.  
    33. import org.bukkit.permissions.Permission;
    34.  
    35. import org.bukkit.plugin.PluginDescriptionFile;
    36.  
    37. import org.bukkit.plugin.PluginManager;
    38.  
    39. import org.bukkit.plugin.java.JavaPlugin;
    40.  
    41. import org.bukkit.scoreboard.DisplaySlot;
    42.  
    43. import org.bukkit.scoreboard.Objective;
    44.  
    45. import org.bukkit.scoreboard.Scoreboard;
    46.  
    47.  
    48.  
    49. import SPPlugin.ItemUtils;
    50.  
    51.  
    52.  
    53. public class SPPlugin extends JavaPlugin implements Listener {
    54.  
    55.  
    56.  
    57. public Permission playerPermission1 = new Permission("sp.bedrock");
    58.  
    59. public Permission playerPermission2 = new Permission("sp.godhealth");
    60.  
    61. public Permission playerPermission3 = new Permission("sp.fixhp");
    62.  
    63. public Permission playerPermission4 = new Permission("sp.eggs");
    64.  
    65. public static SPPlugin plugin;
    66.  
    67. public final Logger logger = Logger.getLogger("Minecraft");
    68.  
    69.  
    70.  
    71.  
    72.  
    73. public void onDisable()
    74.  
    75. {
    76.  
    77. PluginDescriptionFile p = getDescription();
    78.  
    79. this.logger.info(p.getName() + " V" + p.getVersion() + " Has been Disabled!");
    80.  
    81. }
    82.  
    83.  
    84.  
    85. public void onEnable()
    86.  
    87. {
    88.  
    89. PluginDescriptionFile p = getDescription();
    90.  
    91. this.logger.info(p.getName() + " V" + p.getVersion() + " Has been Enabled!");
    92.  
    93. getConfig().options().copyDefaults(true);
    94.  
    95. saveConfig();
    96.  
    97. new BlockListener(this);
    98.  
    99. new PlayerListener(this);
    100.  
    101. PluginManager pm = getServer().getPluginManager();
    102.  
    103. pm.addPermission(playerPermission1);
    104.  
    105. getServer().getPluginManager().registerEvents(this, this);
    106.  
    107. //Simple Scoreboard Deleted
    108.  
    109. /*Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    110.  
    111.   *Objective obj = board.registerNewObjective("Health", "health");
    112.  
    113.   *obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    114.  
    115.   *obj.setDisplayName("Health");
    116.  
    117.   *
    118.  
    119.   *Score score = obj.getScore(Bukkit.getOfflinePlayer("Health:"));
    120.  
    121.   *
    122.  
    123.   *for (Player player : Bukkit.getOnlinePlayers()); {
    124.  
    125.   * player.setScoreboard(board);
    126.  
    127.   *
    128.  
    129.   *}
    130.  
    131. */
    132.  
    133. }
    134.  
    135.  
    136.  
    137. public void setupScoreboard(final Player player) {
    138.  
    139. Scoreboard scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
    140.  
    141. final Objective objective = scoreboard.registerNewObjective("test", "health");
    142.  
    143.  
    144.  
    145. objective.setDisplayName("Health Board");
    146.  
    147. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    148.  
    149.  
    150.  
    151. player.setScoreboard(scoreboard);
    152.  
    153. setupScoreboard(player);
    154.  
    155. }
    156.  
    157.  
    158.  
    159.  
    160.  
    161.  
    162.  
    163. public static void updateScoreboard(Player player) {
    164.  
    165. org.bukkit.scoreboard.Scoreboard scoreboard = player.getScoreboard();
    166.  
    167. Objective objective = scoreboard.getObjective("test");
    168.  
    169.  
    170.  
    171. int online = Bukkit.getOnlinePlayers().length;
    172.  
    173.  
    174.  
    175. objective.getScore(Bukkit.getOfflinePlayer("Health:"));
    176.  
    177. }
    178.  
    179.  
    180.  
    181.  
    182.  
    183.  
    184.  
    185. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    186.  
    187. {
    188.  
    189. //News Command
    190.  
    191. Player player = (Player)sender;
    192.  
    193. if (commandLabel.equalsIgnoreCase("news"))
    194.  
    195. {
    196.  
    197. player.sendMessage(ChatColor.GREEN + "[SPNews]: " + (ChatColor.GOLD + getConfig().getString("news").replace("&n", player.getWorld().getName()).replaceAll("&y", new StringBuilder().append(ChatColor.GREEN).toString())));
    198.  
    199. }
    200.  
    201. //Main SP Command
    202.  
    203. else if (commandLabel.equalsIgnoreCase("sp"))
    204.  
    205. {
    206.  
    207. player.sendMessage(ChatColor.GREEN + "======[" + (ChatColor.GOLD + "Servers Paradise Plugin v2.1" + (ChatColor.GREEN + "]======")));
    208.  
    209. player.sendMessage(ChatColor.GREEN + "Commands:");
    210.  
    211. player.sendMessage(ChatColor.GOLD + "/News" + (ChatColor.GREEN + " - Sends the news!"));
    212.  
    213. player.sendMessage(ChatColor.GOLD + "/PVPGod" + (ChatColor.GREEN + " - Sets your health really high! (dont abuse it!) "));
    214.  
    215. player.sendMessage(ChatColor.GOLD + "/fixhp" + (ChatColor.GREEN + " - Retrieves the default max health (20)"));
    216.  
    217. player.sendMessage(ChatColor.GOLD + "/segg" + (ChatColor.GREEN + " - Surprise Eggs!"));
    218.  
    219. player.sendMessage(ChatColor.GREEN + "======[" + (ChatColor.GOLD + "Coded by Gabrielmaennl5" + (ChatColor.GREEN + "]======")));
    220.  
    221.  
    222.  
    223. }
    224.  
    225. //PVPGod Command (Test command)
    226.  
    227. else if (commandLabel.equalsIgnoreCase("pvpgod") && player.hasPermission("sp.godhealth"))
    228.  
    229. {
    230.  
    231. player.setMaxHealth(1000);
    232.  
    233. player.setHealth(1000);
    234.  
    235. player.sendMessage(ChatColor.RED + (ChatColor.BOLD + "PVPGodHealth Enabled! type /fixhp to go back to the normal health!"));
    236.  
    237. }
    238.  
    239. //Fix HP Command
    240.  
    241. else if (commandLabel.equalsIgnoreCase("fixhp") && player.hasPermission("sp.fixhp"))
    242.  
    243. {
    244.  
    245. player.setMaxHealth(20);
    246.  
    247. player.setHealth(20);
    248.  
    249. player.sendMessage(ChatColor.GREEN + "Health Restored");
    250.  
    251. }
    252.  
    253. //Surprise Eggs
    254.  
    255. else if (commandLabel.equalsIgnoreCase("segg"))
    256.  
    257. {
    258.  
    259. player.sendMessage(ChatColor.GREEN + "======[Surprise Eggs]======");
    260.  
    261. player.sendMessage(ChatColor.RED + "/se open - Opens the SurpriseEgg you are holding!");
    262.  
    263. player.sendMessage(ChatColor.RED + "/eggs - Admin Command (Gives an Egg)");
    264.  
    265. player.sendMessage(ChatColor.GREEN + "===========================");
    266.  
    267. }
    268.  
    269. else if (commandLabel.equalsIgnoreCase("eggs") && player.hasPermission("sp.eggs"))
    270.  
    271. {
    272.  
    273. ItemStack dragonegg = new ItemStack(Material.DRAGON_EGG);
    274.  
    275. List<String> lore = new ArrayList<String>();
    276.  
    277. lore.add(ChatColor.GREEN + "Type " + (ChatColor.GOLD) + ChatColor.BOLD + "/se open " + (ChatColor.GREEN + "To open the Egg!"));
    278.  
    279. lore.add(ChatColor.MAGIC + "acoollore");
    280.  
    281. ItemUtils.setLore(dragonegg, lore);
    282.  
    283. ItemUtils.setName(dragonegg, ChatColor.GOLD + "Surprise Egg");
    284.  
    285. player.sendMessage(ChatColor.GREEN + "[SurpriseEggs]: Greetings " + player.getDisplayName() + (ChatColor.GREEN + " You were given a SurpriseEgg!"));
    286.  
    287. player.getInventory().addItem(dragonegg);
    288.  
    289.  
    290.  
    291. }
    292.  
    293. else if (commandLabel.equalsIgnoreCase("")){
    294.  
    295.  
    296.  
    297. }
    298.  
    299.  
    300.  
    301. return false;
    302.  
    303. }
    304.  
    305.  
    306.  
    307.  
    308.  
    309.  
    310.  
    311.  
    312.  
    313.  
    314.  
    315. }


    Console:

    Code:
    [18:23:29 INFO]: gabrielmaennl5[/110.20.247.133:56718] logged in with entity id 29 at ([world] 134.61441048202232, 64.0, 340.7464563158722)
    [18:23:30 ERROR]: Could not pass event PlayerJoinEvent to SPPlugin v2.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.c(PlayerList.java:225) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.a(PlayerList.java:116) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.LoginListener.c(LoginListener.java:78) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.LoginListener.a(LoginListener.java:42) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:149) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
    Caused by: java.lang.StackOverflowError
        at net.minecraft.server.v1_7_R1.Scoreboard.getScoresForObjective(SourceFile:73) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.ScoreboardServer.getScoreboardScorePacketsForObjective(ScoreboardServer.java:138) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.a(PlayerList.java:158) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at org.bukkit.craftbukkit.v1_7_R1.scoreboard.CraftScoreboardManager.setPlayerBoard(CraftScoreboardManager.java:93) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at org.bukkit.craftbukkit.v1_7_R1.entity.CraftPlayer.setScoreboard(CraftPlayer.java:1190) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3024jnks]
        at SPPlugin.SPPlugin.setupScoreboard(SPPlugin.java:76) ~[?:?]
        at SPPlugin.SPPlugin.setupScoreboard(SPPlugin.java:77) ~[?:?]
        at SPPlugin.SPPlugin.setupScoreboard(SPPlugin.java:77) ~[?:?]
        at SPPlugin.SPPlugin.setupScoreboard(SPPlugin.java:77) ~[?:?]
        at SPPlugin.SPPlugin.setupScoreboard(SPPlugin.java:77) ~[?:?]
        at SPPlugin.SPPlugin.setupScoreboard(SPPlugin.java:77) ~[?:?]
     
  15. Offline

    ImPhantom

    gabrielmaennl555
    Why do you have sooo much blank space (White Space) in your code? Its not gonna fix the issue, but it may look a bit more understandable if you organized your code.

    And where is this "PlayerJoinEvent" that it could not pass?
     
  16. ImPhantom hmmm I'm not sure why it pasted like that... Ill repaste
     
Thread Status:
Not open for further replies.

Share This Page