Teleport to nearest airblock

Discussion in 'Plugin Development' started by Nostos, Jan 23, 2018.

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

    Nostos

    so I usually don't post problems because i figure them out eventually but I'm too lazy currently lol anyways my question is how do I teleport to the nearest air block above the player .here's code so you know I'm trying:
    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player player = e.getPlayer();
            Location loc0 = player.getLocation();
            World overworld = Bukkit.getServer().getWorld("World");
            Block loc2 = overworld.getBlockAt(loc0);
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
        if (e.getClickedBlock().getType() == Material.WALL_SIGN || e.getClickedBlock().getType() == Material.SIGN_POST) {
        Sign s = (Sign) e.getClickedBlock().getState();
        if (s.getLine(0).equalsIgnoreCase("[up]")) {
           
            float sety = player.getWorld().getHighestBlockAt(loc0).getY();
            float setz = player.getWorld().getHighestBlockAt(loc0).getZ();
            float setx = player.getWorld().getHighestBlockAt(loc0).getX();
            float findy = (float) player.getLocation().getY();
           
            player.teleport(new Location (Bukkit.getWorld("World"), setx, sety, setz));
                    }
                }
            }
        }
    would be nice if I knew how to do down as well...
     
  2. Offline

    ipodtouch0218

    By "nearest air block" you probably mean on top of the lowest safe solid block?

    1. Loop through all blocks vertically starting from the player's Y value, all the way to 256 (Minecraft Height Limit).
    2. Check if the block is either solid (Uses Craftbukkit) or != Material.AIR
    2a. If no block is found, return without teleporting the player. Maybe display a message or something.
    3. Check if the two blocks above the first block are also solid. If not, keep looking upwards. (Prevents suffocation)
    4. Teleport the player one block above the block (so they don't fall though the block)
     
  3. Offline

    Nostos

    okay but uh well looping is just something i havn't gotten the hang of any help on that?
     
  4. Offline

    Machine Maker

    You can either use a for loop or a while loop (I recommend for). If you don't know how to use those I recommend looking at some java tutorials first. For a for loop, get the players height and the max world height and loops through integers between those numbers. check the block at that y level's type. if its air, check the block above it. if thats air, then there is a 2 block high air gap for a player.
     
Thread Status:
Not open for further replies.

Share This Page