Player nearby a block

Discussion in 'Plugin Development' started by Josh014, Dec 8, 2013.

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

    Josh014

    Hey,
    How can I check if a player is nearby a specific block (Podzol)?
    I tried using getNearbyEntities(x,y,z); but that only works for an entity...

    Thank you,
     
  2. Offline

    amhokies

    The Location class has a distance method to get the distance between two locations.
     
  3. Offline

    i3ick

    The Player is an entity.
     
  4. Offline

    DarkBladee12

    Josh014 That's the method I use for checking if a player is near a block:

    Code:java
    1. public static boolean isNearBlock(Player p, int range, Material mat) {
    2. Location loc = p.getLocation();
    3. World world = p.getWorld();
    4. double cX = loc.getX();
    5. double cY = loc.getY();
    6. double cZ = loc.getZ();
    7. double bPow = Math.pow(range + 0.5D, 2);
    8. double xPow;
    9. double zPow;
    10. for (int z = -range; z <= range; z++) {
    11. zPow = Math.pow(z, 2);
    12. for (int x = -range; x <= range; x++) {
    13. xPow = Math.pow(x, 2);
    14. for (int y = -range; y <= range; y++)
    15. if ((xPow + Math.pow(y, 2) + zPow) <= bPow)
    16. if (world.getBlockAt((int) (cX + x), (int) (cY + y), (int) (cZ + z)).getType() == mat)
    17. return true;
    18. }
    19. }
    20. return false;
    21. }
     
  5. Offline

    Josh014

    DarkBladee12
    :confused:, How can I give players potion effects with this? Oh and when does this trigger? Only when a player is nearby a a block? Can you make it more like a pseudo code so I can understand a bit about what you've done?
     
  6. Offline

    DarkBladee12

    Josh014 Isn't that clear what that is? It's just a method that checks if a player is nearby a block with the specified material, so it doesn't trigger itself at all... It depends on where you want to check it, so if you want to check when the player moves if he is near such a block just use the PlayerMoveEvent.
     
  7. Offline

    Josh014

    DarkBlade12
    How can I trigger it? Like, do I need to copy this and place it in PlayerMoveEvent?

    Edit:
    I have never used this before, sorry.
     
  8. Offline

    DarkBladee12

    Josh014 So you never make own methods, huh? Of course you have to copy it into one of your classes and call the method with isNearBlock(PLAYER, RANGE, MATERIAL);.
     
  9. Offline

    Josh014

    DarkBladee12
    I used it a few times. Btw is the range around the block? If it is, is it possible to make it only go high up in the sky/ground?
     
  10. Offline

    DarkBladee12

    Josh014 It makes a sphere with the desired range about the player and checks if inside the sphere is a block with the specified material. Yeah it's possible to only check if the block with this material is somewhere beneath or above the player.
     
  11. Offline

    Josh014

    DarkBladee12
    Alright I got it to work, but how can I do it that it only goes high up and in the ground? Or just that I can modify the x,y,z of the range?

    Edit:
    How can I check it with the Matrial Podzol?
     
  12. Offline

    Josh014

  13. Offline

    Josh014

  14. Offline

    Josh014

    Bump ^^
     
  15. Offline

    Josh014

    Someone?
     
  16. Offline

    viper_monster

    Josh014 What doesn't work with @DarkBladee12's method?

    EDIT: Oh, sry, I didn't read the whole post.
     
  17. Offline

    Josh014

    spoljo666 it works fine, but I want that the radius goes in a box around it and that you can configure the height/width of the box. Instead of a sphere.
     
  18. Offline

    Josh014

    Bump...
     
Thread Status:
Not open for further replies.

Share This Page