Solved Getting a block directly behind a player

Discussion in 'Plugin Development' started by Coopah, Dec 20, 2015.

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

    Coopah

    How would I check if a player has their back touching a block?
     
  2. Offline

    Zombie_Striker

    @Coopah
    You would have to find out which way the player is looking (Yaw) and then find a way to find the block behind him using that data.
     
  3. Offline

    Coopah

    @Zombie_Striker
    I got their eye location and tried to multiple it by a negative to get their opposite, I then got the block. I'm clearly missing a step because this didn't work aha. Any ideas?
     
  4. Code:
    Block getBlockDirectlyBehindPlayer (Player player) {
           
            String playerDirection;
            float yaw = player.getLocation().getYaw();
            World w = player.getWorld();
            int x = player.getLocation().getBlockX();
            int y = player.getLocation().getBlockY();
            int z = player.getLocation().getBlockZ();
           
            if (yaw < 0) {
                yaw = yaw + 360;
            }
           
            if ( (yaw >= 315) && (yaw <= 360) ) {
                playerDirection = "south";
            } else if ( (yaw >= 0) && (yaw <= 45) ) {
                playerDirection = "south";
            } else if ( (yaw >= 45) && (yaw <= 135) ) {
                playerDirection = "west";
            } else if ( (yaw >= 135) && (yaw <= 180) ) {
                playerDirection = "north";
            } else if ( (yaw >= 180) && (yaw <= 225) ) {
                playerDirection = "north";
            } else if ( (yaw >= 225) && (yaw <= 315) ) {
                playerDirection = "east";
            } else {
                playerDirection = "east";
            }
           
            if (playerDirection == "north") {
                z = z+1;
            } else if (playerDirection == "east") {
                x = x-1;
            } else if (playerDirection == "south") {
                z = z-1;
            } else if (playerDirection == "west") {
                x = x+1;
            }
           
            Block b = w.getBlockAt(x, y, z);
           
            return b;
           
    }
     
    Coopah and Zombie_Striker like this.
  5. Offline

    Coopah

  6. This seems inefficient in my head...
    Also, don't ever check Object (String) equality with ==, always use .equals...
    Code:
            if (playerDirection.equals("north")) {
                z = z+1;
            } else if (playerDirection.equals("east")) {
                x = x-1;
            } else if (playerDirection.equals("south")) {
                z = z-1;
            } else if (playerDirection.equals("west")) {
                x = x+1;
            }
    == checks reference equality, which, most of the case is the same for Strings since JVM tries to keep them at the same place in the memory, but it's not ALWAYS the case, which leads to some fun bugs on occasion :)
    http://stackoverflow.com/questions/7520432/java-vs-equals-confusion

    @Coopah
    I would do this, personally:
    Code:
        private Block getBlockDirectlyBehindPlayer(Player player) {
            // Won't get NPEs from me.
            if (player == null) {
                return null;
            }
            // Get player's location, but at head height. Add 1 to y.
            Location location = player.getLocation().add(0, 1, 0);
           // Get the player's direction and invert it on the x and z axis to get the opposite direction.
            Vector direction = location.getDirection().multiply(new Vector(-1, 0, -1));
    
           // Return the block at the location opposite of the direction the player is looking, 1 block forward.
            return player.getWorld().getBlockAt(location.add(direction));
        }
    Tahg me if you have any questions about how/why this code works :)
     
    Last edited: Dec 21, 2015
    Coopah likes this.
Thread Status:
Not open for further replies.

Share This Page