Getting block infront of silverfish

Discussion in 'Spigot Plugin Development' started by AdamAdam, Jul 13, 2018.

  1. I'm trying to get the block infront of a silverfish but it refuses to work. I've found alot of diffrent threads for this but none seems to work.
    Any ideas?
     
  2. You can try this (not tested):
    Code (Text):

    public Block getBlockInFront(Entity entity) {
        BlockFace face = yawToFace(entity.getLocation().getYaw(), false);
        Block frontBlock = entity.getLocation().getBlock().getRelative(face);
        return frontBlock;
    }

    // source:
    // https://bukkit.org/threads/solved-player-direction.72789/ &
    // https://www.spigotmc.org/threads/player-get-facing-direction.173083/#post-1831629
    public BlockFace yawToFace(float yaw, boolean useSubCardinalDirections) {
        if (useSubCardinalDirections)
            return radial[Math.round(yaw / 45f) & 0x7].getOppositeFace();

        return axis[Math.round(yaw / 90f) & 0x3].getOppositeFace();
    }

    private static final BlockFace[] axis = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
    private static final BlockFace[] radial = { BlockFace.NORTH, BlockFace.NORTH_EAST, BlockFace.EAST, BlockFace.SOUTH_EAST, BlockFace.SOUTH, BlockFace.SOUTH_WEST, BlockFace.WEST, BlockFace.NORTH_WEST };
     
     
  3. Doesn't work :(, it just gives me the silverfishe's position not the one infront.
     
  4. get the direction the silverfish is looking, then do +1 block in that direction. From the you can do all kinds of stuff. Block type location etc.
     
  5. Praya

    Benefactor

    Code (Java):
    final BlockIterator blockIterator = new BlockIterator(varLivingEntity, distance);

    if (blockIterator.hasNext()) {
        final Block block = blockIterator.next();
        final Material material = block.getType();
       
        if (material.isSolid()) {
            return block;
        }
    }
     
  6. Could you please show me an example, that doesn't make any sense to me. I gotta add to a specefic axis, right?
     
  7. Still the same result, weird. I get the silverfishes position not the one infront. Could it potentially be something todo specefically with the silverfish entity type?
     
  8. Just tried out with the player, same result.
     
  9. Praya

    Benefactor

    try LivingEntity.getLineOfSight()
     
  10. Thanks! It worked.