From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Wed, 12 Sep 2018 21:47:01 -0400
Subject: [PATCH] Optimize Biome Mob Lookups for Mob Spawning

Uses an EnumMap as well as a Set paired List for O(1) contains calls.

diff --git a/src/main/java/net/minecraft/server/BiomeSettingsMobs.java b/src/main/java/net/minecraft/server/BiomeSettingsMobs.java
index 558344a2fa7f755152d4674c698d7f47ba1beaa2..a18ca3f0001a771f21012bd39711f581efb2fef0 100644
--- a/src/main/java/net/minecraft/server/BiomeSettingsMobs.java
+++ b/src/main/java/net/minecraft/server/BiomeSettingsMobs.java
@@ -24,18 +24,27 @@ public class BiomeSettingsMobs {
     }, (enumcreaturetype) -> {
         return ImmutableList.of();
     })), ImmutableMap.of(), false);
+    // Paper start- decompile error workaround
+    private static class bProxy extends BiomeSettingsMobs.b {
+        private bProxy(double d0, double d1) {
+            super(d0, d1);
+        }
+    }
+    private static class cProxy extends BiomeSettingsMobs.c {
+        public cProxy(EntityTypes<?> entitytypes, int i, int j, int k) {
+            super(entitytypes, i, j, k);
+        }
+    };
+    // Paper end
     public static final MapCodec<BiomeSettingsMobs> c = RecordCodecBuilder.mapCodec((instance) -> {
-        RecordCodecBuilder recordcodecbuilder = Codec.FLOAT.optionalFieldOf("creature_spawn_probability", 0.1F).forGetter((biomesettingsmobs) -> {
+        RecordCodecBuilder<BiomeSettingsMobs, Float> recordcodecbuilder = Codec.FLOAT.optionalFieldOf("creature_spawn_probability", 0.1F).forGetter((biomesettingsmobs) -> { // Paper - add type to builder
             return biomesettingsmobs.d;
         });
-        Codec codec = EnumCreatureType.g;
-        Codec codec1 = BiomeSettingsMobs.c.b.listOf();
-        Logger logger = BiomeSettingsMobs.LOGGER;
+        // Paper - remove unused vars
 
-        logger.getClass();
-        return instance.group(recordcodecbuilder, Codec.simpleMap(codec, codec1.promotePartial(SystemUtils.a("Spawn data: ", logger::error)), INamable.a(EnumCreatureType.values())).fieldOf("spawners").forGetter((biomesettingsmobs) -> {
+        return instance.group(recordcodecbuilder, Codec.simpleMap(EnumCreatureType.g, cProxy.b.listOf().promotePartial(SystemUtils.a("Spawn data: ", BiomeSettingsMobs.LOGGER::error)), INamable.a(EnumCreatureType.values())).fieldOf("spawners").forGetter((biomesettingsmobs) -> { // Paper - inline codec, cProxy, LOGGER
             return biomesettingsmobs.e;
-        }), Codec.simpleMap(IRegistry.ENTITY_TYPE, BiomeSettingsMobs.b.a, IRegistry.ENTITY_TYPE).fieldOf("spawn_costs").forGetter((biomesettingsmobs) -> {
+        }), Codec.simpleMap(IRegistry.ENTITY_TYPE, bProxy.a, IRegistry.ENTITY_TYPE).fieldOf("spawn_costs").forGetter((biomesettingsmobs) -> { // Paper - decompile error - bProxy
             return biomesettingsmobs.f;
         }), Codec.BOOL.fieldOf("player_spawn_friendly").orElse(false).forGetter(BiomeSettingsMobs::b)).apply(instance, BiomeSettingsMobs::new);
     });
@@ -70,11 +79,43 @@ public class BiomeSettingsMobs {
 
     public static class a {
 
-        private final Map<EnumCreatureType, List<BiomeSettingsMobs.c>> a = (Map) Stream.of(EnumCreatureType.values()).collect(ImmutableMap.toImmutableMap((enumcreaturetype) -> {
+        // Paper start - keep track of data in a pair set to give O(1) contains calls - we have to hook removals incase plugins mess with it
+        public static class MobList extends java.util.ArrayList<BiomeSettingsMobs.c> {
+            java.util.Set<BiomeSettingsMobs.c> biomes = new java.util.HashSet<>();
+
+            @Override
+            public boolean contains(Object o) {
+                return biomes.contains(o);
+            }
+
+            @Override
+            public boolean add(BiomeSettingsMobs.c BiomeSettingsMobs) {
+                biomes.add(BiomeSettingsMobs);
+                return super.add(BiomeSettingsMobs);
+            }
+
+            @Override
+            public BiomeSettingsMobs.c remove(int index) {
+                BiomeSettingsMobs.c removed = super.remove(index);
+                if (removed != null) {
+                    biomes.remove(removed);
+                }
+                return removed;
+            }
+
+            @Override
+            public void clear() {
+                biomes.clear();
+                super.clear();
+            }
+        }
+        // use toImmutableEnumMap collector
+        private final Map<EnumCreatureType, List<BiomeSettingsMobs.c>> a = (Map) Stream.of(EnumCreatureType.values()).collect(Maps.toImmutableEnumMap((enumcreaturetype) -> {
             return enumcreaturetype;
         }, (enumcreaturetype) -> {
-            return Lists.newArrayList();
+            return new MobList(); // Use MobList instead of ArrayList
         }));
+        // Paper end
         private final Map<EntityTypes<?>, BiomeSettingsMobs.b> b = Maps.newLinkedHashMap();
         private float c = 0.1F;
         private boolean d;
