Minecraft 1.19.2 Forge模组开发 12.自定义鞘翅

该文章介绍了如何在Minecraft模组开发中创建一个具有飞行功能的鞘翅盔甲,包括模型制作、动画文件、盔甲类的编写、渲染类的注册以及资源包的制作过程。

本次我们在模组中实现一个可用于飞行的鞘翅。

dress.gif

1.首先参考3D动画盔甲的教程一、二步制作盔甲的模型、动画文件。

2.模型制作完成,接下来需要制作我们的盔甲类

在items包中新建armor包 -> armor包中新建我们的套装类DressArmorItem.java

DressArmorItem.java

package net.joy187.joyggd.item.armor;


import net.joy187.joyggd.Main;
import net.joy187.joyggd.init.ItemInit;
import net.joy187.joyggd.item.CustomArmorMaterial;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import software.bernie.geckolib3.core.IAnimatable;
import software.bernie.geckolib3.core.manager.AnimationData;
import software.bernie.geckolib3.core.manager.AnimationFactory;
import software.bernie.geckolib3.item.GeoArmorItem;

import javax.annotation.Nullable;
import java.util.List;

public class DressArmorItem extends GeoArmorItem implements IAnimatable {
    private AnimationFactory factory = new AnimationFactory(this);

    public DressArmorItem(Item.Properties props, CustomArmorMaterial mat) {
        super(mat, EquipmentSlot.CHEST, props);
    }



    public static boolean isUsable(ItemStack stack) {
        return stack.getDamageValue() < stack.getMaxDamage() - 1;
    }

    public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand handIn) {
        ItemStack itemstack = playerIn.getItemInHand(handIn);
        EquipmentSlot equipmentslottype = Mob.getEquipmentSlotForItem(itemstack);
        ItemStack itemstack1 = playerIn.getItemBySlot(equipmentslottype);
        if (itemstack1.isEmpty()) {
            playerIn.setItemSlot(equipmentslottype, itemstack.copy());
            itemstack.setCount(0);
            return InteractionResultHolder.sidedSuccess(itemstack, worldIn.isClientSide());
        } else {
            return InteractionResultHolder.fail(itemstack);
        }
    }
    //是否可以像鞘翅一样飞行
    @Override
    public boolean canElytraFly(ItemStack stack, net.minecraft.world.entity.LivingEntity entity) {
        return ElytraItem.isFlyEnabled(stack);
    }
    //飞行时的属性,每隔1s掉一个耐久度
    @Override
    public boolean elytraFlightTick(ItemStack stack, net.minecraft.world.entity.LivingEntity entity, int flightTicks) {
        if (!entity.level.isClientSide && (flightTicks + 1) % 20 == 0) {
            stack.hurtAndBreak(1, entity, e -> e.broadcastBreakEvent(net.minecraft.world.entity.EquipmentSlot.CHEST));
        }
        return true;
    }
    //用什么修该物品
    public boolean isValidRepairItem(ItemStack toRepair, ItemStack repair) {
        return repair.getItem() == ItemInit.ORE.get();
    }

    public EquipmentSlot getEquipmentSlot(ItemStack stack) {
        return EquipmentSlot.CHEST;
    }


    @Override
    public void registerControllers(AnimationData data) {

    }

    @Override
    public AnimationFactory getFactory() {
        return factory;
    }

    @Override
    public void appendHoverText(ItemStack stack, Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
        tooltip.add(Component.translatable("tooltip.dress"));
    }
}
之后我们需要在armor包中新建model包->model包中新建我们的盔甲的模型类ModelDress:

ModelDress.java

public class ModelDress extends AnimatedGeoModel<DressArmorItem> {
    @Override
    public ResourceLocation getModelResource(DressArmorItem object) {
        return new ResourceLocation(Main.MOD_ID, "geo/dress.geo.json");
    }

    @Override
    public ResourceLocation getTextureResource(DressArmorItem object) {
        return new ResourceLocation(Main.MOD_ID, "textures/models/armor/dress.png");
    }

    @Override
    public ResourceLocation getAnimationResource(DressArmorItem animatable) {
        return new ResourceLocation(Main.MOD_ID, "animations/chandlier.animation.json");
    }
}
之后我们需要在armor包中新建render包->render包中新建我们的盔甲的渲染类RenderDress:

RenderDress.java

package net.joy187.joyggd.item.armor.render;


import net.joy187.joyggd.item.armor.ChandlierArmorItem;
import net.joy187.joyggd.item.armor.model.ModelChandlier;
import software.bernie.geckolib3.renderers.geo.GeoArmorRenderer;


public class RenderDress extends GeoArmorRenderer<DressArmorItem> {
    //渲染盔甲穿在身上的每一个部位的效果
    public RenderDress() {
        super(new ModelDress());
        //这里要和第二步你blockbench中建模的名称一一对应
        this.headBone = "head";
        this.bodyBone = "dress";
        this.rightArmBone = "rightArm";
        this.leftArmBone = "leftArm";
        this.rightLegBone = "rightLeg";
        this.leftLegBone = "leftLeg";
        this.rightBootBone = "rightBoot";
        this.leftBootBone = "leftBoot";
    }

}

3.在ClientModEventSubscriber类中将我们的盔甲渲染类进行注册:

ClientModEventSubscriber.java

@Mod.EventBusSubscriber(modid = Main.MOD_ID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ClientModEventSubscriber extends ModEventSubscriber{

    @SubscribeEvent
    public static void registerRenderers(final EntityRenderersEvent.AddLayers event) {
        //渲染类进行注册
        GeoArmorRenderer.registerArmorRenderer(DressArmorItem.class, RenderDress::new);
    }
}

4.在ItemInit类中将我们的盔甲进行声明

ItemInit.java

    public static final RegistryObject<Item> DRESS = ITEMS.register("dress",
            () -> new DressArmorItem(new Item.Properties().tab(Main.ITEM_TAB).durability(800), CustomArmorMaterial.ARMOR_MATERIAL_DRESS));

5.代码部分结束,之后参考教程3D动画盔甲第六步进行资源包制作。

6.保存所有文件 -> 进行测试:

在这里插入图片描述

从高处跳下,空格进行飞行!
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 类中绘制槽位和物品图标。 ###
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jay_fearless

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

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

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

打赏作者

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

抵扣说明:

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

余额充值