| 1 | package net.minecraft.world.entity.projectile.throwableitemprojectile;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.doubles.DoubleDoubleImmutablePair;
|
| 4 | import java.util.function.Predicate;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.component.DataComponents;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.tags.BlockTags;
|
| 10 | import net.minecraft.tags.PotionTags;
|
| 11 | import net.minecraft.world.damagesource.DamageSource;
|
| 12 | import net.minecraft.world.entity.EntityType;
|
| 13 | import net.minecraft.world.entity.LivingEntity;
|
| 14 | import net.minecraft.world.entity.animal.axolotl.Axolotl;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.block.AbstractCandleBlock;
|
| 19 | import net.minecraft.world.level.block.CampfireBlock;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.phys.AABB;
|
| 22 | import net.minecraft.world.phys.BlockHitResult;
|
| 23 | import net.minecraft.world.phys.HitResult;
|
| 24 |
|
| 25 | public abstract class AbstractThrownPotion extends ThrowableItemProjectile {
|
| 26 | public static final double SPLASH_RANGE = 4.0;
|
| 27 | protected static final double SPLASH_RANGE_SQ = 16.0;
|
| 28 | public static final Predicate<LivingEntity> WATER_SENSITIVE_OR_ON_FIRE = livingEntity -> livingEntity.isSensitiveToWater() || livingEntity.isOnFire();
|
| 29 |
|
| 30 | public AbstractThrownPotion(final EntityType<? extends AbstractThrownPotion> type, final Level level) {
|
| 31 | super(type, level);
|
| 32 | }
|
| 33 |
|
| 34 | public AbstractThrownPotion(final EntityType<? extends AbstractThrownPotion> type, final Level level, final LivingEntity owner, final ItemStack itemStack) {
|
| 35 | super(type, owner, level, itemStack);
|
| 36 | }
|
| 37 |
|
| 38 | public AbstractThrownPotion(
|
| 39 | final EntityType<? extends AbstractThrownPotion> type, final Level level, final double x, final double y, final double z, final ItemStack itemStack
|
| 40 | ) {
|
| 41 | super(type, x, y, z, level, itemStack);
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected double getDefaultGravity() {
|
| 46 | return 0.05;
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | protected void onHitBlock(final BlockHitResult hitResult) {
|
| 51 | super.onHitBlock(hitResult);
|
| 52 | if (!this.level().isClientSide()) {
|
| 53 | ItemStack potionItemStack = this.getItem();
|
| 54 | Direction hitDirection = hitResult.getDirection();
|
| 55 | BlockPos blockHitPos = hitResult.getBlockPos();
|
| 56 | BlockPos blockEffectPos = blockHitPos.relative(hitDirection);
|
| 57 | PotionContents potion = potionItemStack.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY);
|
| 58 | if (potion.is(PotionTags.DOUSES_FIRE)) {
|
| 59 | this.douseFire(blockEffectPos);
|
| 60 | this.douseFire(blockEffectPos.relative(hitDirection.getOpposite()));
|
| 61 |
|
| 62 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 63 | this.douseFire(blockEffectPos.relative(direction));
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected void onHit(final HitResult hitResult) {
|
| 71 | super.onHit(hitResult);
|
| 72 | if (this.level() instanceof ServerLevel level) {
|
| 73 | ItemStack potionItemStack = this.getItem();
|
| 74 | PotionContents potion = potionItemStack.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY);
|
| 75 | this.affectEntitiesAround(level, potion);
|
| 76 | if (potion.hasEffects()) {
|
| 77 | this.onHitAsPotion(level, potionItemStack, hitResult);
|
| 78 | }
|
| 79 |
|
| 80 | if (potion.potion().isPresent() && potion.potion().get().value().hasInstantEffects()) {
|
| 81 | level.levelEvent(2007, this.blockPosition(), potion.getColor());
|
| 82 | if (!this.isSilent()) {
|
| 83 | level.levelEvent(1054, this.blockPosition(), 0);
|
| 84 | }
|
| 85 | } else {
|
| 86 | level.levelEvent(2002, this.blockPosition(), potion.getColor());
|
| 87 | if (!this.isSilent()) {
|
| 88 | level.levelEvent(1053, this.blockPosition(), 0);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | this.discard();
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | private void affectEntitiesAround(final ServerLevel level, final PotionContents potion) {
|
| 97 | boolean hurtsWaterSensitiveEntities = potion.is(PotionTags.HURTS_WATER_SENSITIVE_ENTITIES);
|
| 98 | boolean extinguishesEntities = potion.is(PotionTags.EXTINGUISHES_ENTITIES);
|
| 99 | AABB aabb = this.getBoundingBox().inflate(4.0, 2.0, 4.0);
|
| 100 | if (hurtsWaterSensitiveEntities || extinguishesEntities) {
|
| 101 | for (LivingEntity entity : this.level().getEntitiesOfClass(LivingEntity.class, aabb, WATER_SENSITIVE_OR_ON_FIRE)) {
|
| 102 | double dist = this.distanceToSqr(entity);
|
| 103 | if (dist < 16.0) {
|
| 104 | if (hurtsWaterSensitiveEntities && entity.isSensitiveToWater()) {
|
| 105 | entity.hurtServer(level, this.damageSources().indirectMagic(this, this.getOwner()), 1.0F);
|
| 106 | }
|
| 107 |
|
| 108 | if (extinguishesEntities && entity.isOnFire() && entity.isAlive()) {
|
| 109 | entity.extinguishFire();
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | if (potion.is(PotionTags.REHYDRATES_AXOLOTLS)) {
|
| 116 | for (Axolotl axolotl : this.level().getEntitiesOfClass(Axolotl.class, aabb)) {
|
| 117 | axolotl.rehydrate();
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | protected abstract void onHitAsPotion(ServerLevel level, ItemStack potionItem, HitResult hitResult);
|
| 123 |
|
| 124 | private void douseFire(final BlockPos pos) {
|
| 125 | BlockState blockState = this.level().getBlockState(pos);
|
| 126 | if (blockState.is(BlockTags.FIRE)) {
|
| 127 | this.level().destroyBlock(pos, false, this);
|
| 128 | } else if (AbstractCandleBlock.isLit(blockState)) {
|
| 129 | AbstractCandleBlock.extinguish(null, blockState, this.level(), pos);
|
| 130 | } else if (CampfireBlock.isLitCampfire(blockState)) {
|
| 131 | this.level().levelEvent(null, 1009, pos, 0);
|
| 132 | CampfireBlock.douse(this.getOwner(), this.level(), pos, blockState);
|
| 133 | this.level().setBlockAndUpdate(pos, blockState.setValue(CampfireBlock.LIT, false));
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public DoubleDoubleImmutablePair calculateHorizontalHurtKnockbackDirection(final LivingEntity hurtEntity, final DamageSource damageSource) {
|
| 139 | double dx = hurtEntity.position().x - this.position().x;
|
| 140 | double dz = hurtEntity.position().z - this.position().z;
|
| 141 | return DoubleDoubleImmutablePair.of(dx, dz);
|
| 142 | }
|
| 143 | }
|
| 144 |
|