Minecraft Forge 1.19.3中BuiltInRegistries.CONFIGURED_FEATURE替代方案咨询
Minecraft 1.19.3 Forge 自定义矿石生成注册方案
1.19.3版本的核心变化
BuiltInRegistries.CONFIGURED_FEATURE已被移除,现在需要通过Bootstrap上下文配合注册事件完成配置化特征(ConfiguredFeature)和放置特征(PlacedFeature)的注册——前者定义矿脉基础属性,后者控制生成规则,两者缺一不可。
完整实现流程
1. 注册矿石方块(逻辑与旧版本一致)
先确保矿石方块及对应物品注册完成:
// 矿石方块 public static final Block MY_ORE_BLOCK = Registry.register( BuiltInRegistries.BLOCK, new ResourceLocation("你的模组ID", "my_ore"), new DropExperienceBlock( BlockBehaviour.Properties.of(Material.STONE) .requiresCorrectToolForDrops() .strength(3.0F, 3.0F), UniformInt.of(3, 7) ) ); // 对应物品 public static final Item MY_ORE_ITEM = Registry.register( BuiltInRegistries.ITEM, new ResourceLocation("你的模组ID", "my_ore"), new BlockItem(MY_ORE_BLOCK, new Item.Properties().tab(CreativeModeTab.TAB_BUILDING_BLOCKS)) );
2. 注册配置化特征和放置特征
在主Mod类中,通过RegistryEvent.Register事件获取BootstrapContext,完成两类特征的注册:
public class YourModMain { public static final String MOD_ID = "你的模组ID"; public YourModMain() { IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); // 用DeferredRegister注册方块和物品(可选,也可以用Registry.register直接注册) DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MOD_ID); BLOCKS.register("my_ore", () -> MY_ORE_BLOCK); BLOCKS.register(modBus); DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MOD_ID); ITEMS.register("my_ore", () -> MY_ORE_ITEM); ITEMS.register(modBus); // 添加特征注册监听器 modBus.addListener(this::registerFeatures); } private void registerFeatures(final RegistryEvent.Register<ConfiguredFeature<?, ?>> event) { BootstrapContext<ConfiguredFeature<?, ?>> configContext = event.getBootstrapContext(); // 定义矿石替换目标(此处以普通石头为替换对象) List<OreConfiguration.TargetBlockState> oreTargets = List.of( OreConfiguration.target(OreConfiguration.Predicates.STONE_ORE_REPLACEABLES, MY_ORE_BLOCK.defaultBlockState()) ); // 注册配置化特征 ConfiguredFeature<?, ?> myOreConfig = new ConfiguredFeature<>( Feature.ORE, new OreConfiguration(oreTargets, 4, 0.5F) // 4=矿脉最大方块数,0.5=分布概率 ); ResourceKey<ConfiguredFeature<?, ?>> configKey = ResourceKey.create(Registry.CONFIGURED_FEATURE_REGISTRY, new ResourceLocation(MOD_ID, "my_ore_large")); configContext.register(configKey, myOreConfig); // 获取放置特征的Bootstrap上下文,注册生成规则 BootstrapContext<PlacedFeature> placedContext = configContext.getContext(Registry.PLACED_FEATURE_REGISTRY); PlacedFeature myOrePlaced = new PlacedFeature( configContext.lookup(Registry.CONFIGURED_FEATURE_REGISTRY).getOrThrow(configKey), List.of( CountPlacement.of(10), // 每个区块尝试生成10次 InSquarePlacement.spread(), // 水平方向分散生成 HeightRangePlacement.uniform(VerticalAnchor.bottom(), VerticalAnchor.absolute(64)), // 生成高度范围:基岩到Y=64 BiomeFilter.biome() // 仅在符合条件的生物群系生成 ) ); ResourceKey<PlacedFeature> placedKey = ResourceKey.create(Registry.PLACED_FEATURE_REGISTRY, new ResourceLocation(MOD_ID, "my_ore_large_placed")); placedContext.register(placedKey, myOrePlaced); } }
3. 让世界生成器加载你的矿石
通过BiomeLoadingEvent将放置特征添加到生物群系的生成流程:
public class YourModMain { // ... 之前的代码 ... public YourModMain() { // ... 之前的注册代码 ... modBus.addListener(this::onBiomeLoad); } private void onBiomeLoad(final BiomeLoadingEvent event) { ResourceKey<PlacedFeature> placedKey = ResourceKey.create( Registry.PLACED_FEATURE_REGISTRY, new ResourceLocation(MOD_ID, "my_ore_large_placed") ); // 将矿石添加到地下矿石生成阶段 event.getGeneration().getFeatures(GenerationStep.Decoration.UNDERGROUND_ORES) .add(() -> placedKey); } }
关键提示
- 若要替换深板岩,将
STONE_ORE_REPLACEABLES替换为DEEPSLATE_ORE_REPLACEABLES即可。 BootstrapContext只能从注册事件中获取,无需手动实例化,这就是你之前困惑的核心点。- 放置特征的规则可按需调整:比如
HeightRangePlacement.triangle(VerticalAnchor.absolute(32), VerticalAnchor.absolute(64))可让矿石在Y=32到64区间集中生成。
内容的提问来源于stack exchange,提问作者CodingNoob
相关产品推荐
相关产品推荐

