2015-04-05 90 views
1

我正在尝试生成一个我现在生成为空的世界。这就是我想要的,但现在我想为每个参与者创建一个正方形或土地。我想让它看起来像一个网格。正方形不会连接。我可以用对角线的方式做到这一点,但我不知道如何将它变成网格。Bukkit生成世界

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); 
    } 
} 

这就是我创造的世界土地32x32的情节,我可以只改变x和z与x <和Z <斜角移动它一起。我如何将它变成网格?

+1

您的方法可能非常低效。您应该使用[世界发电机](https://bukkit.org/threads/how-to-create-custom-world-generators.79066/)。 – 2015-04-06 10:15:29

+0

我在我的插件中有一个世界发电机我只是不想要大量未使用的地块 – 2015-04-06 16:42:42

回答

0

试试这个:

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; 
      } 
     } 

这应该产生一个网格。它检查

1

您可以通过使用使土地32x32的情节:

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); 
    } 
} 

所以,你可以通过做一个4x4网格:

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); 

     } 
    } 

    } 
} 

如果你想使积有几块更深,你可以只添加一个for环路下面

​​

,并使其循环ŧ通过y值你想要的情节。例如,如果你想要的地块为4块高,从y = 60y = 64,你可以使用:

for(int y = 60; y <= 64; y++) 

如果你想,当你需要,你可以用它来创建地块:

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); 

    } 
    } 
} 

然后,您可以保存绘图创建次数的记录,并相应地生成新绘图。例如,如果您想要在x轴上有10个图,则可以使用:

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 
+0

这是否允许我只在需要时创建地块或者是否一次生成它们 – 2015-04-05 22:15:50

+0

@WilliamRandall我刚刚更新了我的答案包括如何在需要时创建新地块 – Jojodmo 2015-04-05 22:31:10

+0

好的,现在用生成地块方法有没有办法让无限长的32块地块变宽?因为我不能限制地块的数量。你明白我的意思了吗? – 2015-04-05 23:05:49