Scoreboard Help

Discussion in 'Plugin Development' started by ZodiacTheories, Aug 5, 2014.

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

    ZodiacTheories

    Hi, I have never used scoreboards before, and I was making a scoreboard. I am trying to make it so that they get a point every time they kill an entity. I have registered my events and I get nothing in the console. Here is my code:

    Code:java
    1. package org.zodiactheories.pandorm;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Statistic;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.PlayerDeathEvent;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.scoreboard.DisplaySlot;
    12. import org.bukkit.scoreboard.Objective;
    13. import org.bukkit.scoreboard.Score;
    14. import org.bukkit.scoreboard.Scoreboard;
    15.  
    16. public class Scoreboards implements Listener {
    17.  
    18. Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    19. Objective obj = board.registerNewObjective("Kills", "dummy");
    20.  
    21. public void updateScoreboard(Player p) {
    22. if(p.getScoreboard() == null) {
    23. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    24. obj.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Kills");
    25. Score score = obj.getScore(ChatColor.GREEN + "" + ChatColor.BOLD + "Kills");
    26. score.setScore(p.getStatistic(Statistic.KILL_ENTITY));
    27. p.setScoreboard(board);
    28. }
    29. }
    30. @EventHandler
    31. public void onKill(PlayerDeathEvent e) {
    32. if(e.getEntity().getKiller() instanceof Player) {
    33. updateScoreboard(e.getEntity().getKiller());
    34. }
    35. }
    36. @EventHandler
    37. public void onJoin(PlayerJoinEvent e) {
    38. Player p = e.getPlayer();
    39. updateScoreboard(p);
    40. p.setScoreboard(board);
    41. }
    42. }
    43.  
    44.  
     
  2. Offline

    MOMOTHEREAL

    ZodiacTheories
    Code:
     if(p.getScoreboard() == null) {
    Why would you try to check that?
     
  3. Offline

    ZodiacTheories

  4. Offline

    ZodiacTheories

  5. Offline

    Gater12

  6. Offline

    ZodiacTheories

    Gater12

    Changed it to MOB_KILLS, killed a chicken, nothing happened :(
     
  7. Offline

    ZodiacTheories

  8. Offline

    xXMaTTHDXx

  9. Offline

    ZodiacTheories

    xXMaTTHDXx

    Nothing happens, updated code:

    Code:java
    1. package org.zodiactheories.pandorm;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Statistic;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.PlayerDeathEvent;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.scoreboard.DisplaySlot;
    12. import org.bukkit.scoreboard.Objective;
    13. import org.bukkit.scoreboard.Score;
    14. import org.bukkit.scoreboard.Scoreboard;
    15.  
    16. public class Scoreboards implements Listener {
    17.  
    18. Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    19. Objective obj = board.registerNewObjective("Your Stats", "totalKillCount");
    20.  
    21. public void updateScoreboard(Player p) {
    22. if(p.getScoreboard() == null)
    23. obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    24. obj.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + p.getName());
    25. Score score = obj.getScore(ChatColor.GREEN + "" + ChatColor.BOLD + "Kills");
    26. score.setScore(p.getStatistic(Statistic.MOB_KILLS));
    27. p.setScoreboard(board);
    28. }
    29. @EventHandler
    30. public void onKill(PlayerDeathEvent e) {
    31. if(e.getEntity().getKiller() instanceof Player) {
    32. updateScoreboard(e.getEntity().getKiller());
    33. }
    34. }
    35. @EventHandler
    36. public void onJoin(PlayerJoinEvent e) {
    37. Player p = e.getPlayer();
    38. this.updateScoreboard(p);
    39. }
    40. }
    41.  
    42.  
     
  10. Offline

    ZodiacTheories

    Madbump
     
    jthort likes this.
  11. Offline

    xXMaTTHDXx

  12. Offline

    ZodiacTheories

    xXMaTTHDXx

    Code:java
    1. @Override
    2. public void onEnable() {
    3. getServer().getPluginManager().registerEvents(new Scoreboards(), this);
    4. }


    jthort

    I felt like it was right to Madbump

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

    jthort

    What doesn't work, it just doesn't show up? Or is the point system not updating the scoreboard?
     
  14. Offline

    ZodiacTheories

    jthort

    Nothing happens when I join, it doesn't show up, no error in the console either
     
  15. Offline

    user_90854156

    You should probably like, put braces there...
    if () {
    //code
    }
     
  16. Offline

    mrgreen33gamer

    Here dude, let me help you out, Firstly. If you want to get the amount of kills the player has gotten. Just put them in a HashMap and then set them to the ScoreBoard. Put for each time a player kills an entity you put as 1+. Here is the code just for you!
    |
    Code:java
    1.  
    2. package com.mrgreen33gamer.test;
    3.  
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.entity.LivingEntity;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDeathEvent;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.scoreboard.DisplaySlot;
    16. import org.bukkit.scoreboard.Objective;
    17. import org.bukkit.scoreboard.Score;
    18. import org.bukkit.scoreboard.Scoreboard;
    19. import org.bukkit.scoreboard.ScoreboardManager;
    20.  
    21. public class Kills extends JavaPlugin implements Listener {
    22.  
    23. HashMap<String, Integer> kills = new HashMap<String, Integer>();
    24.  
    25. public void ShowScoreboard(Player player){
    26. ScoreboardManager manager = Bukkit.getScoreboardManager();
    27. Scoreboard board = manager.getNewScoreboard();
    28. Objective objective = board.registerNewObjective("main", "dummy");
    29. objective.setDisplayName(ChatColor.RED + "You're Status:");
    30. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    31. Score kill = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GREEN + "Kills: "));
    32. kill.setScore(kills.get(player.getName()));
    33. player.setScoreboard(board);
    34. }
    35.  
    36. @Override
    37. public void onEnable(){
    38. getServer().getPluginManager().registerEvents(this, this);
    39. }
    40.  
    41. @Override
    42. public void onDisable(){
    43.  
    44. }
    45.  
    46. void addKill(Player player){
    47. kills.put(player.getName(), kills.get(player.getName()) + 1);
    48. ShowScoreboard(player);
    49. }
    50.  
    51. void setKills(Player player, Integer asd){
    52. kills.put(player.getName(), asd);
    53. ShowScoreboard(player);
    54. }
    55.  
    56. void putKills(Player player){
    57. kills.put(player.getName(), 0);
    58. ShowScoreboard(player);
    59. }
    60.  
    61. @EventHandler
    62. public void addKill(EntityDeathEvent event){
    63. if(event.getEntity() instanceof LivingEntity){
    64. if(event.getEntity().getKiller() instanceof Player){
    65. Player killer = event.getEntity().getKiller();
    66. if(kills.containsKey(killer.getName())){
    67. addKill(killer);
    68. }else{
    69. setKills(killer, 1);
    70. }
    71. }
    72. }
    73. }
    74.  
    75. @EventHandler
    76. public void onPlayerJoin(PlayerJoinEvent event){
    77. Player player = event.getPlayer();
    78. if(kills.containsKey(player.getName())) return;
    79. putKills(player);
    80. }
    81.  
    82. }
    83.  


    Enjoy my friend!

    Opps. Forgot to put: ZodiacTheories
    Sorry. RE-EDITED! Please look back again!

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

    ZodiacTheories

    MrTang

    Oh yeah, I just removed them for testing, forgot to add them back :3

    mrgreen33gamer

    Why do I need to use a HashMap when the scoreboard is storing my data? Isn't that just going to waste more memory?

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

    mrgreen33gamer

    Well if your trying to store kills and you cannot do that. Then what is the point? Storing a HashMap or using a ScoreBoard will take up just as much memory using either one.
     
  19. Offline

    ZodiacTheories

    mrgreen33gamer

    But that is my point, I will have to be using 3-4 hashmaps for each player, because the 'Kills' objective is just a test objective, I will be adding more later on
     
  20. Offline

    mrgreen33gamer

    ZodiacTheories Ohh so you're just trying to use a ScoreBoard to hold your kills instead of using another HashMap?
     
  21. Offline

    ZodiacTheories

    mrgreen33gamer

    I'm just trying to make each player have their own personal scoreboard with different stats on it. I have never used scoreboards before, so the kills thing is just a test. For some reason, the scoreboard doesn't show up with the code posted before. I don't really want to use HashMaps because that will use lots of memory (I think).
     
  22. Offline

    mrgreen33gamer


    ZodiacTheories Static HashMaps intend to use a lot of memory. Regular HashMaps, I don't think, use a lot of memory. But, if you want to store the kills that a player has into a CUSTOM yml file. Send me a PM.
     
  23. Offline

    ZodiacTheories

    mrgreen33gamer

    I know how to create yml files...I have no intent to PM you about this as I don''t think you fully understand my question. Besides, I want it on a scoreboard, like said in the previous 50 posts...
     
  24. Offline

    mrgreen33gamer

    ZodiacTheories I know how to put them on ScoreBoards... Loading them and saving them. I use them on my server. That's why I am trying to ask you some of the basic information about you're question.
     
  25. Offline

    ZodiacTheories

    mrgreen33gamer

    What don't you understand?

     
  26. Offline

    mrgreen33gamer

    ZodiacTheories Nvm. I am just gonna leave. Lol. Good luck with this!
     
  27. Offline

    ZodiacTheories

  28. Offline

    mrgreen33gamer

    ZodiacTheories I want to help someone else. From many angles you are saying what you want differently. Firstly, using HashMaps. Secondly, using a scoreboard.

    I can't go either way.
     
  29. Offline

    ZodiacTheories

    mrgreen33gamer

    At which point did I say I wanted to use HashMaps? I always said I wanted to use scoreboards... Sorry if I am taking up too much of your time. Hopefully you can be bothered to 'help' the next person you see.
     
  30. Offline

    mrgreen33gamer

    ZodiacTheories I am helping a lot more other people. I am willing to spend time helping you, I just get confused on you changing your answers. You say your using a ScoreBoard for testing.... Why not use a HashMap in the first place. That's back at my post. You said this:

    Quote on Quote:
     
Thread Status:
Not open for further replies.

Share This Page