Is there anything wrong with this code developers?

Discussion in 'Plugin Development' started by shohouku, Nov 29, 2014.

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

    shohouku

    The code I posted below. It's buggy for some reason, I get no errors and yes I have listeners and they're registered.

    I have a class that controls the "BarAPI", which is a custom boss bar with custom messages. I'm showing the boss bar like this, player.getHealth/player.getMaxHealth.

    So it's showing the players' normal health then its maximum health. But when it tries to update the numbers go crazy and random and it doesn't match the players current health.

    Any ideas?


    Code:java
    1. @EventHandler
    2. public void onLogin(PlayerJoinEvent e) {
    3. final Player p = e.getPlayer();
    4. final int health = (int)p.getHealth();
    5. int health1 = (int)p.getMaxHealth();
    6. final String max = Integer.toString((int)health1);
    7. Plugin plugin = Bukkit.getPluginManager().getPlugin("TubeRed");
    8. Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
    9. public void run() {
    10. try {
    11. BarAPI.getInstance().setStatus(p, "" + ChatColor.RED + ChatColor.BOLD + health + "/" + max, (float) (((p.getHealth() + 1) / p.getMaxHealth())) * 100, true);
    12. // TODO Auto-generated catch block
    13. e1.printStackTrace();
    14. } catch (SecurityException e1) {
    15. // TODO Auto-generated catch block
    16. e1.printStackTrace();
    17. } catch (InstantiationException e1) {
    18. // TODO Auto-generated catch block
    19. e1.printStackTrace();
    20. } catch (IllegalAccessException e1) {
    21. // TODO Auto-generated catch block
    22. e1.printStackTrace();
    23. // TODO Auto-generated catch block
    24. e1.printStackTrace();
    25. } catch (NoSuchMethodException e1) {
    26. // TODO Auto-generated catch block
    27. e1.printStackTrace();
    28. }
    29. }
    30. }, 0);
    31. }
    32. @EventHandler
    33. public void onDamage(EntityDamageEvent e) {
    34. if(e.getEntity() instanceof Player) {
    35. final Player p = (Player)e.getEntity();
    36. final int health = (int)p.getHealth();
    37. int health1 = (int)p.getMaxHealth();
    38. final String max = Integer.toString((int)health1);
    39. Plugin plugin = Bukkit.getPluginManager().getPlugin("TubeRed");
    40. Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
    41. public void run() {
    42. try {
    43. BarAPI.getInstance().setStatus(p, "" + ChatColor.RED + ChatColor.BOLD + health + "/" + max, (float) (((p.getHealth() + 1) / p.getMaxHealth())) * 100, true);
    44. // TODO Auto-generated catch block
    45. e1.printStackTrace();
    46. } catch (SecurityException e1) {
    47. // TODO Auto-generated catch block
    48. e1.printStackTrace();
    49. } catch (InstantiationException e1) {
    50. // TODO Auto-generated catch block
    51. e1.printStackTrace();
    52. } catch (IllegalAccessException e1) {
    53. // TODO Auto-generated catch block
    54. e1.printStackTrace();
    55. // TODO Auto-generated catch block
    56. e1.printStackTrace();
    57. } catch (NoSuchMethodException e1) {
    58. // TODO Auto-generated catch block
    59. e1.printStackTrace();
    60. }
    61. }
    62. }, 0);
    63. }
    64. }
    65.  
    66.  
    67. @EventHandler
    68. public void onPlayerRegain(EntityRegainHealthEvent e) {
    69. if(e.getEntity() instanceof Player) {
    70. final Player p = (Player)e.getEntity();
    71. final int health = (int)p.getHealth();
    72. int health1 = (int)p.getMaxHealth();
    73. final String max = Integer.toString((int)health1);
    74. Plugin plugin = Bukkit.getPluginManager().getPlugin("TubeRed");
    75. Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
    76. public void run() {
    77. try {
    78. BarAPI.getInstance().setStatus(p, "" + ChatColor.RED + ChatColor.BOLD + health + "/" + max, (float) (((p.getHealth() + 1) / p.getMaxHealth())) * 100, true);
    79. // TODO Auto-generated catch block
    80. e1.printStackTrace();
    81. } catch (SecurityException e1) {
    82. // TODO Auto-generated catch block
    83. e1.printStackTrace();
    84. } catch (InstantiationException e1) {
    85. // TODO Auto-generated catch block
    86. e1.printStackTrace();
    87. } catch (IllegalAccessException e1) {
    88. // TODO Auto-generated catch block
    89. e1.printStackTrace();
    90. // TODO Auto-generated catch block
    91. e1.printStackTrace();
    92. } catch (NoSuchMethodException e1) {
    93. // TODO Auto-generated catch block
    94. e1.printStackTrace();
    95. }
    96. }
    97. }, 0);
    98. }
    99. }
    100.  
     
  2. Offline

    teej107

    • You don't need to cast an int to an int. The int is already an int so there is no need to cast the int to an int. (if you think that was repetitive, look at your code.)
    • Haven't used the BarAPI but,
      Code:java
      1. BarAPI.getInstance().setStatus(p, "" + ChatColor.RED + ChatColor.BOLD + health + "/" + max, (float) (((p.getHealth() + 1) / p.getMaxHealth())) * 100, true);
      Does that method really require all those catch statements?
    Just to let you know, player's health are doubles, not integers. You'll lose information by casting a double to an int.
     
    Rocoty and shohouku like this.
  3. Offline

    shohouku

    I knew I would loose information lol, thanks for the tip.

    So is there anyway I could produce a health number that can be displayed?

    Anyone? lol

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  4. p.getHealth().length;
     
  5. Offline

    Assist

    JiNJaProductionz
    Since when has Integer (or Double) had a variable called "length"?
     
    ChipDev likes this.
  6. Offline

    Zupsub

    Your whole code is just to messy..
    - 1st: do what teej107 said
    - 2nd: why do you use the scheduler to schedule the task? Why do you use #runTaskLater, when you want your Runnable to run in 0 ticks?
    - 3rd: Please make one method like update(Player) that does your logic, and then call this method from your events. Otherwise you have your code duplicated three times
    - 4th: remove all unnecessary code for debug purposes, like the ChatColors
    - 5th: if you still can't find your mistake, post your updated code
     
  7. Offline

    Rocoty

    shohouku It would be nice if you could post the output of your code. You know, show us what happens and why that's wrong? For instance. It may just be a number full of decimal places, and not a crazy and random number as you put it.
     
Thread Status:
Not open for further replies.

Share This Page