[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

    soulofw0lf

    you can get score information from it, just add a checker in an onDeath listener to check scores and see if anyone has 10 kills
     
  2. IMPORTANT: If you colored the player's name in the tablist or changed it in some way you have to set the score from the OfflinePlayer with the colored/changed name.
     
  3. so... is there a way to have different variables on the same scoreboard for difference players? so that different people can have different tags under their name?
     
  4. Offline

    rguz10

    Hey guys, I am a major Java noob and giving a go at my first mini-game. But for some reason I keep getting a NullPointerException to Scoreboard board = manager.getNewScoreboard(); This is a simple, single class plugin.

    Code:java
    1. package com.shadowfirez.novaplugin;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Effect;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.EventPriority;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16. import org.bukkit.event.player.PlayerMoveEvent;
    17. import org.bukkit.plugin.PluginDescriptionFile;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19. import org.bukkit.scoreboard.DisplaySlot;
    20. import org.bukkit.scoreboard.Objective;
    21. import org.bukkit.scoreboard.Scoreboard;
    22. import org.bukkit.scoreboard.ScoreboardManager;
    23. import org.bukkit.scoreboard.Team;
    24.  
    25. public class main extends JavaPlugin implements Listener{
    26.  
    27. public final Logger logger = Logger.getLogger("Minecraft");
    28.  
    29. @Override
    30. public void onDisable() {
    31. PluginDescriptionFile pdfFile = this.getDescription();
    32. this.logger.info(pdfFile.getName() + "has been enabled!");
    33. }
    34.  
    35. @Override
    36. public void onEnable() {
    37. getServer().getPluginManager().registerEvents(this, this);
    38. PluginDescriptionFile pdfFile = this.getDescription();
    39. this.logger.info(pdfFile.getName() + "version" + pdfFile.getVersion() + "has been enabled!");
    40. }
    41.  
    42. ScoreboardManager manager = Bukkit.getScoreboardManager();
    43. Scoreboard board = manager.getNewScoreboard();
    44. Team red = board.registerNewTeam("red");
    45. Team blue = board.registerNewTeam("blue");
    46. public ArrayList<Player> redTeam = new ArrayList<Player>();
    47. public ArrayList<Player> blueTeam = new ArrayList<Player>();
    48.  
    49. @EventHandler(priority=EventPriority.NORMAL)
    50. public void addPlayer(PlayerJoinEvent event, Player player) {
    51. Objective objective = board.registerNewObjective("showhealth", "health");
    52. objective.setDisplaySlot(DisplaySlot.BELOW_NAME);
    53. objective.setDisplayName("/ 20");
    54.  
    55. for(Player online : Bukkit.getOnlinePlayers()){
    56. online.setScoreboard(board);
    57. online.setHealth(online.getHealth());
    58. }
    59.  
    60. int average = (int) Math.ceil(1.*Bukkit.getOnlinePlayers().length/2);
    61. if (blueTeam.size() < average) {
    62. blueTeam.add(player);
    63. blue.addPlayer(player);
    64. blue.setPrefix(ChatColor.BLUE + "Blue");
    65. blue.setAllowFriendlyFire(false);
    66. player.sendMessage(ChatColor.BLUE + "You were put on team blue!");
    67. }
    68. else if (redTeam.size() < average) {
    69. redTeam.add(player);
    70. red.addPlayer(player);
    71. red.setPrefix(ChatColor.RED + "Red");
    72. red.setAllowFriendlyFire(false);
    73. player.sendMessage(ChatColor.RED + "You were put on team red!");
    74. }
    75. }
    76. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args, Player p){
    77. if(cmd.getName().equalsIgnoreCase("test")){
    78. p.sendMessage(ChatColor.BLUE + "Testtttt");
    79. }
    80. return true;
    81. }
    82. @EventHandler(priority=EventPriority.NORMAL)
    83. public void onPlayerMove(PlayerMoveEvent event) {
    84. if (event.getFrom().getBlock().equals(event.getTo().getBlock()));
    85. Player p = event.getPlayer();
    86. for (int MoVe = 0; MoVe < 360;MoVe+=0) {
    87. p.getWorld().playEffect(p.getLocation(),Effect.POTION_BREAK, MoVe);
    88. MoVe+=10;
    89. }
    90. }
    91. }


    And this is the Stack Trace

    Code:
    2013-07-07 22:07:30 [SEVERE] Could not load 'plugins/nova.jar' in folder 'plugins'
    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:306)
    >      at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
    >      at org.bukkit.craftbukkit.v1_5_R3.CraftServer.loadPlugins(CraftServer.java:262)
    >      at org.bukkit.craftbukkit.v1_5_R3.CraftServer.<init>(CraftServer.java:240)
    >      at net.minecraft.server.v1_5_R3.PlayerList.<init>(PlayerList.java:55)
    >      at net.minecraft.server.v1_5_R3.DedicatedPlayerList.<init>(SourceFile:11)
    >      at net.minecraft.server.v1_5_R3.DedicatedServer.init(DedicatedServer.java:58)
    >      at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:388)
    >      at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    >      at com.shadowfirez.novaplugin.main.<init>(main.java:43)
    >      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    >      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    >      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    >      at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    >      at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178)
    >      ... 9 more
     
  5. Offline

    michael566

    Can you show me an example?
     
  6. Offline

    soulofw0lf

    sure it's pretty basic

    Code:java
    1. @EventHandler
    2. public void onDeath (PlayerDeathEvent event){
    3. if (event.getEntity().getKiller() instanceof Player){
    4. Player p = (Player)event.getEntity().getKiller();
    5. Score score = obj.getScore(p);
    6. Integer i = score.getScore();
    7. if (i >= 10){
    8. //do code here
    9. }
    10. }
    11. }


    or you could have a timed for loop to go through every person on the board and check their score if you wanted. but i think this is a bit more efficient.

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

    worstboy32

    I'm really not sure how to make this working.
    What i need is to display a colored integer with a % symbol behind it.
    Like 100% etc and the color changes when the percentage changes.
    So for example when it's 0% it's dark green and when it's 200% it's dark red etc.

    I have tried multiple things but the integer always stays white and for everything i do now it's staying 0.
    I'm sure that i'm doing things wrong but i have been trying for hours now to get it working and nothing worked.
    I'm kinda new to java so i hope someone can help me.
    What i did so far is:
    Main class: https://pastebin.com/8kz0QMLN
    Event class: https://pastebin.com/cZUSpbHP
    Removed all extra stuff not related to the scoreboard in that classes.
    Hopeful someone can help me and tell me what i did wrong.
    Or even give a working example that i can use.
     
  8. Offline

    michael566

    Thanks!

    One more thing,how do you make it say who won?

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

    soulofw0lf

    that's just a broadcast message, or however you choose to display it, then clear your scoreboards
     
  10. Offline

    michael566

    Ca
    Can you show me how you make say who won. Sorry I am kinda new to this.
     
  11. Offline

    soulofw0lf

    Code:
     @EventHandler
        public void onDeath (PlayerDeathEvent event){
            if (event.getEntity().getKiller() instanceof Player){
                Player p = (Player)event.getEntity().getKiller();
                Score score = obj.getScore(p);
                Integer i = score.getScore();
                if (i >= 10){
                    Bukkit.broadcastMessage (p.getName() + " has won this round");
                }
            }
        }
     
  12. Offline

    michael566

    It doesn't work for me here is my code:



    Code:
    public class DeathListener implements Listener {
       
       
        @EventHandler (priority = EventPriority.HIGH)
        public void onDeath (PlayerDeathEvent event){
            if (event.getEntity().getKiller() instanceof Player){
                Player p = (Player)event.getEntity().getKiller();
                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = manager.getNewScoreboard();
                Objective obj = board.registerNewObjective("Kills", "totalKillCount");
                Score score = obj.getScore(p);
                Integer i = score.getScore();
                if (i >= 10)
                    Bukkit.broadcastMessage(p.getName() + ChatColor.GOLD + "[PVP] Has Won!!");
                   
                }
            }
        }
    
     
  13. Offline

    soulofw0lf

    you're making a brand new scoreboard for each death event, of course it's not working that way, you need to make it elsewhere and just get the score on your onDeath event.
     
  14. Offline

    michael566

    Ok I removed that stuff, now it says that obj cannot be resolved.
     
  15. Offline

    WarmakerT

    Is there a maximum number of teams you can have? And if there isn't, would 100 be too laggy?
     
  16. Offline

    etaxi341

    Teams / Scoreboards do minimal Server lag. So it probably would use about 100 - 250 MB RAM.
     
  17. Offline

    michael566

    It says obj cannot be resolved.
     
  18. Offline

    StrangeOne101

    This is really good. :)

    However, could I somehow use this in packets? I'd like to have a scoreboard showing the player's score, without everyone elses score there too.
     
  19. Offline

    soulofw0lf

    view previous posts about storing boards for individual players in hash maps
     
  20. Offline

    etaxi341

    StrangeOne101 Look at my post at Page 8. I posted a Method to send players different scoreboards.
     
  21. Offline

    StrangeOne101

    etaxi341 Yeah, I got it. Just forgot to post back. :)

    Thanks again! :D
     
  22. Offline

    xXMaTTHDXx

    chasechocolate I wanted to use your exact health scoreboard but it didnt work for me, I couldn't set the board when a player joined
    Code:
    public void SetScoreboard(){
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getNewScoreboard();
     
            Objective objective = board.registerNewObjective("showhealth", "health");
            objective.setDisplaySlot(DisplaySlot.BELOW_NAME);
            objective.setDisplayName("/ 20");
     
            for(Player online : Bukkit.getOnlinePlayers()){
                online.setScoreboard(board);
                online.setHealth(online.getHealth()); //Update their health
            }
        }
       
        public void onJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
            p.setScoreboard(board); //error under board
           
        }
       
    }
     
  23. Offline

    chasechocolate

    xXMaTTHDXx it's because you don't have a "board" variable. Just call SetScoreboard() in there.
     
  24. Offline

    michael566

    Can you please help me?
     
  25. Offline

    soulofw0lf

    look at the first post for how to set up your board initially, your death event should only be to update the score.
     
  26. Offline

    michael566

    Can you show me an example?
     
  27. Offline

    soulofw0lf

    the very first post is the example, and i already showed you how to set up your onDeath event
     
  28. Offline

    Erasio

    I have a kinda odd problem and maybe I'm just thinking from the wrong end but how to I subtract or add someting to the Score?
    I have a dummy score and I want to add 1 on a specific even which works so far. However I get a Score variable back with which I can't calculate...
    It's probably a very easy problem but I can't seem to figure it out :S
     
  29. Offline

    soulofw0lf

    Erasio
    here's how i do it in one of my mini games
    Code:java
    1. if (guardians.contains(p.getName())){
    2. Score score = obj.getScore(Bukkit.getOfflinePlayer("Guardian Lives"));
    3. Integer s = score.getScore();
    4. score.setScore(s - 1);
    5. }
    6. if (zealots.contains(p.getName())){
    7. Score score = obj.getScore(Bukkit.getOfflinePlayer("Zealot Lives"));
    8. Integer s = score.getScore();
    9. score.setScore(s - 1);
    10. }
     
  30. Offline

    Erasio

    soulofw0lf
    Weird I thought I tried this one...
    However it works perfectly thanks! I feel kinda dumb now^^
     
Thread Status:
Not open for further replies.

Share This Page