Scoreboard creation in different class

Discussion in 'Plugin Development' started by Dubehh, Jun 6, 2014.

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

    Dubehh

    Hi!

    Im rewriting my plugin to make it more clean and nicer,
    and in my first version I had a scoreboard, which was made in my onEnable() method.

    This time, I've created a new package + class for just my scoreboard, and I want to know how to make a scoreboard outside of the onEnable().

    Scoreboard class:
    Code:java
    1. package com.dubehh.ScoreBoard;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.scoreboard.Objective;
    5. import org.bukkit.scoreboard.Scoreboard;
    6. import org.bukkit.scoreboard.ScoreboardManager;
    7.  
    8. public class ScoreBoard {
    9.  
    10. public static ScoreboardManager manager;
    11. public static Scoreboard board;
    12. public Objective obj;
    13.  
    14. // onEnable - ish {
    15. manager = Bukkit.getServer().getScoreboardManager();
    16. board = manager.getNewScoreboard();
    17. }
    18.  
    19.  
    20.  
    21.  
    22. }
    23.  



    Main
    Code:java
    1. package com.dubehh.CoreDefense;
    2.  
    3. import org.bukkit.plugin.Plugin;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. import com.dubehh.GameMechanics.GameState;
    7. import com.dubehh.Teams.Commands;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. private static Plugin plugin;
    12.  
    13. public static Plugin getPlugin() {
    14. return plugin;
    15. }
    16.  
    17.  
    18. public void onEnable(){
    19. getLogger().info(" Enabled!");
    20. GameState.setGameState(GameState.LOBBY);
    21. loadCmd();
    22. // Something directing to the Scoreboard class here?
    23. }
    24.  
    25. public void onDisable(){
    26. getLogger().info(" Disabled!");
    27. }
    28.  
    29. public void loadCmd(){
    30. getCommand("Team").setExecutor(new Commands());
    31. }
    32.  
    33.  
    34.  
    35. }


    Thank you :)

    Actually,
    I would rather know how to
    acces the 'onEnable()' method overall, not just using scoreboards.

    I tried using a static method in the 'Scoreboard' class, which gave me a NPE.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  2. Offline

    fireblast709

    Step 1: remove static
    Step 2: stop using static
    Probably will solve most of the solutions + you will encounter less random (which are actually quite logical if you understand static) problems
     
  3. Offline

    Dubehh

    fireblast709

    Thank you, but how would I be able to 'create' this scoreboard without having a onEnable in my class?
    + Isn't static supposed to be there so I can acces it through all classes? Or did I got the wrong impression of it?

    Im not an expert of Java, so please don't be too harsh.
     
  4. Offline

    fireblast709

    Dubehh the issue with a lot of new programmers is that they treat static as this magical keyword that will solve all of their issues. You can call
    Code:java
    1. Bukkit.getScoreboardManager().getNewScoreboard();
    from all methods already, so that should be no issue. If you, for example, need your main class somewhere (perhaps for runnables), you can just use the constructor to pass it along.
    Code:java
    1. public class SomeClass
    2. {
    3. private final <main class> plugin;
    4.  
    5. public SomeClass(<main class> plugin)
    6. {
    7. this.plugin = plugin;
    8. }
    9.  
    10. public void anyMethod()
    11. {
    12. this.plugin.<any method you can call in plugin, can be called here>
    13. }
    14. }
    And the best thing is: no static involved whatsoever!

    To get back to your question about static, that is not the reason it was created. Static fields and methods are class methods. They belong to the class, rather than to a specific instance of that class. And since you can access the class (assuming it is public) everywhere, you can access the fields and methods anywhere.

    But they can be hard to work with. You see a lot of people trying to use them, and causing 'random' NullPointerExceptions in the process. That is why I discourage people to use it, until you actually understand how it works and how to deal with it.
     
  5. Offline

    Dubehh

    fireblast709

    Thanks :)
    That actually helped. ;)

    fireblast709
    Hi,

    How about the other way around?
    What if I want to call a method from another class into the onEnable() ?

    because I have this Scoreboard which needs to be called when the server starts up.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  6. Offline

    L33m4n123

    AoH_Ruthless likes this.
  7. Offline

    Dubehh

    L33m4n123 fireblast709

    Hi both,

    I am getting a NPE on start up.

    main
    Code:java
    1. public final ScoreBoard score;
    2. public Main(){
    3. score = new ScoreBoard();
    4. }
    5.  
    6. public void onEnable(){
    7. getLogger().info(" Enabled!");
    8. GameState.setGameState(GameState.LOBBY);
    9. loadCmd();
    10. loadScoreBoard();
    11.  
    12. }
    13.  
    14. public void onDisable(){
    15. getLogger().info(" Disabled!");
    16. }
    17.  
    18. public void loadCmd(){
    19. getCommand("Team").setExecutor(new Commands());
    20. }
    21. void loadScoreBoard(){
    22. score.newSb(); //scoreboard class
    23. }


    Scoreboard class
    Code:java
    1. public class ScoreBoard {
    2.  
    3. Scoreboard board;
    4. ScoreboardManager manager;
    5. Objective obj;
    6.  
    7. public void newSb(){
    8. board = manager.getNewScoreboard();
    9. obj = board.registerNewObjective("Test", "dummy");
    10. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    11. obj.setDisplayName(ChatColor.DARK_AQUA + "board");
    12.  
    13. }
    14.  
    15.  
    16.  
    17. }


    Error log
    Code:
    [11:36:06 ERROR]: Error occurred while enabling CoreDefense v0.1 (Is it up to date?)
    java.lang.NullPointerException
            at com.dubehh.ScoreBoard.ScoreBoard.newSb(ScoreBoard.java:16) ~[?:?]
            at com.dubehh.CoreDefense.Main.loadScoreBoard(Main.java:32) ~[?:?]
            at com.dubehh.CoreDefense.Main.onEnable(Main.java:20) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[craftbukkit.jar:git-B
    ukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:350) [craftbuk
    kit.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:384) [craftbu
    kkit.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.java:300) [craftbukkit.
    jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.java:282) [craftbukk
    it.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.reload(CraftServer.java:632) [craftbukkit.jar:
    git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.Bukkit.reload(Bukkit.java:279) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-14-g15b0
    4d8-b2991jnks]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:199) [craftbukkit.jar:
    git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:544) [craftbu
    kkit.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(CraftServer.java:531) [c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:286) [craftbukkit.ja
    r:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:251) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.j
    ar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  8. Offline

    L33m4n123

    you get an NPE because you call manager.getNewScoreboard(); yet you never assigned a value to manager
     
  9. Offline

    Dubehh

    L33m4n123

    Wow Thanks, that did it!
    Thanks for your response.

    L33m4n123

    Hi! Sorry for the question and all,

    but why do I get an NPE here:

    TeamClass
    Code:java
    1. public class Teams {
    2.  
    3. public Team red;
    4. public Team blue;
    5.  
    6. public final ScoreBoard score;
    7. public Teams(){
    8. score = new ScoreBoard();
    9. }
    10.  
    11. public void registerTeams() {
    12. red = score.board.registerNewTeam("red");
    13. blue = score.board.registerNewTeam("blue");
    14. red.setAllowFriendlyFire(false);
    15. blue.setAllowFriendlyFire(false);
    16. red.setPrefix(ChatColor.RED + "");
    17. blue.setPrefix(ChatColor.BLUE + "");
    18. }


    (the 'score' class is the same as the code I posted earlier)

    Adding:
    Code:java
    1. public void addPlayerRed(Player p){
    2. p.sendMessage(ChatColor.GOLD + "[CoreDefense] " + ChatColor.YELLOW
    3. + "Welcome in the " + ChatColor.RED + "Red " + ChatColor.YELLOW
    4. + "team!");
    5. team.red.addPlayer(p);
    6. }



    When I use the addPlayerRed(Player p) method, in a onCommand,
    I get:

    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'team' in plugin CoreDefe
    nse v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Buk
    kit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:199) ~[craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:544) ~[craftb
    ukkit.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:932) [c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [craftbukkit.j
    ar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.ja
    r:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukk
    it.jar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:g
    it-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukk
    it-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.j
    ar:git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
    Caused by: java.lang.NullPointerException
            at com.dubehh.Teams.TeamMethods.addPlayerRed(TeamMethods.java:32) ~[?:?]
            at com.dubehh.Teams.TeamCmd.onCommand(TeamCmd.java:41) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Buk
    kit-1.7.2-R0.2-14-g15b04d8-b2991jnks]
            ... 13 more

    I looked into it, and I have everything assigned right?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  10. Offline

    fireblast709

  11. Offline

    Dubehh

    fireblast709
    TeamMethods
    Code:java
    1. public class TeamMethods {
    2.  
    3. public final Teams team;
    4. public TeamMethods(){
    5. team = new Teams();
    6. }
    7.  
    8. public boolean isInTeam(Player p){
    9. if(team.blue.hasPlayer(p) || team.red.hasPlayer(p))
    10. return true;
    11. return false;
    12. }
    13.  
    14. public boolean isRed(Player p){
    15. if(team.red.hasPlayer(p)) return true;
    16. return false;
    17. }
    18. public boolean isBlue(Player p){
    19. if(team.blue.hasPlayer(p)) return true;
    20. return false;
    21. }
    22.  
    23. public void addPlayerRed(Player p){
    24. p.sendMessage(ChatColor.GOLD + "[CoreDefense] " + ChatColor.YELLOW
    25. + "Welcome in the " + ChatColor.RED + "Red " + ChatColor.YELLOW
    26. + "team!");
    27. team.red.addPlayer(p);
    28. }
    29. public void addPlayerBlue(Player p){
    30. p.sendMessage(ChatColor.GOLD + "[CoreDefense] " + ChatColor.YELLOW
    31. + "Welcome in the " + ChatColor.BLUE + "Blue " + ChatColor.YELLOW
    32. + "team!");
    33. team.blue.addPlayer(p);
    34. }
    35. }


    Registering Teams
    Code:java
    1. public class Teams {
    2.  
    3. public Team red;
    4. public Team blue;
    5.  
    6. public final ScoreBoard score;
    7. public Teams(){
    8. score = new ScoreBoard();
    9. }
    10.  
    11. public void registerTeams() {
    12. red = score.board.registerNewTeam("red");
    13. blue = score.board.registerNewTeam("blue");
    14. red.setAllowFriendlyFire(false);
    15. blue.setAllowFriendlyFire(false);
    16. red.setPrefix(ChatColor.RED + "");
    17. blue.setPrefix(ChatColor.BLUE + "");
    18. }


    TeamCmd
    Code:java
    1. public class TeamCmd implements CommandExecutor {
    2.  
    3. public final Teams team;
    4. public final TeamMethods teammethods;
    5. public TeamCmd(){
    6. team = new Teams();
    7. teammethods = new TeamMethods();
    8. }
    9.  
    10.  
    11. @Override
    12. public boolean onCommand(CommandSender sender, Command cmd, String label,
    13. String[] args) {
    14.  
    15. String prefix = ChatColor.GOLD + "[CoreDefense] ";
    16.  
    17. if(!(sender instanceof Player)){
    18. sender.sendMessage(prefix + ChatColor.YELLOW + "You must be ingame to execute this command.");
    19. return true;
    20. }
    21. Player p = (Player) sender;
    22. if(cmd.getName().equalsIgnoreCase("Team")){
    23. if(args.length == 0){
    24. p.sendMessage(ChatColor.GOLD + "[CoreDefense] "
    25. + ChatColor.YELLOW + "/Team <team color>");
    26. return true;
    27. }else if(args[0].equalsIgnoreCase("red")){
    28. teammethods.addPlayerRed(p);
    29. }
    30. }
    31.  
    32.  
    33.  
    34. return false;
    35. }
    36.  
    37. }
    38.  


    Incase you want the updated ScoreBoard class
    Code:java
    1. public class ScoreBoard {
    2.  
    3. public Scoreboard board;
    4. public ScoreboardManager manager;
    5. public Objective obj;
    6. public Map<String, Integer> teamScores = new HashMap<String, Integer>();
    7.  
    8. public void newSb() {
    9. manager = Bukkit.getScoreboardManager();
    10. board = manager.getNewScoreboard();
    11. obj = board.registerNewObjective("Test", "dummy");
    12. teamScores.put("Core", 70);
    13. teamScores.put("Core2", 70);
    14.  
    15. try{
    16. this.update(obj);
    17. e.printStackTrace();
    18. }
    19.  
    20. }


    It has trouble finding the team. So I've missed something , I probably did not assign the 'team' right, since I get a NPE

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  12. Offline

    fireblast709

    Dubehh you create like 4 instances of Teams, that will give a lot of issues later since if you change one, the other one remains unchanged. Also Team has two uninitialized fields, blue and red
     
  13. Offline

    Dubehh

    fireblast709
    But I did:
    Code:java
    1. red = score.board.registerNewTeam("red");
    2. blue = score.board.registerNewTeam("blue");

    ?

    And call you tell me how to do it proper?
    Because I created instance's to acces them w/o making them static
     
  14. Offline

    fireblast709

    Dubehh You never called that method, did you? Your Teams object should be in your main class instead (the one that extends JavaPlugin), and you should create a getter method for it so other classes can access it.
     
  15. Offline

    Dubehh

    fireblast709
    I understand what you mean,

    but why does the Scoreboard work, and the Teams not?

    It is basically the same idea right?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  16. Offline

    fireblast709

    Dubehh To repeat myself: first you create like 4 instances of the Teams class, which will never work like you expect it to. Secondly, you never call the method that initializes Team red and Team blue.
     
  17. Offline

    Dubehh

    fireblast709
    Hey :)
    Sorry for the late response,
    but I fixed it by removing every instance, and only using one instance for the Main Class,
    I can call every method I want w/o making it static and I managed to make the teams outside of the onEnable (and w/o errors).

    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page