SnowballItem .java

net.minecraft.world.item.SnowballItem (class): members: PROJECTILE_SHOOT_POWER, asProjectile, use. imports: Snowball, Position, Projectile, Stats.

net.minecraft.world.itemclass50 linescommonextends Item1.6 KB

Implements

API Surface (2)

InteractionResult use(final Level level, final Player player, final InteractionHand hand)
Projectile asProjectile(final Level level, final Position position, final ItemStack itemStack, final Direction direction)

Fields & Constants (1)

PROJECTILE_SHOOT_POWER

Full Source

SnowballItem.java50 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import net.minecraft.core.Direction;
4
import net.minecraft.core.Position;
5
import net.minecraft.server.level.ServerLevel;
6
import net.minecraft.sounds.SoundEvents;
7
import net.minecraft.sounds.SoundSource;
8
import net.minecraft.stats.Stats;
9
import net.minecraft.world.InteractionHand;
10
import net.minecraft.world.InteractionResult;
11
import net.minecraft.world.entity.player.Player;
12
import net.minecraft.world.entity.projectile.Projectile;
13
import net.minecraft.world.entity.projectile.throwableitemprojectile.Snowball;
14
import net.minecraft.world.level.Level;
15
16
public class SnowballItem extends Item implements ProjectileItem {
17
	public static final float PROJECTILE_SHOOT_POWER = 1.5F;
18
19
	public SnowballItem(final Item.Properties properties) {
20
		super(properties);
21
	}
22
23
	@Override
24
	public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
25
		ItemStack itemStack = player.getItemInHand(hand);
26
		level.playSound(
27
			null,
28
			player.getX(),
29
			player.getY(),
30
			player.getZ(),
31
			SoundEvents.SNOWBALL_THROW,
32
			SoundSource.NEUTRAL,
33
			0.5F,
34
			0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F)
35
		);
36
		if (level instanceof ServerLevel serverLevel) {
37
			Projectile.spawnProjectileFromRotation(Snowball::new, serverLevel, itemStack, player, 0.0F, 1.5F, 1.0F);
38
		}
39
40
		player.awardStat(Stats.ITEM_USED.get(this));
41
		itemStack.consume(1, player);
42
		return InteractionResult.SUCCESS;
43
	}
44
45
	@Override
46
	public Projectile asProjectile(final Level level, final Position position, final ItemStack itemStack, final Direction direction) {
47
		return new Snowball(level, position.x(), position.y(), position.z(), itemStack);
48
	}
49
}
50

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