[UNSOLVED]Velocity and getting nearest player :/

Discussion in 'Plugin Development' started by ZodiacTheories, May 7, 2014.

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

    ZodiacTheories

    Hi, so I am coding a plugin so where if a player is on the ground, a dispenser fires an arrow at the player and the arrow explodes.

    I am having trouble with two things, one is how to make the arrow shoot up in the air when it is fired, two, is how to get the nearest player to the dispenser?

    Here is my code:

    Code:java
    1. package me.thesamster8.clashofclans.defenses;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Dispenser;
    6. import org.bukkit.entity.Arrow;
    7. import org.bukkit.entity.Projectile;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.EntityExplodeEvent;
    11. import org.bukkit.event.entity.ProjectileHitEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.PlayerInventory;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.projectiles.BlockProjectileSource;
    16. import org.bukkit.projectiles.ProjectileSource;
    17.  
    18. public class Mortar implements Listener {
    19.  
    20. public void giveMortar(ItemStack mortar, PlayerInventory pi) {
    21. mortar = new ItemStack(Material.DISPENSER);
    22. ItemMeta meta = mortar.getItemMeta();
    23. meta.setDisplayName(ChatColor.GRAY + "" + ChatColor.BOLD + "Mortar");
    24. mortar.setItemMeta(meta);
    25. pi.addItem(mortar);
    26. }
    27.  
    28. @EventHandler
    29. public void onExplode(ProjectileHitEvent e) {
    30. Projectile p = e.getEntity();
    31. if(p instanceof Arrow) {
    32. Arrow a = (Arrow) p;
    33. ProjectileSource shooter = a.getShooter();
    34. if(shooter instanceof BlockProjectileSource) {
    35. if(((BlockProjectileSource) shooter).getBlock() != null) {
    36. if(((BlockProjectileSource) shooter).getBlock().getState() instanceof Dispenser) {
    37. Dispenser d = (Dispenser) a.getShooter();
    38. a.getWorld().createExplosion(a.getLocation(), 4F);
    39. }
    40. }
    41. }
    42. }
    43. }
    44.  
    45. @EventHandler
    46. public void onExploder(EntityExplodeEvent e) {
    47. e.blockList().clear();
    48. }
    49. }


    I have tried to mess around with getNearbyEntities but that doesn't work for dispensers, I also tried casting it to a dispenser which failed aswell.
     
  2. Offline

    Glumpz

    ZodiacTheories #.getDistance() to get the distance of each player and find out which distance is the smallest.
     
  3. Offline

    ZodiacTheories

    Glumpz

    Can't seem to find the getDistance() method
     
  4. Offline

    JBoss925

    Why are two of your methods named the same thing?
     
  5. Offline

    ZodiacTheories

    JBoss925

    Didn't realise that :3 Changed it!

    Anyone? (Sorry for the bump, its just really urgent)

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

    ZodiacTheories

  7. Offline

    MOMOTHEREAL

    It is player.getLocation().distance(newlocationtocompare)
     
  8. Offline

    Panjab

    distance() is actually a pretty bad choice. Use distanceSquared instead:

    Code:java
    1.  
    2. double distance = loc1.distanceSquared(loc2);
    3. double backSquared = Math.pow(distance, 2);
    4.  
     
  9. Offline

    ZodiacTheories

    MOMOTHEREAL Panjab

    Thanks,

    now that I have that setup, how would I do the velocity, I have tried a.setVelocity(new Vector although I couldn't find any methods that were to do with the direction of the arrow.

    Updated Code:

    Code:java
    1. package me.thesamster8.clashofclans.defenses;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.Dispenser;
    7. import org.bukkit.entity.Arrow;
    8. import org.bukkit.entity.Entity;
    9. import org.bukkit.entity.Projectile;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityExplodeEvent;
    13. import org.bukkit.event.entity.ProjectileHitEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.PlayerInventory;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.projectiles.BlockProjectileSource;
    18. import org.bukkit.projectiles.ProjectileSource;
    19.  
    20. public class Mortar implements Listener {
    21.  
    22. public void giveMortar(ItemStack mortar, PlayerInventory pi) {
    23. mortar = new ItemStack(Material.DISPENSER);
    24. ItemMeta meta = mortar.getItemMeta();
    25. meta.setDisplayName(ChatColor.GRAY + "" + ChatColor.BOLD + "Mortar");
    26. mortar.setItemMeta(meta);
    27. pi.addItem(mortar);
    28. }
    29.  
    30. @EventHandler
    31. public void onExplode(ProjectileHitEvent e) {
    32. Projectile p = e.getEntity();
    33. if(p instanceof Arrow) {
    34. Arrow a = (Arrow) p;
    35. ProjectileSource shooter = a.getShooter();
    36. if(shooter instanceof BlockProjectileSource) {
    37. if(((BlockProjectileSource) shooter).getBlock() != null) {
    38. if(((BlockProjectileSource) shooter).getBlock().getState() instanceof Dispenser) {
    39. Dispenser d = (Dispenser) a.getShooter();
    40. a.getWorld().createExplosion(a.getLocation(), 4F);
    41. for(Entity ent : a.getNearbyEntities(25, 25, 25)) {
    42. d.getLocation().distance((Location) ent);
    43. }
    44. }
    45. }
    46. }
    47. }
    48. }
    49. @EventHandler
    50. public void onExploder(EntityExplodeEvent e) {
    51. e.blockList().clear();
    52. }
    53. }
    54.  


    Thanks
     
  10. Offline

    werter318

    Panjab It's distanceSQUARED, why would you use the Math.pow(,2); It should actually be Math.sqrt() <--- but that's the thing that makes distance() a heavy method.
     
  11. Offline

    ZodiacTheories

  12. Offline

    ZodiacTheories

  13. Offline

    Rocoty

    ZodiacTheories
    Code:java
    1. d.getLocation().distance((Location) ent);

    At this line you have to ask yourself this: Is the Entity a Location? Can it ever even be a Location? Those are the questions you must ask yourself before casting something. Now, ask yourself this: How do I get the Location of an Entity?
     
  14. Offline

    ZodiacTheories

    Rocoty

    I don't get what you mean
     
  15. Offline

    Rocoty

    ZodiacTheories Have you learned about inheritance and polymorphism? If not, you must read about it. It is crucial to your success.
     
  16. Offline

    ZodiacTheories

    Rocoty

    Still learning java, I know inheritance, currently learning polymorphism :p
     
  17. Offline

    ZodiacTheories

  18. Offline

    ZodiacTheories

    Bump :/

    BUMP this is urgent D:

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

    ZodiacTheories

    Bump. Please help
     
  20. Offline

    Rocoty

    You will get what I mean once you learn inheritance and polymorphism. Basically, you cannot cast an entity to a location because an entity is not, and will never be, a location.
     
  21. Offline

    ZodiacTheories

    Rocoty

    I didn't get what you meant because of the wording, not the java behind it.
     
Thread Status:
Not open for further replies.

Share This Page