Checking if a player is standing on a block.

Discussion in 'Plugin Development' started by killgoblen, Jun 23, 2011.

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

    killgoblen

    I am making a teleporting mod where players will simply have to stand on top of a certain block to be teleported. I thought about doing this with onPlayerMove, checking their location, then comparing it with the block's location. However, this seems incredibly inefficient, as it would constantly check every player's position and compare it to every block that's set up as a teleporter. Is there a more efficient way to do this?

    Thanks for any help!
     
  2. Offline

    WinSock

    Code:
    public class listener extends PlayerListener {
        Map<Block, Location> teleportBlocks = new HashMap<Block, Location>(); // A Set to contain all of the blocks that act as teleporters
    
        @Override
        public void onPlayerMove(PlayerMoveEvent event) {
            Block below = event.getPlayer().getWorld().getBlockAt(event.getPlayer().getLocation().getBlockX(), event.getPlayer().getLocation().getBlockY() - 1, event.getPlayer().getLocation().getBlockZ());
            if (teleportBlocks.containsKey(below)) {
                event.getPlayer().teleport(teleportBlocks.get(below));
            }
        }
    }
     
  3. Offline

    matejdro

    What is wrong with location.GetBlock()? It would make this much shorter.
     
  4. Offline

    nisovin

    That would get the block the player is standing in, not the one they're standing on.
     
  5. Offline

    fossil58

    location.subtract(0,1,0);
    For the block below?
     
  6. Offline

    Weltall 7

    what about:
    Code:
    event.getPlayer().getLocation().getBlock().getFace(BlockFace.DOWN)
     
  7. Offline

    zonedabone

    Well, the players head is in one block. The block at their legs is below that, and the one they stand on is below that.

    Code:
    event.getPlayer().getLocation().subract(0,2,0).getBlock();
     
  8. Offline

    matejdro

    event.getPlayer().getLocation() returns leg location.
     
Thread Status:
Not open for further replies.

Share This Page