# Find nearest 2x2x2 air area

Discussion in 'Plugin Development' started by FisheyLP, Aug 3, 2016.

Not open for further replies.
1. Offline

### Forge_User_40559162

My goal is to find the nearest 2x2x2 air area with atleast 1 floor block from a location for a horse.

That's how I think it should work:
Code:
```for (Starting from center y; ...; all upper blocks first, then lower ones) {
for (int size = 1; size < 2 * radius; size++) {
// horizontal search area = size
// check if there is a 2x2x2 area of air + atleast 1 floor block underneith.
}
}```
But I have no idea on how to implement it. Especially the search thingy.

2. Offline

#2
3. Offline

### Lordloss

@bcohen9685 it seems like this thread has nothing to do with the question, or it simply overcomplicates things.

@FisheyLP just iterate over the blocks in the radius (x = range-5; x < range+5; x++) or something like that, and check the material of the block and its neighbours. I.e. with b.getRealtive().getType().

#3
4. Offline

### Forge_User_40559162

I wanna achieve something like this:
Blue are blocks to search, white are old blocks that have been searched already.

First layer, 2x2x2:

4x2x4

6x2x6 -2x2x2:

8x2x8 -4x2x4

Next layer, 2x2x2

5. Offline

### Lordloss

Well i told you the hopefully simplest solution. If you want it more complex, you can expand it yourself. You have to mess with loops and recursive functions, but this is all logical java stuff. We wont write the code for you, you have to think about it yourself, and show us at least what you have tried so far.

#5
6. Offline

### Forge_User_40559162

I already have this (just java, no bukkit, for testing):
Code:
```// arr is a 2D String array

// center
int c = arr.length / 2;

arr[c + (int) x][c + (int) z] = "#";
}
}
print(arr);
}```
And it prints:
Code:
```Size 2:
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - # # - - - -
- - - # # - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -

Size 4:
- - - - - - - - -
- - - - - - - - -
- - # # # # - - -
- - # # # # - - -
- - # # # # - - -
- - # # # # - - -
- - - - - - - - -
- - - - - - - - -
- - - - - - - - -

Size 6:
- - - - - - - - -
- # # # # # # - -
- # # # # # # - -
- # # # # # # - -
- # # # # # # - -
- # # # # # # - -
- # # # # # # - -
- - - - - - - - -
- - - - - - - - -

Size 8:
# # # # # # # # -
# # # # # # # # -
# # # # # # # # -
# # # # # # # # -
# # # # # # # # -
# # # # # # # # -
# # # # # # # # -
# # # # # # # # -
- - - - - - - - -
```
But 1. how can I exclude the inner area (size 4+, look at images) and only search the border?
And 2. iterate through every 2x2 blocks of the border?

For 2.question:
Code:
```# # #
# # #```
would contain 2 available areas.

The 4x2x4 area from the image above would have 8 available areas. Excluding the 2x2 in the middle, but this should work:
Code:
```- # # -
- # # -
- - - -
- - - -```

Last edited: Aug 4, 2016
7. Offline

### MarinD99

Correct me if I'm wrong, but if you'd like to get just the horse's 2*2*2 area (or any area for that matter), I believe this can be done in an easy manner. Take a look at this.

Code:
```private void spawnHorse(Location l, double radius, double x, double y, double z) {
l = new Location(l.getWorld(), l.getBlockX()+x, l.getBlockY()+y, l.getBlockZ()+z);
// You can play around with whatever you want here, I just broadcasted the volume as an example
}

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

if(!(sender instanceof Player)) return true;

Player p = (Player) sender;
if(cmd.getName().equalsIgnoreCase("test")) {
if(!(args.length > 0)) {
Horse horse = (Horse) p.getWorld().spawnEntity(p.getLocation(), EntityType.HORSE);
spawnHorse(horse.getLocation(), 2, 2, 2, 2);
return true;
} else return true;
}

return true;
}

@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
}
```

#7
8. Offline

### Forge_User_40559162

That just adds 2, 2, 2 to the players location. Not what I am looking for...

9. Offline

### MarinD99

No, the method I made accepts "2" (in the command) as the paramater for the radius and the horse's coordinates. It calculates the volume of the horse's location multiplied by the given radius, which you said you wanted to be 2. The command was but a mere example.

#9
10. Offline

### Forge_User_40559162

Yes, it calculates the volume, but the volume is same if there are blocks or not and it also doesn't search in a bigger radius then.

EDIT: Would sin/cos be a better way to do it?​