From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: jmp <jasonpenilla2@me.com>
Date: Sat, 22 Aug 2020 20:47:11 -0700
Subject: [PATCH] Allow toggling special MobSpawners per world

In vanilla, these are all hardcoded on for world type 0 (overworld) and hardcoded off for every other world type. Default config behaviour matches this.

diff --git a/src/main/java/net/minecraft/server/level/WorldServer.java b/src/main/java/net/minecraft/server/level/WorldServer.java
index 8d1debb8152176581041a14e28cfcca8f92ae261..373b4c24e105c343f9ba125514853a0d0dbdc524 100644
--- a/src/main/java/net/minecraft/server/level/WorldServer.java
+++ b/src/main/java/net/minecraft/server/level/WorldServer.java
@@ -92,6 +92,7 @@ import net.minecraft.world.entity.EnumCreatureType;
 import net.minecraft.world.entity.ReputationHandler;
 import net.minecraft.world.entity.ai.navigation.NavigationAbstract;
 import net.minecraft.world.entity.ai.village.ReputationEvent;
+import net.minecraft.world.entity.ai.village.VillageSiege;
 import net.minecraft.world.entity.ai.village.poi.VillagePlace;
 import net.minecraft.world.entity.ai.village.poi.VillagePlaceType;
 import net.minecraft.world.entity.animal.EntityAnimal;
@@ -101,6 +102,8 @@ import net.minecraft.world.entity.animal.horse.EntityHorseSkeleton;
 import net.minecraft.world.entity.boss.EntityComplexPart;
 import net.minecraft.world.entity.boss.enderdragon.EntityEnderDragon;
 import net.minecraft.world.entity.item.EntityItem;
+import net.minecraft.world.entity.npc.MobSpawnerCat;
+import net.minecraft.world.entity.npc.MobSpawnerTrader;
 import net.minecraft.world.entity.npc.NPC;
 import net.minecraft.world.entity.player.EntityHuman;
 import net.minecraft.world.entity.raid.PersistentRaid;
@@ -133,6 +136,8 @@ import net.minecraft.world.level.chunk.storage.RegionFile;
 import net.minecraft.world.level.dimension.DimensionManager;
 import net.minecraft.world.level.dimension.end.EnderDragonBattle;
 import net.minecraft.world.level.levelgen.HeightMap;
+import net.minecraft.world.level.levelgen.MobSpawnerPatrol;
+import net.minecraft.world.level.levelgen.MobSpawnerPhantom;
 import net.minecraft.world.level.levelgen.feature.StructureGenerator;
 import net.minecraft.world.level.levelgen.structure.StructureBoundingBox;
 import net.minecraft.world.level.levelgen.structure.StructureStart;
@@ -550,7 +555,24 @@ public class WorldServer extends World implements GeneratorAccessSeed {
         this.L = new ObjectLinkedOpenHashSet();
         this.Q = flag1;
         this.server = minecraftserver;
-        this.mobSpawners = list;
+        // Purpur start - enable/disable MobSpawners per world
+        this.mobSpawners = new java.util.ArrayList<>();
+        if (purpurConfig.phantomSpawning) {
+            mobSpawners.add(new MobSpawnerPhantom());
+        }
+        if (purpurConfig.patrolSpawning) {
+            mobSpawners.add(new MobSpawnerPatrol());
+        }
+        if (purpurConfig.catSpawning) {
+            mobSpawners.add(new MobSpawnerCat());
+        }
+        if (purpurConfig.villageSiegeSpawning) {
+            mobSpawners.add(new VillageSiege());
+        }
+        if (purpurConfig.villagerTraderSpawning) {
+            mobSpawners.add(new MobSpawnerTrader(iworlddataserver));
+        }
+        // Purpur end
         // CraftBukkit start
         this.worldDataServer = (WorldDataServer) iworlddataserver;
         worldDataServer.world = this;
diff --git a/src/main/java/net/minecraft/world/entity/npc/MobSpawnerTrader.java b/src/main/java/net/minecraft/world/entity/npc/MobSpawnerTrader.java
index 7c8a2151be8a0f48cba1c15d231d5dbdb500b4d6..361771fc4fcf16b1b013c550734019535cef2924 100644
--- a/src/main/java/net/minecraft/world/entity/npc/MobSpawnerTrader.java
+++ b/src/main/java/net/minecraft/world/entity/npc/MobSpawnerTrader.java
@@ -5,6 +5,7 @@ import java.util.Optional;
 import java.util.Random;
 import javax.annotation.Nullable;
 import net.minecraft.core.BlockPosition;
+import net.minecraft.core.EnumDirection;
 import net.minecraft.nbt.NBTTagCompound;
 import net.minecraft.network.chat.IChatBaseComponent;
 import net.minecraft.server.level.EntityPlayer;
@@ -153,7 +154,17 @@ public class MobSpawnerTrader implements MobSpawner {
             int k = blockposition.getX() + this.a.nextInt(i * 2) - i;
             int l = blockposition.getZ() + this.a.nextInt(i * 2) - i;
             int i1 = iworldreader.a(HeightMap.Type.WORLD_SURFACE, k, l);
-            BlockPosition blockposition2 = new BlockPosition(k, i1, l);
+            // Purpur start - allow traders to spawn below nether roof
+            BlockPosition.MutableBlockPosition blockposition2 = new BlockPosition.MutableBlockPosition(k, i1, l);
+            if (iworldreader.getDimensionManager().hasCeiling()) {
+                do {
+                    blockposition2.c(EnumDirection.DOWN);
+                } while (!iworldreader.getType(blockposition2).isAir());
+                do {
+                    blockposition2.c(EnumDirection.DOWN);
+                } while (iworldreader.getType(blockposition2).isAir() && blockposition2.getY() > 0);
+            }
+            // Purpur end
 
             if (SpawnerCreature.a(EntityPositionTypes.Surface.ON_GROUND, iworldreader, blockposition2, EntityTypes.WANDERING_TRADER)) {
                 blockposition1 = blockposition2;
diff --git a/src/main/java/net/minecraft/world/level/World.java b/src/main/java/net/minecraft/world/level/World.java
index b40de7edffa9c14bc5c04217a430c6d7bf0d25b1..ef1ba94bc03d6ee752c69119f615be8d14bf2ffc 100644
--- a/src/main/java/net/minecraft/world/level/World.java
+++ b/src/main/java/net/minecraft/world/level/World.java
@@ -250,7 +250,7 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
         this.paperConfig = new com.destroystokyo.paper.PaperWorldConfig(((net.minecraft.world.level.storage.WorldDataServer) worlddatamutable).getName(), this.spigotConfig); // Paper
         this.chunkPacketBlockController = this.paperConfig.antiXray ? new ChunkPacketBlockControllerAntiXray(this, executor) : ChunkPacketBlockController.NO_OPERATION_INSTANCE; // Paper - Anti-Xray
         this.tuinityConfig = new com.tuinity.tuinity.config.TuinityConfig.WorldConfig(((net.minecraft.world.level.storage.WorldDataServer)worlddatamutable).getName()); // Tuinity - Server Config
-        this.purpurConfig = new net.pl3x.purpur.PurpurWorldConfig((((net.minecraft.world.level.storage.WorldDataServer)worlddatamutable).getName())); // Purpur
+        this.purpurConfig = new net.pl3x.purpur.PurpurWorldConfig((((net.minecraft.world.level.storage.WorldDataServer)worlddatamutable).getName()), env); // Purpur
         this.generator = gen;
         this.world = new CraftWorld((WorldServer) this, gen, env);
         this.ticksPerAnimalSpawns = this.getServer().getTicksPerAnimalSpawns(); // CraftBukkit
diff --git a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
index 4b851fe1f266dd646e51730d41fbe269ad2c9791..b60ac4576578f9dc95b66ad2932b046f7f50317e 100644
--- a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
@@ -7,6 +7,8 @@ import net.minecraft.world.level.block.Blocks;
 import net.minecraft.world.item.Item;
 import net.minecraft.world.item.Items;
 import net.minecraft.resources.MinecraftKey;
+import org.apache.commons.lang3.BooleanUtils;
+import org.bukkit.World;
 import org.bukkit.configuration.ConfigurationSection;
 
 import java.util.ArrayList;
@@ -15,6 +17,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.Predicate;
 import java.util.logging.Level;
 
 import static net.pl3x.purpur.PurpurConfig.log;
@@ -22,9 +25,11 @@ import static net.pl3x.purpur.PurpurConfig.log;
 public class PurpurWorldConfig {
 
     private final String worldName;
+    private final World.Environment environment;
 
-    public PurpurWorldConfig(String worldName) {
+    public PurpurWorldConfig(String worldName, World.Environment environment) {
         this.worldName = worldName;
+        this.environment = environment;
         init();
     }
 
@@ -52,6 +57,12 @@ public class PurpurWorldConfig {
         return PurpurConfig.config.getBoolean("world-settings." + worldName + "." + path, PurpurConfig.config.getBoolean("world-settings.default." + path));
     }
 
+    private boolean getBoolean(String path, Predicate<Boolean> predicate) {
+        String val = getString(path, "default").toLowerCase();
+        Boolean bool = BooleanUtils.toBooleanObject(val, "true", "false", "default");
+        return predicate.test(bool);
+    }
+
     private double getDouble(String path, double def) {
         PurpurConfig.config.addDefault("world-settings.default." + path, def);
         return PurpurConfig.config.getDouble("world-settings." + worldName + "." + path, PurpurConfig.config.getDouble("world-settings.default." + path));
@@ -173,6 +184,21 @@ public class PurpurWorldConfig {
         voidDamageHeight = getDouble("gameplay-mechanics.void-damage-height", voidDamageHeight);
     }
 
+    public boolean catSpawning;
+    public boolean patrolSpawning;
+    public boolean phantomSpawning;
+    public boolean villagerTraderSpawning;
+    public boolean villageSiegeSpawning;
+    private void mobSpawnerSettings() {
+        // values of "default" or null will default to true only if the world environment is normal (aka overworld)
+        Predicate<Boolean> predicate = (bool) -> (bool != null && bool) || (bool == null && environment == World.Environment.NORMAL);
+        catSpawning = getBoolean("gameplay-mechanics.mob-spawning.village-cats", predicate);
+        patrolSpawning = getBoolean("gameplay-mechanics.mob-spawning.raid-patrols", predicate);
+        phantomSpawning = getBoolean("gameplay-mechanics.mob-spawning.phantoms", predicate);
+        villagerTraderSpawning = getBoolean("gameplay-mechanics.mob-spawning.wandering-traders", predicate);
+        villageSiegeSpawning = getBoolean("gameplay-mechanics.mob-spawning.village-sieges", predicate);
+    }
+
     public int elytraDamagePerSecond = 1;
     public double elytraDamageMultiplyBySpeed = 0;
     public boolean elytraIgnoreUnbreaking = false;
