minecraft-src/net/minecraft/world/item/ToolMaterial.java
2025-07-04 03:45:38 +03:00

100 lines
4.6 KiB
Java

package net.minecraft.world.item;
import java.util.List;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.HolderSet;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.ItemTags;
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.EquipmentSlotGroup;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.component.ItemAttributeModifiers;
import net.minecraft.world.item.component.Tool;
import net.minecraft.world.item.component.Weapon;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
public record ToolMaterial(
TagKey<Block> incorrectBlocksForDrops, int durability, float speed, float attackDamageBonus, int enchantmentValue, TagKey<Item> repairItems
) {
public static final ToolMaterial WOOD = new ToolMaterial(BlockTags.INCORRECT_FOR_WOODEN_TOOL, 59, 2.0F, 0.0F, 15, ItemTags.WOODEN_TOOL_MATERIALS);
public static final ToolMaterial STONE = new ToolMaterial(BlockTags.INCORRECT_FOR_STONE_TOOL, 131, 4.0F, 1.0F, 5, ItemTags.STONE_TOOL_MATERIALS);
public static final ToolMaterial IRON = new ToolMaterial(BlockTags.INCORRECT_FOR_IRON_TOOL, 250, 6.0F, 2.0F, 14, ItemTags.IRON_TOOL_MATERIALS);
public static final ToolMaterial DIAMOND = new ToolMaterial(BlockTags.INCORRECT_FOR_DIAMOND_TOOL, 1561, 8.0F, 3.0F, 10, ItemTags.DIAMOND_TOOL_MATERIALS);
public static final ToolMaterial GOLD = new ToolMaterial(BlockTags.INCORRECT_FOR_GOLD_TOOL, 32, 12.0F, 0.0F, 22, ItemTags.GOLD_TOOL_MATERIALS);
public static final ToolMaterial NETHERITE = new ToolMaterial(BlockTags.INCORRECT_FOR_NETHERITE_TOOL, 2031, 9.0F, 4.0F, 15, ItemTags.NETHERITE_TOOL_MATERIALS);
private Item.Properties applyCommonProperties(Item.Properties properties) {
return properties.durability(this.durability).repairable(this.repairItems).enchantable(this.enchantmentValue);
}
public Item.Properties applyToolProperties(
Item.Properties properties, TagKey<Block> mineableBlocks, float attackDamage, float attackSpeed, float disableBlockingForSeconfs
) {
HolderGetter<Block> holderGetter = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.BLOCK);
return this.applyCommonProperties(properties)
.component(
DataComponents.TOOL,
new Tool(
List.of(
Tool.Rule.deniesDrops(holderGetter.getOrThrow(this.incorrectBlocksForDrops)),
Tool.Rule.minesAndDrops(holderGetter.getOrThrow(mineableBlocks), this.speed)
),
1.0F,
1,
true
)
)
.attributes(this.createToolAttributes(attackDamage, attackSpeed))
.component(DataComponents.WEAPON, new Weapon(2, disableBlockingForSeconfs));
}
private ItemAttributeModifiers createToolAttributes(float attackDamage, float attackSpeed) {
return ItemAttributeModifiers.builder()
.add(
Attributes.ATTACK_DAMAGE,
new AttributeModifier(Item.BASE_ATTACK_DAMAGE_ID, attackDamage + this.attackDamageBonus, AttributeModifier.Operation.ADD_VALUE),
EquipmentSlotGroup.MAINHAND
)
.add(
Attributes.ATTACK_SPEED, new AttributeModifier(Item.BASE_ATTACK_SPEED_ID, attackSpeed, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND
)
.build();
}
public Item.Properties applySwordProperties(Item.Properties properties, float attackDamage, float attackSpeed) {
HolderGetter<Block> holderGetter = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.BLOCK);
return this.applyCommonProperties(properties)
.component(
DataComponents.TOOL,
new Tool(
List.of(
Tool.Rule.minesAndDrops(HolderSet.direct(Blocks.COBWEB.builtInRegistryHolder()), 15.0F),
Tool.Rule.overrideSpeed(holderGetter.getOrThrow(BlockTags.SWORD_INSTANTLY_MINES), Float.MAX_VALUE),
Tool.Rule.overrideSpeed(holderGetter.getOrThrow(BlockTags.SWORD_EFFICIENT), 1.5F)
),
1.0F,
2,
false
)
)
.attributes(this.createSwordAttributes(attackDamage, attackSpeed))
.component(DataComponents.WEAPON, new Weapon(1));
}
private ItemAttributeModifiers createSwordAttributes(float attackDamage, float attackSpeed) {
return ItemAttributeModifiers.builder()
.add(
Attributes.ATTACK_DAMAGE,
new AttributeModifier(Item.BASE_ATTACK_DAMAGE_ID, attackDamage + this.attackDamageBonus, AttributeModifier.Operation.ADD_VALUE),
EquipmentSlotGroup.MAINHAND
)
.add(
Attributes.ATTACK_SPEED, new AttributeModifier(Item.BASE_ATTACK_SPEED_ID, attackSpeed, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND
)
.build();
}
}