Scoreboard confusing D:

Discussion in 'Plugin Development' started by XxZHALO13Xx, Nov 17, 2014.

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

    XxZHALO13Xx

    I created a scoreboard and when a plyer dies the killer gets 1 added.. that works... the thing is the victim also gets it... why is that?
    Code:java
    1. package me.xxzhalo13xx.thewalkingdead;
    2.  
    3. import java.util.Arrays;
    4. import java.util.HashMap;
    5. import java.util.List;
    6.  
    7. import me.xxzhalo13xx.thewalkingdead.commands.TWDCommands;
    8. //import me.xxzhalo13xx.thewalkingdead.events.PlayerJoin;
    9. import me.xxzhalo13xx.thewalkingdead.events.PlayerKillPlayer;
    10. import me.xxzhalo13xx.thewalkingdead.sponge.SpongeCrate;
    11. import me.xxzhalo13xx.thewalkingdead.weapons.DarylsBow;
    12.  
    13. import org.bukkit.Bukkit;
    14. import org.bukkit.ChatColor;
    15. import org.bukkit.Material;
    16. import org.bukkit.OfflinePlayer;
    17. import org.bukkit.entity.Entity;
    18. import org.bukkit.entity.Player;
    19. import org.bukkit.entity.Zombie;
    20. import org.bukkit.event.EventHandler;
    21. import org.bukkit.event.Listener;
    22. import org.bukkit.event.entity.EntityDeathEvent;
    23. import org.bukkit.event.entity.PlayerDeathEvent;
    24. import org.bukkit.event.player.PlayerJoinEvent;
    25. import org.bukkit.inventory.ItemStack;
    26. import org.bukkit.inventory.ShapedRecipe;
    27. import org.bukkit.inventory.meta.ItemMeta;
    28. import org.bukkit.plugin.java.JavaPlugin;
    29. import org.bukkit.scoreboard.DisplaySlot;
    30. import org.bukkit.scoreboard.Objective;
    31. import org.bukkit.scoreboard.Score;
    32. import org.bukkit.scoreboard.Scoreboard;
    33.  
    34. public class Core extends JavaPlugin implements Listener{
    35.  
    36. private Scoreboard board;
    37. private Objective o;
    38. private HashMap<OfflinePlayer, Score> kills = new HashMap<OfflinePlayer, Score>();
    39.  
    40. public static String MAIN = ChatColor.GRAY + "[" + ChatColor.YELLOW + "" + ChatColor.BOLD + "The Walking Dead" + ChatColor.GRAY + "] ";
    41.  
    42.  
    43. public void onEnable(){
    44. getLogger().info(MAIN + "The Walking Dead has been loaded!");
    45. getCommand("twd").setExecutor(new TWDCommands());
    46. getServer().getPluginManager().registerEvents(this, this);
    47. //getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
    48. getServer().getPluginManager().registerEvents(new PlayerKillPlayer(), this);
    49. getServer().getPluginManager().registerEvents(new SpongeCrate(), this);
    50. getServer().getPluginManager().registerEvents(new DarylsBow(), this);
    51. saveConfig();
    52.  
    53. board = Bukkit.getServer().getScoreboardManager().getNewScoreboard();
    54.  
    55. o = board.registerNewObjective("test", "dummy");
    56. o.setDisplayName(ChatColor.RED + "Player Kills");
    57. o.setDisplaySlot(DisplaySlot.SIDEBAR);
    58.  
    59. saveDefaultConfig();
    60.  
    61. List<String> s = getConfig().getStringList("kills");
    62.  
    63.  
    64. for (String str : s) {
    65. String[] words = str.split(":");
    66. kills.put(Bukkit.getServer().getOfflinePlayer(words[0]), o.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.GREEN + "Kills:")));
    67. kills.get(Bukkit.getServer().getOfflinePlayer(words[0])).setScore(Integer.parseInt(words[1]));
    68. }
    69. //Recipes
    70.  
    71. hrecipe();
    72. }
    73.  
    74.  
    75.  
    76. public void onDisable() {
    77. List<String> s = getConfig().getStringList("kills");
    78.  
    79. for (OfflinePlayer p : kills.keySet()) {
    80. s.add(p.getName() + ":" + kills.get(p).getScore());
    81. }
    82.  
    83. getConfig().set("kills", s);
    84. saveConfig();
    85. }
    86.  
    87.  
    88.  
    89. //@EventHandler
    90. //public void onZombieDeath(EntityDeathEvent e){
    91. // Entity deadEntity = e.getEntity();
    92. // Entity killer = e.getEntity().getKiller();
    93.  
    94. // if(killer instanceof Player && deadEntity instanceof Zombie){
    95. // Player p = (Player) killer;
    96.  
    97. //if (!kills.containsKey(p.getName())) {
    98. // kills.put(p.getName(), 1);
    99. // saveConfig();
    100. // p.sendMessage(MAIN + ChatColor.RED + "You now have " + kills.get(p.getName() + " Zombie Kills!"));
    101.  
    102. // } else {
    103. // kills.put(p.getName(), kills.get(p.getName()) + 1);
    104. // saveConfig();
    105. // p.sendMessage(MAIN + ChatColor.RED + "You now have " + kills.get(p.getName() + " Zombie Kills!"));
    106.  
    107.  
    108. //}
    109. //}
    110. //}
    111.  
    112. private void hrecipe() {
    113. ItemStack bow = new ItemStack(Material.BOW, 1);
    114. ItemMeta meta = bow.getItemMeta();
    115. meta.setDisplayName(ChatColor.GREEN + "Daryl's Bow");
    116. meta.setLore(Arrays.asList("This bow", "is very special", "Use it wisely."));
    117. bow.setItemMeta(meta);
    118.  
    119. ShapedRecipe hrecipe = new ShapedRecipe(bow);
    120. hrecipe.shape(
    121. " # ",
    122. " @ ");
    123. hrecipe.setIngredient('@', Material.BOW);
    124. hrecipe.setIngredient('#', Material.TNT);
    125. Bukkit.getServer().addRecipe(hrecipe);
    126. }
    127.  
    128. @EventHandler
    129. public void onPlayerJoin(PlayerJoinEvent e) {
    130. Player p = e.getPlayer();
    131.  
    132. p.setScoreboard(board);
    133.  
    134. if (kills.get(p) == null) kills.put(p, o.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.GREEN + "Kills:")));
    135. }
    136.  
    137. @EventHandler
    138. public void onPlayerDeath(PlayerDeathEvent e) {
    139.  
    140. Player victim = e.getEntity().getPlayer();
    141. Player killer = victim.getKiller();
    142.  
    143. if(killer instanceof Player){
    144. kills.get(killer).setScore(kills.get(killer).getScore() + 1);
    145. killer.sendMessage(ChatColor.RED + "You now have " + kills.get(killer).getScore() + " kills!");
    146. }
    147. }
    148. }
    149.  
     
  2. Offline

    mine-care

    Does victim also recive the message "you now have..."?
    Any other events we need to know about?
    Have you tried to print out killer/victim names?
     
  3. Offline

    XxZHALO13Xx

    mine-care no they just get the scoreboard with the same amount of kills as the killer :/. None that would interfere with that... Not sure why they get the board too
     
  4. Offline

    Zombieghost_391

    What it sounds like to me you need to create a new scoreboard everytime a play joins and save the new objective to the player using a hashmap. Then when you want to update a score for like kills just use the hashmap to get the score to edit.
    I hoped I helped you.
    Sorry I can't give example code, I'm typed this on my school pc :p
     
  5. Offline

    teej107

  6. Offline

    XxZHALO13Xx

  7. Offline

    teej107

    XxZHALO13Xx be more specific I said more than 1 thing.
     
  8. Offline

    Zombieghost_391

  9. Offline

    XxZHALO13Xx

    Zombieghost_391 teej107 basically what im wanting is when they join they get a score board that says The Walking Dead
    then they have Walker Kills
    then they have Player Kills
    then they have $Money and they are all at 0 till they kill something. say they kill a player. it increases by one. idk how to do that
     
  10. Offline

    Zombieghost_391

    Code:java
    1. //HashMap keeping player's objective.
    2. static HashMap<Player, Objective> scoreboardList = new HashMap<Player, Objective>();
    3.  
    4. //Creating a new scoreboard for the player.
    5. public static void setManager(Player player) {
    6. ScoreboardManager manager = Bukkit.getScoreboardManager();
    7. Scoreboard board = manager.getNewScoreboard();
    8.  
    9. Objective objective = board.registerNewObjective("dummy", "this");
    10. objective.setDisplayName("§2The Walking Dead");
    11. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    12.  
    13. {
    14. Score score = objective.getScore("§2Walkers Kills");
    15. score.setScore(0); //Set Integer of walkers killed.
    16. }
    17.  
    18. {
    19. Score score = objective.getScore("§6Players Kills");
    20. score.setScore(0); //Set Integer of players killed.
    21. }
    22.  
    23. {
    24. Score score = objective.getScore("§aMoney");
    25. score.setScore(0); //Set Integer of players money.
    26. }
    27.  
    28. player.setScoreboard(board);
    29. scoreboardList.put(player, objective);
    30. }
    31.  
    32. //This is the basic way in editing scores (my opinion)
    33. public void changeMoney(Player player, Integer money){
    34. Objective objective = scoreboardList.get(player);
    35. objective.getScore("§aMoney").setScore(money);
    36. }
     
  11. Offline

    teej107

    What's the point if your setting a new scoreboard for each player? As long as you are setting a new objective for each scoreboard, you can have unique scores and use what Bukkit provides.
     
  12. Offline

    Zombieghost_391

    How would you update theses, when I update them it would always show the updated score for the newest player.

    My way makes it much more private.

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

    teej107

    player.getScoreboard() and scoreboard.getObjective(String or DisplaySlot)

    What ever that means. You way is more work and isn't using the API to its full potential.
     
  14. Offline

    Zombieghost_391

    How are you not taking full potential of the api, the only thing that I an think of is that you wont be able to use teams, but you always can just use arrayLists and NameTagEdit to mimic teams.

    Plus I tried your way and it just lead me back to were I started when I messed around in scoreboards.

    We shows players tokens, another player joins there is only one score for tokens so it updates for the player but shows it for everyone which is why I have a constant of creating objectives to defeat this problem, is there a better solution maybe but my way does work.
     
  15. Offline

    teej107

    I wasn't thinking of teams when I said
    But it does help my argument. I was thinking of the methods I posted of earlier.
    Player#getScoreboard() and Scoreboard#getObjective(org.bukkit.scoreboard.DisplaySlot), or Scoreboard#getObjective(java.lang.String)
    Code:java
    1. HashMap<Player, Objective> scoreboardList = new HashMap<Player, Objective>()
    The posted methods replace the need for a HashMap. Why should you make an extra object to keep track of objects when Bukkit is already keeping track of them for you? The rest of your code seems like it works (apart from the obvious mistakes), it's just the HashMap that you do not need.


    Using the methods I posted works. They get the same objects without the need of saving them to a variable manually.
     
  16. Offline

    Zombieghost_391

    The point of this is to help the user with his scoreboard not to decide whether which one works better. Let XxZHALO13Xx decide which one he wants to continue with. They both work so it wouldn't matter.
     
Thread Status:
Not open for further replies.

Share This Page