Distance from a certain block?

Discussion in 'Plugin Development' started by kieranmclean, Dec 14, 2013.

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

    kieranmclean

    Hello.

    I was wondering could a Minecraft server get the distance from a player and a certain block and add a potion effect to the player? example. A player standing near a fire and gaining a regeneration potion effect.

    Thanks.
     
  2. Offline

    CubieX

    Loop over all online players and for each player use a cascaded "for" loop
    (one "for" for each coordinate dimension x, y, z)
    to check all blocks within a given distance from the players location for a fire block.
    And if you found one, apply the effect to the player.
    You probably want to do this within a repeating task to check it every second or so.
     
  3. Offline

    thepaperboy99

  4. Offline

    kieranmclean

    I understand I have to get the location of the player and loop over all online players I'm stuck on how to check the blocks... thepaperboy99 Thanks for the thread kind of helped.
    CubieX could you throw me an example of how I could check the blocks around the player?
     
  5. Offline

    jillheden

    By using the space diagonal you can check the distance from 1 point to another.
    [​IMG]
    The distance is from point A to B where for example the fire is B and the player is A.
    From the example image, the formula would look like so:
    Code:java
    1. float distance = Math.sqrt(CD^2+BC^2+AD^2)

    Where CD is the distance between C and D.

    Example 2: this is your positions:
    Code:
    Position A:
    x = 22
    y = 51
    z = 43
     
    Position B:
    x = 24
    y = 52
    z = 40
    Then the formula would look something like so:
    Code:java
    1. distance = Math.sqrt(
    2. Math.abs(Ax-Bx)^2
    3. +Math.abs(Ay-By)^2
    4. +Math.abs(Az-Bz)^2
    5. )

    Math.abs only forces the numbers positive. Ex: Math.abs(2) -> 2, Math.abs(-7) -> 7
    But with this example you end up with a distance of around 3.74 blocks (sqrt of 14 to be exact ;) )

    I have not tested this, but it should work.
     
  6. Offline

    The_Doctor_123

    kieranmclean
    Boy, you want to check all nearby blocks when players move? That's not a cheap operation.

    Schedule a repeating task to run every second, or so and check the surrounding blocks around each and every player. Here's some resources:
    - Block
    - Location
    - Player
     
    CubieX likes this.
  7. Offline

    kieranmclean

    Thanks guys for your help. Harder then I thought Hahah
     
  8. Offline

    CubieX

    I guess you do not need the exact distance between a player and a fire block.
    You just want to know if there is a fire block within a certain radius or area around the player, right?

    Here is a cascaded for-loop to check for fire in a given cuboid area around the player, like I mentioned above:
    Code:
    for (int checkedX = x1; checkedX <= x2; checkedX++)
          {
            for (int checkedY = y1; checkedY <= y2; checkedY++)
            {
                for (int checkedZ = z1; checkedZ <= z2; checkedZ++)
                {
                  // check if block at this location is a fire block. If so, do stuff and break the loop
                }
            }
          }
    x1, y1 and z1 are the coordinates of one cube corner and x2, y2 nd z2 are the coordinates of the adjacent corner.
    checkedX,Y,Z are the currently checked coordinates.
    You only have to set the corner coordinate values, so the cube will be centered around the player.
    If you want a spherical search area to really check a radius instead of a box, you need to do some math...
     
  9. Offline

    NathanWolf

    If you knew where these special blocks were (like its a block you place, not "every fire block") this could be doable. But yeah otherwise checking all blocks around every player anytime they move is going to get expensive!
     
  10. Offline

    CubieX

    A timer wth 1 second cycle to check that sould be sufficient, like The_Doctor_123 suggested.
    Of course the search radius should be kept as small as possible.
     
Thread Status:
Not open for further replies.

Share This Page