[Tutorial] Scoreboards/Teams with the Bukkit API

Discussion in 'Resources' started by chasechocolate, Apr 5, 2013.

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


    How i make a scoreboard with setScore string's?

    *Sorry for my english
  2. Is it possible to create 1 scoreboard but have different values for different players(dummy type)? like 1 player has 10 Ammo while another has 8 and if I would have to create separate boards how could i do so?
  3. Offline


    Check the OP.
    Create a HashMap<String, Scoreboard> for storing player scoreboards, and every time the ammo changes, decrease/increase the score of the specific objective on the player's scoreboard.
  4. Offline


    chasechocolate - On the board that displays the health under the name it was always showing a 0 after reloads. Not sure if it is doing this for others, but I fixed that by adding
    1. online.setHealth(online.getHealth());
    in the
    1. ]for(Player online : Bukkit.getOnlinePlayers())
    2. {
    4. online.setScoreboard(board);
    6. }

    chunk of code. So it looks like
    1. ]for(Player online : Bukkit.getOnlinePlayers())
    2. {
    4. online.setScoreboard(board);
    5. online.setHealth(online.getHealth());
    7. }
  5. Offline


    is it possible to set the health scoreboard to display "/ max health" for each player rather than /20?
  6. Offline


    How can I show a custom integer below each players name.
    I want to show their remaining lives. I was going to use sidebar but im using that for other things

    Im trying to use a hashmap with string + scoreboard as the key and value.

    Any help would be greatly appreciated :)
  7. Offline


    Yes. Simply use what I had in the OP except change the objective's display name to whatever you want.
    I can show you when I get on my computer or I may add it in the OP.
  8. Offline


    i was trying it like
    ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getNewScoreboard();
            board.registerNewObjective("showhealth", "health");
            Objective objective = board.getObjective("showhealth");
            Integer mhp = player.getMaxHealth();
            objective.setDisplayName("/ " + mhp);
            for(Player online : Bukkit.getOnlinePlayers()){
    but calling it in player log on would just set everyone's second value to the newest player online's max health. I've been at it for a bit now and can't seem to get it to show the current players max health as the display name.
  9. Offline


    soulofw0lf player.getMaxHealth() will always be the same for every player.
  10. Offline


    chasechocolate sorry i should explain more i have a custom stat / leveling system where players max health is based on their gear they have equipped and their current level so max health is anywhere from 20 - 12500. It gets the max health of the last player that logged in right, but it sets everyone's score board on the server to that max health. no way of trying to iterate through players seems to fix that.
  11. Offline


    Ok thanks :D
  12. Offline


  13. Offline


  14. Offline


    Ok so I have this code :
    package fr.dotcraft.Dotplug;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    public class ScoreBoard {
        ScoreboardManager manager = Bukkit.getScoreboardManager();
        Scoreboard board = manager.getNewScoreboard();
        Objective objective = board.registerNewObjective("lives", "dummy");
        for(Player online : Bukkit.getOnlinePlayers()){
          Score score = objective.getScore(online);
          score.setScore(5); //Example
        for(Player online : Bukkit.getOnlinePlayers()){
    And I'm getting loads of errors like "Syntax error on token ".", ... expected" or "Syntax error on misplaced constructor(s)" or "Syntax error on token ""lives", delete this token" ...
    I'm using craftbukkit 1.5.2-R0.1 bêta and all my plugin works.

    Thanks for helping
  15. Offline


    How to remove score: "0" from below name?
  16. Offline


    My scoreboard won't work, is this because I am REQUIRED to add an objective?
  17. Offline


    The score has to be shown; there is no way to remove it.
    Show your code.
  18. Offline


    chasechocolate Is it possible to have player-specific BELOW_NAME player tags? I've been wanting to hand out player titles for some time now, but am unsure how to make it so only one player has a specific title.
  19. Offline


    Cryptite maybe by creating different objectives for each player.
  20. Offline


    how do you add a point do an objective... I suck at this scoreboard stuff
  21. Offline


    Please no one take my code, if you do, you should know that you're a jackass :)

    package me.dturner699.Arena;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;

    public class Arena extends JavaPlugin{
    public final Logger logger = Logger.getLogger("Minecraft");
    public static Arena plugin;
    public final HashMap<Player, ArrayList<Player>> teams = new HashMap<Player, ArrayList<Player>>();

    public void onDisable(){
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " has been disabled!");

    public void onEnable(){
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " by " + pdfFile.getAuthors() + " has been enabled!");
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    public void run() {
    List<Player> onlinePlayers = Arrays.asList(Bukkit.getServer().getOnlinePlayers());
    Iterator<Player> iterator = onlinePlayers.iterator();
    Player onlinePlayer = iterator.next();

    Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "Game Starting...");
    World world = Bukkit.getServer().getWorld("world");
    Location location = new Location(world, -310,73,-267);
    ScoreboardManager manager = Bukkit.getScoreboardManager();
    Scoreboard board = manager.getNewScoreboard();
    Team red = board.registerNewTeam(ChatColor.RED + "Red");
    Team blue = board.registerNewTeam(ChatColor.BLUE + "Blue");
    red.setPrefix(ChatColor.RED + "Red");
    blue.setPrefix(ChatColor.BLUE + "Blue");
    Objective objective = board.registerNewObjective("Kills", "playerKillCount");

    if(red.getSize() == blue.getSize()){

    if(red.getSize() >= blue.getSize()){

    if(blue.getSize() >= red.getSize()){

    PlayerInventory playerInventory = onlinePlayer.getInventory();
    if(onlinePlayer instanceof Player);

    playerInventory.addItem(new ItemStack(Material.IRON_SWORD, 1));}
    playerInventory.addItem(new ItemStack(Material.STONE_SWORD, 1));}}

    playerInventory.setHelmet(new ItemStack(Material.IRON_HELMET, 1));}
    playerInventory.setHelmet(new ItemStack(Material.LEATHER_HELMET, 1));}}

    playerInventory.setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));}
    playerInventory.setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE, 1));}}

    playerInventory.setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));}
    playerInventory.setLeggings(new ItemStack(Material.LEATHER_LEGGINGS, 1));}}

    playerInventory.setBoots(new ItemStack(Material.IRON_BOOTS, 1));}
    playerInventory.setBoots(new ItemStack(Material.LEATHER_BOOTS, 1));}}


    }, 200 , 600);


    The code works without the score board stuff (it gives me the items and teleports me) but when I add it in it doesnt work! Please help!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Oct 30, 2015
  22. Offline


    ok what i'm trying to do is get it so that under each players name they have 2 numbers Current health / max health. my plugin changes the max health of players based on their stats and level.
    here's my code.

    2. @EventHandler
    3. public void onDamage(EntityDamageEvent event){
    4. if(event.getEntity() instanceof Player){
    5. Player p = (Player)event.getEntity();
    6. setScore(p);
    9. }
    10. }
    11. public static void setScore(Player p){
    12. Team team = null;
    13. for (Player online : Bukkit.getOnlinePlayers()){
    14. String name1 = online.getName();
    15. ScoreboardManager manager = Bukkit.getScoreboardManager();
    16. Scoreboard board = manager.getNewScoreboard();
    17. if (board.getTeam(name1) == null){
    18. team = board.registerNewTeam(name1);
    19. team.addPlayer(online);
    20. }
    21. if (board.getObjective(name1) == null){
    22. board.registerNewObjective(name1, "dummy");
    23. }
    24. board.getObjective(name1).setDisplaySlot(DisplaySlot.BELOW_NAME);
    25. String display = Integer.toString(online.getMaxHealth());
    26. board.getObjective(name1).setDisplayName("§2" + display);
    27. Score score = board.getObjective(name1).getScore(online);
    28. score.setScore(online.getHealth());
    30. online.setScoreboard(board);
    31. }
    32. }

    It doesn't work at all, won't even get the scoreboard to show up in the slightest. what i tired doing was setting a different objective for each player, and setting each player to a new team named after them. I'm sure there's a fairly obvious reason as to why this isn't working, but i've been trying various ways to get it to work for two weeks now and just can't for the life of me get it to display right so any help would be greatly appreciated!

    ok so that initial problem was just me making a stupid syntax error, reposted the code for what i have now it shows your max health on everyone elses head, and it does that for everyone. also on the last person to log online actually has their current health update.
  23. Offline


    I'd like to have all players' health displayed under their head, but if they view the stats leaderboard (uses scoreboards) the health leaderboard won't go away. Is that possible?
  24. Offline


    Ther´s a way to display only 5 names?
  25. Offline


    Yes. Check the post I made on your thread.
  26. Offline


    I'm trying to get a bit more functionality by actually using the Score labels to hold scores, so I can use text.
    I'm running into a weird issue.
    The scores I set under an objective seemed to come out ordered randomly.
    After playing around with it a bit I noticed that the Scoreboard auto-sorts the scores in descending order, but if the scores are all the same it jumbles them up randomly.
    That's annoying. Any way to prevent that?

    Additional question.
    Lets say I create the player's board on join, but then I want to alter a score's label in a method called updateBoard()
    How should I go about that? It seems like I could create a new board each time, reconstructing it with the scores I want each time the method is called, but is there another way?
  27. Offline


    How would you set different objectives for different players?

    For example if I made a scoreboard with objectives for 5 different titles I could still only display 1 at a time.

    or if I made 5 different scoreboards each with 1 objective it still wouldn't work, because it would just show the players own title above everyone else.


    If I made a scoreboard with objectives with display names of "Hero, Prince, King, Peasant" only 1 would be able to show up and it would show for everyone, regardless of what their title should be.

    If I made Scoreboards like that and set Hero scoreboard to player they would see Hero above everyone, and someone with Prince scoreboard would see Prince above everyone.
  28. Offline


    Why is everyone having so many issues? Just read the op, and get over it.
  29. Offline


    I never figured it out, but apparently somebody has. This works even better for me as it allows changing the title of the playername rather than being forced to use 0 TEXT in the BELOW_NAME field. You can check out his source code on github too if you want to delve into how he did it.
  30. Offline


    It uses teams. I wanted the text below their names, but I ended up using teams as well in my code.
Thread Status:
Not open for further replies.

Share This Page