From 9665ac3903b364dc4abb99c73b7b07649ab22fda Mon Sep 17 00:00:00 2001
From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com>
Date: Sat, 10 Feb 2024 20:27:29 +0100
Subject: [PATCH] Fix WaterBottleSplashEvent not forwarding hit result (#10203)

---
 patches/api/Add-WaterBottleSplashEvent.patch  |  9 +++-
 .../server/Fix-potions-splash-events.patch    | 47 +++++++++++++++++--
 patches/server/More-Projectile-API.patch      |  2 +-
 3 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/patches/api/Add-WaterBottleSplashEvent.patch b/patches/api/Add-WaterBottleSplashEvent.patch
index e4b96586bd..b628e95522 100644
--- a/patches/api/Add-WaterBottleSplashEvent.patch
+++ b/patches/api/Add-WaterBottleSplashEvent.patch
@@ -16,11 +16,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +import java.util.Map;
 +import java.util.Set;
 +import java.util.stream.Collectors;
++import org.bukkit.block.Block;
++import org.bukkit.block.BlockFace;
++import org.bukkit.entity.Entity;
 +import org.bukkit.entity.LivingEntity;
 +import org.bukkit.entity.ThrownPotion;
 +import org.bukkit.event.entity.PotionSplashEvent;
 +import org.jetbrains.annotations.ApiStatus;
 +import org.jetbrains.annotations.NotNull;
++import org.jetbrains.annotations.Nullable;
 +import org.jetbrains.annotations.Unmodifiable;
 +
 +/**
@@ -35,11 +39,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    @ApiStatus.Internal
 +    public WaterBottleSplashEvent(
 +        final @NotNull ThrownPotion potion,
++        final @Nullable Entity hitEntity,
++        final @Nullable Block hitBlock,
++        final @Nullable BlockFace hitFace,
 +        final @NotNull Map<LivingEntity, Double> affectedEntities,
 +        final @NotNull Set<LivingEntity> rehydrate,
 +        final @NotNull Set<LivingEntity> extinguish
 +    ) {
-+        super(potion, affectedEntities);
++        super(potion, hitEntity, hitBlock, hitFace, affectedEntities);
 +        this.rehydrate = rehydrate;
 +        this.extinguish = extinguish;
 +    }
diff --git a/patches/server/Fix-potions-splash-events.patch b/patches/server/Fix-potions-splash-events.patch
index 309c933e65..5249eb34b5 100644
--- a/patches/server/Fix-potions-splash-events.patch
+++ b/patches/server/Fix-potions-splash-events.patch
@@ -19,7 +19,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
  
              if (flag) {
 -                this.applyWater();
-+                showParticles = this.applyWater(); // Paper - Fix potions splash events
++                showParticles = this.applyWater(hitResult); // Paper - Fix potions splash events
              } else if (true || !list.isEmpty()) { // CraftBukkit - Call event even if no effects to apply
                  if (this.isLingering()) {
 -                    this.makeAreaOfEffectCloud(itemstack, potionregistry, hitResult); // CraftBukkit - Pass MovingObjectPosition
@@ -41,7 +41,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
  
 -    private void applyWater() {
 +    private static final Predicate<net.minecraft.world.entity.LivingEntity> APPLY_WATER_GET_ENTITIES_PREDICATE = ThrownPotion.WATER_SENSITIVE_OR_ON_FIRE.or(Axolotl.class::isInstance); // Paper - Fix potions splash events
-+    private boolean applyWater() { // Paper - Fix potions splash events
++    private boolean applyWater(@Nullable HitResult hitResult) { // Paper - Fix potions splash events
          AABB axisalignedbb = this.getBoundingBox().inflate(4.0D, 2.0D, 4.0D);
 -        List<net.minecraft.world.entity.LivingEntity> list = this.level().getEntitiesOfClass(net.minecraft.world.entity.LivingEntity.class, axisalignedbb, ThrownPotion.WATER_SENSITIVE_OR_ON_FIRE);
 +        // Paper start - Fix potions splash events
@@ -78,10 +78,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 -            Axolotl axolotl = (Axolotl) iterator1.next();
 -
 -            axolotl.rehydrate();
-+        io.papermc.paper.event.entity.WaterBottleSplashEvent event = new io.papermc.paper.event.entity.WaterBottleSplashEvent(
-+            (org.bukkit.entity.ThrownPotion) this.getBukkitEntity(), affected, rehydrate, extinguish
++        io.papermc.paper.event.entity.WaterBottleSplashEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callWaterBottleSplashEvent(
++            this, hitResult, affected, rehydrate, extinguish
 +        );
-+        if (event.callEvent()) {
++        if (!event.isCancelled()) {
 +            for (LivingEntity affectedEntity : event.getToDamage()) {
 +                ((CraftLivingEntity) affectedEntity).getHandle().hurt(this.damageSources().indirectMagic(this, this.getOwner()), 1.0F);
 +            }
@@ -153,3 +153,40 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      }
  
      public boolean isLingering() {
+diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
++++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+@@ -0,0 +0,0 @@ public class CraftEventFactory {
+         return event;
+     }
+ 
++    // Paper start - Fix potions splash events
++    public static io.papermc.paper.event.entity.WaterBottleSplashEvent callWaterBottleSplashEvent(net.minecraft.world.entity.projectile.ThrownPotion potion, @Nullable HitResult hitResult, Map<LivingEntity, Double> affectedEntities, java.util.Set<LivingEntity> rehydrate, java.util.Set<LivingEntity> extinguish) {
++        ThrownPotion thrownPotion = (ThrownPotion) potion.getBukkitEntity();
++
++        Block hitBlock = null;
++        BlockFace hitFace = null;
++        org.bukkit.entity.Entity hitEntity = null;
++
++        if (hitResult != null) {
++            if (hitResult.getType() == HitResult.Type.BLOCK) {
++                BlockHitResult blockHitResult = (BlockHitResult) hitResult;
++                hitBlock = CraftBlock.at(potion.level(), blockHitResult.getBlockPos());
++                hitFace = CraftBlock.notchToBlockFace(blockHitResult.getDirection());
++            } else if (hitResult.getType() == HitResult.Type.ENTITY) {
++                hitEntity = ((EntityHitResult) hitResult).getEntity().getBukkitEntity();
++            }
++        }
++
++        io.papermc.paper.event.entity.WaterBottleSplashEvent event = new io.papermc.paper.event.entity.WaterBottleSplashEvent(
++            thrownPotion, hitEntity, hitBlock, hitFace, affectedEntities, rehydrate, extinguish
++        );
++        event.callEvent();
++        return event;
++    }
++    // Paper end - Fix potions splash events
++
+     /**
+      * BlockFadeEvent
+      */
diff --git a/patches/server/More-Projectile-API.patch b/patches/server/More-Projectile-API.patch
index bf46fcdb2b..fbf8640446 100644
--- a/patches/server/More-Projectile-API.patch
+++ b/patches/server/More-Projectile-API.patch
@@ -30,7 +30,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        // Paper start - More projectile API
 +        this.splash(hitResult);
 +    }
-+    public void splash(@org.jetbrains.annotations.Nullable HitResult hitResult) {
++    public void splash(@Nullable HitResult hitResult) {
 +        // Paper end - More projectile API
          if (!this.level().isClientSide) {
              ItemStack itemstack = this.getItem();