AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

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

作者: 作者的头像   GRID ,  2021-06-12 20:09:00 ,  所有人可见 ,  阅读 1117


3


之前在第十四节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

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

0 评论

你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息