minecraft-src/net/minecraft/world/entity/projectile/ThrownSplashPotion.java
2025-07-04 03:45:38 +03:00

78 lines
2.8 KiB
Java

package net.minecraft.world.entity.projectile;
import java.util.List;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.PotionContents;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.Nullable;
public class ThrownSplashPotion extends AbstractThrownPotion {
public ThrownSplashPotion(EntityType<? extends ThrownSplashPotion> entityType, Level level) {
super(entityType, level);
}
public ThrownSplashPotion(Level level, LivingEntity owner, ItemStack item) {
super(EntityType.SPLASH_POTION, level, owner, item);
}
public ThrownSplashPotion(Level level, double x, double y, double z, ItemStack stack) {
super(EntityType.SPLASH_POTION, level, x, y, z, stack);
}
@Override
protected Item getDefaultItem() {
return Items.SPLASH_POTION;
}
@Override
public void onHitAsPotion(ServerLevel level, ItemStack stack, @Nullable Entity entity) {
PotionContents potionContents = stack.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY);
float f = stack.getOrDefault(DataComponents.POTION_DURATION_SCALE, 1.0F);
Iterable<MobEffectInstance> iterable = potionContents.getAllEffects();
AABB aABB = this.getBoundingBox().inflate(4.0, 2.0, 4.0);
List<LivingEntity> list = this.level().getEntitiesOfClass(LivingEntity.class, aABB);
if (!list.isEmpty()) {
Entity entity2 = this.getEffectSource();
for (LivingEntity livingEntity : list) {
if (livingEntity.isAffectedByPotions()) {
double d = this.distanceToSqr(livingEntity);
if (d < 16.0) {
double e;
if (livingEntity == entity) {
e = 1.0;
} else {
e = 1.0 - Math.sqrt(d) / 4.0;
}
for (MobEffectInstance mobEffectInstance : iterable) {
Holder<MobEffect> holder = mobEffectInstance.getEffect();
if (holder.value().isInstantenous()) {
holder.value().applyInstantenousEffect(level, this, this.getOwner(), livingEntity, mobEffectInstance.getAmplifier(), e);
} else {
int i = mobEffectInstance.mapDuration(ix -> (int)(e * ix * f + 0.5));
MobEffectInstance mobEffectInstance2 = new MobEffectInstance(
holder, i, mobEffectInstance.getAmplifier(), mobEffectInstance.isAmbient(), mobEffectInstance.isVisible()
);
if (!mobEffectInstance2.endsWithin(20)) {
livingEntity.addEffect(mobEffectInstance2, entity2);
}
}
}
}
}
}
}
}
}