ThrownSplashPotion .java

net.minecraft.world.entity.projectile.throwableitemprojectile.ThrownSplashPotion (class, 78 lines): members: onHitAsPotion, getDefaultItem. imports: AABB.

net.minecraft.world.entity.projectile.throwableitemprojectileclass78 linescommonextends AbstractThrownPotion3.2 KB

API Surface (2)

Item getDefaultItem()
void onHitAsPotion(final ServerLevel level, final ItemStack potionItem, final HitResult hitResult)

Full Source

ThrownSplashPotion.java78 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.projectile.throwableitemprojectile;
2
3
import java.util.List;
4
import net.minecraft.core.Holder;
5
import net.minecraft.core.component.DataComponents;
6
import net.minecraft.server.level.ServerLevel;
7
import net.minecraft.world.effect.MobEffect;
8
import net.minecraft.world.effect.MobEffectInstance;
9
import net.minecraft.world.entity.Entity;
10
import net.minecraft.world.entity.EntityType;
11
import net.minecraft.world.entity.EntityTypes;
12
import net.minecraft.world.entity.LivingEntity;
13
import net.minecraft.world.entity.projectile.ProjectileUtil;
14
import net.minecraft.world.item.Item;
15
import net.minecraft.world.item.ItemStack;
16
import net.minecraft.world.item.Items;
17
import net.minecraft.world.item.alchemy.PotionContents;
18
import net.minecraft.world.level.Level;
19
import net.minecraft.world.phys.AABB;
20
import net.minecraft.world.phys.HitResult;
21
22
public class ThrownSplashPotion extends AbstractThrownPotion {
23
	public ThrownSplashPotion(final EntityType<? extends ThrownSplashPotion> type, final Level level) {
24
		super(type, level);
25
	}
26
27
	public ThrownSplashPotion(final Level level, final LivingEntity owner, final ItemStack itemStack) {
28
		super(EntityTypes.SPLASH_POTION, level, owner, itemStack);
29
	}
30
31
	public ThrownSplashPotion(final Level level, final double x, final double y, final double z, final ItemStack itemStack) {
32
		super(EntityTypes.SPLASH_POTION, level, x, y, z, itemStack);
33
	}
34
35
	@Override
36
	protected Item getDefaultItem() {
37
		return Items.SPLASH_POTION;
38
	}
39
40
	@Override
41
	public void onHitAsPotion(final ServerLevel level, final ItemStack potionItem, final HitResult hitResult) {
42
		PotionContents contents = potionItem.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY);
43
		float durationScale = potionItem.getOrDefault(DataComponents.POTION_DURATION_SCALE, 1.0F);
44
		Iterable<MobEffectInstance> mobEffects = contents.getAllEffects();
45
		AABB potionAabb = this.getBoundingBox().move(hitResult.getLocation().subtract(this.position()));
46
		AABB effectAabb = potionAabb.inflate(4.0, 2.0, 4.0);
47
		List<LivingEntity> entities = this.level().getEntitiesOfClass(LivingEntity.class, effectAabb);
48
		float margin = ProjectileUtil.computeMargin(this);
49
		if (!entities.isEmpty()) {
50
			Entity effectSource = this.getEffectSource();
51
52
			for (LivingEntity entity : entities) {
53
				if (entity.isAffectedByPotions()) {
54
					double dist = potionAabb.distanceToSqr(entity.getBoundingBox().inflate(margin));
55
					if (dist < 16.0) {
56
						double scale = 1.0 - Math.sqrt(dist) / 4.0;
57
58
						for (MobEffectInstance effectInstance : mobEffects) {
59
							Holder<MobEffect> effect = effectInstance.getEffect();
60
							if (effect.value().isInstantaneous()) {
61
								effect.value().applyInstantaneousEffect(level, this, this.getOwner(), entity, effectInstance.getAmplifier(), scale);
62
							} else {
63
								int duration = effectInstance.mapDuration(d -> (int)(scale * d * durationScale + 0.5));
64
								MobEffectInstance newEffect = new MobEffectInstance(
65
									effect, duration, effectInstance.getAmplifier(), effectInstance.isAmbient(), effectInstance.isVisible()
66
								);
67
								if (!newEffect.endsWithin(20)) {
68
									entity.addEffect(newEffect, effectSource);
69
								}
70
							}
71
						}
72
					}
73
				}
74
			}
75
		}
76
	}
77
}
78

Decompiled Minecraft 26.3 source of ThrownSplashPotion (net/minecraft/world/entity/projectile/throwableitemprojectile/ThrownSplashPotion.java) with Mojang official mappings, stored verbatim on the site.