scoreboard & packet

Discussion in 'Plugin Development' started by Sicka_gp, Jun 25, 2013.

  1. Offline

    Sicka_gp

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Hi, I need help with scoreboard and packets. This code does not work for multiple items. If I have more than one item to display score so crash the client. I would advise anyone to solve this problem?

    Code:java
    1. package cz.Sicka_gp.ConfigurableMessages;
    2.  
    3. import org.bukkit.craftbukkit.v1_5_R3.entity.CraftPlayer;
    4. import org.bukkit.entity.Player;
    5.  
    6. import cz.Sicka_gp.ConfigurableMessages.Settings.ScoreboardItemsReplacer;
    7.  
    8. import net.minecraft.server.v1_5_R3.Packet;
    9. import net.minecraft.server.v1_5_R3.Packet206SetScoreboardObjective;
    10. import net.minecraft.server.v1_5_R3.Packet207SetScoreboardScore;
    11. import net.minecraft.server.v1_5_R3.Packet208SetScoreboardDisplayObjective;
    12. import net.minecraft.server.v1_5_R3.Scoreboard;
    13. import net.minecraft.server.v1_5_R3.ScoreboardBaseCriteria;
    14. import net.minecraft.server.v1_5_R3.ScoreboardScore;
    15.  
    16. public class ScoreBoardManager {
    17. String name = "Test";
    18. ConfigurableMessages plugin;
    19. Scoreboard sb = new Scoreboard();
    20.  
    21. public ScoreBoardManager(ConfigurableMessages instance){
    22. this.plugin = instance;
    23. }
    24.  
    25. public static void sendPacket(Player player, Packet packet) {
    26. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    27. }
    28.  
    29. public void CreateSidebar(Player p){
    30. sb.registerObjective(name, new ScoreboardBaseCriteria(name));
    31. Packet206SetScoreboardObjective packet = new Packet206SetScoreboardObjective(sb.getObjective(name), 0);
    32. Packet208SetScoreboardDisplayObjective display = new Packet208SetScoreboardDisplayObjective(1, sb.getObjective(name));
    33. for(String score : ConfigurableMessages.getPlugin().getConfig().getStringList("Board")){
    34. String[] scores = score.split(";", 2);
    35. String scorename = scores[0];
    36. String scorevaule = scores[1];
    37. try{
    38. ConfigurableMessages.getPlugin().log.info(scorename + " + "+ scorevaule);
    39. int vaule = ScoreboardItemsReplacer.getReplacedInt(scorevaule, p);
    40. ScoreboardScore scoreItem = sb.getPlayerScoreForObjective(scorename, sb.getObjective(name));
    41. scoreItem.setScore(vaule);
    42. Packet207SetScoreboardScore scoreItemP = new Packet207SetScoreboardScore(scoreItem, 0);
    43. sendPacket(p, packet);
    44. sendPacket(p, display);
    45. sendPacket(p, scoreItemP);
    46. ConfigurableMessages.getPlugin().log.info(scorename + " + ! + "+ scorevaule);
    47. }
    48. }
    49. }
    50. }
  2. Offline

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    don't try to use the packets way, use the bukkit way, less prone to bugs
  3. Offline

    Sicka_gp

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I use it for compatibility with some plugins..
  4. Offline

    Sicka_gp

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Code:java
    1. package cz.Sicka_gp.ConfigurableMessages;
    2.  
    3. import org.bukkit.craftbukkit.v1_5_R3.entity.CraftPlayer;
    4. import org.bukkit.entity.Player;
    5. import cz.Sicka_gp.ConfigurableMessages.Settings.ScoreboardItemsReplacer;
    6.  
    7. import net.minecraft.server.v1_5_R3.Packet;
    8. import net.minecraft.server.v1_5_R3.Packet206SetScoreboardObjective;
    9. import net.minecraft.server.v1_5_R3.Packet207SetScoreboardScore;
    10. import net.minecraft.server.v1_5_R3.Packet208SetScoreboardDisplayObjective;
    11. import net.minecraft.server.v1_5_R3.Scoreboard;
    12. import net.minecraft.server.v1_5_R3.ScoreboardBaseCriteria;
    13. import net.minecraft.server.v1_5_R3.ScoreboardScore;
    14.  
    15. public class ScoreBoardManager {
    16. String name = "Test";
    17. ConfigurableMessages plugin;
    18. Scoreboard sb = new Scoreboard();
    19.  
    20. public ScoreBoardManager(ConfigurableMessages instance){
    21. this.plugin = instance;
    22. }
    23.  
    24. public static void sendPacket(Player player, Packet packet) {
    25. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    26. }
    27.  
    28. public void CreateSidebar(Player p){
    29. sb.registerObjective(name, new ScoreboardBaseCriteria(name));
    30. Packet206SetScoreboardObjective packet = new Packet206SetScoreboardObjective(sb.getObjective(name), 0);
    31. Packet208SetScoreboardDisplayObjective display = new Packet208SetScoreboardDisplayObjective(1, sb.getObjective(name));
    32.  
    33. sendPacket(p, packet);
    34. sendPacket(p, display);
    35.  
    36. for(String score : ConfigurableMessages.getPlugin().getConfig().getStringList("Board")){
    37. String[] scores = score.split(";", 2);
    38. String scorename = scores[0];
    39. String scorevaule = scores[1];
    40. try{
    41. ConfigurableMessages.getPlugin().log.info(scorename + " + "+ scorevaule);
    42. int vaule = ScoreboardItemsReplacer.getReplacedInt(scorevaule, p);
    43. ScoreboardScore scoreItem = sb.getPlayerScoreForObjective(scorename, sb.getObjective(name));
    44. scoreItem.setScore(vaule);
    45. Packet207SetScoreboardScore scoreItemP = new Packet207SetScoreboardScore(scoreItem, 0);
    46. sendPacket(p, scoreItemP);
    47. ConfigurableMessages.getPlugin().log.info(scorename + " + ! + "+ scorevaule);
    48. }
    49. }
    50. }
    51.  
    52. }


    Fixed, Now I have a problem with updating the score value. This code does not work. Should not someone code samples for my problem? Alternatively advise how to solve it? thanks

    Code:java
    1. package cz.Sicka_gp.ConfigurableMessages;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.craftbukkit.v1_5_R3.entity.CraftPlayer;
    5. import org.bukkit.entity.Player;
    6. import cz.Sicka_gp.ConfigurableMessages.Settings.ScoreboardItemsReplacer;
    7.  
    8. import net.minecraft.server.v1_5_R3.Packet;
    9. import net.minecraft.server.v1_5_R3.Packet206SetScoreboardObjective;
    10. import net.minecraft.server.v1_5_R3.Packet207SetScoreboardScore;
    11. import net.minecraft.server.v1_5_R3.Packet208SetScoreboardDisplayObjective;
    12. import net.minecraft.server.v1_5_R3.Scoreboard;
    13. import net.minecraft.server.v1_5_R3.ScoreboardBaseCriteria;
    14. import net.minecraft.server.v1_5_R3.ScoreboardScore;
    15.  
    16. public class ScoreBoardManager {
    17. String name = "Test";
    18. ConfigurableMessages plugin;
    19. Scoreboard sb = new Scoreboard();
    20.  
    21. public ScoreBoardManager(ConfigurableMessages instance){
    22. this.plugin = instance;
    23. }
    24.  
    25. public static void sendPacket(Player player, Packet packet) {
    26. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    27. }
    28.  
    29. public void CreateSidebar(Player p){
    30. sb.registerObjective(name, new ScoreboardBaseCriteria(name));
    31. Packet206SetScoreboardObjective packet = new Packet206SetScoreboardObjective(sb.getObjective(name), 0);
    32. Packet208SetScoreboardDisplayObjective display = new Packet208SetScoreboardDisplayObjective(1, sb.getObjective(name));
    33.  
    34. sendPacket(p, packet);
    35. sendPacket(p, display);
    36.  
    37. for(String score : ConfigurableMessages.getPlugin().getConfig().getStringList("Board")){
    38. String[] scores = score.split(";", 2);
    39. String scorename = scores[0];
    40. String scorevaule = scores[1];
    41. try{
    42. ConfigurableMessages.getPlugin().log.info(scorename + " + "+ scorevaule);
    43. int vaule = ScoreboardItemsReplacer.getReplacedInt(scorevaule, p);
    44. ScoreboardScore scoreItem = sb.getPlayerScoreForObjective(scorename, sb.getObjective(name));
    45. scoreItem.setScore(vaule);
    46. Packet207SetScoreboardScore scoreItemP = new Packet207SetScoreboardScore(scoreItem, 0);
    47. sendPacket(p, scoreItemP);
    48. ConfigurableMessages.getPlugin().log.info(scorename + " + ! + "+ scorevaule);
    49. }
    50. }
    51. }
    52.  
    53. public void UpdateScore(){
    54. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(ConfigurableMessages.getPlugin(), new Runnable(){
    55.  
    56. @Override
    57. public void run() {
    58. for (Player p : Bukkit.getOnlinePlayers()){
    59. CreateSidebar(p);
    60. }
    61. }
    62. }, 0L, 1*20L);
    63.  
    64. }
    65. }



    Code:
    2013-06-25 18:58:22 [WARNING] [ConfigurableMessages] Task #24 for ConfigurableMessages v0.5 generated an exception
    java.lang.IllegalArgumentException: An objective with the name 'Test' already exists!
        at net.minecraft.server.v1_5_R3.Scoreboard.registerObjective(SourceFile:28)
        at cz.Sicka_gp.ConfigurableMessages.ScoreBoardManager.CreateSidebar(ScoreBoardManager.java:30)
        at cz.Sicka_gp.ConfigurableMessages.ScoreBoardManager$1.run(ScoreBoardManager.java:60)
        at org.bukkit.craftbukkit.v1_5_R3.scheduler.CraftTask.run(CraftTask.java:53)
        at org.bukkit.craftbukkit.v1_5_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
        at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:513)
        at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
        at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
        at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)

    This post has been edited 3 times. It was last edited by Sicka_gp Jun 25, 2013.
  5. Offline

    iFamasssxD

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    You need to check to see if "Test" objective already exists. If it does either remove it and re-create it or just skip the section of code that creates it again and go to the updating section.

    Code:
    java.lang.IllegalArgumentException: An objective with the name 'Test' already exists!

    This post has been edited 1 time. It was last edited by iFamasssxD Jun 25, 2013.
  6. Offline

    Sicka_gp

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME




    Thank you, for now works without error
    Code:java
    1. package cz.Sicka_gp.ConfigurableMessages;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.craftbukkit.v1_5_R3.entity.CraftPlayer;
    5. import org.bukkit.entity.Player;
    6. import cz.Sicka_gp.ConfigurableMessages.Settings.ScoreboardItemsReplacer;
    7.  
    8. import net.minecraft.server.v1_5_R3.Packet;
    9. import net.minecraft.server.v1_5_R3.Packet206SetScoreboardObjective;
    10. import net.minecraft.server.v1_5_R3.Packet207SetScoreboardScore;
    11. import net.minecraft.server.v1_5_R3.Packet208SetScoreboardDisplayObjective;
    12. import net.minecraft.server.v1_5_R3.Scoreboard;
    13. import net.minecraft.server.v1_5_R3.ScoreboardBaseCriteria;
    14. import net.minecraft.server.v1_5_R3.ScoreboardScore;
    15.  
    16. public class ScoreBoardManager {
    17. static String name = "Test";
    18. ConfigurableMessages plugin;
    19. static Scoreboard sb = new Scoreboard();
    20.  
    21. public ScoreBoardManager(ConfigurableMessages instance){
    22. this.plugin = instance;
    23. }
    24.  
    25. public static void sendPacket(Player player, Packet packet) {
    26. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    27. }
    28.  
    29. public void CreateSidebar(Player p){
    30. if(sb.getObjective(name) == null){
    31. sb.registerObjective(name, new ScoreboardBaseCriteria(name));
    32. }
    33. Packet206SetScoreboardObjective packet = new Packet206SetScoreboardObjective(sb.getObjective(name), 0);
    34. Packet208SetScoreboardDisplayObjective display = new Packet208SetScoreboardDisplayObjective(1, sb.getObjective(name));
    35.  
    36. sendPacket(p, packet);
    37. sendPacket(p, display);
    38.  
    39. for(String score : ConfigurableMessages.getPlugin().getConfig().getStringList("Board")){
    40. String[] scores = score.split(";", 2);
    41. String scorename = scores[0];
    42. String scorevaule = scores[1];
    43. try{
    44. ConfigurableMessages.getPlugin().log.info(scorename + " + "+ scorevaule);
    45. int vaule = ScoreboardItemsReplacer.getReplacedInt(scorevaule, p);
    46. ScoreboardScore scoreItem = sb.getPlayerScoreForObjective(scorename, sb.getObjective(name));
    47. scoreItem.setScore(vaule);
    48. Packet207SetScoreboardScore scoreItemP = new Packet207SetScoreboardScore(scoreItem, 0);
    49. sendPacket(p, scoreItemP);
    50. ConfigurableMessages.getPlugin().log.info(scorename + " + ! + "+ scorevaule);
    51. }
    52. }
    53. }
    54.  
    55. public void UpdateScore(){
    56. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(ConfigurableMessages.getPlugin(), new Runnable(){
    57.  
    58. @Override
    59. public void run() {
    60. for (Player p : Bukkit.getOnlinePlayers()){
    61. Packet206SetScoreboardObjective packet = new Packet206SetScoreboardObjective(sb.getObjective(name), 2);
    62. Packet208SetScoreboardDisplayObjective display = new Packet208SetScoreboardDisplayObjective(1, sb.getObjective(name));
    63.  
    64. sendPacket(p, packet);
    65. sendPacket(p, display);
    66.  
    67. for(String score : ConfigurableMessages.getPlugin().getConfig().getStringList("Board")){
    68. String[] scores = score.split(";", 2);
    69. String scorename = scores[0];
    70. String scorevaule = scores[1];
    71. try{
    72. ConfigurableMessages.getPlugin().log.info(scorename + " + "+ scorevaule);
    73. int vaule = ScoreboardItemsReplacer.getReplacedInt(scorevaule, p);
    74. ScoreboardScore scoreItem = sb.getPlayerScoreForObjective(scorename, sb.getObjective(name));
    75. scoreItem.setScore(vaule);
    76. Packet207SetScoreboardScore scoreItemP = new Packet207SetScoreboardScore(scoreItem, 0);
    77. sendPacket(p, scoreItemP);
    78. ConfigurableMessages.getPlugin().log.info(scorename + " + ! + "+ scorevaule);
    79. }
    80. }
    81. }
    82. }
    83. }, 0L, 10*20L);
    84.  
    85. }
    86. }
  7. Offline

    Sicka_gp

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    how to remove the objective for the player? For example, when disconnected from the server. For this code?
  8. Online

    chasechocolate

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @Sicka_gp I think it requires replacing one of the 0s with a 1 (or vice-versa). @Tzeentchful might know.
  9. Offline

    Sicka_gp

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME

Share This Page