# Flat square

Discussion in 'Plugin Development' started by Deleted user, Aug 31, 2012.

Not open for further replies.
1. Offline

### Deleted user

I need to generate a flat (5x5) square of lets say grass? How would I do this..?

#1
2. Offline

### sternmin8or

relative to a player?

#2
3. Offline

### Deleted user

At a specified location, for example:

Location l = Bukkit.getServer().getWorld("world").getSpawnLocation();

#3
4. Offline

#4
5. Offline

### Forge_User_06133043

just pseudo code:
Code:
```for(int x = 0; x < 5; x++) {
for(int z = 0; z < 5; z++) {
Location l = new Location(x, spawn.getY(), z, world);
world.getBlockAt(l).setType(Material.DIRT);
}
}```
something like that, you just need to figure out where to get the y coordinate and the world from, but this is how to do it i think

6. Offline

### sternmin8or

Code:java
` y = l.getY()for(int x = l.getX()-2;i<l.getX()+3;x++){  for(int z = l.getZ()-2;i<l.getZ()-3;z++){    getServer().getWorld().getBlockAt(x,y,z).setType(Material.GRASS)  }} `

edit: too slow, but be careful about declaring variables inside loops, it takes up a lot of resources. I think my way is the most efficient (but i could be wrong)

#6
7. Offline

### Deleted user

Considering getBlockAt requires int's I had to change getY / getZ to getBlockY/getBlockZ and what exactly is 'i' suppose to represent?

Code:java
` int y = l.getBlockY();for (int x = l.getBlockX() - 2; i < l.getX() + 3; x++) {for (int z = l.getBlockZ() - 2; i < l.getZ() - 3; z++) {Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setType(Material.NETHER_BRICK);}} `

#7
8. Offline

### sternmin8or

oops, the is are supposed to be x and z respectively (sorry habit). You dont use getY(), x,y, and z are allready integers defined by the for loops

#8
9. Offline

#9
10. Offline

### sternmin8or

The "i"s are supposed to be x and z respectively (sorry, it is a habit of mine). You do not in fact need to use getY() because "x","y", and "z" are all integers defined in the "for" loops.

#10
11. Offline

### Deleted user

Okay so...

Code:java
` int y = l.getBlockY();for (int x = l.getBlockX() - 2; l.getBlockX() < l.getX() + 3; x++) {for (int z = l.getBlockZ() - 2; l.getBlockZ() < l.getZ() - 3; z++) {Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setType(Material.NETHER_BRICK);}} `

#11
12. Offline

### sternmin8or

no, just x and z, not l.getBlockX(). Also, change all get to getBlock.

#12
13. Offline

### Deleted user

So:

Code:
```int y = l.getBlockY();
for (int x = l.getBlockX() - 2; x < l.getX() + 3; x++) {
for (int z = l.getBlockZ() - 2; z < l.getZ() - 3; z++) {
Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setType(Material.NETHER_BRICK);
}
}
```

#13
14. Offline

### sternmin8or

lol almost, sorry about all the miscommunication. Change : l.getX() to l.getBlockX() and l.getZ() to l.getBlockZ()

#14
15. Offline

### Deleted user

It's alright I appriciate the help, anyways this:

Code:
```int y = l.getBlockY();
for (int x = l.getBlockX() - 2; x < l.getBlockX() + 3; x++) {
for (int z = l.getBlockZ() - 2; z < l.getBlockZ() - 3; z++) {
Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setType(Material.NETHER_BRICK);
}
}
```
Doesn't do anything..

#15
16. Offline

### sternmin8or

Where do you have it?

#16
17. Offline

### Deleted user

In a public void. When I call upon the void everything else in the void happens just not that.

#17
18. Offline

### sternmin8or

are you sure you are looking at the right area. Where is your location l?

#18
19. Offline

### Deleted user

It's

Bukkit.getServer().getWorld("world).getSpawnLocation();

#19
20. Offline

### sternmin8or

and you dont have any sort of spawn location mod?

#20
21. Offline

#21
22. Offline

### Deleted user

Bump - still don't got this figured out.

#22
23. Offline

### calebbfmv

Make sure you have the have registered it in the onEnable, and are there any errors in the console?

#23
24. Offline

### kyle1320

I realize you guys have this all figured out, just thought I'd post some code from a mod I made which allows the size to be configurable and whatnot, it spawns a platform underneath the player but this can obviously be changed to anywhere:
Code:
```Location loc = player.getLocation();
Integer width = (**width** / 2) * 2 + 1; // rounds the width up to the closest odd number
Material mat = **material**;
Block block = loc.getBlock().getRelative(0, -1, 0); // block under the player
Block[] newBlocks = getPlatform(width, block); // every block in their platform

for (Block b : newBlocks) {
b.setType(mat); // set each block in the platform to the material
}

public Block[] getPlatform(Integer width, Block center) {

Block corner = center.getRelative(width / 2, 0, width / 2); //corner block of their platform
Block[] newBlocks = new Block[(int)Math.pow(width, 2)]; // width squared

for (int i=0; i<width; i++) {
for (int j=0; j<width; j++) {
newBlocks[i * width + j] = corner.getRelative(-i, 0, -j);
}
}

return newBlocks;
}```

#24