[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

    xMinecraft

    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

    chasechocolate

    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

    Eats_Rainbows

    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
    Code:java
    1. online.setHealth(online.getHealth());
    in the
    Code:java
    1. ]for(Player online : Bukkit.getOnlinePlayers())
    2. {
    3.  
    4. online.setScoreboard(board);
    5.  
    6. }

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

    soulofw0lf

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

    njb_said

    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

    chasechocolate

    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

    soulofw0lf

    @chasechocolate
    i was trying it like
    Code:
    ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getNewScoreboard();
            board.registerNewObjective("showhealth", "health");
           
            Objective objective = board.getObjective("showhealth");
            objective.setDisplaySlot(DisplaySlot.BELOW_NAME);
            Integer mhp = player.getMaxHealth();
            objective.setDisplayName("/ " + mhp);
            for(Player online : Bukkit.getOnlinePlayers()){
     
                  online.setScoreboard(board);
                  online.setHealth(online.getHealth());
                }
    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

    chasechocolate

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

    soulofw0lf

    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

    njb_said

    Ok thanks :D
     
  12. Offline

    xMinecraft

  13. Offline

    chasechocolate

  14. Offline

    colozz

    Ok so I have this code :
    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");
        objective.setDisplaySlot(DisplaySlot.BELOW_NAME);
        objective.setDisplayName("lives");
       
        for(Player online : Bukkit.getOnlinePlayers()){
          Score score = objective.getScore(online);
          score.setScore(5); //Example
        }
       
        for(Player online : Bukkit.getOnlinePlayers()){
          online.setScoreboard(board);
        }
       
    }
    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

    Quaro

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

    dturner699

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

    chasechocolate

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

    Cryptite

    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

    chasechocolate

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

    Ibix13

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

    dturner699

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

    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>>();

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

    @Override
    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);
    while(iterator.hasNext()){
    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");
    red.setAllowFriendlyFire(false);
    blue.setAllowFriendlyFire(false);
    Objective objective = board.registerNewObjective("Kills", "playerKillCount");
    objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    objective.setDisplayName("Kills");
    onlinePlayer.setScoreboard(board);

    if(red.getSize() == blue.getSize()){
    red.addPlayer(onlinePlayer);
    }

    if(red.getSize() >= blue.getSize()){
    blue.addPlayer(onlinePlayer);
    }

    if(blue.getSize() >= red.getSize()){
    red.addPlayer(onlinePlayer);
    }


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


    if(onlinePlayer.hasPermission("ri.isword")){
    playerInventory.addItem(new ItemStack(Material.IRON_SWORD, 1));}
    if(!onlinePlayer.hasPermission("ri.isword")){
    if(onlinePlayer.hasPermission("ri.ssword")){
    playerInventory.addItem(new ItemStack(Material.STONE_SWORD, 1));}}


    if(onlinePlayer.hasPermission("ri.ihelmet")){
    playerInventory.setHelmet(new ItemStack(Material.IRON_HELMET, 1));}
    if(!onlinePlayer.hasPermission("ri.ihelmet")){
    if(onlinePlayer.hasPermission("ri.lhelmet")){
    playerInventory.setHelmet(new ItemStack(Material.LEATHER_HELMET, 1));}}


    if(onlinePlayer.hasPermission("ri.ichestplate")){
    playerInventory.setChestplate(new ItemStack(Material.IRON_CHESTPLATE, 1));}
    if(!onlinePlayer.hasPermission("ri.ichestplate")){
    if(onlinePlayer.hasPermission("ri.lchestplate")){
    playerInventory.setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE, 1));}}


    if(onlinePlayer.hasPermission("ri.ileggings")){
    playerInventory.setLeggings(new ItemStack(Material.IRON_LEGGINGS, 1));}
    if(!onlinePlayer.hasPermission("ri.ileggings")){
    if(onlinePlayer.hasPermission("ri.lleggings")){
    playerInventory.setLeggings(new ItemStack(Material.LEATHER_LEGGINGS, 1));}}


    if(onlinePlayer.hasPermission("ri.iboots")){
    playerInventory.setBoots(new ItemStack(Material.IRON_BOOTS, 1));}
    if(!onlinePlayer.hasPermission("ri.iboots")){
    if(onlinePlayer.hasPermission("ri.lboots")){
    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

    soulofw0lf

    @chasechocolate
    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.

    Code:java
    1.  
    2. @EventHandler
    3. public void onDamage(EntityDamageEvent event){
    4. if(event.getEntity() instanceof Player){
    5. Player p = (Player)event.getEntity();
    6. setScore(p);
    7.  
    8.  
    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());
    29.  
    30. online.setScoreboard(board);
    31. }
    32. }
    33.  


    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

    YoFuzzy3

    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

    LordManegane

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

    chasechocolate

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

    cotjones

    Question.
    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

    peman3232

    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.

    Example:

    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

    Ultimate_n00b

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

    Cryptite

    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

    peman3232


    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