Solved Get nearest wood block

Discussion in 'Plugin Development' started by hawkfalcon, Sep 29, 2012.

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

    hawkfalcon

    Anybody have a method they wish to share so I can get the nearest wood block? It needs to be in the same y axis as the starting block.

    Nobody? Wow.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  2. Offline

    chaseoes

    Something like this?

    if (startinglocation.getBlock().getType() == Material.WOOD && startinglocation.add(0, 1, 0).getBlock().getType == Material.WOOD) {
     
    hawkfalcon likes this.
  3. Offline

    hawkfalcon

    Yeah, I guess I'll have to do it this way. :3
    I was hoping somebody had a class similar to this.
     
  4. Offline

    Icyene

    I use this in some of my plugins:

    Code:Java
    1.  
    2. public boolean isLocationNearBlock(Location loc, List<Integer> blocks, int radius) {
    3.  
    4. final World world = loc.getWorld();
    5. for (int y = 1; y > -radius; y--) {
    6. for (int x = 1; x > -radius; x--) {
    7. for (int z = 1; z > -radius; z--) {
    8. Block scan = world
    9. .getBlockAt(
    10. (int) loc
    11. .getX()
    12. + x,
    13. (int) loc
    14. .getY()
    15. + y,
    16. (int) loc
    17. .getZ()
    18. + z);
    19. if (blocks.contains(scan
    20. .getTypeId())) {
    21. return true;
    22. }
    23. }
    24. }
    25. }
    26.  
    27. return false;
    28. }
    29.  


    Code is pretty self-explainable. You would call it as:

    Code:Java
    1.  
    2. isLocationNearBlock(Location, new ArrayList<Integer>() {{
    3. add(Block.WOOD.id);
    4. }}, radius);
    5.  


    Since the search gets quite intensive with a large radius, you could only check if the block exists on the y level of the start location. Once you find a wood block (e.g. from a tree), you could run a recursive algorithm to detect all wood blocks attached to that tree, effectively getting all the blocks in the tree.

    Hope that helped!
     
    Maulss, hawkfalcon and Courier like this.
  5. Offline

    Courier

    This is good, but a few things could be better.

    A Set<Block> (specifically, HashSet<Block>) would be much better than a List<Block>.

    Also, you are calling loc.get_() and casting it to an int way more often than you need to. You should only do it once, and store int x, y, z at the beginning of the method.

    Here is what I would do to solve his original problem:
    Code:java
    1. /**
    2.  * Finds the closest block in a vertical column.
    3.  * @param origin The location around which to search.
    4.  * This location will NOT be included in the search, but all other locations in the column will.
    5.  * @param types A Set (preferably a HashSet) that contains the type IDs of blocks to search for
    6.  * @return The closest block, or null if one was not found in the column.
    7.  * In the case of a tie, the higher block wins.
    8.  */
    9. public static Block closestBlock(Location origin, Set<Integer> types)
    10. {
    11. int x = origin.getBlockX();
    12. int y = origin.getBlockY();
    13. int z = origin.getBlockZ();
    14. World world = origin.getWorld();
    15.  
    16. for(int cy = 2; cy < 512; cy++)
    17. {
    18. int testY;
    19. if((cy & 1) == 0) //if even
    20. {
    21. testY = y + cy / 2;
    22. if(testY > 255)
    23. continue;
    24. }
    25. else
    26. {
    27. testY = y - cy / 2;
    28. if(testY < 0)
    29. continue;
    30. }
    31.  
    32. if(types.contains(world.getBlockTypeIdAt(x, testY, z)))
    33. return world.getBlockAt(x, testY, z);
    34. }
    35.  
    36. return null;
    37. }
    38. ...
    39. //example usage
    40. Block closest = closestBlock(loc, new HashSet<Integer>(Arrays.asList(Material.WOOD.getId())));
    41.  
    This is assuming that by "in the same y axis" you meant "in the same vertical column". If you meant the same horizontal plane, the solution would be slightly different, and would need to have some sort of limiting radius.
     
    Icyene, hawkfalcon and Ahmet094 like this.
  6. Offline

    hawkfalcon

    /facepalm. I meant x axis. Horizontal plane. But I'll modify your example. Thank you both:)
     
  7. Offline

    wizzinangel

    Courier hawkfalcon How would you do this for the Horizontal plane? So like a radius of 5 or so?
     
Thread Status:
Not open for further replies.

Share This Page