Solved Method not running when called

Discussion in 'Plugin Development' started by Horsey, Jul 29, 2017.

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

    Horsey

    Hi there,

    I've been coding a plugin where you can shoot fireworks such that they explode on impact and deal damage, however, I seem to have hit a wall as to why the following code is not running when there are no entities near the impact point. The debug messages tell me that it runs once I get close enough that it damages me. The same goes for the explosion bit, it only ever runs when it hits an entity, or I get near it.

    Here's my code:
    Code:Java
    1. new BukkitRunnable(){
    2. @Override
    3. public void run(){
    4. // The block at the firework's location
    5. Material block = firework.getWorld().getBlockAt(firework.getLocation()).getType();
    6.  
    7. // Check whether the block is solid
    8. if (block != Material.AIR && block != Material.WATER && block != Material.STATIONARY_WATER && !block.isTransparent()){
    9. explodeFireworks(firework);
    10. cancel();
    11. return;
    12. }
    13. // Check whether the firework hit an entity
    14. if (firework.getNearbyEntities(0.5,0.5,0.5).size() > 0){
    15. explodeFireworks(firework);
    16. cancel();
    17. return;
    18. }
    19. // If the firework didn't hit something, just reset its direction
    20. firework.setVelocity(vector);
    21.  
    22. }
    23. }.runTaskTimer(main, 0L, 1L);
    24.  
    25. }
    26.  
    27. private void explodeFireworks(Firework firework){
    28. // Detonate the firework (this is the only bit that triggers on impact)
    29. firework.detonate();
    30. // If enabled, create an explosion (does not run when no nearby entities)
    31. if (main.getConfig().getBoolean("do-hand-explosions"))
    32. firework.getWorld().createExplosion(firework.getLocation(), main.getConfig().getInt("hand-explosion-size"));
    33. // If enabled, deal damage to nearby entities
    34. if (main.getConfig().getBoolean("do-hand-extra-damage"))
    35. damageNearbyEntities(firework);
    36. }
    37.  
    38. private void damageNearbyEntities(Entity source){
    39. Bukkit.broadcastMessage("Started!");
    40. // Get the radius for damage
    41. double radius = main.getConfig().getDouble("hand-damage-radius");
    42. // Get the damage to be dealt
    43. double damage = main.getConfig().getDouble("hand-extra-damage");
    44. // Iterate over all nearby entities and kill them
    45. for (Entity hurt: source.getNearbyEntities(radius, radius, radius)){
    46. if (hurt instanceof Damageable) {
    47. try{
    48. if (true)
    49. ((Damageable) hurt).setHealth(((Damageable) hurt).getHealth() - damage);
    50. }catch (IllegalArgumentException e){((Damageable) hurt).setHealth(0);}
    51. }
    52. else hurt.remove();
    53. }
    54. Bukkit.broadcastMessage("Ended!");
    55. }

    So, to sum up, the firework detonates, but there is no explosion or damage dealt till I get near it, at which point it hurts me.
    Hopefully you guys can shed some light on why it doesn't work :)
     
  2. Offline

    Zombie_Striker

    @Horsey
    If you want it to trigger only if it hit solid blocks, why not use if Material#isSolid()?

    Are you sure the is solid block ever returns true? Have you tried printing out the block's type?
     
  3. Offline

    Horsey

    That's a good idea lol, but yeah, it does return true and the firework does explode (only the firework, not the explosion I created).
     
  4. Offline

    Zombie_Striker

    @Horsey
    What would be the explosion you created? What that be the explodeFireworks() method? If so, can you post that method?
     
  5. Offline

    Horsey

    Scroll down, I have included that method in the code.
     
  6. Offline

    Zombie_Striker

    @Horsey
    1. What happens if you hard code the values from the config?
    2. Since the config values should not change while the server is one, move them outside of the methods.
    3. Use Math.max(((Damageable)hurt).getHealth-damage,0.0); so you don't need to use the try/catch statement.
    4. You say it only sends the messages when you get close. Does that happen in the ExploseFireworks method? What about in the runnable?
     
  7. Offline

    Horsey

Thread Status:
Not open for further replies.

Share This Page