Toggling Player Flight Safely

Discussion in 'Spigot Plugin Development' started by Spedwards, May 31, 2016.

  1. I'm working on a plugin, and I need to remove flight from a player but I need to do it safely. I had originally thought that just using `player.setAllowFlight(false);` would allow them to fly until they toggled it, but after testing it I realised that isn't the case.

    I created a method that would teleport the player to the highest block below them, but in testing it teleported me to a lava pool below ground.

    Code (Text):

    Location loc = player.getLocation();
    int X = loc.getBlockX();
    int Y = loc.getBlockY();
    int Z = loc.getBlockZ();
    if (loc.getBlock().getType() == Material.AIR) {
        for (int i = Y; i > 1; i--) {
            Location newLoc = new Location(loc.getWorld(), X, i, Z);
            if (newLoc.getBlock().getType() == Material.AIR) {
            } else {
                player.teleport(new Location(loc.getWorld(), X, i + 1, Z));
    Could someone please explain what's wrong with that method, or provide a better method?
  2. There's nothing wrong with the method. It saw that the block was not air so it teleported you there. If you want to specify no lava then you need to include that in your code. Code itself doesn't know what you want, you need to explicitly write it for it to happen.
  3. I was above ground. The lava pool was in a cave. Something's not right with it as it should have only teleported me 4 blocks down.
  4. You never stop your loop once you find a suitable place to teleport the player. Essentially you are really rapidly teleporting the player to all of the landing blocks until they end up at the lowest one.
    • Agree Agree x 1
  5. Ahh! Good catch! Thank you, good sir.
    • Like Like x 1