From 1cb6ba69425e509af35dea31f1826ebe096d5825 Mon Sep 17 00:00:00 2001
From: CraftBukkit/Spigot <noreply+git-craftbukkit@papermc.io>
Date: Sat, 29 Mar 2014 13:44:25 -0400
Subject: [PATCH] Configurable dragon death and wither spawn sounds

By: drXor <mcyoungsota@gmail.com>
---
 .../boss/enderdragon/EnderDragon.java.patch   | 78 ++++++++++++-------
 .../entity/boss/wither/WitherBoss.java.patch  | 11 +--
 .../world/item/EnderEyeItem.java.patch        | 29 ++++++-
 .../java/org/spigotmc/SpigotWorldConfig.java  | 18 +++++
 4 files changed, 103 insertions(+), 33 deletions(-)

diff --git a/paper-server/patches/sources/net/minecraft/world/entity/boss/enderdragon/EnderDragon.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/boss/enderdragon/EnderDragon.java.patch
index 0e4dc8d073..c8c9dfefa1 100644
--- a/paper-server/patches/sources/net/minecraft/world/entity/boss/enderdragon/EnderDragon.java.patch
+++ b/paper-server/patches/sources/net/minecraft/world/entity/boss/enderdragon/EnderDragon.java.patch
@@ -91,7 +91,7 @@
  
          for (int k1 = i; k1 <= l; ++k1) {
              for (int l1 = j; l1 <= i1; ++l1) {
-@@ -467,14 +494,61 @@
+@@ -467,7 +494,11 @@
  
                      if (!iblockdata.isAir() && !iblockdata.is(BlockTags.DRAGON_TRANSPARENT)) {
                          if (world.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING) && !iblockdata.is(BlockTags.DRAGON_IMMUNE)) {
@@ -104,11 +104,10 @@
                          } else {
                              flag = true;
                          }
-                     }
-+                }
-+            }
-+        }
-+
+@@ -476,6 +507,49 @@
+             }
+         }
+ 
 +        // CraftBukkit start - Set off an EntityExplodeEvent for the dragon exploding all these blocks
 +        // SPIGOT-4882: don't fire event if nothing hit
 +        if (!flag1) {
@@ -130,7 +129,7 @@
 +                org.bukkit.Material blockId = block.getType();
 +                if (blockId.isAir()) {
 +                    continue;
-                 }
++                }
 +
 +                CraftBlock craftBlock = ((CraftBlock) block);
 +                BlockPos blockposition = craftBlock.getPosition();
@@ -148,13 +147,14 @@
 +                nmsBlock.wasExploded((ServerLevel) this.level(), blockposition, this.explosionSource);
 +
 +                this.level().removeBlock(blockposition, false);
-             }
-         }
++            }
++        }
 +        // CraftBukkit end
- 
++
          if (flag1) {
              BlockPos blockposition1 = new BlockPos(i + this.random.nextInt(l - i + 1), j + this.random.nextInt(i1 - j + 1), k + this.random.nextInt(j1 - k + 1));
-@@ -531,14 +605,29 @@
+ 
+@@ -531,7 +605,7 @@
  
      @Override
      public void kill(ServerLevel world) {
@@ -163,13 +163,12 @@
          this.gameEvent(GameEvent.ENTITY_DIE);
          if (this.dragonFight != null) {
              this.dragonFight.updateDragon(this);
-             this.dragonFight.setDragonKilled(this);
-+        }
-+
-+    }
-+
+@@ -540,7 +614,22 @@
+ 
+     }
+ 
 +    // CraftBukkit start - SPIGOT-2420: Special case, the ender dragon drops 12000 xp for the first kill and 500 xp for every other kill and this over time.
-+    @Override
+     @Override
 +    public int getExpReward(ServerLevel worldserver, Entity entity) {
 +        // CraftBukkit - Moved from #tickDeath method
 +        boolean flag = worldserver.getGameRules().getBoolean(GameRules.RULE_DOMOBLOOT);
@@ -177,15 +176,17 @@
 +
 +        if (this.dragonFight != null && !this.dragonFight.hasPreviouslyKilledDragon()) {
 +            short0 = 12000;
-         }
- 
++        }
++
 +        return flag ? short0 : 0;
-     }
++    }
 +    // CraftBukkit end
- 
-     @Override
++
++    @Override
      protected void tickDeath() {
-@@ -555,16 +644,21 @@
+         if (this.dragonFight != null) {
+             this.dragonFight.updateDragon(this);
+@@ -555,21 +644,43 @@
              this.level().addParticle(ParticleTypes.EXPLOSION_EMITTER, this.getX() + (double) f, this.getY() + 2.0D + (double) f1, this.getZ() + (double) f2, 0.0D, 0.0D, 0.0D);
          }
  
@@ -208,7 +209,30 @@
                  ExperienceOrb.award(worldserver, this.position(), Mth.floor((float) short0 * 0.08F));
              }
  
-@@ -592,7 +686,7 @@
+             if (this.dragonDeathTime == 1 && !this.isSilent()) {
+-                worldserver.globalLevelEvent(1028, this.blockPosition(), 0);
++                // CraftBukkit start - Use relative location for far away sounds
++                // worldserver.globalLevelEvent(1028, this.blockPosition(), 0);
++                int viewDistance = worldserver.getCraftServer().getViewDistance() * 16;
++                for (net.minecraft.server.level.ServerPlayer player : worldserver.getServer().getPlayerList().players) {
++                    double deltaX = this.getX() - player.getX();
++                    double deltaZ = this.getZ() - player.getZ();
++                    double distanceSquared = deltaX * deltaX + deltaZ * deltaZ;
++                    if ( worldserver.spigotConfig.dragonDeathSoundRadius > 0 && distanceSquared > worldserver.spigotConfig.dragonDeathSoundRadius * worldserver.spigotConfig.dragonDeathSoundRadius ) continue; // Spigot
++                    if (distanceSquared > viewDistance * viewDistance) {
++                        double deltaLength = Math.sqrt(distanceSquared);
++                        double relativeX = player.getX() + (deltaX / deltaLength) * viewDistance;
++                        double relativeZ = player.getZ() + (deltaZ / deltaLength) * viewDistance;
++                        player.connection.send(new net.minecraft.network.protocol.game.ClientboundLevelEventPacket(1028, new BlockPos((int) relativeX, (int) this.getY(), (int) relativeZ), 0, true));
++                    } else {
++                        player.connection.send(new net.minecraft.network.protocol.game.ClientboundLevelEventPacket(1028, new BlockPos((int) this.getX(), (int) this.getY(), (int) this.getZ()), 0, true));
++                    }
++                }
++                // CraftBukkit end
+             }
+         }
+ 
+@@ -592,7 +703,7 @@
              if (world1 instanceof ServerLevel) {
                  ServerLevel worldserver1 = (ServerLevel) world1;
  
@@ -217,7 +241,7 @@
                      ExperienceOrb.award(worldserver1, this.position(), Mth.floor((float) short0 * 0.2F));
                  }
  
-@@ -600,7 +694,7 @@
+@@ -600,7 +711,7 @@
                      this.dragonFight.setDragonKilled(this);
                  }
  
@@ -226,7 +250,7 @@
                  this.gameEvent(GameEvent.ENTITY_DIE);
              }
          }
-@@ -814,6 +908,7 @@
+@@ -814,6 +925,7 @@
          super.addAdditionalSaveData(nbt);
          nbt.putInt("DragonPhase", this.phaseManager.getCurrentPhase().getPhase().getId());
          nbt.putInt("DragonDeathTime", this.dragonDeathTime);
@@ -234,7 +258,7 @@
      }
  
      @Override
-@@ -827,6 +922,11 @@
+@@ -827,6 +939,11 @@
              this.dragonDeathTime = nbt.getInt("DragonDeathTime");
          }
  
diff --git a/paper-server/patches/sources/net/minecraft/world/entity/boss/wither/WitherBoss.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/boss/wither/WitherBoss.java.patch
index 79099a6e18..8f87fcdada 100644
--- a/paper-server/patches/sources/net/minecraft/world/entity/boss/wither/WitherBoss.java.patch
+++ b/paper-server/patches/sources/net/minecraft/world/entity/boss/wither/WitherBoss.java.patch
@@ -38,7 +38,7 @@
  
  public class WitherBoss extends Monster implements RangedAttackMob {
  
-@@ -252,15 +261,40 @@
+@@ -252,15 +261,41 @@
              i = this.getInvulnerableTicks() - 1;
              this.bossEvent.setProgress(1.0F - (float) i / 220.0F);
              if (i <= 0) {
@@ -62,6 +62,7 @@
 +                        double deltaX = this.getX() - player.getX();
 +                        double deltaZ = this.getZ() - player.getZ();
 +                        double distanceSquared = deltaX * deltaX + deltaZ * deltaZ;
++                        if ( world.spigotConfig.witherSpawnSoundRadius > 0 && distanceSquared > world.spigotConfig.witherSpawnSoundRadius * world.spigotConfig.witherSpawnSoundRadius ) continue; // Spigot
 +                        if (distanceSquared > viewDistance * viewDistance) {
 +                            double deltaLength = Math.sqrt(distanceSquared);
 +                            double relativeX = player.getX() + (deltaX / deltaLength) * viewDistance;
@@ -82,7 +83,7 @@
              }
  
          } else {
-@@ -305,6 +339,7 @@
+@@ -305,6 +340,7 @@
                          if (!list.isEmpty()) {
                              LivingEntity entityliving1 = (LivingEntity) list.get(this.random.nextInt(list.size()));
  
@@ -90,7 +91,7 @@
                              this.setAlternativeTarget(i, entityliving1.getId());
                          }
                      }
-@@ -331,6 +366,11 @@
+@@ -331,6 +367,11 @@
                          BlockState iblockdata = world.getBlockState(blockposition);
  
                          if (WitherBoss.canDestroy(iblockdata)) {
@@ -102,7 +103,7 @@
                              flag = world.destroyBlock(blockposition, true, this) || flag;
                          }
                      }
-@@ -342,7 +382,7 @@
+@@ -342,7 +383,7 @@
              }
  
              if (this.tickCount % 20 == 0) {
@@ -111,7 +112,7 @@
              }
  
              this.bossEvent.setProgress(this.getHealth() / this.getMaxHealth());
-@@ -499,7 +539,7 @@
+@@ -499,7 +540,7 @@
      @Override
      public void checkDespawn() {
          if (this.level().getDifficulty() == Difficulty.PEACEFUL && this.shouldDespawnInPeaceful()) {
diff --git a/paper-server/patches/sources/net/minecraft/world/item/EnderEyeItem.java.patch b/paper-server/patches/sources/net/minecraft/world/item/EnderEyeItem.java.patch
index 3eec9b6ef0..1287382a8d 100644
--- a/paper-server/patches/sources/net/minecraft/world/item/EnderEyeItem.java.patch
+++ b/paper-server/patches/sources/net/minecraft/world/item/EnderEyeItem.java.patch
@@ -1,6 +1,33 @@
 --- a/net/minecraft/world/item/EnderEyeItem.java
 +++ b/net/minecraft/world/item/EnderEyeItem.java
-@@ -99,7 +99,11 @@
+@@ -62,7 +62,25 @@
+                         }
+                     }
+ 
+-                    world.globalLevelEvent(1038, blockposition1.offset(1, 0, 1), 0);
++                    // CraftBukkit start - Use relative location for far away sounds
++                    // world.globalLevelEvent(1038, blockposition1.offset(1, 0, 1), 0);
++                    int viewDistance = world.getCraftServer().getViewDistance() * 16;
++                    BlockPos soundPos = blockposition1.offset(1, 0, 1);
++                    for (ServerPlayer player : world.getServer().getPlayerList().players) {
++                        double deltaX = soundPos.getX() - player.getX();
++                        double deltaZ = soundPos.getZ() - player.getZ();
++                        double distanceSquared = deltaX * deltaX + deltaZ * deltaZ;
++                        if (world.spigotConfig.endPortalSoundRadius > 0 && distanceSquared > world.spigotConfig.endPortalSoundRadius * world.spigotConfig.endPortalSoundRadius) continue; // Spigot
++                        if (distanceSquared > viewDistance * viewDistance) {
++                            double deltaLength = Math.sqrt(distanceSquared);
++                            double relativeX = player.getX() + (deltaX / deltaLength) * viewDistance;
++                            double relativeZ = player.getZ() + (deltaZ / deltaLength) * viewDistance;
++                            player.connection.send(new net.minecraft.network.protocol.game.ClientboundLevelEventPacket(1038, new BlockPos((int) relativeX, (int) soundPos.getY(), (int) relativeZ), 0, true));
++                        } else {
++                            player.connection.send(new net.minecraft.network.protocol.game.ClientboundLevelEventPacket(1038, soundPos, 0, true));
++                        }
++                    }
++                    // CraftBukkit end
+                 }
+ 
+                 return InteractionResult.SUCCESS;
+@@ -99,7 +117,11 @@
                  entityendersignal.setItem(itemstack);
                  entityendersignal.signalTo(blockposition);
                  world.gameEvent((Holder) GameEvent.PROJECTILE_SHOOT, entityendersignal.position(), GameEvent.Context.of((Entity) user));
diff --git a/paper-server/src/main/java/org/spigotmc/SpigotWorldConfig.java b/paper-server/src/main/java/org/spigotmc/SpigotWorldConfig.java
index e986d172d7..b871982be6 100644
--- a/paper-server/src/main/java/org/spigotmc/SpigotWorldConfig.java
+++ b/paper-server/src/main/java/org/spigotmc/SpigotWorldConfig.java
@@ -276,4 +276,22 @@ public class SpigotWorldConfig
         this.enableZombiePigmenPortalSpawns = this.getBoolean( "enable-zombie-pigmen-portal-spawns", true );
         this.log( "Allow Zombie Pigmen to spawn from portal blocks: " + this.enableZombiePigmenPortalSpawns );
     }
+
+    public int dragonDeathSoundRadius;
+    private void keepDragonDeathPerWorld()
+    {
+        this.dragonDeathSoundRadius = this.getInt( "dragon-death-sound-radius", 0 );
+    }
+
+    public int witherSpawnSoundRadius;
+    private void witherSpawnSoundRadius()
+    {
+        this.witherSpawnSoundRadius = this.getInt( "wither-spawn-sound-radius", 0 );
+    }
+
+    public int endPortalSoundRadius;
+    private void endPortalSoundRadius()
+    {
+        this.endPortalSoundRadius = this.getInt( "end-portal-sound-radius", 0 );
+    }
 }