ToolMaterial .java

net.minecraft.world.item.ToolMaterial (record, 110 lines): members: incorrectBlocksForDrops, durability, speed, attackDamageBonus, enchantmentValue, WOOD.

net.minecraft.world.itemrecord110 linescommon5.0 KB

API Surface (3)

record ToolMaterial(TagKey<Block> incorrectBlocksForDrops, int durability, float speed, float attackDamageBonus, int enchantmentValue, TagKey<Item> repairItems)
Item.Properties applyToolProperties(final Item.Properties properties, final TagKey<Block> minesEfficiently, final float attackDamageBaseline, final float attackSpeedBaseline, final float disableBlockingSeconds)
Item.Properties applySwordProperties(final Item.Properties properties, final float attackDamageBaseline, final float attackSpeedBaseline)

Fields & Constants (11)

WOOD
STONE
COPPER
IRON
DIAMOND
GOLD
NETHERITE
properties
minesEfficiently
attackDamageBaseline
attackSpeedBaseline

Full Source

ToolMaterial.java110 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import java.util.List;
4
import net.minecraft.core.HolderGetter;
5
import net.minecraft.core.HolderSet;
6
import net.minecraft.core.component.DataComponents;
7
import net.minecraft.core.registries.BuiltInRegistries;
8
import net.minecraft.tags.BlockTags;
9
import net.minecraft.tags.ItemTags;
10
import net.minecraft.tags.TagKey;
11
import net.minecraft.world.entity.EquipmentSlotGroup;
12
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
13
import net.minecraft.world.entity.ai.attributes.Attributes;
14
import net.minecraft.world.item.component.ItemAttributeModifiers;
15
import net.minecraft.world.item.component.Tool;
16
import net.minecraft.world.item.component.Weapon;
17
import net.minecraft.world.level.block.Block;
18
import net.minecraft.world.level.block.Blocks;
19
20
public record ToolMaterial(
21
	TagKey<Block> incorrectBlocksForDrops, int durability, float speed, float attackDamageBonus, int enchantmentValue, TagKey<Item> repairItems
22
) {
23
	public static final ToolMaterial WOOD = new ToolMaterial(BlockTags.INCORRECT_FOR_WOODEN_TOOL, 59, 2.0F, 0.0F, 15, ItemTags.WOODEN_TOOL_MATERIALS);
24
	public static final ToolMaterial STONE = new ToolMaterial(BlockTags.INCORRECT_FOR_STONE_TOOL, 131, 4.0F, 1.0F, 5, ItemTags.STONE_TOOL_MATERIALS);
25
	public static final ToolMaterial COPPER = new ToolMaterial(BlockTags.INCORRECT_FOR_COPPER_TOOL, 190, 5.0F, 1.0F, 13, ItemTags.COPPER_TOOL_MATERIALS);
26
	public static final ToolMaterial IRON = new ToolMaterial(BlockTags.INCORRECT_FOR_IRON_TOOL, 250, 6.0F, 2.0F, 14, ItemTags.IRON_TOOL_MATERIALS);
27
	public static final ToolMaterial DIAMOND = new ToolMaterial(BlockTags.INCORRECT_FOR_DIAMOND_TOOL, 1561, 8.0F, 3.0F, 10, ItemTags.DIAMOND_TOOL_MATERIALS);
28
	public static final ToolMaterial GOLD = new ToolMaterial(BlockTags.INCORRECT_FOR_GOLD_TOOL, 32, 12.0F, 0.0F, 22, ItemTags.GOLD_TOOL_MATERIALS);
29
	public static final ToolMaterial NETHERITE = new ToolMaterial(BlockTags.INCORRECT_FOR_NETHERITE_TOOL, 2031, 9.0F, 4.0F, 15, ItemTags.NETHERITE_TOOL_MATERIALS);
30
31
	private Item.Properties applyCommonProperties(final Item.Properties properties) {
32
		return properties.durability(this.durability).repairable(this.repairItems).enchantable(this.enchantmentValue);
33
	}
34
35
	public Item.Properties applyToolProperties(
36
		final Item.Properties properties,
37
		final TagKey<Block> minesEfficiently,
38
		final float attackDamageBaseline,
39
		final float attackSpeedBaseline,
40
		final float disableBlockingSeconds
41
	) {
42
		HolderGetter<Block> registrationLookup = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.BLOCK);
43
		return this.applyCommonProperties(properties)
44
			.component(
45
				DataComponents.TOOL,
46
				new Tool(
47
					List.of(
48
						Tool.Rule.deniesDrops(registrationLookup.getOrThrow(this.incorrectBlocksForDrops)),
49
						Tool.Rule.minesAndDrops(registrationLookup.getOrThrow(minesEfficiently), this.speed)
50
					),
51
					1.0F,
52
					1,
53
					true
54
				)
55
			)
56
			.attributes(this.createToolAttributes(attackDamageBaseline, attackSpeedBaseline))
57
			.component(DataComponents.WEAPON, new Weapon(2, disableBlockingSeconds));
58
	}
59
60
	private ItemAttributeModifiers createToolAttributes(final float attackDamageBaseline, final float attackSpeedBaseline) {
61
		return ItemAttributeModifiers.builder()
62
			.add(
63
				Attributes.ATTACK_DAMAGE,
64
				new AttributeModifier(Item.BASE_ATTACK_DAMAGE_ID, attackDamageBaseline + this.attackDamageBonus, AttributeModifier.Operation.ADD_VALUE),
65
				EquipmentSlotGroup.MAINHAND
66
			)
67
			.add(
68
				Attributes.ATTACK_SPEED,
69
				new AttributeModifier(Item.BASE_ATTACK_SPEED_ID, attackSpeedBaseline, AttributeModifier.Operation.ADD_VALUE),
70
				EquipmentSlotGroup.MAINHAND
71
			)
72
			.build();
73
	}
74
75
	public Item.Properties applySwordProperties(final Item.Properties properties, final float attackDamageBaseline, final float attackSpeedBaseline) {
76
		HolderGetter<Block> registrationLookup = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.BLOCK);
77
		return this.applyCommonProperties(properties)
78
			.component(
79
				DataComponents.TOOL,
80
				new Tool(
81
					List.of(
82
						Tool.Rule.minesAndDrops(HolderSet.direct(Blocks.COBWEB.builtInRegistryHolder()), 15.0F),
83
						Tool.Rule.overrideSpeed(registrationLookup.getOrThrow(BlockTags.SWORD_INSTANTLY_MINES), Float.MAX_VALUE),
84
						Tool.Rule.overrideSpeed(registrationLookup.getOrThrow(BlockTags.SWORD_EFFICIENT), 1.5F)
85
					),
86
					1.0F,
87
					2,
88
					false
89
				)
90
			)
91
			.attributes(this.createSwordAttributes(attackDamageBaseline, attackSpeedBaseline))
92
			.component(DataComponents.WEAPON, new Weapon(1));
93
	}
94
95
	private ItemAttributeModifiers createSwordAttributes(final float attackDamageBaseline, final float attackSpeedBaseline) {
96
		return ItemAttributeModifiers.builder()
97
			.add(
98
				Attributes.ATTACK_DAMAGE,
99
				new AttributeModifier(Item.BASE_ATTACK_DAMAGE_ID, attackDamageBaseline + this.attackDamageBonus, AttributeModifier.Operation.ADD_VALUE),
100
				EquipmentSlotGroup.MAINHAND
101
			)
102
			.add(
103
				Attributes.ATTACK_SPEED,
104
				new AttributeModifier(Item.BASE_ATTACK_SPEED_ID, attackSpeedBaseline, AttributeModifier.Operation.ADD_VALUE),
105
				EquipmentSlotGroup.MAINHAND
106
			)
107
			.build();
108
	}
109
}
110

Decompiled Minecraft 26.3 source of ToolMaterial (net/minecraft/world/item/ToolMaterial.java) with Mojang official mappings, stored verbatim on the site.