1.15.2 Take no fall damage when turning off /fly

Discussion in 'Spigot Plugin Development' started by xFadedxShadow, Feb 7, 2020.

  1. Code (Text):
    package xFadedxShadow.Essentials.Commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import xFadedxShadow.Essentials.Main;
    import xFadedxShadow.Essentials.Utils.Utils;

    public class FlyCommand implements CommandExecutor {

        private Main plugin;

        public FlyCommand(Main plugin) {
            this.plugin = plugin;
            plugin.getCommand("fly").setExecutor(this);
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(Utils.chat(plugin.getConfig().getString("permission_error_message")));
                return true;
            }

            Player p = (Player) sender;

            if (p.hasPermission("faded-essentials.fly")) {
                if (!(p.getAllowFlight() == true)) {
                    p.setAllowFlight(true);
                    p.sendMessage(Utils.chat(plugin.getConfig().getString("FlyCommand.flying_enabled_message")));
                    return true;
                } else {
                    p.setFlying(false);
                    p.setAllowFlight(false);
                    p.sendMessage(Utils.chat(plugin.getConfig().getString("FlyCommand.flying_disabled_message")));
                    return true;
                }
            } else {
                p.sendMessage(Utils.chat(plugin.getConfig().getString("permission_error_message")));
            }

            return false;
        }

    }
     
     
  2. I want the player not to take fall damage after they disable /fly but only one time
     
  3. Store player's UUID in Set(or some other way, like metadata or persistentdata). When a player takes fall damage - check if(set.remove(uuid)), then just cancel event.
     
    • Agree Agree x 1
  4. I recomend you changing
    Code (Java):
    if (!(p.getAllowFlight() == true)) {
    for
    Code (Java):
    if (!p.getAllowFlight()) {
     
  5. Could you show me an example possibly I'm pretty new to Plugin Development
     
  6. Something Like this? Then use a Hasmap or something?


    Code (Text):
    if(p.getUniqueId().equals(uuid) {
        return p;
    }
     
  7. Declare Set variable(near your private Main plugin;), like private Set<UUID> hadFlight;
    Initialize it in constructor - hadFlight = new HashSet<>();.
    When player turns fly on, add its UUID to set - hadFlight.add(p.getUniqueId());
    Finally, on EntityDamageEvent with player and fall checks, check for player's UUID in set and cancel event if player was flying.
    Code (Java):
    if(hadFlight.remove(player.getUniqueId()))
        e.setCancelled(true);
     
  8. In the class that extends JavaPlugin create a
    Code (Java):
    Set<UUID> flyingPlayers= LinkedHashSet<>();
    Then make a getter for that variable
    Code (Java):
    public Set<UUID> getFlyingPlayers(){
      return name;
    }
    Now when a player activates the fly use
    Code (Java):
    YourMainClassInstance.getFlyingPlayers().add(Player.getUniqueID());
    Now create a EntityDamageEvent and check if the damage type is DamageCause.FALL and if the damaged is a player
    Then, use
    Code (Java):
    if(YourMainClassInstance.getFlyingPlayers().contains(Player.getUniqueID()) && !p.getAllowFlight()){
       e.setCancelled(true);
       YourMainClassInstance.getFlyingPlayers().remove(Player.getUniqueID());
    };
    Now when a player that has stoped flying will not recive fall damage the first time they deactivate the flying mode
     
  9. Set.remove returns the object removed or null if the set didn't had the object
    Code (Text):
    hadFlight.remove(p.getUniqueId()) == null
    will be true if the set contained the player UUID and false if it didn't
     
  10. Pretty sure it's about Maps, not Sets. For Sets it has no sense just because you already have the object which you want to remove from set.
     
  11. Strahan

    Benefactor

    Just remember if you do /restart any players in flight will no longer be protected unless you log the collection to config and reload when it comes back up.