aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches-Unmapped/0287-Optimize-Biome-Mob-Lookups-for-Mob-Spawning.patch
blob: 681fbbbb312cea5237c68c77033e13d8c9f85b6f (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
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/world/level/biome/BiomeSettingsMobs.java b/src/main/java/net/minecraft/world/level/biome/BiomeSettingsMobs.java
index 5adaf5fdaaec25220878213df2c0839ccf025d63..233ae33b5cbf1aafc7d2632149ccb84c0b243162 100644
--- a/src/main/java/net/minecraft/world/level/biome/BiomeSettingsMobs.java
+++ b/src/main/java/net/minecraft/world/level/biome/BiomeSettingsMobs.java
@@ -30,18 +30,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);
     });
@@ -76,11 +85,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;