Reputation: 167
I'm trying to generate a world I have it to currently generate to empty. That's what I want but now I want to generate a square or land for each player when they join. I would like to make it look like a grid. The squares will not connect. I can do it in a diagonal fashion but I don't know how to go about making it a grid.
for(int x = 0; x < 32; x++) {
for(int z = 0; z < 32; z++) {
Location l = new Location(getClashWorld(), x, 64, z);
getClashWorld().getBlockAt(l).setType(Material.GRASS);
}
}
That's how I create a 32x32 plot of land in the world and i can just change the x and z along with the x < and the z < to move it diagonally. How would I make this into a grid?
Upvotes: 1
Views: 130
Reputation: 23616
You could make 32x32 plot of land by using:
int y = 64;
for(int x = 0; x < 32; x++){
for(int z = 0; z < 32; z++){
Location location = new Location(world, x, y, z);
location.getBlock().setType(Material.GRASS);
}
}
So, you could make a 4x4 grid by using:
int y = 64; //the position on the y axis that this plot will be created
for(int xTimes = 0; xTimes < 4; xTimes++){//xTimes < 4 makes it so this will create 4 plots on the x axis
for(int zTimes = 0; zTimes < 4; zTimes++){//zTimes < 4 makes it so this will create 4 plots on the z axis
//create the plot of land
for(int x = 0; x < 32; x++){//x < 32 makes it so this will create a plot 32 long
for(int z = 0; z < 32; z++){//z < 32 makes it so this will create a plot 32 wide
//get the x and z locations for the plot
//multiplying the below values by 64 makes it so there will be a 32x32 gap between each plot
//(below multiplication value - plot size = gap), so the gap will be 64 - 32 = 32 blocks
int xPos = x + (xTimes * 64);
int zPos = z + (zTimes * 64);
//get the location for the block, and then set the block to grass (or set it to whatever you want)
Location location = new Location(world, xPos, y, zPos);
location.getBlock().setType(Material.GRASS);
}
}
}
}
If you wanted to make the plot a few blocks deeper, you could just add another for
loop below
for(int z = 0; z < 32; z++)
And make it iterate through the y
values that you would like the plot to be at. For example, if you wanted the plots to be 4 blocks high, going from y = 60
to y = 64
, you could use:
for(int y = 60; y <= 64; y++)
If you wanted to create the plots when you need to, you could use:
public void generatePlot(int xTime, int zTime){
for(int x = 0; x < 32; x++){
for(int z = 0; z < 32; z++){
int xPos = x + (xTime * 64);
int zPos = z + (zTime * 64);
Location location = new Location(world, xPos, y, zPos);
location.getBlock().setType(Material.GRASS);
}
}
}
You could then keep a record of the amount of times a plot has been created, and generate new plots accordingly. For example, if you wanted there to be 10 plots on the x axis, you could use:
int plotsCreated = 100; //load this from a configuration file, or something
int xTime = plotsCreated % 10;
int yTime = Math.floor(plotsCreated / 10);
generatePlot(xTime, yTime);
plotsCreated++;
//save the new plotsCreated variable somewhere
Upvotes: 1
Reputation: 950
Try this:
final int w = 32;
final int h = 32;
int offset = 0;
for(int x = 0; x < w; x ++){
for(int y = 0; y < h; y ++){
if(((x + offset) & 1) == 0){
//generate block A
}else{
//generate block B
}
if(offset == 0)
offset = 1;
else
offset = 0;
}
}
That should generate a grid. It checks
Upvotes: 0