Error - Health must be between 0 and 20.0

Discussion in 'Plugin Development' started by StatywPlay, Aug 20, 2014.

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

    StatywPlay

    How to fix it
    Error:
    Code:
    [11:31:22] [Server thread/WARN]: [MiniEvents] Task #612 for MiniEvents v1.4 generated an exception
    java.lang.IllegalArgumentException: Health must be between 0 and 20.0
        at org.bukkit.craftbukkit.v1_7_R3.entity.CraftLivingEntity.setHealth(CraftLivingEntity.java:81) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at Handlers.EventHandlers.ComplexEvents.PaintBall$1.run(PaintBall.java:41) ~[?:?]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:600) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Code:
    Code:java
    1. package Handlers.EventHandlers.ComplexEvents;
    2.  
    3. import Mains.MiniEvents;
    4. import Util.Effects.ParticleEffect;
    5. import org.bukkit.*;
    6. import org.bukkit.entity.Entity;
    7. import org.bukkit.entity.Item;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    13. import org.bukkit.event.entity.PlayerDeathEvent;
    14. import org.bukkit.event.player.PlayerDropItemEvent;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.event.player.PlayerPickupItemEvent;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.scheduler.BukkitRunnable;
    19.  
    20. import java.util.*;
    21.  
    22. public class PaintBall implements Listener {
    23. public MiniEvents plugin;
    24.  
    25. public PaintBall(MiniEvents plugin) {
    26. this.plugin = plugin;
    27. }
    28.  
    29. public void runThis(final Item item, final Player player) {
    30. item.setVelocity(player.getLocation().getDirection().multiply(3.0D));
    31. item.setPickupDelay(Integer.MAX_VALUE);
    32. item.getWorld().playSound(item.getLocation(), Sound.ARROW_HIT, 5, 10);
    33. new BukkitRunnable() {
    34. @Override
    35. public void run() {
    36. for (Entity entity : item.getNearbyEntities(0.8, 0.8, 0.8)) {
    37. if (entity instanceof Player) {
    38. if (!((Player) entity).getName().equalsIgnoreCase(player.getName())) {
    39. if (plugin.getPlayerEvent(player).equals("paint")) {
    40. if (plugin.getInfo().eventstarted) {
    41. ((Player) entity).setHealth(((Player) entity).getHealth() - plugin.getConfig().getDouble("events.paintball-damage"));
    42. ((Player) entity).damage(0, player);
    43. ParticleEffect.LARGE_SMOKE.display(entity.getLocation(), 1F, 1F, 1F, 1F, 30);
    44. }
    45. }
    46. }
    47. cancel();
    48. return;
    49. }
    50. }
    51. }
    52. }.runTaskTimer(plugin, 2, 1);
    53. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    54. @Override
    55. public void run() {
    56. item.remove();
    57. }
    58. }, 3 * 20L);
    59. }
    60.  
    61. @EventHandler
    62. public void onPlayerInteract(PlayerInteractEvent event) {
    63. final Player player = event.getPlayer();
    64. if (plugin.getPlayerEvent(player).equals("paint")) {
    65. if (plugin.getInfo().eventstarted) {
    66. ItemStack c = plugin.getMethods().translateItemStack("events.paintball-item");
    67. if (player.getItemInHand().getType().equals(c.getType()) && player.getItemInHand().getData().getData() == c.getData().getData()) {
    68. event.setCancelled(true);
    69. if (!plugin.getInfo().sbefore.contains(player.getName())) {
    70. if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    71. Random dice = new Random();
    72. int number;
    73. number = dice.nextInt(6);
    74. switch (number) {
    75. case 1:
    76. final Item item = player.getWorld().dropItem(player.getEyeLocation(), new ItemStack(Material.INK_SACK, 1, (short) 8));
    77. runThis(item, player);
    78. break;
    79. case 2:
    80. final Item item2 = player.getWorld().dropItem(player.getEyeLocation(), new ItemStack(Material.INK_SACK, 1, (short) 9));
    81. runThis(item2, player);
    82. break;
    83. case 3:
    84. final Item item3 = player.getWorld().dropItem(player.getEyeLocation(), new ItemStack(Material.INK_SACK, 1, (short) 10));
    85. runThis(item3, player);
    86. break;
    87. case 4:
    88. final Item item4 = player.getWorld().dropItem(player.getEyeLocation(), new ItemStack(Material.INK_SACK, 1, (short) 5));
    89. runThis(item4, player);
    90. break;
    91. case 5:
    92. final Item item5 = player.getWorld().dropItem(player.getEyeLocation(), new ItemStack(Material.INK_SACK, 1, (short) 13));
    93. runThis(item5, player);
    94. break;
    95. }
    96. }
    97. }
    98. }
    99. }
    100. }
    101. }
    102.  
    103. @EventHandler
    104. public void onDeath(PlayerDeathEvent event) {
    105. Player player = event.getEntity();
    106. if (plugin.getPlayerEvent(player).equals("paint")) {
    107. if (plugin.getInfo().eventstarted) {
    108. plugin.getMethods().basicLose(player);
    109. }
    110. }
    111. }
    112.  
    113. @EventHandler
    114. public void onDamage(EntityDamageByEntityEvent event) {
    115. if (event.getEntity() instanceof Player) {
    116. Player player = (Player) event.getEntity();
    117. if (event.getDamager() instanceof Player) {
    118. if (plugin.getPlayerEvent(player).equals("paint")) {
    119. if (plugin.getInfo().eventstarted) {
    120. event.setCancelled(true);
    121. }
    122. }
    123. }
    124. }
    125. }
    126.  
    127. @EventHandler
    128. public void onPickUp(PlayerPickupItemEvent event) {
    129. if (plugin.getPlayerEvent(event.getPlayer()).equals("paint")) {
    130. if (plugin.getInfo().eventstarted) {
    131. event.setCancelled(true);
    132. }
    133. }
    134. }
    135.  
    136. @EventHandler
    137. public void onDrop(PlayerDropItemEvent event) {
    138. if (plugin.getPlayerEvent(event.getPlayer()).equals("paint")) {
    139. if (plugin.getInfo().eventstarted) {
    140. event.setCancelled(true);
    141. }
    142. }
    143. }
    144. }
    145.  
     
  2. StatywPlay Debug to see what you're trying to set the health to
     
  3. easy. safe the value you want to set the players health (line 41) into a variable and check it. if it is < 0 set it to 0, if it is > 20 set it to 20. but you better should check for > player.getMaxHealth() and set it to player.getMaxHealth() if so. then set the players health to the value you got.​
    btw your code is ugly!! why are you casting so much? for example just check if(entity instanceof Player) -> Player p = (Player) entity; so you can use p instead of cast every time to player...​

    it said a value <0 or > player.getMaxHealth ... so just safe it and check. if its bigger setHealth(player.getMaxHealth); else if its smaller than 0 player.setHealth(0); else player.setHealth(value);

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Shmobi Always better to show them how to figure out what they're doing wrong that to give them the solution :)
     
  5. AdamQpzm mhm, right :) but dont you think that the error "health must be between 0 and 20" doesnt tell it? xD i mean cmon -.- health... between..0..20 :OOOO but yes youre right
     
  6. Shmobi True, but I think him seeing actual proof of what he was trying to set it to (i.e. -1 or 24) would've been helpful.
     
  7. Ur setting the players health using some math, but if the snowball damage is bigger than the players health, it goes under 0, so its out of bounds, below that line, u got a line that damages the player, just set the damage number(it's 0 there) to the snowball damage config.
     
  8. AdamQpzm well... i dont really think that it would have helped very much. if you look at the error you know that. the value does not care if -1 -2 -1000 or 3892, if it says not between 0-20 there is a given range it can be and its wrong. you should have let him think about it instead of waiting until he recognized that his brain wasnt working together with his eyes xD
     
  9. Offline

    AoH_Ruthless

    Shmobi
    I think AdamQpzm is saying to let OP figure out how to actually solve the problem itself. Debugging it just helps OP realize what the problem is, and then when you know what the problem is, you can implement a solution easily. However, the disagreement is with you explicitly telling the OP the code, outright.

    Also, you are incorrect.
    Say we check for damage > player.getMaxHealth().
    Let us say player.getMaxHealth() returns 20, and the damage is 19. What if the player's health is 10. 10 - 19 = -9 which means the error will still occur. For this reason, check against player#getHealth().
     
    skyrimfan1 and AdamQpzm like this.
  10. AoH_Ruthless if you would've read my post you would know, that i don't told him to do this... seems like your eyes arent working, too. i said "save the value you want to set the players health into a variable" NOT THE DAMAGE. i dont fucking care if your damage is 19 or (408943*(-284))³² -.- maybe it is, maybe it isn't. i would save into my variable player.getHealth() - damage; and then check this.
    1. read
    2. think
    3. talk
    !!!!!
     
  11. Offline

    stormneo7

    Code:java
    1. ((Player) entity).setHealth(((Player) entity).getHealth() - plugin.getConfig().getDouble("events.paintball-damage"));

    Before doing that, check if the health you're trying to set is NOT below 0 and not ABOVE 20. If it is, change it.
     
  12. Offline

    skyrimfan1


    I think your logic is faulty. And telling people that they don't have eyes is completely uncalled for in situations like these. This is a HELP forum.

    Judging from the above convo, if they're blind, then, you must have dementia because you obviously contradicted yourself in your last post. AoH was simply trying to help you.

    EDIT: Don't spoonfeed him the answer. Like Adam said, it's a relatively simple error that can be solved by some debugging and variable bounds checking.
     
    AoH_Ruthless likes this.
  13. Offline

    AoH_Ruthless


    :'(
     
  14. Offline

    AngryCola

    Absorption effect works
     
  15. skyrimfan1 my logic:
    Code:
    Player p = (Player) entity;
    double damage = plugin.getConfig().getDouble("events.paintball-damage"));
    double newHealth = p.getHealth()-damage;
    if(newHealth<0){
    p.setHealth(0);
    }else if(newHealth>p.getMaxHealth()){
    p.setHealth(p.getMaxHealth());
    }else{
    p.setHealth(newHealth);
    }
    If you use this, you cant set the health to an illegal value!!!

    AoH_Ruthless i apologize for my attack on you. it sounded harder than it was meant to, but at one point you must say it's true. StaywPlay's only error was to not read the message of the exception. to explain that "health must be between 0-20" means that you tried to set it to <0 or >20 is just stupid. Thats something you should get on your own. So nobody can blame me for just telling him what's to do.
    skyrimfan1 and if you would have read his post compleatly you would know that he didn't help me, he said i made something wrong and told me how to do it right while my solution was already right and he just didn't read my post so well. i can accept if i do an error, but i can't accept if i become criticized for an error someone else made. for sure not by an error like being not able to read the text i'm gonna complain about compleatly or right or whatever :O just nope :)
     
  16. Offline

    AoH_Ruthless

    Shmobi
    Technically though in that code you don't need the second if-check because Bukkit does that for you. Therefore, the code that you gave OP (which AdamQpzm and I were just kindly trying to point out to you isn't the always the best approach to helping, no need to lose your temper) could be shortened.

    The way you explained your code was very flawed and did not make much sense. The implications of it suggested some wrong logic:

    "if its bigger setHealth(player.getMaxHealth)" makes no sense in English standards, whether English is your first language or not. It was impossible to discern what you were saying. Followed by the phrase "if its smaller than 0 ...", this implies that by "bigger" you meant bigger than 0, not bigger than the max health. This interpretation means you were wrong, whether you knew what you were talking about or not.

    Communication of ideas is important in trying to help people. Just because something makes sense in your head, it would be best if you wrote complete thoughts while helping people (no, i'm not trying to put you down, tell you are completely wrong, etc..).
     
    skyrimfan1 likes this.
  17. AoH_Ruthless well... if you quote it like you did you're right. it does not make the sense it should do, but if you read the few words directly in front of "if its bigger ..." which are "it said a value <0 or > player.getMaxHealth ... so just safe it and check" you should know, that i was talking about the value you should have saved. btw no, dont call me a nazi or such shit now, i am from germany, so english is not my first language.
    And i could swear, if you try to set the health of a player bigger than his maxHealth, you will get an exception! also you get one if you try to set it to a negative value! so nope, you cant leave the else if part. or did i get something wrong you tried to explain me?

    so look at it and think:
    the value you saved must be between 0 and getMaxHealth() to be able to set it as the players health. and i said you must check if it is in the range. if i say then "if it is bigger" how do you imagine, that i meant 0? sure you can get it that way, but you should recognize "oowwwww, errrorrr" and reread it to make sure you did not make an error. And at least then, you must understand that i am talking about the range between 0 and getMaxHealth(). I believe the error was that you or he or who ever read not the whole thing! he/she/it read just this one sentence and then you can get it wrong.

    it's a pretty stupid example, but it happened to me in a forum. Someone wrote "i've a german keyboard layout. My Penis goes from a - z on the keyboard" (dont think wrong now, it was a minecraft forum! :D ). Everybody made fun of him because they just read from a-z but not the part with german, you can imagine the rest on your own. well on an european layout, depending on the keyboard you got, this are 5-10 cm difference. And here it is exactly the same

    i really dont wanted to put you down, too. i just cant understand your problem. It's totaly fine in my opinion. Yes the discription was short, but the solution was posted so often that i thought everybody got what i/we meant! And thats what is making me angry: people complaining about things they could easily understand, but fuck it up with being sloppy
     
  18. Offline

    skyrimfan1


    My code:
    Code:
    double remainingHealth = player.getHealth() - damage;
    double actualHealth = remainingHealth > player.getMaxHealth() ? player.getMaxHealth() : remainingHealth;
    actualHealth = actualHealth < 0 ? 0 : actualHealth;
    player.setHealth(actualHealth)
    See the conciseness?

    And by the way, he was trying to help. It's called constructive criticism, and it's something that you absorb and learn off rather than trying to fight. In fact, you actually incorporated his suggestion as I see:

    Code:
    double newHealth = p.getHealth()-damage;
    }
    So quit arguing. Also, don't tag me twice in the same post; it's redundant.
     
  19. skyrimfan1 your code is just shorter but does the same. and it's not as readable as mine. both have thier benefits. And i quoted you 2 times because i hoped you will read the whole post instead of just the lower part
     
  20. Offline

    skyrimfan1

    Shmobi
    I always read the whole post. And mine's simply two tertiary statements in succession, it's still fairly easy to read if you know Java.

    Anyhow, it would be nice if a moderator locked this thread. It's developing into a flame/bickering war, and the author of the thread seems to have plenty of answers to choose from.
     
Thread Status:
Not open for further replies.

Share This Page