Make a player slowly fall?

Discussion in 'Spigot Plugin Development' started by xUbalubex, May 28, 2018.

  1. So, I am making a parachute, and when you right click it, it will make you slowly fall. I am confused on how to do it though.

    Code (Text):
    package me.uba;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {

        public void onEnable()
        {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("setParachuteItem"))
            {
                Player p = (Player) sender;
               
                ItemStack Parachute = p.getInventory().getItemInMainHand();
                ItemMeta meta = Parachute.getItemMeta();
                meta.setDisplayName(ChatColor.BLUE + "Parachute " + ChatColor.GREEN + "(Right Click)");
                meta = Parachute.getItemMeta();
               
                this.getConfig().set("Parachute", Parachute);
               
                p.sendMessage(ChatColor.GREEN + "Parachute Item Set");
                return true;
            }
            return false;
           
        }
        public void onPlayerUse(PlayerInteractEvent event){
            Player player = event.getPlayer();
       
            if(event.getAction() == Action.RIGHT_CLICK_AIR){
                if(player.getItemInHand().getType() == this.getConfig().get("Parachute")){
                   
                }
            }
        }
    }
     
  2. Praya

    Benefactor

    just set the y velocity
     
  3. Sorry, I haven't coded for a while and I forgot how to do this, could you explain it? You don't have to spoonfeed me but can you give me an example?
     
  4. Praya

    Benefactor

    ex you want to make the max fall speed = -0.5

    - get player
    - get velocity
    - if y velocity < -0.5
    - set the y velocity to -0.5
     
  5. Okay, so I think i am going in the right path. But I cannot figure out how to set the y velocity to -0.5
     
  6. Praya

    Benefactor

    Vector.setY(-0.5);
     
  7. Wait I don't get it. How do I do that? I can't just do Vector.setY(-0.5);
     
  8. I looked into it more. I do not know if this is correct but this is what I have.
    Code (Text):
        public void onPlayerUse(PlayerInteractEvent event){
            Player player = event.getPlayer();
           
       
            if(event.getAction() == Action.RIGHT_CLICK_AIR){
                if(player.getItemInHand().getType() == this.getConfig().get("Parachute")){
                    if(player.getVelocity().getY() < -0.5)
                    {
                        player.getLocation().setY(-0.5);
                    }
                }
            }
        }
     
  9. It's
    Code (Java):
    Player.setVelocity(Player.getVelocity.setY(-0,5));
     
  10. Praya

    Benefactor

    Code (Java):
    final Vector velocity = player.getVelocity();

    if (velocity.getY() < -0.5) {
        velocity.setY(-0.5);
        player.setVelocity(velocity);
    }
     
  11. Okay so I have
    Code (Text):
        public void onPlayerUse(PlayerInteractEvent event){
            Player player = event.getPlayer();
           
       
            if(event.getAction() == Action.RIGHT_CLICK_AIR){
                if(player.getInventory().getItemInMainHand() == this.getConfig().get("Parachute")){
                    if(player.getVelocity().getY() < -0.5)
                    {
                        player.setVelocity(player.getVelocity().setY(-0.5));
                        player.sendMessage("Parachute Enabled");
                    }
                }
            }
        }
    But it wont enable the parachute item.
    Code (Text):
    package me.uba;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {

        public void onEnable()
        {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("setParachuteItem"))
            {
                Player p = (Player) sender;
               
                ItemStack Parachute = p.getInventory().getItemInMainHand();
                ItemMeta meta = Parachute.getItemMeta();
                meta.setDisplayName(ChatColor.BLUE + "Parachute " + ChatColor.GREEN + "(Right Click)");
                meta = Parachute.getItemMeta();
               
                this.getConfig().set("Parachute", Parachute);
               
                p.sendMessage(ChatColor.GREEN + "Parachute Item Set");
                return true;
            }
            if(cmd.getName().equalsIgnoreCase("getParachute"))
            {
                ItemStack Parachute = this.getConfig().getItemStack("Parachute");
               
                Player p = (Player) sender;
               
                p.getInventory().setItemInMainHand(Parachute);
                p.sendMessage(ChatColor.GREEN + "Given Parachute!");
                return true;
            }
            return false;
           
        }
        public void onPlayerUse(PlayerInteractEvent event){
            Player player = event.getPlayer();
           
       
            if(event.getAction() == Action.RIGHT_CLICK_AIR){
                if(player.getInventory().getItemInMainHand() == this.getConfig().get("Parachute")){
                    if(player.getVelocity().getY() < -0.5)
                    {
                        player.setVelocity(player.getVelocity().setY(-0.5));
                        player.sendMessage("Parachute Enabled");
                    }
                }
            }
        }
    }
     
    This is my full code
     
  12. If your comparing item stacks you should use
    Code (Java):
    getItemStack("Parachute")
     
  13. I changed it but it still does not work.
    Code (Text):
    package me.uba;

    import net.md_5.bungee.api.ChatColor;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {

        public void onEnable()
        {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("setParachuteItem"))
            {
                Player p = (Player) sender;
               
                ItemStack Parachute = p.getInventory().getItemInMainHand();
                ItemMeta meta = Parachute.getItemMeta();
                meta.setDisplayName(ChatColor.BLUE + "Parachute " + ChatColor.GREEN + "(Right Click)");
                meta = Parachute.getItemMeta();
               
                this.getConfig().set("Parachute", Parachute);
               
                p.sendMessage(ChatColor.GREEN + "Parachute Item Set");
                return true;
            }
            if(cmd.getName().equalsIgnoreCase("getParachute"))
            {
                ItemStack Parachute = this.getConfig().getItemStack("Parachute");
               
                Player p = (Player) sender;
               
                p.getInventory().setItemInMainHand(Parachute);
                p.sendMessage(ChatColor.GREEN + "Given Parachute!");
                return true;
            }
            return false;
           
        }
        public void onPlayerUse(PlayerInteractEvent event){
            Player player = event.getPlayer();
           
       
            if(event.getAction() == Action.RIGHT_CLICK_AIR){
                if(player.getInventory().getItemInMainHand() == this.getConfig().getItemStack("Parachute")){
                    if(player.getVelocity().getY() < -0.5)
                    {
                        player.setVelocity(player.getVelocity().setY(-0.5));
                        player.sendMessage("Parachute Enabled");
                    }
                }
            }
        }
    }
     
     
  14. Hmm, I was thinking about spawing a chicken on top of you if you don't want to control the falling velocity and such.
     
  15. Well, I am making this for my friends Battleroyale server. and the item has a parachute model to it. So thats why I want it to make you parachute on right click.
     
  16. Why don't try to spawn an invinsible chicken and made the player ride it? (Or the opposite, can't remember). I remember a specific Parachute plugin did this.
     
    • Agree Agree x 1
  17. Strahan

    Benefactor

    This works for me:

    Code (Text):
    @EventHandler
    public void onPME(PlayerMoveEvent e) {
        if (e.getFrom().getBlockY() != e.getTo().getBlockY() && parachuteActive) {
            Vector v = e.getPlayer().getVelocity();
            if (v.getY() < -0.5) {
                v.setY(-0.5);
                e.getPlayer().setVelocity(v);
            }
        }
    }
    Still have to capture the damage event though and nullify it, as even if you make the player fall slowly the same damage occurs as if they fell naturally.
     
    • Agree Agree x 1
  18. Ride an invisible chicken ;)
     
    • Agree Agree x 1
    • Useful Useful x 1
  19. Strahan

    Benefactor

    Can you mute mobs? I wouldn't want a parachute that clucks, lol
     
    • Funny Funny x 2
    • Like Like x 1
  20. [​IMG]
     
    • Funny Funny x 1