Set Players Velocity for Parachute Effect

Discussion in 'Plugin Development' started by KungFuGoat, Mar 21, 2014.

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

    KungFuGoat

    Hello,

    How would I go about setting a players velocity to create a Parachute effect? I've done all the Checking if player is falling, even setting their Y velocity, but I want to set their X or Z to whatever direction they're looking at. So they'll fall, and slowly move in whatever direction they are facing.
     
  2. Offline

    Noxyro

    I would expect this to cause some problems, because gravity and falling is rendered client-side.

    But here you go:
    Code:java
    1. // Change the 1 to whatever you need, because 1 might be too strong
    2. player.setVelocity(new Vector(player.getVelocity().getX(), 1, player.getVelocity().getZ());
     
  3. Offline

    KungFuGoat

    Thanks, but it's just affecting my Y, my X and Z stay the same no matter where I look :p
     
  4. Offline

    Noxyro

    Normally this keeps the current X and Z velocity, but like I said this might be a bit glitchy, because it's rendered client-side.

    I'm doing some testing on my own server and will be back soon with results.
     
  5. Offline

    KungFuGoat

    Yup, I fall like I'm having a seizure or something
     
  6. Offline

    Noxyro

    Ok, I've coded a nice litte plugin that works perfectly on my localhost server - so a little bumping in online servers will be normal.

    Parachute.java
    Code:java
    1. public class Parachute extends JavaPlugin {
    2. private static Plugin instance;
    3.  
    4. // Saves the runnable tasks and also signals if a player has parachute active
    5. private static Map<String, Integer> parachuteMap = new HashMap<String, Integer>();
    6.  
    7. @Override
    8. public void onEnable() {
    9. instance = this;
    10. Bukkit.getPluginManager().registerEvents(new ParachuteListener(), this);
    11. }
    12.  
    13. @Override
    14. public void onDisable() {
    15. // Making sure every task is cancelled and deleted ... even if it's not necessary :D
    16.  
    17. for (int id : parachuteMap.values()) {
    18. Bukkit.getScheduler().cancelTask(id);
    19. }
    20.  
    21. parachuteMap.clear();
    22. }
    23.  
    24. public static Plugin getInstance() {
    25. return instance;
    26. }
    27.  
    28. // Static call from ParachuteListener's PlayerInteractEvent handler
    29. public static void onPlayerInteract(PlayerInteractEvent e) {
    30. // Checking right click
    31. if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    32. // Checking for a specific item... I used blaze rod for testing
    33. if (e.getPlayer().getItemInHand().getType().equals(Material.BLAZE_ROD)) {
    34. // Create final Player variable which could be needed in Runnable (thats why it's final)
    35. final Player player = e.getPlayer();
    36. // Checking the map for an existing entry (means parachute is already active)
    37. if (parachuteMap.containsKey(player.getName())) {
    38. // Cancel task and remove player from map (means parachute effect will not be active any longer)
    39. Bukkit.getScheduler().cancelTask(parachuteMap.get(player.getName()));
    40. parachuteMap.remove(player.getName());
    41. } else {
    42. // Player had parachute not active so we create an entry in the map
    43. parachuteMap.put(player.getName(),
    44. // If you did not know: scheduling a task returns an integer, that's why we could write it directly into the .put()
    45. Bukkit.getScheduler().scheduleSyncRepeatingTask(getInstance(), new BukkitRunnable() {
    46. @Override
    47. public void run() {
    48. // Check if players is even online (because it's a final variable)
    49. if (player.isOnline()) {
    50. // Check if the player is over an specific speed 0 = "zero gravity", 0.0001 = DAMN SLOW fall, 0.1 = very slow fall, 0.2 = slow fall ... and so on
    51. if (player.getVelocity().getY() < -0.2) {
    52. // Keep X and Z movement of the player and dampen his fall in a smooth way
    53. player.setVelocity(new Vector(player.getVelocity().getX(), player.getVelocity().getY()/1.25, player.getVelocity().getZ()));
    54. // Don't forget to reduce fall distance as well or else, even if the player is falling slow he will crush on the floor with a 32 block or so
    55. player.setFallDistance(player.getFallDistance()/1.25F);
    56. // Checks if the player is too slow for a parachute (if he only jumps 1 block)
    57. } else if (player.getVelocity().getY() > -0.15) {
    58. // Remove his parachute immediately
    59. Bukkit.getScheduler().cancelTask(parachuteMap.get(player.getName()));
    60. parachuteMap.remove(player.getName());
    61. }
    62. // If player is not online
    63. } else {
    64. // Remove his entries
    65. Bukkit.getScheduler().cancelTask(parachuteMap.get(player.getName()));
    66. parachuteMap.remove(player.getName());
    67. }
    68. }
    69. }, 0L, 0L)
    70. );
    71. }
    72. }
    73. }
    74. }
    75. }


    ParachuteListener.java
    Code:java
    1. public class ParachuteListener implements Listener {
    2.  
    3. @EventHandler
    4. public void onPlayerInteract(PlayerInteractEvent e) {
    5. Parachute.onPlayerInteract(e);
    6. }
    7. }
     
  7. Offline

    KungFuGoat

    Thank you, I appreciate it :D

    I can work with this :)
     
Thread Status:
Not open for further replies.

Share This Page