EggItem .java

net.minecraft.world.item.EggItem (class, 43 lines): members: PROJECTILE_SHOOT_POWER, asProjectile, use. imports: ThrownEgg, Position, Projectile, Stats.

net.minecraft.world.itemclass43 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

EggItem.java43 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.ThrownEgg;
14
import net.minecraft.world.level.Level;
15
16
public class EggItem extends Item implements ProjectileItem {
17
	public static final float PROJECTILE_SHOOT_POWER = 1.5F;
18
19
	public EggItem(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, player.getX(), player.getY(), player.getZ(), SoundEvents.EGG_THROW, SoundSource.PLAYERS, 0.5F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F)
28
		);
29
		if (level instanceof ServerLevel serverLevel) {
30
			Projectile.spawnProjectileFromRotation(ThrownEgg::new, serverLevel, itemStack, player, 0.0F, 1.5F, 1.0F);
31
		}
32
33
		player.awardStat(Stats.ITEM_USED.get(this));
34
		itemStack.consume(1, player);
35
		return InteractionResult.SUCCESS;
36
	}
37
38
	@Override
39
	public Projectile asProjectile(final Level level, final Position position, final ItemStack itemStack, final Direction direction) {
40
		return new ThrownEgg(level, position.x(), position.y(), position.z(), itemStack);
41
	}
42
}
43

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