BlockInit.java
package com.joy187.re8joymod.init;import com.joy187.re8joymod.Main;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.StainedGlassBlock;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;import java.util.function.Function;
import java.util.function.Supplier;public class BlockInit {public static DeferredRegister BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Main.MOD_ID);public static final DeferredRegister- ITEMS = ItemInit.ITEMS;//普通矿石,UniformInt.of(a,b)意思是该矿石挖掘后奖励多少经验,范围在[a,b]public static final RegistryObject
FANTOM_ORE = registerBlock("fantom_ore",() -> new DropExperienceBlock(BlockBehaviour.Properties.of(Material.STONE).strength(5f).requiresCorrectToolForDrops(), UniformInt.of(3, 10)), Main.TUTORIAL_TAB);//深板岩矿石public static final RegistryObject DEEPSLATE_FANTOM_ORE = registerBlock("deepslate_fantom_ore",() -> new Block(BlockBehaviour.Properties.of(Material.STONE).strength(7f).requiresCorrectToolForDrops()), Main.TUTORIAL_TAB);private static RegistryObject registerBlock(final String name,final Supplier extends T> block) {return BLOCKS.register(name, block);}private static RegistryObject register(final String name, final Supplier extends T> block,Function, Supplier extends Item>> item) {RegistryObject obj = registerBlock(name, block);ITEMS.register(name, item.apply(obj));return obj;}private static RegistryObject registerBlock(String name, Supplier block, CreativeModeTab tab) {RegistryObject toReturn = BLOCKS.register(name, block);registerBlockItem(name, toReturn, tab);return toReturn;}private static RegistryObject- registerBlockItem(String name, RegistryObject
block,CreativeModeTab tab) {return ItemInit.ITEMS.register(name, () -> new BlockItem(block.get(),new Item.Properties().tab(tab)));}private static RegistryObject registerBlockWithoutBlockItem(String name, Supplier block) {return BLOCKS.register(name, block);}public static Supplier createStainedGlassFromColor(DyeColor color) {return () -> new StainedGlassBlock(color, BlockBehaviour.Properties.of(Material.GLASS, color).strength(0.3F).sound(SoundType.GLASS).noOcclusion().isValidSpawn(BlockInit::never).isRedstoneConductor(BlockInit::never).isSuffocating(BlockInit::never).isViewBlocking(BlockInit::never));}public static boolean always(BlockState state, BlockGetter reader, BlockPos pos) {return true;}public static boolean never(BlockState state, BlockGetter reader, BlockPos pos) {return false;}public static boolean always(BlockState state, BlockGetter reader, BlockPos pos, EntityType> entityType) {return true;}public static boolean never(BlockState state, BlockGetter reader, BlockPos pos, EntityType> entityType) {return false;}
}
ModConfiguredFeatures,ModOrePlacement,ModPlacedFeatures:
ModOrePlacement.java
package com.joy187.re8joymod.world.feature;import net.minecraft.world.level.levelgen.placement.*;import java.util.List;public class ModOrePlacement {//默认矿石摆放public static List orePlacement(PlacementModifier p_195347_, PlacementModifier p_195348_) {return List.of(p_195347_, InSquarePlacement.spread(), p_195348_, BiomeFilter.biome());}//普通矿石摆放public static List commonOrePlacement(int p_195344_, PlacementModifier p_195345_) {return orePlacement(CountPlacement.of(p_195344_), p_195345_);}//稀有矿石摆放public static List rareOrePlacement(int p_195350_, PlacementModifier p_195351_) {return orePlacement(RarityFilter.onAverageOnceEvery(p_195350_), p_195351_);}}
ModPlacedFeatures.java
package com.joy187.re8joymod.world.feature;import com.joy187.re8joymod.Main;
import net.minecraft.core.Registry;
import net.minecraft.world.level.levelgen.VerticalAnchor;
import net.minecraft.world.level.levelgen.placement.*;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.RegistryObject;import static com.joy187.re8joymod.world.feature.ModOrePlacement.commonOrePlacement;public class ModPlacedFeatures {public static final DeferredRegister PLACED_FEATURES =DeferredRegister.create(Registry.PLACED_FEATURE_REGISTRY, Main.MOD_ID);//矿石摆放public static final RegistryObject FANTOM_ORE_PLACED = PLACED_FEATURES.register("fantom_ore_placed",() -> new PlacedFeature(ModConfiguredFeatures.FANTOM_ORES.getHolder().get(),commonOrePlacement(7, //每个区块生成多少矿石HeightRangePlacement.triangle(VerticalAnchor.aboveBottom(-60), VerticalAnchor.aboveBottom(60))))); //-60,60分别指矿石生成高度范围介于[-60,60]public static void register(IEventBus eventBus) {PLACED_FEATURES.register(eventBus);}}
ModConfiguredFeatures.java
package com.joy187.re8joymod.world.feature;import com.google.common.base.Suppliers;
import com.joy187.re8joymod.Main;
import com.joy187.re8joymod.init.BlockInit;
import net.minecraft.core.Registry;
import net.minecraft.data.worldgen.features.OreFeatures;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.levelgen.feature.configurations.*;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.RegistryObject;import java.util.List;
import java.util.function.Supplier;public class ModConfiguredFeatures {public static final DeferredRegister> CONFIGURED_FEATURES =DeferredRegister.create(Registry.CONFIGURED_FEATURE_REGISTRY, Main.MOD_ID);//将我们第一步的两种矿石分别填入其中public static final Supplier> OVERWORLD_FANTOM_ORES = Suppliers.memoize(() -> List.of(OreConfiguration.target(OreFeatures.STONE_ORE_REPLACEABLES, BlockInit.FANTOM_ORE.get().defaultBlockState()), //普通岩层OreConfiguration.target(OreFeatures.DEEPSLATE_ORE_REPLACEABLES, BlockInit.DEEPSLATE_FANTOM_ORE.get().defaultBlockState()))); //深板岩层//将这种矿石生成类型进行注册public static final RegistryObject> FANTOM_ORES = CONFIGURED_FEATURES.register("fantom_ore",() -> new ConfiguredFeature<>(Feature.ORE, new OreConfiguration(OVERWORLD_FANTOM_ORES.get(),7)));public static void register(IEventBus eventBus) {CONFIGURED_FEATURES.register(eventBus);}}
public Main(){IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();bus.addListener(this::commonSetup);bus.addListener(this::setup);bus.addListener(this::clientSetup);ItemInit.ITEMS.register(bus);BlockInit.BLOCKS.register(bus);EntityInit.ENTITY_TYPES.register(bus);//添加这两个ModConfiguredFeatures.register(bus);ModPlacedFeatures.register(bus);MinecraftForge.EVENT_BUS.register(this);}

add_fantom_ore,在features中填入我们在ModPlacedFeatures类中定义的名称:add_fantom_ore.json
{"type": "forge:add_features","biomes": "#minecraft:is_overworld","features": "re8joymod:fantom_ore_placed","step": "underground_ores"
}
