前言
通过fabric编写模组,创建一套自定义工具(铜镐
,铜锄
,铜锹
,铜斧
,铜剑
)
在 Minecraft 开发中,ToolMaterials
类是一个非常重要的类,它主要用于定义工具的材质属性。
首先,创建一个铜质的工具材料,统一管理耐久度
,挖掘速度
,攻击力
,附魔能力
等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
public class CopperToolMaterial implements ToolMaterial {
public static final CopperToolMaterial INSTANCE = new CopperToolMaterial();
private final Supplier<Ingredient> repairIngredient = () -> Ingredient.ofItems(Items.COPPER_INGOT);
@Override public int getDurability() { return 200; }
@Override public float getMiningSpeedMultiplier() { return 5.0f; }
@Override public float getAttackDamage() { return 1.5f; }
@Override public TagKey<Block> getInverseTag() { return BlockTags.INCORRECT_FOR_STONE_TOOL; }
@Override public int getEnchantability() { return 10; }
@Override public Ingredient getRepairIngredient() { return repairIngredient.get(); }
public static Item register(Item item, String id) { Identifier itemID = Identifier.of("mymod", id); return Registry.register(Registries.ITEM, itemID, item); } }
|
注册物品
通过register
方法进行注册。分别将铜镐、铜锄、铜锹、铜斧和铜剑的攻击伤害
和攻击速度
修改为[3.5, 1, 4, 9, 5.5]
与[1.2, 2, 1, 0.8, 1.6]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| public class CopperPickaxe { public static final Logger LOGGER = LoggerFactory.getLogger("MyMod");
public static final Item copperPickaxe = CopperToolMaterial.register( new PickaxeItem(CopperToolMaterial.INSTANCE, new Item.Settings().attributeModifiers(PickaxeItem.createAttributeModifiers(CopperToolMaterial.INSTANCE, 1, -2.8f))), "copper_pickaxe" );
public static void initialize() { ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOLS) .register((itemGroup) -> itemGroup.add(copperPickaxe)); LOGGER.info("[DEBUG] 铜镐注册完成"); } }
public class CopperHoe { public static final Logger LOGGER = LoggerFactory.getLogger("MyMod");
public static final Item CopperHoe = CopperToolMaterial.register( new HoeItem(CopperToolMaterial.INSTANCE, new Item.Settings().attributeModifiers(HoeItem.createAttributeModifiers(CopperToolMaterial.INSTANCE, -1.5f, -2))), "copper_hoe" );
public static void initialize() { ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOLS) .register((itemGroup) -> itemGroup.add(CopperHoe));
LOGGER.info("[DEBUG] 铜锄注册完成"); } }
public class CopperShovel { public static final Logger LOGGER = LoggerFactory.getLogger("MyMod");
public static final Item CopperShovel = CopperToolMaterial.register( new ShovelItem(CopperToolMaterial.INSTANCE, new Item.Settings().attributeModifiers(ShovelItem.createAttributeModifiers(CopperToolMaterial.INSTANCE, 1.5f, -3))), "copper_shovel" );
public static void initialize() { ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOLS) .register((itemGroup) -> itemGroup.add(CopperShovel));
LOGGER.info("[DEBUG] 铜锹注册完成"); } }
public class CopperAxe { public static final Logger LOGGER = LoggerFactory.getLogger("MyMod");
public static final Item CopperAxe = CopperToolMaterial.register( new AxeItem(CopperToolMaterial.INSTANCE, new Item.Settings().attributeModifiers(AxeItem.createAttributeModifiers(CopperToolMaterial.INSTANCE, 6.5f, -3.2f))), "copper_axe" );
public static void initialize() { ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOLS) .register((itemGroup) -> itemGroup.add(CopperAxe));
LOGGER.info("[DEBUG] 铜斧注册完成"); } }
public class CopperSword { public static final Logger LOGGER = LoggerFactory.getLogger("MyMod");
public static final Item copperSword = CopperToolMaterial.register( new SwordItem(CopperToolMaterial.INSTANCE, new Item.Settings().attributeModifiers(SwordItem.createAttributeModifiers(CopperToolMaterial.INSTANCE, 3, -2.4f))), "copper_sword" );
public static void initialize() { ItemGroupEvents.modifyEntriesEvent(ItemGroups.COMBAT) .register((itemGroup) -> itemGroup.add(copperSword)); LOGGER.info("[DEBUG] 铜剑注册完成"); } }
|
添加翻译
en_us
打开main\resources\assets\mymod\lang\en_us.json
文件,不存在则新建,添加英语翻译
1 2 3 4 5 6 7
| { "item.mymod.copper_pickaxe": "Copper Pickaxe", "item.mymod.copper_axe": "Copper Axe", "item.mymod.copper_hoe": "Copper Hoe", "item.mymod.copper_shovel": "Copper Shovel", "item.mymod.copper_sword": "Copper Sword" }
|
zh_ch
打开main\resources\assets\mymod\lang\zh_ch.json
文件,不存在则新建,添加中文翻译
1 2 3 4 5 6 7
| { "item.mymod.copper_pickaxe": "铜镐", "item.mymod.copper_axe": "铜斧", "item.mymod.copper_hoe": "铜锄", "item.mymod.copper_shovel": "铜锹", "item.mymod.copper_sword": "铜剑" }
|
添加纹理模型
在main\resources\assets\mymod\models\item
文件夹下,新建copper_axe.json
,copper_hoe.json
,copper_pickaxe.json
, copper_shovel.json
,copper_sword.json
等文件,写入对应模型文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| { "parent": "item/handheld", "textures": { "layer0": "mymod:item/copper_axe" } }
{ "parent": "item/handheld", "textures": { "layer0": "mymod:item/copper_hoe" } }
{ "parent": "item/handheld", "textures": { "layer0": "mymod:item/copper_pickaxe" } }
{ "parent": "item/handheld", "textures": { "layer0": "mymod:item/copper_shovel" } }
{ "parent": "item/handheld", "textures": { "layer0": "mymod:item/copper_sword" } }
|
需要注意的是,此时parent填入的是item/handheld
模型,表示物品将使用默认的手持物品显示方式。
在main\resources\assets\mymod\textures\item
文件夹下,放入对应纹理文件。
合成
以上的代码已经可以在游戏里正常工作了,但是想要在生存模式中使用,就需要设计对应的合成方式。
在main\resources\data\mymod\recipe
文件夹下,分别新建对应工具的json文件,写入合成方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| { "type": "minecraft:crafting_shaped", "pattern": [ "ccc", " / ", " / " ], "key": { "c": { "item": "minecraft:copper_ingot" }, "/": { "item": "minecraft:stick" } }, "result": { "id": "mymod:copper_pickaxe", "count": 1 } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| { "type": "minecraft:crafting_shaped", "pattern": [ " c ", " / ", " / " ], "key": { "c": { "item": "minecraft:copper_ingot" }, "/": { "item": "minecraft:stick" } }, "result": { "id": "mymod:copper_shovel", "count": 1 } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| { "type": "minecraft:crafting_shaped", "pattern": [ "cc ", " / ", " / " ], "key": { "c": { "item": "minecraft:copper_ingot" }, "/": { "item": "minecraft:stick" } }, "result": { "id": "mymod:copper_hoe", "count": 1 } }
{ "type": "minecraft:crafting_shaped", "pattern": [ " cc", " / ", " / " ], "key": { "c": { "item": "minecraft:copper_ingot" }, "/": { "item": "minecraft:stick" } }, "result": { "id": "mymod:copper_hoe", "count": 1 } }
|


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| { "type": "minecraft:crafting_shaped", "pattern": [ " c ", " c ", " / " ], "key": { "c": { "item": "minecraft:copper_ingot" }, "/": { "item": "minecraft:stick" } }, "result": { "id": "mymod:copper_sword", "count": 1 } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| { "type": "minecraft:crafting_shaped", "pattern": [ "cc ", "c/ ", " / " ], "key": { "c": { "item": "minecraft:copper_ingot" }, "/": { "item": "minecraft:stick" } }, "result": { "id": "mymod:copper_axe", "count": 1 } }
{ "type": "minecraft:crafting_shaped", "pattern": [ " cc", " /c", " / " ], "key": { "c": { "item": "minecraft:copper_ingot" }, "/": { "item": "minecraft:stick" } }, "result": { "id": "mymod:copper_axe", "count": 1 } }
|


附魔
当这些自定义工具,放置到附魔台上时,发现无法进行附魔。查阅了一些资料发现,需要对这些工具打上附魔的tag
标签。
同样在main\resources\data\minecraft\tags\item\enchantable
文件夹下(文件夹不存在则新建),分别创建mining.json
和sword.json
文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| { "replace": false, "values": [ "mymod:copper_axe", "mymod:copper_hoe", "mymod:copper_pickaxe", "mymod:copper_shovel" ] }
{ "replace": false, "values": [ "mymod:copper_sword" ] }
|
此时,放到附魔台上就可以正常附魔了。
实现效果
