IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Minecraft 1.18.1、1.18.2模组开发 15.自定义生成建筑(structure) -> 正文阅读

[移动开发]Minecraft 1.18.1、1.18.2模组开发 15.自定义生成建筑(structure)

我们今天在1.18的模组中实现一个建筑的生成。

往期教程:

1.12.2自定义建筑生成

1.16.5自定义建筑生成

1.首先我们按照往常要制作一个建筑的并保存为nbt文件。B站教程

2.在Java包中新建一个world包 -> world包中新建一个 structure包 -> structure包中新建一个建筑类ChurchStructure

ChurchStructure.java

package com.joy187.re8joymod.world.structure;

import java.util.Optional;

import org.apache.logging.log4j.Level;

import com.joy187.re8joymod.Main;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.levelgen.feature.StructureFeature;
import net.minecraft.world.level.levelgen.feature.configurations.JigsawConfiguration;
import net.minecraft.world.level.levelgen.structure.BuiltinStructureSets;
import net.minecraft.world.level.levelgen.structure.PoolElementStructurePiece;
import net.minecraft.world.level.levelgen.structure.PostPlacementProcessor;
import net.minecraft.world.level.levelgen.structure.pieces.PieceGenerator;
import net.minecraft.world.level.levelgen.structure.pieces.PieceGeneratorSupplier;
import net.minecraft.world.level.levelgen.structure.pools.JigsawPlacement;

public class ChurchStructure extends StructureFeature<JigsawConfiguration>{
    public ChurchStructure() {
        // Create the pieces layout of the structure and give it to the game
        super(JigsawConfiguration.CODEC, ChurchStructure::createPiecesGenerator, PostPlacementProcessor.NONE);
    }
    
    @Override
    public GenerationStep.Decoration step() {
        return GenerationStep.Decoration.SURFACE_STRUCTURES;
    }


    private static boolean isFeatureChunk(PieceGeneratorSupplier.Context<JigsawConfiguration> context) {
        // Grabs the chunk position we are at
        ChunkPos chunkpos = context.chunkPos();

        // Checks to make sure our structure does not spawn within 10 chunks of an Ocean Monument
        // to demonstrate how this method is good for checking extra conditions for spawning
        return !context.chunkGenerator().hasFeatureChunkInRange(BuiltinStructureSets.VILLAGES, context.seed(), chunkpos.x, chunkpos.z, 10);
    }
    
    public static Optional<PieceGenerator<JigsawConfiguration>> createPiecesGenerator(PieceGeneratorSupplier.Context<JigsawConfiguration> context) {

        // Check if the spot is valid for our structure. This is just as another method for cleanness.
        // Returning an empty optional tells the game to skip this spot as it will not generate the structure.
        if (!ChurchStructure.isFeatureChunk(context)) {
            return Optional.empty();
        }

        // Turns the chunk coordinates into actual coordinates we can use. (Gets center of that chunk)
        BlockPos blockpos = context.chunkPos().getMiddleBlockPosition(0);

        // Find the top Y value of the land and then offset our structure to 60 blocks above that.
        // WORLD_SURFACE_WG will stop at top water so we don't accidentally put our structure into the ocean if it is a super deep ocean.
        int topLandY = context.chunkGenerator().getFirstFreeHeight(blockpos.getX(), blockpos.getZ(), Heightmap.Types.WORLD_SURFACE_WG, context.heightAccessor());
        
        //方块开始建造的位置,如果你改成 topLandY+40 的话就可以生成天空岛
        blockpos = blockpos.above(topLandY);

        Optional<PieceGenerator<JigsawConfiguration>> structurePiecesGenerator =
        		JigsawPlacement.addPieces(
                        context, // Used for JigsawPlacement to get all the proper behaviors done.
                        PoolElementStructurePiece::new, // Needed in order to create a list of jigsaw pieces when making the structure's layout.
                        blockpos, // Position of the structure. Y value is ignored if last parameter is set to true.
                        false,  // Special boundary adjustments for villages. It's... hard to explain. Keep this false and make your pieces not be partially intersecting.
                        // Either not intersecting or fully contained will make children pieces spawn just fine. It's easier that way.
                        false // Place at heightmap (top land). Set this to false for structure to be place at the passed in blockpos's Y value instead.
                        // Definitely keep this false when placing structures in the nether as otherwise, heightmap placing will put the structure on the Bedrock roof.
                );

        if(structurePiecesGenerator.isPresent()) {
            // 方便我们进入游戏后找到建筑.
            Main.LOGGER.log(Level.DEBUG, "Church Structrue is at {}", blockpos);
        }

        return structurePiecesGenerator;
    }
    
    
}

3.在structure包中新建一个ModStructures类对我们所有的建筑进行注册:

ModStructures.java

package com.joy187.re8joymod.world.structure;

import com.joy187.re8joymod.Main;
import net.minecraft.world.level.levelgen.feature.StructureFeature;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

public class ModStructures {

    public static final DeferredRegister<StructureFeature<?>> DEFERRED_REGISTRY_STRUCTURE =
            DeferredRegister.create(ForgeRegistries.STRUCTURE_FEATURES, Main.MOD_ID);

    //我们刚刚的建筑类
    public static final RegistryObject<StructureFeature<?>> CHURCH_STRUCTURE =
            DEFERRED_REGISTRY_STRUCTURE.register("church_structure", ChurchStructure::new);
    
    //public static final RegistryObject<StructureFeature<?>> Tank_STRUCTURE =
    //        DEFERRED_REGISTRY_STRUCTURE.register("tank_structure", TankStructure::new);
    
    public static void register(IEventBus eventBus) {
        DEFERRED_REGISTRY_STRUCTURE.register(eventBus);
    }
    
}

在我们的项目主类的主函数中对所有建筑进行注册事件

	public Main() {
		IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();

		ItemInit.ITEMS.register(bus);
		BlockInit.BLOCKS.register(bus);
		EntityInit.ENTITY_TYPES.register(bus);
		EffectInit.EFFECTS.register(bus);
		PotionInit.POTIONS.register(bus);
		SoundInit.SOUNDS.register(bus);
		
		//添加这个
        ModStructures.register(bus);


		MinecraftForge.EVENT_BUS.register(this);
	}

4.代码部分结束,来到资源包制作环节:

数据包文件树

cr1.png

1.在resources\data\你的modid中新建structures包并将第一步的nbt建筑放进入其中

2.新建一个tags包 -> 包中新建worldgen包 -> 新建biome包 -> biome包中新建has_structure包 -> 新建我们的建筑文件用来指明建筑在哪些地形生成

church.json

{
  "replace": false,

  "_comment": " This biome tag can specify the biome directly. Or specify another biome tag by starting with # ",
  "values": [
    "#minecraft:is_jungle",
    "#minecraft:is_forest",
    "#minecraft:is_taiga",
    "minecraft:desert",
    "minecraft:plains",
    "minecraft:savanna",
    "minecraft:savanna_plateau"
  ]
}

3.新建一个worldgen包 -> 包中新建configured_structure_feature包 -> 包中新建一个建筑文件来

church.json

{
  // 第三步我们ModStructure.java中定义的建筑类
  "type": "re8joymod:church_structure",

  "config": {
    // 指明我们的建筑生成文件
    "start_pool": "re8joymod:church/start_pool",

    // This is how many pieces away from the starting piece a piece of the structure can spawn
    // Think of it like the length of the branch of the structure
    "size": 2
  },

  // 上一步我们的地形生成文件
  "biomes": "#re8joymod:has_structure/church",

  // If true, land will be add around the bottom of the structure. (Based on the starting piece's y value)
  "adapt_noise": true,

  //建筑中可以生成什么生物
  "spawn_overrides": {
    "creature": {
      "bounding_box": "piece",
      "spawns": [
        {
          "type": "minecraft:bat",
          //生成权重
          "weight": 1,
          //最少生成几个
          "minCount": 1,
          //最多几个
          "maxCount": 2
        }
      ]
    }
  }
}

4.在worldgen包中新建一个structure_set包 -> 包中新建一个建筑文件说明这个建筑内生成的所有建筑:

church.json

{
  // What structures to pick to try and spawn if a spot passes the placement check.
  // If two or more structures in this list can spawn in a biome at a spot, a random one based on weight is chosen to spawn
  "structures": [
    {
    //这个建筑是我们的nbt文件名称
      "structure": "re8joymod:church",
      "weight": 1
    }
  ],
  "placement": {
    // 这个数介于int范围之内,请设置一个独一无二的大小
    "salt": 2036674,

    // The average distance apart in chunks for spawn attempts
    "spacing": 20,

    // Minimum distance apart in chunks for spawn attempts
    // MUST ALWAYS BE SMALLER THAN spacing ABOVE
    "separation": 6,

    // The kind of placement to use. The other kind is ring based like strongholds use.
    "type": "minecraft:random_spread"
  }
}

5.worldgen包中新建一个template_pool包 -> 包中新建我们的建筑包church -> 包中新建start_pool.json文件说明我们建筑从哪里开始生成。

start_pool.json

{
//我们建筑的开始生成的文件
  "name": "re8joymod:church/start_pool",
  "fallback": "minecraft:empty",
  "elements": [
    {
      "weight": 1,
      "element": {
        "location": "re8joymod:church",
        "processors": "minecraft:empty",
        "projection": "rigid",
        "element_type": "minecraft:single_pool_element"
      }
    }
  ]
}

5.保存所有文件 -> 进入游戏

我们新建一个世界进行测试:

按T键进入命令行输入指令找到我们的建筑

/locate re8joymod:church

cr3.png

之后传送到建筑的位置

str.png

我们成功找到了生成的建筑!

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-05-24 18:20:14  更:2022-05-24 18:22:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/25 1:00:52-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码