PiercingWeapon .java

net.minecraft.world.item.component.PiercingWeapon (record, 93 lines): members: dealsKnockback, dismounts, sound, hitSound, makeHitSound, makeSound, attack.

net.minecraft.world.item.componentrecord93 linescommon3.8 KB

API Surface (5)

record PiercingWeapon(boolean dealsKnockback, boolean dismounts, Optional<Holder<SoundEvent>> sound, Optional<Holder<SoundEvent>> hitSound)
void makeSound(final Entity causer)
void makeHitSound(final Entity causer)
boolean canHitEntity(final Entity jabber, final Entity target)
void attack(final LivingEntity attacker, final EquipmentSlot hand)

Fields & Constants (2)

CODEC
STREAM_CODEC

String literals (4)

"deals_knockback"
"dismounts"
"sound"
"hit_sound"

Full Source

PiercingWeapon.java93 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.component;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import java.util.Collection;
6
import java.util.List;
7
import java.util.Optional;
8
import net.minecraft.core.Holder;
9
import net.minecraft.network.RegistryFriendlyByteBuf;
10
import net.minecraft.network.codec.ByteBufCodecs;
11
import net.minecraft.network.codec.StreamCodec;
12
import net.minecraft.sounds.SoundEvent;
13
import net.minecraft.world.InteractionHand;
14
import net.minecraft.world.entity.Entity;
15
import net.minecraft.world.entity.EquipmentSlot;
16
import net.minecraft.world.entity.Interaction;
17
import net.minecraft.world.entity.LivingEntity;
18
import net.minecraft.world.entity.ai.attributes.Attributes;
19
import net.minecraft.world.entity.player.Player;
20
import net.minecraft.world.entity.projectile.ProjectileUtil;
21
import net.minecraft.world.item.ItemStack;
22
import net.minecraft.world.level.ClipContext;
23
import net.minecraft.world.phys.EntityHitResult;
24
25
public record PiercingWeapon(boolean dealsKnockback, boolean dismounts, Optional<Holder<SoundEvent>> sound, Optional<Holder<SoundEvent>> hitSound) {
26
	public static final Codec<PiercingWeapon> CODEC = RecordCodecBuilder.create(
27
		i -> i.group(
28
				Codec.BOOL.optionalFieldOf("deals_knockback", true).forGetter(PiercingWeapon::dealsKnockback),
29
				Codec.BOOL.optionalFieldOf("dismounts", false).forGetter(PiercingWeapon::dismounts),
30
				SoundEvent.CODEC.optionalFieldOf("sound").forGetter(PiercingWeapon::sound),
31
				SoundEvent.CODEC.optionalFieldOf("hit_sound").forGetter(PiercingWeapon::hitSound)
32
			)
33
			.apply(i, PiercingWeapon::new)
34
	);
35
	public static final StreamCodec<RegistryFriendlyByteBuf, PiercingWeapon> STREAM_CODEC = StreamCodec.composite(
36
		ByteBufCodecs.BOOL,
37
		PiercingWeapon::dealsKnockback,
38
		ByteBufCodecs.BOOL,
39
		PiercingWeapon::dismounts,
40
		SoundEvent.STREAM_CODEC.apply(ByteBufCodecs::optional),
41
		PiercingWeapon::sound,
42
		SoundEvent.STREAM_CODEC.apply(ByteBufCodecs::optional),
43
		PiercingWeapon::hitSound,
44
		PiercingWeapon::new
45
	);
46
47
	public void makeSound(final Entity causer) {
48
		this.sound
49
			.ifPresent(s -> causer.level().playSound(causer, causer.getX(), causer.getY(), causer.getZ(), (Holder<SoundEvent>)s, causer.getSoundSource(), 1.0F, 1.0F));
50
	}
51
52
	public void makeHitSound(final Entity causer) {
53
		this.hitSound
54
			.ifPresent(s -> causer.level().playSound(null, causer.getX(), causer.getY(), causer.getZ(), (Holder<SoundEvent>)s, causer.getSoundSource(), 1.0F, 1.0F));
55
	}
56
57
	public static boolean canHitEntity(final Entity jabber, final Entity target) {
58
		if (target.isInvulnerableToPiercingWeapon() || !target.isAlive()) {
59
			return false;
60
		} else if (target instanceof Interaction) {
61
			return true;
62
		} else if (!target.canBeHitByProjectile()) {
63
			return false;
64
		} else {
65
			return target instanceof Player targetPlayer && jabber instanceof Player jabbingPlayer && !jabbingPlayer.canHarmPlayer(targetPlayer)
66
				? false
67
				: !jabber.isPassengerOfSameVehicle(target);
68
		}
69
	}
70
71
	public void attack(final LivingEntity attacker, final EquipmentSlot hand) {
72
		float damage = (float)attacker.getAttributeValue(Attributes.ATTACK_DAMAGE);
73
		ItemStack weaponItem = attacker.getItemBySlot(hand);
74
		SwingAnimation swingAnimation = weaponItem.getAttackAnimation();
75
		AttackRange attackRange = attacker.getAttackRangeWith(weaponItem);
76
		boolean hitSomething = false;
77
78
		for (EntityHitResult hitResult : ProjectileUtil.getHitEntitiesAlong(attacker, attackRange, e1 -> canHitEntity(attacker, e1), ClipContext.Block.COLLIDER)
79
			.map(a -> List.of(), e -> (Collection)e)) {
80
			hitSomething |= attacker.stabAttack(hand, hitResult.getEntity(), damage, true, this.dealsKnockback, this.dismounts);
81
		}
82
83
		attacker.onAttack();
84
		attacker.postPiercingAttack();
85
		if (hitSomething) {
86
			this.makeHitSound(attacker);
87
		}
88
89
		this.makeSound(attacker);
90
		attacker.swingAndResetAttackStrength(InteractionHand.MAIN_HAND, swingAnimation, false);
91
	}
92
}
93

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