Launch Players into the air

Discussion in 'Plugin Development' started by iKeirNez, Feb 19, 2012.

Thread Status:
Not open for further replies.
  1. I am currently coding a Plugin and one of the features will launch players into the air.

    What code do I need to do this?

    Keir
     
  2. this will send players 5 blocks up:
    in a onCommand:
    Player p = (Player)sender;
    Location loc = p.getLocation();
    loc.setY(loc.getY() + 5);
    p.teleport(loc);

    in a event:
    Player p = (Player) event.getPlayer();
    Location loc = p.getLocation();
    loc.setY(loc.getY() + 5);
    p.teleport(loc);
     
  3. Will that have the effect of them being launched though? I don't want them just to tp up high and then fall.

    I want it to look kinda like the player has just been launched out of a cannon.

    Keir
     
  4. Offline

    desht

    No, that will just teleport the player up by 5 blocks. This will do what you need:
    Code:java
    1.  
    2. player.setVelocity(new Vector(x, y, z));
    3.  

    where x, y, z is the new velocity for the player. A Vector of (0, 0.4, 0) is roughly equivalent to a normal jump, straight upwards.

    If you're simulating a cannon, you'll need to do some trigonometry, based on the cannon's pitch (up/down) and yaw (left/right). The good news is that Bukkit can do this for you already, with the Location class - plug your pitch and yaw into a Location object, and get the direction vector out with getDirection(). You can then increase the vector as needed with the Vector multiply() method, and apply the new Vector back to the player (or whatever entity) with setVelocity(), as above.

    The following code will catapult the player with significant force in the direction they're currently facing (some kind of fall damage prevention is recommended :) ):
    Code:java
    1.  
    2. Vector dir = player.getLocation().getDirection());
    3. player.setVelocity(dir.multiply(10));
    4.  
     
  5. I'll try this out, it is a fun command I am working on that admins can use, they can do /launch <player> and it'll launch them straight up. Also is there any code I can use to only prevent fall damage and no other form of damage.

    Thanks
    Keir
     
  6. Offline

    desht

    Just set up a handler for EntityDamageEvent, and check if 1) the entity is a player, 2) the damage cause is FALL, and 3) the player was recently launched.

    (1) and (2) are trivial. For (3) you'll need to track the last time a player was launched - I suggest a Map<String,Long>, mapping player name to time of launch - update that when you launch a player. Then compare that with the current time in the EntityDamageEvent listener to see if the last launch was within a predefined period (1 second maybe?).
     
  7. I could make a boolean and set it to false, then on player launch it would be set to true. The player damage event would listen for fall damage events, if the boolean is set to true it would cancel then event and then reset the boolean to false.

    I'll try this as soon as I get home and post the code here if it suceeds.

    Keir
     
  8. Offline

    Shamebot

  9. Offline

    desht

    OK, if you take that approach, it would be better to just have a Set<String> of player names who've been launched. Add the player to the set when you launch him & check for the player name in the set when you're in the event handler.
    Good point.
     
  10. Offline

    BloodShura

    player.setVelocity(new Vector(0, 64, 0));

    End.
     
  11. Offline

    Shamebot

    Did you read what desht and I said?
     
    Darkman2412 likes this.
  12. Offline

    BloodShura

    No. I don't read answers, I reply what you say in topic. Well, if you don't want to use what I give you and ignore, no problem.

    This don't kick the players or anything else. Ah, and to the player don't take fall damage, use:

    player.setFallDistance(-100.0F);

    Just don't be ignorant with who are trying to help you.
     
  13. Thanks everyone for your replies. BloodShura so I can do this

    player.setVelocity(...
    player.setFallDistance(...

    and that will launch them and stop them from being kicked for flying etc?

    Keir
     
  14. Offline

    BloodShura

  15. Awesome I will try that out! Will that automatically start giving players fall damage again once they land on the ground?

    I would test this myself but I am currently not at my coding pc.

    Keir
     
  16. Offline

    BloodShura

    Yes. I don't know very hell how setFallDistance works, but for me, I use the setVelocity with vector 64, and the setFallDistance(-100.0F). It doesn't give the damage of this fall, but after this fall, all the other ones will do normal damage.

    /Sorry for my bad english.
     
  17. Thanks you for confirming this, i'll test it just now.

    Keir

    BloodShura I have done the setFallDistance and after being launched and I don't take any damage for about a minute, then if I jump off a really high building I barely take any damage and slowly after a few minutes I start taking more and more damage.

    I believe we need to find another way, I will start looking at Shamebot 's code.

    Keir

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  18. Offline

    BloodShura

    I think the FallDistance, will set no damage for "his value" vectors. Try setFallDistance(-64.0F), because the vector is 64.

    If it don't works, tell me, I have another way to work with this, but first, try what I say above.
     
  19. Ok I will, also I have an issue launching other users other than myself although I'll create another topic for that later.

    Thanks
    Keir
     
Thread Status:
Not open for further replies.

Share This Page