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<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Main.MOD_ID);
public static final DeferredRegister<Item> ITEMS = ItemInit.ITEMS;
//普通矿石,UniformInt.of(a,b)意思是该矿石挖掘后奖励多少经验,范围在[a,b]
public static final RegistryObject<Block> 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<Block> DEEPSLATE_FANTOM_ORE = registerBlock("deepslate_fantom_ore",
() -> new Block(BlockBehaviour.Properties.of(Material.STONE)
.strength(7f).requiresCorrectToolForDrops()), Main.TUTORIAL_TAB);
private static <T extends Block> RegistryObject<T> registerBlock(final String name,
final Supplier<? extends T> block) {
return BLOCKS.register(name, block);
}
private static <T extends Block> RegistryObject<T> register(final String name, final Supplier<? extends T> block,
Function<RegistryObject<T>, Supplier<? extends Item>> item) {
RegistryObject<T> obj = registerBlock(name, block);
ITEMS.register(name, item.apply(obj));
return obj;
}
private static <T extends Block> RegistryObject<T> registerBlock(String name, Supplier<T> block, CreativeModeTab tab) {
RegistryObject<T> toReturn = BLOCKS.register(name, block);
registerBlockItem(name, toReturn, tab);
return toReturn;
}
private static <T extends Block> RegistryObject<Item> registerBlockItem(String name, RegistryObject<T> block,
CreativeModeTab tab) {
return ItemInit.ITEMS.register(name, () -> new BlockItem(block.get(),
new Item.Properties().tab(tab)));
}
private static <T extends Block> RegistryObject<T> registerBlockWithoutBlockItem(String name, Supplier<T> block) {
return BLOCKS.register(name, block);
}
public static Supplier<Block> 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<PlacementModifier> orePlacement(PlacementModifier p_195347_, PlacementModifier p_195348_) {
return List.of(p_195347_, InSquarePlacement.spread(), p_195348_, BiomeFilter.biome());
}
//普通矿石摆放
public static List<PlacementModifier> commonOrePlacement(int p_195344_, PlacementModifier p_195345_) {
return orePlacement(CountPlacement.of(p_195344_), p_195345_);
}
//稀有矿石摆放
public static List<PlacementModifier> 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<PlacedFeature> PLACED_FEATURES =
DeferredRegister.create(Registry.PLACED_FEATURE_REGISTRY, Main.MOD_ID);
//矿石摆放
public static final RegistryObject<PlacedFeature> 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<ConfiguredFeature<?, ?>> CONFIGURED_FEATURES =
DeferredRegister.create(Registry.CONFIGURED_FEATURE_REGISTRY, Main.MOD_ID);
//将我们第一步的两种矿石分别填入其中
public static final Supplier<List<OreConfiguration.TargetBlockState>> 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<ConfiguredFeature<?, ?>> 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"
}