Featured image of post MC 1.21.4 盔甲物品注册及相关类详解

MC 1.21.4 盔甲物品注册及相关类详解

FirstFabricModDev 7

参考视频:

盔甲注册的新方法

同"《MC 1.21.4 中工具物品注册方法及相关类变更详解》“这篇博客中所写的register方法大致一样,这里给个例子:

1
2
3
public class ModItems {
  public static final Item GA_HELMET = register("ga_helmet", settings -> new ArmorItem(ModArmorMaterials.GALLIUM_ARMOR, EquipmentType.HELMET, settings));
}

ModArmorMaterials类的变化

好消息:你Mojang又把这ModArmorMaterials改回interface(接口)了(真是活母操作)!
于是你只需要新建一个接口类然后定义变量就好了。具体例子如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public interface ModArmorMaterials {
    ArmorMaterial GALLIUM = new ArmorMaterial(20, Util.make(new EnumMap(EquipmentType.class), map -> {
        map.put(EquipmentType.BOOTS, 3);
        map.put(EquipmentType.LEGGINGS, 6);
        map.put(EquipmentType.CHESTPLATE, 8);
        map.put(EquipmentType.HELMET, 3);
        map.put(EquipmentType.BODY, 11);
    }), 10, SoundEvents.ITEM_ARMOR_EQUIP_IRON, 2.0F, 0.0F, ModItemTags.GALLIUM_TAG, ModEquipmentAssetKeys.GALLIUM);
    //SoundEvents.ITEM_ARMOR_EQUIP_IRON表示穿戴音效
    //ModItemTags.GALLIUM_TAG表示修复材料
    //ModEquipmentAssetKeys.GALLIUM表示装备穿戴的时候渲染相关的量(我不确定)
}

ModEquipmentAssetKeys类

这个是表示装备穿戴的时候渲染相关的量(我不确定),总之也是个接口类,写上如下变量:

1
2
3
4
5
6
public interface ModEquipmentAssetKeys {
    RegistryKey<EquipmentAsset> GALLIUM = register("gallium");
    static RegistryKey<EquipmentAsset> register(String name) {
        return RegistryKey.of(REGISTRY_KEY, Identifier.of(<YOURMODID>,name));
    }
}

PS:注意MOD_ID(这玩意卡了我半天,甚至还去Discord问了,结果是直接用了原版的方法发现原版写的是Identifier.ofVanilla(name),淦!)。

盔甲材质文件目录的变更

这里先给出老版本目录第三人称盔甲材质的目录结构:
/main/resources/assets/<YOURMODID>/textures/models/armor/
然后新版本改到了:
/main/resources/assets/<YOURMODID>/textures/entity/equipment/humanoid
/main/resources/assets/<YOURMODID>/textures/entity/equipment/humanoid_leggings
此外你还需要在: /main/resources/assets/<YOURMODID>/equipment下新建一个json,
名字格式为RegistryKey<EquipmentAsset>变量名
内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
  "layers": {
    "humanoid": [
      {
        "texture": "firstfabricmod:gallium"
      }
    ],
    "humanoid_leggings": [
      {
        "texture": "firstfabricmod:gallium"
      }
    ]
  }
}

注:这里省了马铠的声明,具体见原版的json。

getMaterial()方法的移除

Mojang移除了Item类中的getMaterial()方法,这导致在后面实现穿戴盔甲时给予玩家药水效果时不能用ExampleHelmet.getMaterial() = YourMaterial这样的句子,于是我直接改用了如下判断:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class ModArmorItem extends ArmorItem {
  ···
    private boolean hasCorrectArmorSet(PlayerEntity player) {
        Item helmet = player.getInventory().getArmorStack(3).getItem();
        Item chestplate = player.getInventory().getArmorStack(2).getItem();
        Item legging = player.getInventory().getArmorStack(1).getItem();
        Item boots = player.getInventory().getArmorStack(0).getItem();

        return helmet == ModItems.GALLIUM_HELMET && chestplate == ModItems.GALLIUM_CHESTPLATE &&
                legging == ModItems.GALLIUM_LEGGINGS && boots == ModItems.GALLIUM_BOOTS;
                //这里直接判断物品相等
    }
  ···
}

参考文档

Fabric文档:自定义盔甲

本作品采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可(CC BY-NC-SA 4.0)
文章浏览量:Loading
Powered By MC ZBD Studio
发表了21篇文章 · 总计29.03k字
载入天数...载入时分秒...
总浏览量Loading | 访客总数Loading

主题 StackJimmy 设计
由ZephyrBD修改