aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0156-Send-attack-SoundEffects-only-to-players-who-can-see.patch
blob: 8645b2df0d867233260b2a906b20ba9d63b747bd (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
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Brokkonaut <hannos17@gmx.de>
Date: Tue, 31 Oct 2017 03:26:18 +0100
Subject: [PATCH] Send attack SoundEffects only to players who can see the
 attacker


diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java
index d603a7211c7e7eb2567881a530ae7bcbc8def5a6..0e14f9f233a1f41e9b660e19950820c9cd1913a5 100644
--- a/src/main/java/net/minecraft/world/entity/player/Player.java
+++ b/src/main/java/net/minecraft/world/entity/player/Player.java
@@ -1256,7 +1256,7 @@ public abstract class Player extends LivingEntity {
                     boolean flag1;
 
                     if (this.isSprinting() && flag) {
-                        this.level().playSound((Player) null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_KNOCKBACK, this.getSoundSource(), 1.0F, 1.0F);
+                        sendSoundEffect(this, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_KNOCKBACK, this.getSoundSource(), 1.0F, 1.0F); // Paper - send while respecting visibility
                         flag1 = true;
                     } else {
                         flag1 = false;
@@ -1335,9 +1335,9 @@ public abstract class Player extends LivingEntity {
                                 }
                             }
 
-                            this.level().playSound((Player) null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_SWEEP, this.getSoundSource(), 1.0F, 1.0F);
-                            this.sweepAttack();
-                        }
+                                sendSoundEffect(this, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_SWEEP, this.getSoundSource(), 1.0F, 1.0F); // Paper - send while respecting visibility
+                                this.sweepAttack();
+                            }
 
                         if (target instanceof ServerPlayer && target.hurtMarked) {
                             // CraftBukkit start - Add Velocity Event
@@ -1362,18 +1362,18 @@ public abstract class Player extends LivingEntity {
                             // CraftBukkit end
                         }
 
-                        if (flag2) {
-                            this.level().playSound((Player) null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_CRIT, this.getSoundSource(), 1.0F, 1.0F);
-                            this.crit(target);
-                        }
+                            if (flag2) {
+                                sendSoundEffect(this, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_CRIT, this.getSoundSource(), 1.0F, 1.0F); // Paper - send while respecting visibility
+                                this.crit(target);
+                            }
 
-                        if (!flag2 && !flag3) {
-                            if (flag) {
-                                this.level().playSound((Player) null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_STRONG, this.getSoundSource(), 1.0F, 1.0F);
-                            } else {
-                                this.level().playSound((Player) null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_WEAK, this.getSoundSource(), 1.0F, 1.0F);
+                            if (!flag2 && !flag3) {
+                                if (flag) {
+                                    sendSoundEffect(this, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_STRONG, this.getSoundSource(), 1.0F, 1.0F); // Paper - send while respecting visibility
+                                } else {
+                                    sendSoundEffect(this, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_WEAK, this.getSoundSource(), 1.0F, 1.0F); // Paper - send while respecting visibility
+                                }
                             }
-                        }
 
                         if (f1 > 0.0F) {
                             this.magicCrit(target);
@@ -1425,16 +1425,19 @@ public abstract class Player extends LivingEntity {
                             }
                         }
 
-                        this.causeFoodExhaustion(this.level().spigotConfig.combatExhaustion, EntityExhaustionEvent.ExhaustionReason.ATTACK); // CraftBukkit - EntityExhaustionEvent // Spigot - Change to use configurable value
-                    } else {
-                        this.level().playSound((Player) null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_NODAMAGE, this.getSoundSource(), 1.0F, 1.0F);
-                        // CraftBukkit start - resync on cancelled event
-                        if (this instanceof ServerPlayer) {
-                            ((ServerPlayer) this).getBukkitEntity().updateInventory();
+                            this.causeFoodExhaustion(this.level().spigotConfig.combatExhaustion, EntityExhaustionEvent.ExhaustionReason.ATTACK); // CraftBukkit - EntityExhaustionEvent // Spigot - Change to use configurable value
+                        } else {
+                            sendSoundEffect(this, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_ATTACK_NODAMAGE, this.getSoundSource(), 1.0F, 1.0F); // Paper - send while respecting visibility
+                            if (flag4) {
+                                target.clearFire();
+                            }
+                            // CraftBukkit start - resync on cancelled event
+                            if (this instanceof ServerPlayer) {
+                                ((ServerPlayer) this).getBukkitEntity().updateInventory();
+                            }
+                            // CraftBukkit end
                         }
-                        // CraftBukkit end
                     }
-                }
 
             }
         }
@@ -1822,6 +1825,14 @@ public abstract class Player extends LivingEntity {
     public int getXpNeededForNextLevel() {
         return this.experienceLevel >= 30 ? 112 + (this.experienceLevel - 30) * 9 : (this.experienceLevel >= 15 ? 37 + (this.experienceLevel - 15) * 5 : 7 + this.experienceLevel * 2);
     }
+    // Paper start - send while respecting visibility
+    private static void sendSoundEffect(Player fromEntity, double x, double y, double z, SoundEvent soundEffect, SoundSource soundCategory, float volume, float pitch) {
+        fromEntity.level().playSound(fromEntity, x, y, z, soundEffect, soundCategory, volume, pitch); // This will not send the effect to the entity himself
+        if (fromEntity instanceof ServerPlayer) {
+            ((ServerPlayer) fromEntity).connection.send(new net.minecraft.network.protocol.game.ClientboundSoundPacket(net.minecraft.core.registries.BuiltInRegistries.SOUND_EVENT.wrapAsHolder(soundEffect), soundCategory, x, y, z, volume, pitch, fromEntity.random.nextLong()));
+        }
+    }
+    // Paper end - send while respecting visibility
 
     // CraftBukkit start
     public void causeFoodExhaustion(float exhaustion) {