Check if a player is on a edge of a block

Discussion in 'Plugin Development' started by Aljed_The_Legit, Nov 3, 2013.

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

    Aljed_The_Legit

    Hi, I have a problem for my plugin, how do I check if a player is on an edge of a block?
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            TNTRunArena arena = plugin.arenas.get(plugin.playerArena.get(player));
            if (plugin.playerArena.get(player) != null) {
                if (arena.getTimer() != null && arena.getTimer() < 0) {
                    Location playerLocation = player.getLocation();
                    World playerWorld = player.getWorld();   
                    int x = playerLocation.getBlockX();
                    int y = playerLocation.getBlockY() - 1;
                    int z = playerLocation.getBlockZ();
                    int x2 = playerLocation.getBlockX();
                    int y2 = playerLocation.getBlockY() - 2;
                    int z2 = playerLocation.getBlockZ();
                    Location location = new Location(playerWorld, x, y, z);
                    Location location2 = new Location(playerWorld, x2, y2, z2);
                    if (location.getBlock().getType().isSolid() && location2.getBlock().getType().isSolid()) {
                        arena.getBlocks().put(location, location.getBlock().getState());
                        arena.getBlocks().put(location2, location2.getBlock().getState());
                        location.getBlock().setType(Material.AIR);
                        location2.getBlock().setType(Material.AIR);
                        return;
                    }
                   
                    if (playerLocation.getY() < 1) {
                        arena.getPlayers().remove(player);
                        plugin.playerArena.remove(player);
                        player.sendMessage("§7[§bTNTRun§7] §bYou fell out of the arena!");
                        player.setFallDistance(0);
                        player.teleport(arena.getEnd());
                        for (Player players : arena.getPlayers().keySet()) {
                            players.sendMessage("§7[§bTNTRun§7] §bPlayer §f" + player.getName() + " §bfell out of the arena!");
                        }
                    }
                    if (arena.getPlayers().size() == 1) {
                        arena.endGame();
                        plugin.playerArena.remove(player);
                        player.sendMessage("§7[§bTNTRun§7] §bPlayer §f" + player.getName() + " §bwon on the arena §f" + arena.getName() + "§b!");
                    }
                }
            }
    Bump, please help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. Offline

    xTrollxDudex

    Aljed_The_Legit
    PHP:
    if(e.getTo().getZ() - e.getTo().getBlockZ() > 0.5 /*or whatever value suits you*/ || e.getTo().getBlockZ() - e.getTo().getZ() > 0.5 /*check for X's, don't check for Y*/) {
        
    //do stuff
    }
     
  3. Offline

    Aljed_The_Legit

    So I do the exact samething just with X?
     

  4. Yep. You could condense it down to this:

    Code:java
    1. if(Math.abs(e.getTo().getZ() - e.getTo().getBlockZ()) > 0.5 /*or whatever value suits you*/ || Math.abs(e.getTo().getX() - e.getTo().getBlockX()) > 0.5 {
    2. //do stuff
    3. }


    Using the absolute value function lets you make one check instead of two.
     
  5. Offline

    Aljed_The_Legit

    What value should I use?
     

  6. I'm pretty sure that 0.5 means that the player has to be sneaking over the edge of the block to be detected by this check. You'll have to experiment with it until you get what you need.
     
  7. Offline

    Aljed_The_Legit

    This is what I have:
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            TNTRunArena arena = plugin.arenas.get(plugin.playerArena.get(player));
            if (plugin.playerArena.get(player) != null) {
                if (arena.getTimer() != null && arena.getTimer() < 0) {
                    Location playerLocation = player.getLocation();
                    World playerWorld = player.getWorld(); 
                    int x = playerLocation.getBlockX();
                    int y = playerLocation.getBlockY() - 1;
                    int z = playerLocation.getBlockZ();
                    int x2 = playerLocation.getBlockX();
                    int y2 = playerLocation.getBlockY() - 2;
                    int z2 = playerLocation.getBlockZ();
                    Location location = new Location(playerWorld, x, y, z);
                    Location location2 = new Location(playerWorld, x2, y2, z2);
                    if (location.getBlock().getType().isSolid() && location2.getBlock().getType().isSolid()) {
                        if (Math.abs(location.getZ() - location.getBlockZ()) > 0.5 || Math.abs(location.getX() - location.getBlockX()) > 0.5 && Math.abs(location2.getZ() - location2.getBlockZ()) > 0.5 || Math.abs(location2.getX() - location2.getBlockX()) > 0.5) {
                            arena.getBlocks().put(location, location.getBlock().getState());
                            arena.getBlocks().put(location2, location2.getBlock().getState());
                            location.getBlock().setType(Material.AIR);
                            location2.getBlock().setType(Material.AIR);
                        }
                    }
                    if (playerLocation.getY() < 1) {
                        arena.getPlayers().remove(player);
                        plugin.playerArena.remove(player);
                        player.sendMessage("§7[§bTNTRun§7] §bYou fell out of the arena!");
                        player.setFallDistance(0);
                        player.teleport(arena.getEnd());
                        for (Player players : arena.getPlayers().keySet()) {
                            players.sendMessage("§7[§bTNTRun§7] §bPlayer §f" + player.getName() + " §bfell out of the arena!");
                        }
                        arena.endGame();
                    }
                }
            }
        }
    But it's still not working...
    And how do I check if a player is not moving?

    Bump...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  8. Offline

    Shevchik

    You can use a function from my tntrun that returns first non air block under player feet.
     
  9. Offline

    Aljed_The_Legit

    I tried looking, I just don't understand it... ;(
     
Thread Status:
Not open for further replies.

Share This Page