ThrowablePotionItem .java

net.minecraft.world.item.ThrowablePotionItem (class, 51 lines): members: createPotion, PROJECTILE_SHOOT_POWER, createDispenseConfig, asProjectile, use.

net.minecraft.world.itemclass51 linescommonextends PotionItem1.9 KB

Implements

API Surface (3)

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)
ProjectileItem.DispenseConfig createDispenseConfig()

Fields & Constants (1)

PROJECTILE_SHOOT_POWER

Full Source

ThrowablePotionItem.java51 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.stats.Stats;
7
import net.minecraft.world.InteractionHand;
8
import net.minecraft.world.InteractionResult;
9
import net.minecraft.world.entity.LivingEntity;
10
import net.minecraft.world.entity.player.Player;
11
import net.minecraft.world.entity.projectile.Projectile;
12
import net.minecraft.world.entity.projectile.throwableitemprojectile.AbstractThrownPotion;
13
import net.minecraft.world.level.Level;
14
15
public abstract class ThrowablePotionItem extends PotionItem implements ProjectileItem {
16
	public static final float PROJECTILE_SHOOT_POWER = 0.5F;
17
18
	public ThrowablePotionItem(final Item.Properties properties) {
19
		super(properties);
20
	}
21
22
	@Override
23
	public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
24
		ItemStack itemStack = player.getItemInHand(hand);
25
		if (level instanceof ServerLevel serverLevel) {
26
			Projectile.spawnProjectileFromRotation(this::createPotion, serverLevel, itemStack, player, -20.0F, 0.5F, 1.0F);
27
		}
28
29
		player.awardStat(Stats.ITEM_USED.get(this));
30
		itemStack.consume(1, player);
31
		return InteractionResult.SUCCESS;
32
	}
33
34
	protected abstract AbstractThrownPotion createPotion(ServerLevel level, LivingEntity owner, ItemStack itemStack);
35
36
	protected abstract AbstractThrownPotion createPotion(Level level, Position position, ItemStack itemStack);
37
38
	@Override
39
	public Projectile asProjectile(final Level level, final Position position, final ItemStack itemStack, final Direction direction) {
40
		return this.createPotion(level, position, itemStack);
41
	}
42
43
	@Override
44
	public ProjectileItem.DispenseConfig createDispenseConfig() {
45
		return ProjectileItem.DispenseConfig.builder()
46
			.uncertainty(ProjectileItem.DispenseConfig.DEFAULT.uncertainty() * 0.5F)
47
			.power(ProjectileItem.DispenseConfig.DEFAULT.power() * 1.25F)
48
			.build();
49
	}
50
}
51

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