Sidebar Stats Display

Discussion in 'Plugin Development' started by AakashKcX, Aug 18, 2015.

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

    AakashKcX

    I am making a Kit-PvP plugin where I am trying to have a scoreboard on the side showing the player's name, kills and deaths. I have got so far to showing up the scoreboard and it's layout, but I am completly lost on how to show the player's name, kills and deaths. This is all I have so far:
    Code:
    ScoreboardManager manager = Bukkit.getScoreboardManager();
        Scoreboard board = manager.getNewScoreboard();
    
        @Override
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(this, this);
            Objective health = board.registerNewObjective("health", "health");
            Objective sidebar = board.registerNewObjective("sidebar", "dummy");
            health.setDisplaySlot(DisplaySlot.BELOW_NAME);
            health.setDisplayName(ChatColor.RED + "❤");
            sidebar.setDisplaySlot(DisplaySlot.SIDEBAR);
            sidebar.setDisplayName(ChatColor.DARK_GRAY + "--- " + ChatColor.DARK_RED + ChatColor.BOLD + "Kit-PvP" + ChatColor.DARK_GRAY + " ---");
            Score score1 = sidebar.getScore(ChatColor.GRAY + "" + ChatColor.BOLD + "» " + ChatColor.WHITE + "You");
            score1.setScore(-1);
            Score score2 = sidebar.getScore(ChatColor.DARK_GREEN + "Player");
            score2.setScore(-2);
            Score score3 = sidebar.getScore("");
            score3.setScore(-3);
            Score score4 = sidebar.getScore(ChatColor.GRAY + "" + ChatColor.BOLD + "» " + ChatColor.WHITE + "Stats");
            score4.setScore(-4);
            Score score5 = sidebar.getScore(ChatColor.GOLD + "Kills" + ChatColor.DARK_GRAY + ": " + ChatColor.RED + "0");
            score5.setScore(-5);
            Score score6 = sidebar.getScore(ChatColor.GOLD + "Deaths" + ChatColor.DARK_GRAY + ": " + ChatColor.RED + "0");
            score6.setScore(-6);
            for(Player player : Bukkit.getOnlinePlayers()){
                player.setScoreboard(board);
                player.setHealth(player.getHealth());
            }
        }
    
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            Location spawn = new Location(player.getWorld(), 0.5, 100, 0.5, 0, 0);
            player.teleport(spawn);
            player.getInventory().clear();
            player.setScoreboard(board);
            player.setHealth(player.getHealth());
        }
    Another problem I am facing is that whenever I run the server, it comes up with an error where the plugin will stop working until I reload the server.
    Code:
    [14:07:37] [Server thread/INFO]: Starting minecraft server version 1.8.8
    [14:07:37] [Server thread/INFO]: Loading properties
    [14:07:37] [Server thread/INFO]: Default game type: SURVIVAL
    [14:07:37] [Server thread/INFO]: Generating keypair
    [14:07:37] [Server thread/INFO]: Starting Minecraft server on *:25565
    [14:07:37] [Server thread/INFO]: Using default channel type
    [14:07:37] [Server thread/INFO]: This server is running CraftBukkit version git-Bukkit-53fac9f (MC: 1.8.8) (Implementing API version 1.8.8-R0.1-SNAPSHOT)
    [14:07:37] [Server thread/ERROR]: Could not load 'plugins\Kit-PvP.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:133) ~[craftbukkit.jar:git-Bukkit-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[craftbukkit.jar:git-Bukkit-53fac9f]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [craftbukkit.jar:git-Bukkit-53fac9f]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:289) [craftbukkit.jar:git-Bukkit-53fac9f]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.<init>(CraftServer.java:251) [craftbukkit.jar:git-Bukkit-53fac9f]
        at net.minecraft.server.v1_8_R3.PlayerList.<init>(PlayerList.java:69) [craftbukkit.jar:git-Bukkit-53fac9f]
        at net.minecraft.server.v1_8_R3.DedicatedPlayerList.<init>(SourceFile:14) [craftbukkit.jar:git-Bukkit-53fac9f]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:179) [craftbukkit.jar:git-Bukkit-53fac9f]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:503) [craftbukkit.jar:git-Bukkit-53fac9f]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.NullPointerException
        at com.aakashkcx.kitpvp.Main.<init>(Main.java:35) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_51]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_51]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_51]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_51]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_51]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit.jar:git-Bukkit-53fac9f]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[craftbukkit.jar:git-Bukkit-53fac9f]
        ... 9 more
    [14:07:37] [Server thread/INFO]: [PermissionsEx] Loading PermissionsEx v1.23.3
    [14:07:37] [Server thread/INFO]: Preparing level "world"
    [14:07:37] [Server thread/INFO]: Preparing start region for level 0 (Seed: 5817366584098317681)
    [14:07:38] [Server thread/INFO]: Preparing start region for level 1 (Seed: -7669446181325196222)
    [14:07:38] [Server thread/INFO]: [PermissionsEx] Enabling PermissionsEx v1.23.3
    [14:07:38] [Server thread/INFO]: [PermissionsEx] Initializing file backend
    [14:07:38] [Server thread/INFO]: [PermissionsEx] Permissions file successfully reloaded
    [14:07:38] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [14:07:38] [Server thread/INFO]: Done (1.036s)! For help, type "help" or "?"
    [14:07:40] [Server thread/INFO]: CONSOLE: [0;31;1mPlease note that this command is not supported and may cause issues when using some plugins.[m
    [14:07:40] [Server thread/INFO]: CONSOLE: [0;31;1mIf you encounter any issues please use the /stop command to restart your server.[m
    [14:07:40] [Server thread/INFO]: [PermissionsEx] Disabling PermissionsEx v1.23.3
    [14:07:41] [Server thread/INFO]: [PermissionsEx] Loading PermissionsEx v1.23.3
    [14:07:41] [Server thread/INFO]: [Kit-PvP] Loading Kit-PvP v1.0
    [14:07:41] [Server thread/INFO]: [PermissionsEx] Enabling PermissionsEx v1.23.3
    [14:07:41] [Server thread/INFO]: [PermissionsEx] Initializing file backend
    [14:07:41] [Server thread/INFO]: [PermissionsEx] Permissions file successfully reloaded
    [14:07:41] [Server thread/INFO]: [Kit-PvP] Enabling Kit-PvP v1.0
    [14:07:41] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
    [14:07:41] [Server thread/INFO]: CONSOLE: [0;32;1mReload complete.[m
    [14:07:43] [Server thread/INFO]: Stopping the server
    [14:07:43] [Server thread/INFO]: Stopping server
    [14:07:43] [Server thread/INFO]: [Kit-PvP] Disabling Kit-PvP v1.0
    [14:07:43] [Server thread/INFO]: [PermissionsEx] Disabling PermissionsEx v1.23.3
    [14:07:43] [Server thread/INFO]: Saving players
    [14:07:43] [Server thread/INFO]: Saving worlds
    [14:07:43] [Server thread/INFO]: Saving chunks for level 'world'/Overworld
    [14:07:43] [Server thread/INFO]: Saving chunks for level 'world_the_end'/The End
    
     
  2. Offline

    mine-care

    thats the highlights of what happend ^

    Please point out what is line 35 of the Main class.

    also why is that nessesary?
     
  3. Offline

    AakashKcX

    Line 35 is this:
    Code:
    Scoreboard board = manager.getNewScoreboard();
    I saw it on the a Scoreboard tutorial, I was thinking the same thing but I just left it in as it didn't do anything
     
  4. Offline

    mine-care

    @AakashKcX Dont forget to tahg me, i just looked at the thread by luck.
    Well that means that manager is null. Mabe you need to initalise those onEnable not on object creation as you do currently.
     
  5. Offline

    Konato_K

    @mine-care Setting the player health updates the health of the player in order to show properly in the scoreboard :p
     
  6. Offline

    mine-care

    @Konato_K Hmm. There is an easyer way tho by setting the scoreboard value directly to the health but anyway.
     
  7. Offline

    AakashKcX

    @mine-care If i add it on the onEnable I cant set it to people when they join through the PlayerJoinEvent
    @Konato_K :eek: Konato!
     
    Last edited: Aug 18, 2015
  8. Offline

    AakashKcX

  9. Offline

    Konato_K

    @AakashKcX What's the problem? Have you fixed the NPE?
     
  10. Offline

    AakashKcX

    @Konato_K If i add the scorebaord on the onEnable I cant set it to people when they join through the PlayerJoinEvent. Also, how would I get the scoreboard to show the Kills, Death and Player's name?
     
  11. Offline

    AdobeGFX

    I think this is what @mine-care meant..
    Code:
    Scoreboard board;
    
    public void onEnable() {
    board = manager.getNewScoreboard();
    }
     
    mine-care likes this.
  12. @AakashKcX @mine-care @AdobeGFX
    The reason manager is null because it is called before any worlds have loaded (Bukkit's ScoreboardManager is loaded after the first world has loaded), so you have to call Bukkit.getScoreboardManager() in your onEnable. If your plugin loads on startup, you need to schedule a 1 tick delayed task and then set the scoreboard manager, because after 1 tick everything is loaded.
     
  13. Offline

    mine-care

    @megamichiel
    :p
    Not nessesarily.. This can be avoided, i think onEnable it would be just fine.
     
  14. @mine-care
    Only if it doesn't load on STARTUP, because that means it enables before any worlds have loaded
     
  15. Offline

    AakashKcX

    @AdobeGFX Thats perfect, I don't know why I didn't think of that.

    How would I get the player's kills, deaths and their name to show up?
     
  16. Offline

    AakashKcX

    Bump 2

    How would I get each player's kills, deaths and name to show up on their individual scoreboard?
    Code:
    ScoreboardManager manager = Bukkit.getScoreboardManager();
        Scoreboard board = manager.getNewScoreboard();
        @Override
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(this, this);
            Objective health = board.registerNewObjective("health", "health");
            Objective sidebar = board.registerNewObjective("sidebar", "dummy");
            health.setDisplaySlot(DisplaySlot.BELOW_NAME);
            health.setDisplayName(ChatColor.RED + "❤");
            sidebar.setDisplaySlot(DisplaySlot.SIDEBAR);
            sidebar.setDisplayName(ChatColor.DARK_GRAY + "--- " + ChatColor.DARK_RED + ChatColor.BOLD + "Kit-PvP" + ChatColor.DARK_GRAY + " ---");
            Score score1 = sidebar.getScore(ChatColor.GRAY + "" + ChatColor.BOLD + "» " + ChatColor.WHITE + "You");
            score1.setScore(-1);
            Score score2 = sidebar.getScore(ChatColor.DARK_GREEN + "Player");
            score2.setScore(-2);
            Score score3 = sidebar.getScore("");
            score3.setScore(-3);
            Score score4 = sidebar.getScore(ChatColor.GRAY + "" + ChatColor.BOLD + "» " + ChatColor.WHITE + "Stats");
            score4.setScore(-4);
            Score score5 = sidebar.getScore(ChatColor.GOLD + "Kills" + ChatColor.DARK_GRAY + ": " + ChatColor.RED + "0");
            score5.setScore(-5);
            Score score6 = sidebar.getScore(ChatColor.GOLD + "Deaths" + ChatColor.DARK_GRAY + ": " + ChatColor.RED + "0");
            score6.setScore(-6);
            for(Player player : Bukkit.getOnlinePlayers()){
                player.setScoreboard(board);
                player.setHealth(player.getHealth());
            }
        }
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            Location spawn = new Location(player.getWorld(), 0.5, 100, 0.5, 0, 0);
            player.teleport(spawn);
            player.getInventory().clear();
            player.setScoreboard(board);
            player.setHealth(player.getHealth());
        }
     
  17. Offline

    AakashKcX

  18. Offline

    AakashKcX

    I've got it working, abit. The scoreboard is the same for each player so when someone kills another player, every player gets Kills: 1, Deaths: 1. How would I get the scoreboards to be separate?
    Code:java
    1.  
    2. ScoreboardManager manager;
    3. Scoreboard board;
    4. Objective health;
    5. Objective sidebar;
    6. HashMap<String, Score> playerKills = new HashMap<String, Score>();
    7. HashMap<String, Score> playerDeaths = new HashMap<String, Score>();
    8.  
    9. @Override
    10. public void onEnable(){
    11. Bukkit.getPluginManager().registerEvents(this, this);
    12. manager = Bukkit.getScoreboardManager();
    13. board = manager.getNewScoreboard();
    14. health = board.registerNewObjective("health", "health");
    15. sidebar = board.registerNewObjective("sidebar", "dummy");
    16. health.setDisplaySlot(DisplaySlot.BELOW_NAME);
    17. health.setDisplayName(ChatColor.RED + "❤");
    18. sidebar.setDisplaySlot(DisplaySlot.SIDEBAR);
    19. sidebar.setDisplayName(ChatColor.DARK_GRAY + "--- " + ChatColor.DARK_RED + ChatColor.BOLD + "Kit-PvP" + ChatColor.DARK_GRAY + " ---");
    20. playerKills.clear();
    21. playerDeaths.clear();
    22. for(Player player : Bukkit.getOnlinePlayers()){
    23. Location spawn = new Location(player.getWorld(), 0.5, 100, 0.5, 0, 0);
    24. player.teleport(spawn);
    25. player.getInventory().clear();
    26. player.setScoreboard(board);
    27. player.setHealth(player.getHealth());
    28. playerKills.put(player.getName(), sidebar.getScore((ChatColor.GOLD + "Kills" + ChatColor.DARK_GRAY + ":")));
    29. playerKills.get(player.getName()).setScore(0);
    30. playerDeaths.put(player.getName(), sidebar.getScore((ChatColor.GOLD + "Deaths" + ChatColor.DARK_GRAY + ":")));
    31. playerDeaths.get(player.getName()).setScore(0);
    32. }
    33. }
    34.  
    35. @EventHandler
    36. public void onPlayerJoin(PlayerJoinEvent event) {
    37. Player player = event.getPlayer();
    38. Location spawn = new Location(player.getWorld(), 0.5, 100, 0.5, 0, 0);
    39. player.teleport(spawn);
    40. player.getInventory().clear();
    41. player.setScoreboard(board);
    42. player.setHealth(player.getHealth());
    43. playerKills.put(player.getName(), sidebar.getScore((ChatColor.GOLD + "Kills" + ChatColor.DARK_GRAY + ":")));
    44. playerKills.get(player.getName()).setScore(0);
    45. playerDeaths.put(player.getName(), sidebar.getScore((ChatColor.GOLD + "Deaths" + ChatColor.DARK_GRAY + ":")));
    46. playerDeaths.get(player.getName()).setScore(0);
    47. }
    48.  
    49. @EventHandler
    50. public void onPlayerDeath(PlayerDeathEvent event){
    51. event.getDrops().clear();
    52. Player killed = event.getEntity();
    53. Player killer = event.getEntity().getKiller();
    54. event.setDeathMessage(ChatColor.RED + killed.getName() + ChatColor.GOLD + " was killed by " + ChatColor.RED + killer.getName() + ChatColor.DARK_GRAY + " (" + ChatColor.WHITE + (int) killer.getHealth() + " " + ChatColor.RED + "❤" + ChatColor.DARK_GRAY + ")");
    55. killer.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE));
    56. playerKills.get(killer.getName()).setScore(playerKills.get(killer.getName()).getScore() + 1);
    57. playerDeaths.get(killed.getName()).setScore(playerDeaths.get(killed.getName()).getScore() + 1);
    58. }
    59.  


    Bump?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
Thread Status:
Not open for further replies.

Share This Page