之前在第十四节Minecraft 1.12.2模组开发建筑生成中我们对自定义的建筑在主世界生成进行了设置,可是如果使用先前的方法,你会发现不管你添加了多少个建筑模型,在主世界只会生成你最后一个添加的建筑。这个时候就要用到我们本次介绍的方法了。
我们可以使用一个next指针来确定下一个要生成的建筑是什么,在ModWorldGenCustomStructure.java中进行修改:
package com.Joy187.newmod.world;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldType;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.BiomePlains;
import net.minecraft.world.biome.BiomeSavanna;
import net.minecraft.world.biome.BiomeStoneBeach;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.IChunkGenerator;
import net.minecraft.world.gen.feature.WorldGenerator;
import net.minecraftforge.fml.common.IWorldGenerator;
public class ModWorldGenCustomStructure implements IWorldGenerator {
private int next = 1; //定义一个指针,让其默认指向第一个建筑
// public final ModWorldGenStructure XCHAMBERX = new ModWorldGenStructure("xchamberx");
// public final ModWorldGenStructure DCHURCH = new ModWorldGenStructure("dchurc");
@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator,
IChunkProvider chunkePrvider) {
if (world.provider.getDimension() == 0) {
if(next==1) //如果next的值为1,就生成我们的建筑1
this.generateStructureXC(new ModWorldGenStructure("xchamberx"), world, random, chunkX, chunkZ, 35, Blocks.GRASS, BiomePlains.class);
else //next值不为1 我们就生成建筑2
this.generateStructureDCH(new ModWorldGenStructure("dchurc"), world, random, chunkX, chunkZ, 35, Blocks.GRASS, BiomeSavanna.class);
//可以续写建筑3、4、5……
}
}
//建筑1的生成方法
private void generateStructureXC(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ,
int chance, Block topBlock, Class<?>... classes) {
ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
int x = (chunkX * 32) + random.nextInt(15) +8;
int z = (chunkZ * 32) + random.nextInt(15) +8;
int y = calculateGenerationHeight(world, x, z, topBlock);
BlockPos pos = new BlockPos(x, y, z);
Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
if (world.getWorldType() != WorldType.FLAT) {
if (classesList.contains(biome)) {
if (random.nextInt(chance) == 0) {
generator.generate(world, random, pos);
next=0; //当建筑1生成后将next指向0,即生成建筑2
}
}
}
}
//建筑2的生成方法
private void generateStructureDCH(WorldGenerator generator, World world, Random random, int chunkX, int chunkZ,
int chance, Block topBlock, Class<?>... classes) {
ArrayList<Class<?>> classesList = new ArrayList<Class<?>>(Arrays.asList(classes));
int x = (chunkX * 32) + random.nextInt(15) +8;
int z = (chunkZ * 32) + random.nextInt(15) +8;
int y = calculateGenerationHeight(world, x, z, topBlock);
BlockPos pos = new BlockPos(x, y, z);
Class<?> biome = world.provider.getBiomeForCoords(pos).getClass();
if (world.getWorldType() != WorldType.FLAT) {
if (classesList.contains(biome)) {
if (random.nextInt(chance) == 0) {
generator.generate(world, random, pos);
next=1; //当建筑2生成后将next指向1,即生成建筑1
}
}
}
}
private static int calculateGenerationHeight(World world, int x, int z, Block topBlock) {
int y = world.getHeight();
boolean foundGround = false;
while (!foundGround && y-- >= 0) {
Block block = world.getBlockState(new BlockPos(x, y, z)).getBlock();
foundGround = block == topBlock;
}
return y;
}
}
2.保存文件 -> 运行游戏 -> 创建一个新世界(以适应新的生成规则)