Launching Projectiles

Discussion in 'Plugin Development' started by XvBaseballkidvX, Aug 31, 2013.

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

    XvBaseballkidvX

    Hello everyone!

    I am still working on that RPG plugin for Wands Special Weapons etc, and have come across a problem. What I want to do is make a spread of Snowballs (about 5 - 7) and if one of those Snowballs Hits a player, it would set them on fire! The problem I am having is setting the Metadata and a Vector to the same Projectile (Snowball.class).

    Code:
    Code:java
    1. @EventHandler
    2. public void Snowball_to_fire_event(PlayerInteractEvent event){
    3.  
    4. ItemStack is = new ItemStack(Material.STICK);
    5. ItemMeta im = is.getItemMeta();
    6. im.setDisplayName(ChatColor.DARK_RED + "SnowballFire Wand");
    7. im.setLore(Arrays.asList(ChatColor.GOLD + "Spawns a Damage Potion"));
    8. is.setItemMeta(im);
    9.  
    10. Player player = event.getPlayer();
    11. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    12. if(player.getItemInHand().equals(is)){
    13. if(player.hasPermission("wand.snowballfire")){
    14. //Cooldown Stuff
    15. if(Snowballfirewandcooldown.containsKey(player.getName())){
    16. player.sendMessage(ChatColor.GOLD + "Woah there, You can't use this spell for another: " +
    17. ChatColor.RED + getTimeLeft(player, Snowballfirewandcooldown) + ChatColor.GOLD + " seconds!");
    18.  
    19. }else{
    20.  
    21. //I can set the Metadata, but I can not set the Velocity!
    22.  
    23. player.launchProjectile(Snowball.class).setMetadata("SnowballToFire", new FixedMetadataValue(p, true));
    24. }
    25. }else{
    26. player.sendMessage(deny);
    27. }
    28. }
    29. }
    30.  
    31.  
    32.  
    33.  
    34. }


    I tried doing this:

    Code:java
    1. Vector first = player.getLocation().getDirection().normalize().multiply(1);
    2.  
    3. Projectile ball = player.launchProjectile(Snowball.class);
    4. ball.setMetadata("SnowballToFire", new FixedMetadataValue(p, true));
    5. ball.setVelocity(first);


    But I have not found out a way to launch "ball".

    EDIT: Fixed Typo :p

    Thank you for reading!
    All help is much appreciated! :D
     
  2. Offline

    CoderCloud

    You could use the player.getWorld().spawnEntity(...) Method store the entity in a variable and set the velocity, location and metadata. To fire them in a cone you could rotate the vector and spawn a new entity at the same location, or a little bit away, so they cant interact with eachother (I would put them in a smal circle around the location where the cone should start).
     
  3. Offline

    XvBaseballkidvX

    CoderCloud I was able to fire the Snowball using my code, but I am having a problem while using the Vector. When I try to fire the snowball in a certain direction it will fire right in from of me then stop, and drop to the ground. Its very weird. I am not sure what is causing this. It will also start firing in one direction and then curve all of a sudden making a drastic change in direction.

    EDIT: Fixed Typo
     
  4. Offline

    monkeymanboy

    try using this instead
    Code:
    Snowball ball = event.getEntity().launchProjectile(Snowball.class);
     
  5. Offline

    XvBaseballkidvX

    Will do monkeymanboy ! Let me test that out really quick.

    monkeymanboy You can't do event.getEntity(); in a PlayerInteractEvent.
    It wont let me.

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

    monkeymanboy

    get player then
     
  7. Offline

    XvBaseballkidvX

  8. Offline

    CoderCloud

    XvBaseballkidvX
    Im using this in my Plugin:

    Code:java
    1.  
    2. Arrow arrow = SpawnEntityAtPlayer(e.getPlayer(), Arrow.class);
    3. arrow.setShooter(e.getPlayer());
    4. arrow.setVelocity(e.getPlayer().getEyeLocation().getDirection().multiply(2));
    5. arrow.setMetadata("ArrowType", new MyMetadata(this, "stick"));
    6.  


    Maybe its the getEyeLocation(). You could try to raise the speed of the snowball too.

    XvBaseballkidvX I would also recommend to not use schedulers for your cooldowns, for the following reasons:
    1. You cant get the exact time left for the cooldown (like 0.5)
    2. Threads take up more memory than most other methods would

    Im always using this system:
    Code:java
    1. HashMap<String, Long> cooldowns = new HashMap<>();
    2.  
    3. public float getTimeLeft(Player p, String cooldown) {
    4. String format = p.getName() + "." + cooldown;
    5. try {
    6. long l = cooldowns.get(format);
    7. long diff = l-System.currentTimeMillis();
    8. if(diff < 0)
    9. diff = 0;
    10. float left = diff/1000f;
    11. return left;
    12. } catch (NullPointerException e) {
    13. return 0f;
    14. }
    15. }
    16.  
    17. public boolean isOnCooldown(Player p, String cooldown) {
    18. return getTimeLeft(p, cooldown) != 0;
    19. }
    20.  
    21. public void setCooldown(Player p, String cooldown, float time) {
    22. String format = p.getName() + "." + cooldown;
    23. long l = System.currentTimeMillis() + (long) (time*1000);
    24. cooldowns.put(format, l);
    25. }


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

    XvBaseballkidvX

    CoderCloud Thank you so much! I am going to try that now!
     
  10. Offline

    CoderCloud

    XvBaseballkidvX
    If its not working i can give you the whole code of the plugin (its only one class).
     
Thread Status:
Not open for further replies.

Share This Page