1.15.2 Help please!

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

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

    import java.util.HashSet;
    import java.util.Set;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;

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

    public class FlyCommand implements CommandExecutor, Listener {

        private Main plugin;
        private Set<UUID> hadFlight;

        public FlyCommand(Main plugin) {
            this.plugin = plugin;
            hadFlight = new HashSet<>();
            plugin.getCommand("fly").setExecutor(this);

            Bukkit.getPluginManager().registerEvents(this, plugin);
        }

        @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()) {
                    p.setAllowFlight(true);
                    p.sendMessage(Utils.chat(plugin.getConfig().getString("FlyCommand.flying_enabled_message")));
                    return true;
                } else {
                    float fallDistance = p.getFallDistance();
                    if (fallDistance >= 4) {
                        hadFlight.add(p.getUniqueId());
                        p.setFlying(false);
                        p.setAllowFlight(false);
                        p.sendMessage(Utils.chat(plugin.getConfig().getString("FlyCommand.flying_disabled_message")));
                        return true;
                    } else {
                        hadFlight.remove(p.getUniqueId());
                        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;

        }

        @EventHandler
        public void onFallDamage(EntityDamageEvent e) {
            if (e.getEntity() instanceof Player) {
                Player p = (Player) e.getEntity();
                if (e.getCause() == DamageCause.FALL) {
                    if (hadFlight.contains(p.getUniqueId())) {
                        e.setCancelled(true);
                        hadFlight.remove(p.getUniqueId());
                    }
                }
            }

        }

    }
     
     
  2. Pretty useless there - player didn't fall yet. Also I think it's better to store UUID when player turns fly on - player can lose ability to fly for some reasons without using command (eg third-party plugin)
     
  3. Hi, What do you need help with? can you explain a bit more?
     
  4. Strahan

    Benefactor

    We're not mind readers; what's the problem?

    Also, you can be more efficient with your design. A lot of that duplication can be removed. Plus you know you can just do:
    Code (Text):
    p.setAllowFlight(!p.getAllowFlight());
    if (p.getAllowFlight()) {
      // tell them they have flight
      return true
    }

    //do flight disable bit
    A bit cleaner.