[SOLVED] Shooting fireballs from a enderdragon

Discussion in 'Plugin Development' started by V10lator, Jul 24, 2012.

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

    V10lator

    I'm currently adding new functions into RideThaDragon, one of them should make it possible to shoot fireballs. This is the code I tried:
    Code:java
    1. @EventHandler(ignoreCancelled = false)
    2. public void shoot(PlayerInteractEvent event)
    3. {
    4. if(event.getAction() != Action.LEFT_CLICK_AIR)
    5. return;
    6. Player p = event.getPlayer();
    7. if(!p.hasPermission("ridetha.shoot"))
    8. return;
    9. String pn = p.getName();
    10. if(!plugin.dragons.containsKey(pn))
    11. return;
    12. LivingEntity ld = plugin.dragons.get(pn);
    13. Entity pa = ld.getPassenger();
    14. if(pa == null || pa.getUniqueId() != p.getUniqueId())
    15. return;
    16. final Fireball f = ld.launchProjectile(Fireball.class);
    17. f.setShooter(p);
    18. System.out.print("Shooted:"+f.getLocation().getX()+" - "+f.getLocation().getY()+" - "+f.getLocation().getZ());
    19. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){public void run(){
    20. if(!f.isDead())
    21. System.out.print(f.getLocation().getX()+" - "+f.getLocation().getY()+" - "+f.getLocation().getZ());
    22. }}, 0L, 1L);
    23. }

    And this is the debugging output:
    10:49:26 [INFORMATION] Shooted:87.29999998807868 - 154.5999999999992 - 2336.194694107435

    So the ball seems to die instantly. :(

    Any ideas?
     
  2. Offline

    bartboy8

    Your firing a fireball from a playername the way I see it. get(pn) get playername. That is ur livingentity. Hint: EntityType.ENDER_DRAGON
     
  3. Offline

    V10lator

    plugin.dragons is a HashMap playername -> LivingEntity, where the LivingEntity is the dragon the player owns. How else should this code work:
    Code:java
    1. Entity pa = ld.getPassenger();
    2. if(pa == null || pa.getUniqueId() != p.getUniqueId())
    3. return;

    The player can't be a passenger of itself... ;)

    The codes are correct, it even spawns the fireball as you see with the debugging message, but it seems like it dies instantly as no debugging message from the runnable appears in the log.
     
  4. Offline

    bartboy8

    Code:java
    1.  
    2. net.minecraft.server.World notchWorld = null;
    3. EntityFireball notchEntity = null;
    4. Vector velocity = null;
    5. CraftPlayer playerE = null;
    6. playerE = (CraftPlayer) player;
    7. Location location = player.getEyeLocation();
    8. notchWorld = ((CraftWorld) player.getWorld()).getHandle();
    9. EntityLiving playerEntity = playerE.getHandle();
    10. notchEntity = new EntityFireball(notchWorld, playerEntity,
    11. location.getX(), location.getY(), location.getZ());
    12. velocity = player.getEyeLocation().getDirection().multiply(5);
    13. notchWorld.addEntity(notchEntity);
    14. Entity Bukkitentity = notchEntity.getBukkitEntity();
    15. Bukkitentity.teleport(player.getEyeLocation());
    16. Bukkitentity.setVelocity(velocity);
    17. World world = player.getWorld();

    I took this code from an unactive project. You can tinker with it as you will. It may work.
     
  5. Offline

    V10lator

    Thanks, new code:
    Code:java
    1. @EventHandler(ignoreCancelled = false)
    2. public void shoot(PlayerInteractEvent event)
    3. {
    4. if(event.getAction() != Action.LEFT_CLICK_AIR)
    5. return;
    6. Player p = event.getPlayer();
    7. if(!p.hasPermission("ridetha.shoot"))
    8. return;
    9. String pn = p.getName();
    10. if(!plugin.dragons.containsKey(pn))
    11. return;
    12. LivingEntity ld = plugin.dragons.get(pn);
    13. Entity pa = ld.getPassenger();
    14. if(pa == null || pa.getUniqueId() != p.getUniqueId())
    15. return;
    16. Location loc = ld.getEyeLocation();
    17. net.minecraft.server.World nw = ((CraftWorld)ld.getWorld()).getHandle();
    18. EntityFireball nf = new EntityFireball(nw, ((CraftPlayer)p).getHandle(), loc.getX(), loc.getY(), loc.getZ());
    19. nw.addEntity(nf);
    20. final Fireball f = (Fireball)nf.getBukkitEntity();
    21. f.setVelocity(loc.getDirection().multiply(5));
    22. System.out.print("Shooted:"+f.getLocation().getX()+" - "+f.getLocation().getY()+" - "+f.getLocation().getZ());
    23. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){public void run(){
    24. if(!f.isDead())
    25. System.out.print(f.getLocation().getX()+" - "+f.getLocation().getY()+" - "+f.getLocation().getZ());
    26. }}, 0L, 1L);
    27. }

    Debugging output:
    Code:
    11:50:47 [INFORMATION] Shooted:73.49999998807903 - 67.00000000000004 - 2340.994694107421
    11:50:47 [INFORMATION] 73.49999998807903 - 67.00000000000004 - 2340.994694107421
    So it lives a little bit longer, but before it's even able to move it dies. :(
     
  6. Offline

    desht

    How about going back to your original code, but including the setVelocity() call? The fireball may have been dying because it didn't have a velocity.
     
  7. Offline

    V10lator

    With this I could finally see the fireball in-game, but i instantly died (looked like it crashed with a ComplexEntityPart of the dragon). But I have another code I'm currently working on which seems to work better. I have to tune it a bit more before I show it... ;)

    //EDIT: Here's the final code. Please note that it's designed for RideThaDragon, hence you can't simply copy&paste it for other purposes and you may not like the result:
    Code:java
    1. @EventHandler(ignoreCancelled = false)
    2. public void shoot(PlayerInteractEvent event)
    3. {
    4. if(event.getAction() != Action.LEFT_CLICK_AIR)
    5. return;
    6. Player p = event.getPlayer();
    7. if(!p.hasPermission("ridetha.shoot"))
    8. return;
    9. String pn = p.getName();
    10. if(!plugin.dragons.containsKey(pn))
    11. return;
    12. LivingEntity ld = plugin.dragons.get(pn);
    13. Entity pa = ld.getPassenger();
    14. if(pa == null || pa.getUniqueId() != p.getUniqueId())
    15. return;
    16. Location loc = ld.getLocation();
    17. float yaw = loc.getYaw();
    18.  
    19. if(yaw < 22.5F || yaw > 337.5F)
    20. {
    21. loc.setZ(loc.getZ() - 10.0F);
    22. }
    23. else if(yaw < 67.5F)
    24. {
    25. loc.setZ(loc.getZ() - 7.0F);
    26. loc.setX(loc.getX() + 7.0F);
    27. }
    28. else if(yaw < 112.5F)
    29. {
    30. loc.setX(loc.getX() + 10.0F);
    31. }
    32. else if(yaw < 157.5F)
    33. {
    34. loc.setZ(loc.getZ() + 7.0F);
    35. loc.setX(loc.getX() + 7.0F);
    36. }
    37. else if(yaw < 202.5F)
    38. {
    39. loc.setZ(loc.getZ() + 10.0F);
    40. }
    41. else if(yaw < 247.5F)
    42. {
    43. loc.setZ(loc.getZ() + 7.0F);
    44. loc.setX(loc.getX() - 7.0F);
    45. }
    46. else if(yaw < 292.5)
    47. {
    48. loc.setX(loc.getX() - 10.0F);
    49. }
    50. else
    51. {
    52. loc.setZ(loc.getZ() - 7.0F);
    53. loc.setX(loc.getX() - 7.0F);
    54. }
    55.  
    56. yaw += 180.0F;
    57. while(yaw > 360)
    58. yaw -=360;
    59. loc.setYaw(yaw);
    60.  
    61. V10Dragon vd = (V10Dragon)((CraftLivingEntity)ld).getHandle();
    62. if(vd.upDown == 1)
    63. loc.setPitch(45.0F);
    64. else if(vd.upDown == 2)
    65. loc.setPitch(-45.0F);
    66. else
    67. loc.setPitch(0.0F);
    68.  
    69. net.minecraft.server.World nw = ((CraftWorld)ld.getWorld()).getHandle();
    70. EntityFireball nf = new EntityFireball(nw, ((CraftPlayer)p).getHandle(), loc.getX(), loc.getY(), loc.getZ());
    71. nw.addEntity(nf);
    72. final Fireball f = (Fireball)nf.getBukkitEntity();
    73. f.teleport(loc);
    74. double speed = 5.0D;
    75. if(!vd.br)
    76. {
    77. speed += plugin.rideSpeed;
    78. speed += vd.fl;
    79. }
    80. Vector v = loc.getDirection().multiply(speed);
    81. f.setDirection(v);
    82. f.setVelocity(v);
    83. }
     
Thread Status:
Not open for further replies.

Share This Page