Snowball firing twice

Discussion in 'Plugin Development' started by glasseater, Nov 27, 2014.

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

    glasseater

    Hey everyone,
    I seem to be having an issue with my code where the snowball is shooting twice. One is shooting at the modified velocity and one at the default velocity. I cannot seem to figure out why. I feel like it is a noob mistake.

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onPlayerUseSnowball(PlayerInteractEvent e){
    4. Player p = e.getPlayer();
    5.  
    6. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    7. Snowball fire = p.getWorld().spawn(e.getPlayer().getLocation().add(0, 5, 0), Snowball.class);
    8. fire.setShooter(p);
    9. fire.setVelocity(p.getEyeLocation().getDirection());
    10.  
    11. }
    12.  
    13. @EventHandler
    14. public void onSnowballlandEvent(ProjectileHitEvent e){
    15. BlockIterator iterator = new BlockIterator(e.getEntity().getWorld(), e.getEntity().getLocation().toVector(), e.getEntity().getVelocity().normalize(), 0.0D, 5);
    16. Block hitBlock = null;
    17. if(e.getEntityType().equals(EntityType.SNOWBALL)){
    18. while (iterator.hasNext()) {
    19. hitBlock = iterator.next();
    20.  
    21. if (hitBlock.getType() != Material.AIR) {
    22. break;
    23. }
    24. }
    25. if (hitBlock.getType() != Material.AIR) {
    26. hitBlock.setType(Material.BEDROCK);
    27. }
    28. }
    29. }
    30. }

    Any help is much appreciated. Also, for all you of you celebrating thanksgiving, hop e you have a good one!
     
  2. Offline

    Skionz

  3. Offline

    glasseater

    Skionz Nope, do they need to be?
     
  4. Offline

    Skionz

    glasseater No was just asking because if you had them registered twice the events method would be called twice. Try adding a debug line before the snowball is launched when the player interacts and see if it is sent twice.
     
  5. Offline

    glasseater

    Skionz Added debug, no luck :/
     
  6. Offline

    jpjunho

    glasseater
    You should cancel the interact event
     
  7. Offline

    glasseater

  8. Offline

    Skionz

  9. Offline

    glasseater

    Skionz Im sorry what do you mean? The way I did it was that if it sent me the message twice there would be a problem with the event. It only fired once, and I made it say
    Code:java
    1. p.sendMessage(ChatColor.RED + "Debug message event may fire twice");
     
  10. Offline

    Skionz

    glasseater Alright pastebin the entire class please.
     
  11. Offline

    glasseater

    Skionz Sure thing.

    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2.  
    3. public void onEnable() {
    4.  
    5. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    6. }
    7.  
    8.  
    9.  
    10. @SuppressWarnings("deprecation")
    11. @EventHandler
    12. public void onPlayerUseSnowball(PlayerInteractEvent e){
    13. Player p = e.getPlayer();
    14.  
    15. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    16. Snowball fire = p.getWorld().spawn(e.getPlayer().getLocation().add(0, 5, 0), Snowball.class);
    17. fire.setShooter(p);
    18. fire.setVelocity(p.getEyeLocation().getDirection());
    19. p.sendMessage(ChatColor.RED + "Debug message event may fire twice");
    20. }
    21.  
    22.  
    23. @EventHandler
    24. public void onSnowballlandEvent(ProjectileHitEvent e){
    25. BlockIterator iterator = new BlockIterator(e.getEntity().getWorld(), e.getEntity().getLocation().toVector(), e.getEntity().getVelocity().normalize(), 0.0D, 5);
    26. Block hitBlock = null;
    27. if(e.getEntityType().equals(EntityType.SNOWBALL)){
    28. while (iterator.hasNext()) {
    29. hitBlock = iterator.next();
    30.  
    31. if (hitBlock.getType() != Material.AIR) {
    32. break;
    33. }
    34. }
    35. if (hitBlock.getType() != Material.AIR) {
    36. hitBlock.setType(Material.BEDROCK);
    37. }
    38. }
    39. }
    40. }
     
  12. Offline

    indyetoile

    glasseater
    It's because you aren't cancelling the ProjectileLaunchEvent. Currently you throw a Snowball and spawn a new one, which means you launched two.
     
  13. Offline

    glasseater

Thread Status:
Not open for further replies.

Share This Page