aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0583-Fix-potions-splash-events.patch
blob: 15190dbc0fe7ad9d4b8679d4d30505fa583789fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Thu, 20 May 2021 20:40:53 -0700
Subject: [PATCH] Fix potions splash events

Fix PotionSplashEvent for water splash potions
Fixes SPIGOT-6221: https://hub.spigotmc.org/jira/projects/SPIGOT/issues/SPIGOT-6221
Fix splash events cancellation that still show particles/sound

diff --git a/src/main/java/net/minecraft/world/entity/projectile/ThrownPotion.java b/src/main/java/net/minecraft/world/entity/projectile/ThrownPotion.java
index 6e921bf7101224d6b8261ab8d87724080c4095d7..0204257ca0245830534592922e400a362c347715 100644
--- a/src/main/java/net/minecraft/world/entity/projectile/ThrownPotion.java
+++ b/src/main/java/net/minecraft/world/entity/projectile/ThrownPotion.java
@@ -106,56 +106,77 @@ public class ThrownPotion extends ThrowableItemProjectile implements ItemSupplie
             Potion potionregistry = PotionUtils.getPotion(itemstack);
             List<MobEffectInstance> list = PotionUtils.getMobEffects(itemstack);
             boolean flag = potionregistry == Potions.WATER && list.isEmpty();
+            boolean showParticles = true; // Paper - Fix potions splash events
 
             if (flag) {
-                this.applyWater();
+                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
+                    showParticles = this.makeAreaOfEffectCloud(itemstack, potionregistry, hitResult); // CraftBukkit - Pass MovingObjectPosition // Paper
                 } else {
-                    this.applySplash(list, hitResult.getType() == HitResult.Type.ENTITY ? ((EntityHitResult) hitResult).getEntity() : null, hitResult); // CraftBukkit - Pass MovingObjectPosition
+                    showParticles = this.applySplash(list, hitResult.getType() == HitResult.Type.ENTITY ? ((EntityHitResult) hitResult).getEntity() : null, hitResult); // CraftBukkit - Pass MovingObjectPosition // Paper
                 }
             }
 
+            if (showParticles) { // Paper - Fix potions splash events
             int i = potionregistry.hasInstantEffects() ? 2007 : 2002;
 
             this.level().levelEvent(i, this.blockPosition(), PotionUtils.getColor(itemstack));
+            } // Paper - Fix potions splash events
             this.discard(EntityRemoveEvent.Cause.HIT); // CraftBukkit - add Bukkit remove cause
         }
     }
 
-    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(@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
+        List<net.minecraft.world.entity.LivingEntity> list = this.level().getEntitiesOfClass(net.minecraft.world.entity.LivingEntity.class, axisalignedbb, ThrownPotion.APPLY_WATER_GET_ENTITIES_PREDICATE);
+        Map<LivingEntity, Double> affected = new HashMap<>();
+        java.util.Set<LivingEntity> rehydrate = new java.util.HashSet<>();
+        java.util.Set<LivingEntity> extinguish = new java.util.HashSet<>();
         Iterator iterator = list.iterator();
 
         while (iterator.hasNext()) {
             net.minecraft.world.entity.LivingEntity entityliving = (net.minecraft.world.entity.LivingEntity) iterator.next();
+            if (entityliving instanceof Axolotl axolotl) {
+                rehydrate.add(((org.bukkit.entity.Axolotl) axolotl.getBukkitEntity()));
+            }
             double d0 = this.distanceToSqr((Entity) entityliving);
 
             if (d0 < 16.0D) {
                 if (entityliving.isSensitiveToWater()) {
-                    entityliving.hurt(this.damageSources().indirectMagic(this, this.getOwner()), 1.0F);
+                    affected.put(entityliving.getBukkitLivingEntity(), 1.0);
                 }
 
                 if (entityliving.isOnFire() && entityliving.isAlive()) {
-                    entityliving.extinguishFire();
+                    extinguish.add(entityliving.getBukkitLivingEntity());
                 }
             }
         }
 
-        List<Axolotl> list1 = this.level().getEntitiesOfClass(Axolotl.class, axisalignedbb);
-        Iterator iterator1 = list1.iterator();
-
-        while (iterator1.hasNext()) {
-            Axolotl axolotl = (Axolotl) iterator1.next();
-
-            axolotl.rehydrate();
+        io.papermc.paper.event.entity.WaterBottleSplashEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callWaterBottleSplashEvent(
+            this, hitResult, affected, rehydrate, extinguish
+        );
+        if (!event.isCancelled()) {
+            for (LivingEntity affectedEntity : event.getToDamage()) {
+                ((CraftLivingEntity) affectedEntity).getHandle().hurt(this.damageSources().indirectMagic(this, this.getOwner()), 1.0F);
+            }
+            for (LivingEntity toExtinguish : event.getToExtinguish()) {
+                ((CraftLivingEntity) toExtinguish).getHandle().extinguishFire();
+            }
+            for (LivingEntity toRehydrate : event.getToRehydrate()) {
+                if (((CraftLivingEntity) toRehydrate).getHandle() instanceof Axolotl axolotl) {
+                    axolotl.rehydrate();
+                }
+            }
+            // Paper end - Fix potions splash events
         }
+        return !event.isCancelled(); // Paper - Fix potions splash events
 
     }
 
-    private void applySplash(List<MobEffectInstance> list, @Nullable Entity entity, HitResult position) { // CraftBukkit - Pass MovingObjectPosition
+    private boolean applySplash(List<MobEffectInstance> list, @Nullable Entity entity, HitResult position) { // CraftBukkit - Pass MovingObjectPosition // Paper - Fix potions splash events
         AABB axisalignedbb = this.getBoundingBox().inflate(4.0D, 2.0D, 4.0D);
         List<net.minecraft.world.entity.LivingEntity> list1 = this.level().getEntitiesOfClass(net.minecraft.world.entity.LivingEntity.class, axisalignedbb);
         Map<LivingEntity, Double> affected = new HashMap<LivingEntity, Double>(); // CraftBukkit
@@ -173,6 +194,7 @@ public class ThrownPotion extends ThrowableItemProjectile implements ItemSupplie
                     if (d0 < 16.0D) {
                         double d1;
 
+                        // Paper - diff on change, used when calling the splash event for water splash potions
                         if (entityliving == entity) {
                             d1 = 1.0D;
                         } else {
@@ -227,10 +249,11 @@ public class ThrownPotion extends ThrowableItemProjectile implements ItemSupplie
                 }
             }
         }
+        return !event.isCancelled(); // Paper - Fix potions splash events
 
     }
 
-    private void makeAreaOfEffectCloud(ItemStack itemstack, Potion potionregistry, HitResult position) { // CraftBukkit - Pass MovingObjectPosition
+    private boolean makeAreaOfEffectCloud(ItemStack itemstack, Potion potionregistry, HitResult position) { // CraftBukkit - Pass MovingObjectPosition // Paper - return boolean
         AreaEffectCloud entityareaeffectcloud = new AreaEffectCloud(this.level(), this.getX(), this.getY(), this.getZ());
         Entity entity = this.getOwner();
 
@@ -245,10 +268,12 @@ public class ThrownPotion extends ThrowableItemProjectile implements ItemSupplie
         entityareaeffectcloud.setPotion(potionregistry);
         Iterator iterator = PotionUtils.getCustomEffects(itemstack).iterator();
 
+        boolean noEffects = potionregistry.getEffects().isEmpty(); // Paper - Fix potions splash events
         while (iterator.hasNext()) {
             MobEffectInstance mobeffect = (MobEffectInstance) iterator.next();
 
             entityareaeffectcloud.addEffect(new MobEffectInstance(mobeffect));
+            noEffects = false; // Paper - Fix potions splash events
         }
 
         CompoundTag nbttagcompound = itemstack.getTag();
@@ -259,12 +284,13 @@ public class ThrownPotion extends ThrowableItemProjectile implements ItemSupplie
 
         // CraftBukkit start
         org.bukkit.event.entity.LingeringPotionSplashEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callLingeringPotionSplashEvent(this, position, entityareaeffectcloud);
-        if (!(event.isCancelled() || entityareaeffectcloud.isRemoved())) {
+        if (!(event.isCancelled() || entityareaeffectcloud.isRemoved() || (noEffects && entityareaeffectcloud.effects.isEmpty() && entityareaeffectcloud.getPotion().getEffects().isEmpty()))) { // Paper - don't spawn area effect cloud if the effects were empty and not changed during the event handling
             this.level().addFreshEntity(entityareaeffectcloud);
         } else {
             entityareaeffectcloud.discard(null); // CraftBukkit - add Bukkit remove cause
         }
         // CraftBukkit end
+        return !event.isCancelled(); // Paper - Fix potions splash events
     }
 
     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 11b42f349edefb6961b61d709bfbe3ad4b55c7f8..fef616ed1deac6cf400a2f39a2eba11b94921415 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -875,6 +875,32 @@ 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
      */