minecraft-source/src/net/minecraft/world/item/SwordItem.java

79 lines
3.2 KiB
Java

package net.minecraft.world.item;
import net.minecraft.world.entity.monster.SharedMonsterAttributes;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import com.google.common.collect.Multimap;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.block.Block;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
public class SwordItem extends TieredItem {
private final float attackDamage;
private final float attackSpeed;
public SwordItem(final Tier bfp, final int integer, final float float3, final Properties a) {
super(bfp, a);
this.attackSpeed = float3;
this.attackDamage = integer + bfp.getAttackDamageBonus();
}
public float getDamage() {
return this.attackDamage;
}
@Override
public boolean canAttackBlock(final BlockState byg, final Level bjt, final BlockPos fk, final Player ayg) {
return !ayg.isCreative();
}
@Override
public float getDestroySpeed(final ItemStack bek, final BlockState byg) {
final Block bpe4 = byg.getBlock();
if (bpe4 == Blocks.COBWEB) {
return 15.0f;
}
final Material cok5 = byg.getMaterial();
if (cok5 == Material.PLANT || cok5 == Material.REPLACEABLE_PLANT || cok5 == Material.CORAL || byg.is(BlockTags.LEAVES) || cok5 == Material.VEGETABLE) {
return 1.5f;
}
return 1.0f;
}
@Override
public boolean hurtEnemy(final ItemStack bek, final LivingEntity akw2, final LivingEntity akw3) {
bek.<LivingEntity>hurtAndBreak(1, akw3, akw -> akw.broadcastBreakEvent(EquipmentSlot.MAINHAND));
return true;
}
@Override
public boolean mineBlock(final ItemStack bek, final Level bjt, final BlockState byg, final BlockPos fk, final LivingEntity akw) {
if (byg.getDestroySpeed(bjt, fk) != 0.0f) {
bek.<LivingEntity>hurtAndBreak(2, akw, akw -> akw.broadcastBreakEvent(EquipmentSlot.MAINHAND));
}
return true;
}
@Override
public boolean canDestroySpecial(final BlockState byg) {
return byg.getBlock() == Blocks.COBWEB;
}
@Override
public Multimap<String, AttributeModifier> getDefaultAttributeModifiers(final EquipmentSlot aks) {
final Multimap<String, AttributeModifier> multimap3 = super.getDefaultAttributeModifiers(aks);
if (aks == EquipmentSlot.MAINHAND) {
multimap3.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(SwordItem.BASE_ATTACK_DAMAGE_UUID, "Weapon modifier", this.attackDamage, AttributeModifier.Operation.ADDITION));
multimap3.put(SharedMonsterAttributes.ATTACK_SPEED.getName(), new AttributeModifier(SwordItem.BASE_ATTACK_SPEED_UUID, "Weapon modifier", this.attackSpeed, AttributeModifier.Operation.ADDITION));
}
return multimap3;
}
}