How to fix this?

Discussion in 'Plugin Development' started by BriceMetal, Sep 20, 2014.

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

    BriceMetal

    Code:java
    1. @EventHandler
    2. public void onbplace(BlockPlaceEvent e) {
    3. final Location loc = e.getBlock().getLocation();
    4. if (e.getBlock().getType() == Material.REDSTONE_TORCH_ON) {
    5. final int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(
    6. this, new Runnable() {
    7.  
    8. public void run() {
    9. ParticleEffect.HAPPY_VILLAGER.display(3, 3, 3, 1,
    10. 40, loc, 60);
    11. ParticleEffect.INSTANT_SPELL.display(0, 0, 0, 1, 5,
    12. loc, 60);
    13. List<Entity> near = loc.getWorld().getEntities();
    14. for (Entity en : near) {
    15. if (en.getNearbyEntities(3, 3, 3) instanceof Player) {
    16. Player player = (Player) en;
    17. player.sendMessage("Healed");
    18. }
    19. }
    20. }
    21.  
    22. }, 0, 20 * 1);
    23.  
    24. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    25.  
    26. public void run() {
    27. Bukkit.getScheduler().cancelTask(task);
    28. loc.getBlock().setType(Material.AIR);
    29. }
    30. }, 20 * 5);
    31. }
    32. }
     
  2. Offline

    Skionz

    BriceMetal Maybe you should start out by telling us whats wrong with it?
     
  3. Offline

    Gerov

    BriceMetal Looks perfectly fine to me, the only think I can think that is wrong with it is maybe you didn't register the event.
     
  4. Offline

    ToPoEdiTs

    What is the problem or error?
     
  5. Offline

    BriceMetal

  6. Offline

    rbrick

    There is your problem. the getNearbyEntities method returns a collection of entities, It will never be a instance of a Player class. Just test if the entity is the instance of the Player class.
    Code:java
    1.  
    2. for (Entity en : near) {
    3. if (en instanceof Player) {
    4. Player player = (Player) en;
    5. player.sendMessage("Healed");
    6. }
    7. }
     
    BriceMetal likes this.
  7. Offline

    BriceMetal

    How could I set the range then?

    ToPoEdiTs
    Looking for all nearby players within the set range and send the message

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

    Skionz

    BriceMetal Correct me if I am wrong but shouldn't you use a for loop to iterate through the nearby entities?
     
  9. Offline

    ToPoEdiTs

    a.a okey :)
     
  10. Offline

    BriceMetal

    This is what I have so far

    Code:java
    1. @EventHandler
    2. public void onBlockPlace(final BlockPlaceEvent e) {
    3. final Location loc = e.getBlock().getLocation();
    4. if (e.getBlock().getType() == Material.REDSTONE_TORCH_ON) {
    5. final int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(
    6. this, new Runnable() {
    7.  
    8. public void run() {
    9. ParticleEffect.HEART.display(3, 3, 3, 1, 40, loc,
    10. 60);
    11. List<Entity> near = loc.getWorld().getEntities();
    12. for (Entity en : near) {
    13. if (en instanceof Player) {
    14. Player player = (Player) en;
    15. player.sendMessage("§c"
    16. + e.getPlayer().getName()
    17. + "'s§a Healing Torch Healed You For§c"
    18. + " +5");
    19. player.setHealth(player.getHealth() + 5);
    20. }
    21. }
    22. }
    23.  
    24. }, 0, 20 * 1);
    25.  
    26. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    27.  
    28. public void run() {
    29. Bukkit.getScheduler().cancelTask(task);
    30. loc.getBlock().setType(Material.AIR);
    31. loc.getWorld().playEffect(loc, Effect.ENDER_SIGNAL, 10);
    32. }
    33. }, 20 * 5);
    34. }
    35. }


    bump

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

    rrhett

    I don't see anything that would cause this to not work. Do you see any exceptions in your logs? Is it showing the hearts? Is it setting health correctly? Can you confirm the event handler is even being called? Knowing how much is working helps figure out where and why it isn't working.
     
  12. BriceMetal Please learn basic Java, learn more about the Bukkit API, and then debug your code. :)
     
    Hawktasard likes this.
  13. Offline

    Hawktasard

    This.
     
  14. Offline

    BriceMetal

    It all works, I just can't get the affecting area
     
Thread Status:
Not open for further replies.

Share This Page