跳到主要内容

15工具

由于版本差异,这里还是改用1.20.1版本的供学习。

工具枚举

/**
* @author houyunfei
*/
public enum ModToolMaterial implements ToolMaterial {
RUBY(5, 650, 7.0F, 4.5F, 26, () -> Ingredient.ofItems(ModItems.RUBY));

private final int miningLevel;
private final int durability;
private final float miningSpeed;
private final float attackDamage;
private final int enchantability;
private final Supplier<Ingredient> repairIngredient;

ModToolMaterial(int miningLevel, int durability, float miningSpeed, float attackDamage, int enchantability, Supplier<Ingredient> repairIngredient) {
this.miningLevel = miningLevel;
this.durability = durability;
this.miningSpeed = miningSpeed;
this.attackDamage = attackDamage;
this.enchantability = enchantability;
this.repairIngredient = repairIngredient;
}

@Override
public int getDurability() {
return this.durability;
}

@Override
public float getMiningSpeedMultiplier() {
return this.miningSpeed;
}

@Override
public float getAttackDamage() {
return this.attackDamage;
}

@Override
public int getMiningLevel() {
return this.miningLevel;
}

@Override
public int getEnchantability() {
return this.enchantability;
}

@Override
public Ingredient getRepairIngredient() {
return this.repairIngredient.get();
}
}

在ModItems中添加对应的工具,主要是剑,斧头,镐子这些

public static final Item RUBY_PICKAXE = registerItem("ruby_pickaxe",
new PickaxeItem(ModToolMaterial.RUBY, 2, 2f, new FabricItemSettings()));
public static final Item RUBY_AXE = registerItem("ruby_axe",
new AxeItem(ModToolMaterial.RUBY, 3, 1f, new FabricItemSettings()));
public static final Item RUBY_SHOVEL = registerItem("ruby_shovel",
new ShovelItem(ModToolMaterial.RUBY, 0, 0f, new FabricItemSettings()));
public static final Item RUBY_SWORD = registerItem("ruby_sword",
new SwordItem(ModToolMaterial.RUBY, 5, 3f, new FabricItemSettings()));
public static final Item RUBY_HOE = registerItem("ruby_hoe",
new HoeItem(ModToolMaterial.RUBY, 0, 0f, new FabricItemSettings()));

在ModItemGroups添加到物品组:

entries.add(ModItems.RUBY_PICKAXE);
entries.add(ModItems.RUBY_AXE);
entries.add(ModItems.RUBY_SHOVEL);
entries.add(ModItems.RUBY_SWORD);
entries.add(ModItems.RUBY_HOE)

生成器配置:

添加一个挖掘等级为五的方块:ModBlockTagProvider中

getOrCreateTagBuilder(TagKey.of(RegistryKeys.BLOCK, new Identifier("fabric", "needs_tool_level_5")))
.add(ModBlocks.SOUND_BLOCK);

ModModelProvider中添加上述方块对应的json

itemModelGenerator.register(ModItems.RUBY_PICKAXE, Models.HANDHELD);
itemModelGenerator.register(ModItems.RUBY_AXE, Models.HANDHELD);
itemModelGenerator.register(ModItems.RUBY_SHOVEL, Models.HANDHELD);
itemModelGenerator.register(ModItems.RUBY_SWORD, Models.HANDHELD);
itemModelGenerator.register(ModItems.RUBY_HOE, Models.HANDHELD);

效果

对应的几种工具:

image-20240824210816231

并且只有红宝石镐子才可以挖声音块