[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

    Tomjomovies

    I want that when you run a command you will be put in a team is that possible?
    and can someone Write an example
     
  2. Offline

    xXSniperzzXx_SD

    chasechocolate
    Just wondering, is there a way to use a dummy scoreboard and get the stats from a criteria scoreboard?(Show a dummy scoreboard, but be able to get each players stats if I were to use the criteria)
     
  3. Offline

    JPG2000

    chasechocolate I don't understand something. In your first example you set the display name " /20", and you don't even use a .getHealth method. Wouldn't it just set there object name (or in your case under the name) to " /20"?
     
  4. Offline

    chasechocolate

    JPG2000 nope. I set the objective type to "health", and Minecraft will automatically set the score to the player's health.
     
    JPG2000 likes this.
  5. Offline

    matanrak

    How can i do that the scoreboard will only show in the world "cw"
     
  6. Offline

    PieMan456

    chasechocolate
    In the tutorial you should add how to save the Scoreboard if they have stats like Kills so it doesn't reset every time a person logs in.
     
  7. Offline

    chasechocolate

    PieMan456 that's more along the lines of a MySQL or flatfile tutorial, but I may later.
     
    PieMan456 likes this.
  8. Offline

    SourceForums

    chasechocolate
    How can I get players in a list and create a score for each one of them so that others people can see how much score each one of the players have? I'm really having trouble with this. This is what I have right now:
    Code:java
    1. if(onArena1LobbyList.size() == 4){
    2. for(Player targetPlayer : Bukkit.getOnlinePlayers()){
    3. if(onArena1LobbyList.contains(targetPlayer.getName())){
    4. targetPlayer.sendMessage(ChatColor.GOLD + "The game will be starting soon...");
    5. for(String playerList : onArena1LobbyList){
    6. Player players = Bukkit.getServer().getPlayer(playerList);
    7. ScoreboardManager boardManager = Bukkit.getScoreboardManager();
    8. Scoreboard gameStats = boardManager.getNewScoreboard();
    9. Objective objective = gameStats.registerNewObjective("gameStats", "dummy");
    10. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    11. objective.setDisplayName(ChatColor.WHITE + "Game Stats (Lives)");
    12. Score playerIScore = objective.getScore(Bukkit.getOfflinePlayer(players.getDisplayName() + ": " ));
    13. playerIScore.setScore(5);
    14. Score playerIIScore = objective.getScore(Bukkit.getOfflinePlayer(players.getDisplayName() + ": " ));
    15. playerIIScore.setScore(5);
    16. Score playerIIIScore = objective.getScore(Bukkit.getOfflinePlayer(players.getDisplayName() + ": " ));
    17. playerIIIScore.setScore(5);
    18. Score playerIVScore = objective.getScore(Bukkit.getOfflinePlayer(players.getDisplayName() + ": " ));
    19. playerIVScore.setScore(5);
    20. targetPlayer.setScoreboard(gameStats);
    21. }
    22. }
    23. }

    Yes, I do realise that this code is really messed up right now. I've asked many people and each of them are giving me different answers and is confusing me. Please reply ASAP.

    P.S. Don't mind the spacing errors, it's just the input messing up.
     
  9. Offline

    theyanay5

    How to do multiple lines in DisplaySlot.SIDEBAR?
    example:
    [​IMG]
     
  10. Offline

    SourceForums

    theyanay5
    You can simply just create new scores for each of the lines.
     
    theyanay5 likes this.
  11. Offline

    MyNameIsHariK

  12. Offline

    SourceForums

    MyNameIsHariK
    Change your variable 'objective' to 'obj'. (That's how you've registered it.)
     
  13. Offline

    MyNameIsHariK

    SourceForums Nope. :( (I tried changing the name to see if it works forgot to change it back)
     
  14. Offline

    SourceForums

    MyNameIsHariK
    Code:java
    1. ScoreboardManager manager = Bukkit.getScoreboardManager;
    2. Scoreboard board = mamager.getNewScoreboard;
    3. Objective ocjective = board.registierNewObjective("lives", "totalKillCount");
    4. ovjective.setDisplaySlot(DisplaySlot.SIDEBAR);
    5. objective.setDisplayName("Kills");
     
  15. Offline

    MyNameIsHariK

  16. Offline

    SourceForums

    MyNameIsHariK
    What does it say when you click your mouse on them? Does it show the error?
     
  17. Offline

    theyanay5

    Thank you :)
     
  18. Offline

    Newatum

    Hi guys, I have been coding this plugin for a server and i want a scoreboard on it. It all works fine exept when i first start the server i get a massive error but after a reload it works fine how do i fix this as i need the plugin to work on a server which can't reload.

    if u need to see the code i can provide it

    Whats is wrong with this code??
    Code:java
    1. ScoreboardManager manager = Bukkit.getScoreboardManager();
    2. Scoreboard board = this.manager.getNewScoreboard();
    3. Objective obj = board.registerNewObjective("test", "dummy");
    4. Score rock = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_GRAY + "Rock Gym"));
    5. Score water = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.BLUE + "Water Gym"));
    6. Score electric = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.YELLOW + "Electric Gym"));
    7. Score grass = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Grass Gym"));
    8. Score poison = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_PURPLE + "Poison Gym"));
    9. Score psychic = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.LIGHT_PURPLE + "Psychic Gym"));
    10. Score fire = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.DARK_RED + "Fire Gym"));
    11. Score ground = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.GRAY + "Ground Gym"));
    12.  
    13. public void onEnable(){
    14. getConfig().options().copyDefaults(true);
    15. saveConfig();
    16.  
    17. getServer().getPluginManager().registerEvents(this, this);
    18.  
    19. obj.setDisplayName(ChatColor.GREEN + "Open Gyms");
    20. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    21.  
    22. }
    23. public void onDisable(){
    24.  
    25. }
    26. @EventHandler
    27. public void onJoin (PlayerJoinEvent e){
    28. Player p = e.getPlayer();
    29. p.setScoreboard(board);


    This is what is is saying in the console on the first start up. but if i reload it is fine.

    p.s line 48 is:
    1. Scoreboard board = this.manager.getNewScoreboard();

    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182)
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugins(CraftServer.java:239)
    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.<init>(CraftServer.java:217)
    at net.minecraft.server.v1_6_R3.PlayerList.<init>(PlayerList.java:56)
    at net.minecraft.server.v1_6_R3.DedicatedPlayerList.<init>(SourceFile:11)
    at net.minecraft.server.v1_6_R3.DedicatedServer.init(DedicatedServer.java:107)
    at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:393)
    at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
    at me.Newatum.PixelGym.main.<init>(main.java:48)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178)

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

    molenzwiebel

    Move the creation of all your variables inside the onEnable method.
    For example:
    Code:
    Score fire;
    public void onEnable() {
    fire = ....
    }
     
  20. Offline

    Newatum

    tried it and it still does the same thing
     
  21. Offline

    Xssnick

    How can i change color of setScore() value?
     
  22. Offline

    Regagames

    As far as I know, you cannot.
     
  23. Offline

    Acer_Mortem

    Whenver I use this code, it always sets the score under the other players names, and not (p).

    Anyone know why?

    Code:
            Player p = (Player) event.getPlayer();
            Scoreboard b = p.getScoreboard();
            Objective obj = b.registerNewObjective("aaa", "dummy");
            obj.setDisplaySlot(DisplaySlot.BELOW_NAME);
            obj.setDisplayName("ggg");
            Score bob = obj.getScore(p);
            bob.setScore(3);
            p.setScoreboard(b);
     
  24. Offline

    viper_monster

  25. Offline

    Acer_Mortem

  26. Offline

    Terraquis

    I can't seem to get this to work, when I have the scoreboard code in the main class (in the onEnable method), and I can't modify it from other classes/methods.
     
  27. Offline

    Ultimate_n00b

    Yes, you cannot access local variables globally.
     
  28. Offline

    ducduyn31

    Hi! I am having trouble with my plugin and I dont know what is wrong with it because it could not be loaded :( Please help me!

    here is my scoreboard:
    Code:java
    1.  
    2. public class Board{
    3. private Ninjutsu p ;
    4. public void init(){
    5. while(true){
    6. try {
    7. Thread.sleep(1000);
    8. for (Player player : Bukkit.getOnlinePlayers()){
    9. updateScoreboard(player);
    10. }
    11. }catch(Exception ignored){
    12. }
    13. }
    14. }
    15. public void updateScoreboard(Player player){
    16. Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    17. Objective obj = board.registerNewObjective("Info", "dummy");
    18. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    19.  
    20. obj.setDisplayName(ChatColor.BLUE + "=====" + ChatColor.AQUA + "Info" + ChatColor.BLUE + "=====");
    21. Score score = obj.getScore(Bukkit.getOfflinePlayer("" + ChatColor.RED + "Chakra"));
    22.  
    23. score.setScore(p.getConfig().getInt("player." + player.getName()));
    24. player.setScoreboard(board);
    25. }
    26. }
    27.  

    And here is my main class:
    Code:java
    1.  
    2. public class Ninjutsu extends JavaPlugin{
    3. public Board scoreBoard = new Board();
    4.  
    5. @Override
    6. public void onEnable(){
    7. getLogger().info("Ninjutsu is enabled!");
    8. init();
    9. }
    10.  
    11. @Override
    12. public void onDisable(){
    13. getLogger().info("Ninjutsu is disabled!");
    14. saveConfig();
    15. }
    16. void init(){
    17. scoreBoard.init();
    18. getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
    19. }
    20. }
    21.  
     
  29. chasechocolate How would you check if a player has a scoreboard or the scoreboard is registered?
     
  30. Offline

    chasechocolate

    DreTaX player.getScoreboard()?
     
Thread Status:
Not open for further replies.

Share This Page