Minecraft 1.12.2模组开发(二十二) 多种建筑生成

之前在第十四节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.保存文件 -> 运行游戏 -> 创建一个新世界(以适应新的生成规则)

cr1.png

我们发现主世界中成功生成了两种建筑!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值