Minecraft 1.19.2 Forge模组开发 05.矿石生成

本文介绍如何在Minecraft模组中自定义矿石,包括创建不同岩层的矿石、定义生成规则及配置生成位置等步骤。

我们本次尝试在主世界生成模组中自定义的矿石

1.由于1.19的版本出现了深板岩层的矿石,我们要在BlockInit类中声明一个矿石的两种岩层形态:

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"
Minecraft 1.19.2 中使用 Forge 进行模组开发时,实现 GUI(图形用户界面)和工作台效果通常涉及多个步骤,包括注册 GUI、创建容器类、绑定 GUI 与容器,并在客户端渲染 GUI。以下是实现的基本流程和关键代码示例。 ### 创建 GUI 的基本流程 1. **定义容器类(Container)** 容器类用于管理 GUI 中的物品槽位和玩家的交互逻辑。需要继承 `AbstractContainerMenu` 并实现其方法。 ```java public class CustomGuiContainer extends AbstractContainerMenu { private final ContainerLevelAccess access; private final int containerId; public CustomGuiContainer(int containerId, Inventory playerInventory, FriendlyByteBuf extraData) { this(containerId, playerInventory, ContainerLevelAccess.NULL); } public CustomGuiContainer(int containerId, Inventory playerInventory, ContainerLevelAccess access) { super(ModContainers.CUSTOM_GUI_CONTAINER.get(), containerId); this.containerId = containerId; this.access = access; // 添加 GUI 中的槽位 for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { this.addSlot(new Slot(new SimpleContainer(9), j + i * 3, 62 + j * 18, 17 + i * 18)); } } // 添加玩家背包槽位 for (int k = 0; k < 3; ++k) { for (int l = 0; l < 9; ++l) { this.addSlot(new Slot(playerInventory, l + k * 9 + 9, 8 + l * 18, 84 + k * 18)); } } // 添加热键槽位 for (int i1 = 0; i1 < 9; ++i1) { this.addSlot(new Slot(playerInventory, i1, 8 + i1 * 18, 142)); } } @Override public boolean stillValid(Player player) { return true; } @Override public ItemStack quickMoveStack(Player player, int index) { return ItemStack.EMPTY; } public int getContainerId() { return this.containerId; } } ``` 2. **注册容器类型(ContainerType)** 在模组的注册类中注册容器类型,以便 Minecraft 可以识别。 ```java public class ModContainers { public static final DeferredRegister<MenuType<?>> CONTAINERS = DeferredRegister.create(ForgeRegistries.CONTAINERS, "modid"); public static final RegistryObject<MenuType<CustomGuiContainer>> CUSTOM_GUI_CONTAINER = CONTAINERS.register("custom_gui", () -> IForgeContainerType.create(CustomGuiContainer::new)); } ``` 3. **创建 GUI 类(Screen)** GUI 类用于渲染界面。需要继承 `Screen` 并实现其绘制逻辑。 ```java public class CustomGuiScreen extends Screen { private final int containerId; public CustomGuiScreen(int containerId, Inventory playerInventory, Component title) { super(title); this.containerId = containerId; } @Override protected void init() { // 初始化 GUI 元素 } @Override public void render(PoseStack matrixStack, int mouseX, int mouseY, float partialTicks) { this.renderBackground(matrixStack); super.render(matrixStack, mouseX, mouseY, partialTicks); this.drawCenteredString(matrixStack, this.font, "Custom GUI", this.width / 2, 20, 16777215); } @Override public boolean isPauseScreen() { return false; } } ``` 4. **注册 GUI 工厂** 使用 `ScreenManager` 注册 GUI 工厂,以便在打开 GUI 时能够正确创建。 ```java @Mod.EventBusSubscriber public class ClientSetup { @SubscribeEvent public static void onClientSetup(FMLClientSetupEvent event) { ScreenManager.register(ModContainers.CUSTOM_GUI_CONTAINER.get(), CustomGuiScreen::new); } } ``` 5. **打开 GUI** 在某个事件(如玩家右键点击方块)中打开 GUI。 ```java @SubscribeEvent public void onPlayerInteract(PlayerInteractEvent.RightClickBlock event) { if (event.getWorld().getBlockState(event.getPos()).getBlock() == ModBlocks.CUSTOM_BLOCK.get()) { NetworkHooks.openGui(event.getPlayer(), new SimpleMenuProvider((containerId, playerInventory, player) -> new CustomGuiContainer(containerId, playerInventory, ContainerLevelAccess.create(event.getWorld(), event.getPos())), new StringTextComponent("Custom GUI")), buffer -> buffer.writeBlockPos(event.getPos())); } } ``` ### 实现工作台效果 Minecraft 中的“工作台”效果通常指的是玩家在 GUI 中进行物品合成或处理的操作。为了实现类似效果,需要: - 使用 `Slot` 类管理每个物品槽位。 - 在容器类中实现 `quickMoveStack` 方法以支持物品移动。 - 在 GUI 类中绘制槽位和物品图标。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值