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 DeferredRegisterBLOCKS = 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 RegistryObjectDEEPSLATE_FANTOM_ORE = registerBlock("deepslate_fantom_ore",
() ->new Block(BlockBehaviour.Properties.of(Material.STONE)
.strength(7f).requiresCorrectToolForDrops()), Main.TUTORIAL_TAB);
private staticRegistryObjectregisterBlock(final String name,
final Supplier extends T>block) {return BLOCKS.register(name, block);
}
private staticRegistryObjectregister(final String name, final Supplier extends T>block,
Function, Supplier extends Item>>item) {RegistryObjectobj = registerBlock(name, block);
ITEMS.register(name, item.apply(obj));
return obj;
}
private staticRegistryObjectregisterBlock(String name, Supplierblock, CreativeModeTab tab) {RegistryObjecttoReturn = BLOCKS.register(name, block);
registerBlockItem(name, toReturn, tab);
return toReturn;
}
private staticRegistryObject- registerBlockItem(String name, RegistryObject
block,
CreativeModeTab tab) {return ItemInit.ITEMS.register(name, () ->new BlockItem(block.get(),
new Item.Properties().tab(tab)));
}
private staticRegistryObjectregisterBlockWithoutBlockItem(String name, Supplierblock) {return BLOCKS.register(name, block);
}
public static SuppliercreateStainedGlassFromColor(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;
}
}
之后参考Minecraft 1.19.2 Forge模组开发 02.物品栏+方块+物品将两个方块制作出来。
2.准备好了方块,下一步就是定义其生成的内容了。在Java包中新建一个world包->world包中新建一个feature包->feature包中分别新建三个类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 ListorePlacement(PlacementModifier p_195347_, PlacementModifier p_195348_) {return List.of(p_195347_, InSquarePlacement.spread(), p_195348_, BiomeFilter.biome());
}
//普通矿石摆放
public static ListcommonOrePlacement(int p_195344_, PlacementModifier p_195345_) {return orePlacement(CountPlacement.of(p_195344_), p_195345_);
}
//稀有矿石摆放
public static ListrareOrePlacement(int p_195350_, PlacementModifier p_195351_) {return orePlacement(RarityFilter.onAverageOnceEvery(p_195350_), p_195351_);
}
}
其次是我们的矿石生成定义类ModPlacedFeaturesModPlacedFeatures.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 DeferredRegisterPLACED_FEATURES =
DeferredRegister.create(Registry.PLACED_FEATURE_REGISTRY, Main.MOD_ID);
//矿石摆放
public static final RegistryObjectFANTOM_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);
}
}
最后是矿石生成配置类ModConfiguredFeaturesModConfiguredFeatures.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);
}
}
3.在项目主类中添加我们矿石生成配置类和矿石生成定义类的注册事件: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);
}
4.代码部分结束,之后需要在数据包中添加矿石生成的内容在数据包中新建forge包,forge包中新建biome_modifier包->biome_modifier包中新建矿石生成文件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"
}
5.保存所有设置,进入游戏:
我们新建一个存档,之后查看是否能找到模组中的矿石:成功在深板岩层发现了矿石!
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧