aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0383-implement-optional-per-player-mob-spawns.patch
blob: d297b27194d433bfbb3693286428db5669a4175a (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: kickash32 <kickash32@gmail.com>
Date: Mon, 19 Aug 2019 01:27:58 +0500
Subject: [PATCH] implement optional per player mob spawns


diff --git a/src/main/java/co/aikar/timings/WorldTimingsHandler.java b/src/main/java/co/aikar/timings/WorldTimingsHandler.java
index a27dc38d1a29ed1d63d2f44b7984c2b65be487d9..96aaaab5b7685c874463505f9d25e8a0a01a6e7c 100644
--- a/src/main/java/co/aikar/timings/WorldTimingsHandler.java
+++ b/src/main/java/co/aikar/timings/WorldTimingsHandler.java
@@ -57,6 +57,7 @@ public class WorldTimingsHandler {
 
 
     public final Timing miscMobSpawning;
+    public final Timing playerMobDistanceMapUpdate;
 
     public final Timing poiUnload;
     public final Timing chunkUnload;
@@ -122,6 +123,7 @@ public class WorldTimingsHandler {
 
 
         miscMobSpawning = Timings.ofSafe(name + "Mob spawning - Misc");
+        playerMobDistanceMapUpdate = Timings.ofSafe(name + "Per Player Mob Spawning - Distance Map Update");
 
         poiUnload = Timings.ofSafe(name + "Chunk unload - POI");
         chunkUnload = Timings.ofSafe(name + "Chunk unload - Chunk");
diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
index ce0db77ccd24c3543c7894db4044bed03ea776d8..edf4fa474d32ac20ff85cecfa9f03464a7ecf6ec 100644
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
@@ -562,4 +562,9 @@ public class PaperWorldConfig {
             }
         }
     }
+
+    public boolean perPlayerMobSpawns = false;
+    private void perPlayerMobSpawns() {
+        perPlayerMobSpawns = getBoolean("per-player-mob-spawns", false);
+    }
 }
diff --git a/src/main/java/com/destroystokyo/paper/util/PlayerMobDistanceMap.java b/src/main/java/com/destroystokyo/paper/util/PlayerMobDistanceMap.java
new file mode 100644
index 0000000000000000000000000000000000000000..9ebd7ecb7a0ac73ccacf34c097056598bb5d26e8
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/util/PlayerMobDistanceMap.java
@@ -0,0 +1,253 @@
+package com.destroystokyo.paper.util;
+
+import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap;
+import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
+import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
+import net.minecraft.server.ChunkCoordIntPair;
+import net.minecraft.server.EntityPlayer;
+import net.minecraft.server.SectionPosition;
+import org.spigotmc.AsyncCatcher;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/** @author Spottedleaf */
+public final class PlayerMobDistanceMap {
+
+    private static final PooledHashSets.PooledObjectLinkedOpenHashSet<EntityPlayer> EMPTY_SET = new PooledHashSets.PooledObjectLinkedOpenHashSet<>();
+
+    private final Map<EntityPlayer, SectionPosition> players = new HashMap<>();
+    // we use linked for better iteration.
+    private final Long2ObjectOpenHashMap<PooledHashSets.PooledObjectLinkedOpenHashSet<EntityPlayer>> playerMap = new Long2ObjectOpenHashMap<>(32, 0.5f);
+    private int viewDistance;
+
+    private final PooledHashSets<EntityPlayer> pooledHashSets = new PooledHashSets<>();
+
+    public PooledHashSets.PooledObjectLinkedOpenHashSet<EntityPlayer> getPlayersInRange(final ChunkCoordIntPair chunkPos) {
+        return this.getPlayersInRange(chunkPos.x, chunkPos.z);
+    }
+
+    public PooledHashSets.PooledObjectLinkedOpenHashSet<EntityPlayer> getPlayersInRange(final int chunkX, final int chunkZ) {
+        return this.playerMap.getOrDefault(ChunkCoordIntPair.pair(chunkX, chunkZ), EMPTY_SET);
+    }
+
+    public void update(final List<EntityPlayer> currentPlayers, final int newViewDistance) {
+        AsyncCatcher.catchOp("Distance map update");
+        final ObjectLinkedOpenHashSet<EntityPlayer> gone = new ObjectLinkedOpenHashSet<>(this.players.keySet());
+
+        final int oldViewDistance = this.viewDistance;
+        this.viewDistance = newViewDistance;
+
+        for (final EntityPlayer player : currentPlayers) {
+            if (player.isSpectator() || !player.affectsSpawning) {
+                continue; // will be left in 'gone' (or not added at all)
+            }
+
+            gone.remove(player);
+
+            final SectionPosition newPosition = player.getPlayerMapSection();
+            final SectionPosition oldPosition = this.players.put(player, newPosition);
+
+            if (oldPosition == null) {
+                this.addNewPlayer(player, newPosition, newViewDistance);
+            } else {
+                this.updatePlayer(player, oldPosition, newPosition, oldViewDistance, newViewDistance);
+            }
+            //this.validatePlayer(player, newViewDistance); // debug only
+        }
+
+        for (final EntityPlayer player : gone) {
+            final SectionPosition oldPosition = this.players.remove(player);
+            if (oldPosition != null) {
+                this.removePlayer(player, oldPosition, oldViewDistance);
+            }
+        }
+    }
+
+    // expensive op, only for debug
+    private void validatePlayer(final EntityPlayer player, final int viewDistance) {
+        int entiesGot = 0;
+        int expectedEntries = (2 * viewDistance + 1);
+        expectedEntries *= expectedEntries;
+
+        final SectionPosition currPosition = player.getPlayerMapSection();
+
+        final int centerX = currPosition.getX();
+        final int centerZ = currPosition.getZ();
+
+        for (final Long2ObjectLinkedOpenHashMap.Entry<PooledHashSets.PooledObjectLinkedOpenHashSet<EntityPlayer>> entry : this.playerMap.long2ObjectEntrySet()) {
+            final long key = entry.getLongKey();
+            final PooledHashSets.PooledObjectLinkedOpenHashSet<EntityPlayer> map = entry.getValue();
+
+            if (map.referenceCount == 0) {
+                throw new IllegalStateException("Invalid map");
+            }
+
+            if (map.set.contains(player)) {
+                ++entiesGot;
+
+                final int chunkX = ChunkCoordIntPair.getX(key);
+                final int chunkZ = ChunkCoordIntPair.getZ(key);
+
+                final int dist = Math.max(Math.abs(chunkX - centerX), Math.abs(chunkZ - centerZ));
+
+                if (dist > viewDistance) {
+                    throw new IllegalStateException("Expected view distance " + viewDistance + ", got " + dist);
+                }
+            }
+        }
+
+        if (entiesGot != expectedEntries) {
+            throw new IllegalStateException("Expected " + expectedEntries + ", got " + entiesGot);
+        }
+    }
+
+    private void addPlayerTo(final EntityPlayer player, final int chunkX, final int chunkZ) {
+       this.playerMap.compute(ChunkCoordIntPair.pair(chunkX, chunkZ), (final Long key, final PooledHashSets.PooledObjectLinkedOpenHashSet<EntityPlayer> players) -> {
+           if (players == null) {
+               return player.cachedSingleMobDistanceMap;
+           } else {
+               return PlayerMobDistanceMap.this.pooledHashSets.findMapWith(players, player);
+           }
+        });
+    }
+
+    private void removePlayerFrom(final EntityPlayer player, final int chunkX, final int chunkZ) {
+        this.playerMap.compute(ChunkCoordIntPair.pair(chunkX, chunkZ), (final Long keyInMap, final PooledHashSets.PooledObjectLinkedOpenHashSet<EntityPlayer> players) -> {
+            return PlayerMobDistanceMap.this.pooledHashSets.findMapWithout(players, player); // rets null instead of an empty map
+        });
+    }
+
+    private void updatePlayer(final EntityPlayer player, final SectionPosition oldPosition, final SectionPosition newPosition, final int oldViewDistance, final int newViewDistance) {
+        final int toX = newPosition.getX();
+        final int toZ = newPosition.getZ();
+        final int fromX = oldPosition.getX();
+        final int fromZ = oldPosition.getZ();
+
+        final int dx = toX - fromX;
+        final int dz = toZ - fromZ;
+
+        final int totalX = Math.abs(fromX - toX);
+        final int totalZ = Math.abs(fromZ - toZ);
+
+        if (Math.max(totalX, totalZ) > (2 * oldViewDistance)) {
+            // teleported?
+            this.removePlayer(player, oldPosition, oldViewDistance);
+            this.addNewPlayer(player, newPosition, newViewDistance);
+            return;
+        }
+
+        // x axis is width
+        // z axis is height
+        // right refers to the x axis of where we moved
+        // top refers to the z axis of where we moved
+
+        if (oldViewDistance == newViewDistance) {
+            // same view distance
+
+            // used for relative positioning
+            final int up = 1 | (dz >> (Integer.SIZE - 1)); // 1 if dz >= 0, -1 otherwise
+            final int right = 1 | (dx >> (Integer.SIZE - 1)); // 1 if dx >= 0, -1 otherwise
+
+            // The area excluded by overlapping the two view distance squares creates four rectangles:
+            // Two on the left, and two on the right. The ones on the left we consider the "removed" section
+            // and on the right the "added" section.
+            // https://i.imgur.com/MrnOBgI.png is a reference image. Note that the outside border is not actually
+            // exclusive to the regions they surround.
+
+            // 4 points of the rectangle
+            int maxX; // exclusive
+            int minX; // inclusive
+            int maxZ; // exclusive
+            int minZ; // inclusive
+
+            if (dx != 0) {
+                // handle right addition
+
+                maxX = toX + (oldViewDistance * right) + right; // exclusive
+                minX = fromX + (oldViewDistance * right) + right; // inclusive
+                maxZ = fromZ + (oldViewDistance * up) + up; // exclusive
+                minZ = toZ - (oldViewDistance * up); // inclusive
+
+                for (int currX = minX; currX != maxX; currX += right) {
+                    for (int currZ = minZ; currZ != maxZ; currZ += up) {
+                        this.addPlayerTo(player, currX, currZ);
+                    }
+                }
+            }
+
+            if (dz != 0) {
+                // handle up addition
+
+                maxX = toX + (oldViewDistance * right) + right; // exclusive
+                minX = toX - (oldViewDistance * right); // inclusive
+                maxZ = toZ + (oldViewDistance * up) + up; // exclusive
+                minZ = fromZ + (oldViewDistance * up) + up; // inclusive
+
+                for (int currX = minX; currX != maxX; currX += right) {
+                    for (int currZ = minZ; currZ != maxZ; currZ += up) {
+                        this.addPlayerTo(player, currX, currZ);
+                    }
+                }
+            }
+
+            if (dx != 0) {
+                // handle left removal
+
+                maxX = toX - (oldViewDistance * right); // exclusive
+                minX = fromX - (oldViewDistance * right); // inclusive
+                maxZ = fromZ + (oldViewDistance * up) + up; // exclusive
+                minZ = toZ - (oldViewDistance * up); // inclusive
+
+                for (int currX = minX; currX != maxX; currX += right) {
+                    for (int currZ = minZ; currZ != maxZ; currZ += up) {
+                        this.removePlayerFrom(player, currX, currZ);
+                    }
+                }
+            }
+
+            if (dz != 0) {
+                // handle down removal
+
+                maxX = fromX + (oldViewDistance * right) + right; // exclusive
+                minX = fromX - (oldViewDistance * right); // inclusive
+                maxZ = toZ - (oldViewDistance * up); // exclusive
+                minZ = fromZ - (oldViewDistance * up); // inclusive
+
+                for (int currX = minX; currX != maxX; currX += right) {
+                    for (int currZ = minZ; currZ != maxZ; currZ += up) {
+                        this.removePlayerFrom(player, currX, currZ);
+                    }
+                }
+            }
+        } else {
+            // different view distance
+            // for now :)
+            this.removePlayer(player, oldPosition, oldViewDistance);
+            this.addNewPlayer(player, newPosition, newViewDistance);
+        }
+    }
+
+    private void removePlayer(final EntityPlayer player, final SectionPosition position, final int viewDistance) {
+        final int x = position.getX();
+        final int z = position.getZ();
+
+        for (int xoff = -viewDistance; xoff <= viewDistance; ++xoff) {
+            for (int zoff = -viewDistance; zoff <= viewDistance; ++zoff) {
+                this.removePlayerFrom(player, x + xoff, z + zoff);
+            }
+        }
+    }
+
+    private void addNewPlayer(final EntityPlayer player, final SectionPosition position, final int viewDistance) {
+        final int x = position.getX();
+        final int z = position.getZ();
+
+        for (int xoff = -viewDistance; xoff <= viewDistance; ++xoff) {
+            for (int zoff = -viewDistance; zoff <= viewDistance; ++zoff) {
+                this.addPlayerTo(player, x + xoff, z + zoff);
+            }
+        }
+    }
+}
diff --git a/src/main/java/com/destroystokyo/paper/util/PooledHashSets.java b/src/main/java/com/destroystokyo/paper/util/PooledHashSets.java
new file mode 100644
index 0000000000000000000000000000000000000000..4f13d3ff8391793a99f067189f854078334499c6
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/util/PooledHashSets.java
@@ -0,0 +1,241 @@
+package com.destroystokyo.paper.util;
+
+import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
+import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
+import java.lang.ref.WeakReference;
+import java.util.Iterator;
+
+/** @author Spottedleaf */
+public class PooledHashSets<E> {
+
+    // we really want to avoid that equals() check as much as possible...
+    protected final Object2ObjectOpenHashMap<PooledObjectLinkedOpenHashSet<E>, PooledObjectLinkedOpenHashSet<E>> mapPool = new Object2ObjectOpenHashMap<>(64, 0.25f);
+
+    protected void decrementReferenceCount(final PooledObjectLinkedOpenHashSet<E> current) {
+        if (current.referenceCount == 0) {
+            throw new IllegalStateException("Cannot decrement reference count for " + current);
+        }
+        if (current.referenceCount == -1 || --current.referenceCount > 0) {
+            return;
+        }
+
+        this.mapPool.remove(current);
+        return;
+    }
+
+    public PooledObjectLinkedOpenHashSet<E> findMapWith(final PooledObjectLinkedOpenHashSet<E> current, final E object) {
+        final PooledObjectLinkedOpenHashSet<E> cached = current.getAddCache(object);
+
+        if (cached != null) {
+            if (cached.referenceCount != -1) {
+                ++cached.referenceCount;
+            }
+
+            decrementReferenceCount(current);
+
+            return cached;
+        }
+
+        if (!current.add(object)) {
+            return current;
+        }
+
+        // we use get/put since we use a different key on put
+        PooledObjectLinkedOpenHashSet<E> ret = this.mapPool.get(current);
+
+        if (ret == null) {
+            ret = new PooledObjectLinkedOpenHashSet<>(current);
+            current.remove(object);
+            this.mapPool.put(ret, ret);
+            ret.referenceCount = 1;
+        } else {
+            if (ret.referenceCount != -1) {
+                ++ret.referenceCount;
+            }
+            current.remove(object);
+        }
+
+        current.updateAddCache(object, ret);
+
+        decrementReferenceCount(current);
+        return ret;
+    }
+
+    // rets null if current.size() == 1
+    public PooledObjectLinkedOpenHashSet<E> findMapWithout(final PooledObjectLinkedOpenHashSet<E> current, final E object) {
+        if (current.set.size() == 1) {
+            decrementReferenceCount(current);
+            return null;
+        }
+
+        final PooledObjectLinkedOpenHashSet<E> cached = current.getRemoveCache(object);
+
+        if (cached != null) {
+            if (cached.referenceCount != -1) {
+                ++cached.referenceCount;
+            }
+
+            decrementReferenceCount(current);
+
+            return cached;
+        }
+
+        if (!current.remove(object)) {
+            return current;
+        }
+
+        // we use get/put since we use a different key on put
+        PooledObjectLinkedOpenHashSet<E> ret = this.mapPool.get(current);
+
+        if (ret == null) {
+            ret = new PooledObjectLinkedOpenHashSet<>(current);
+            current.add(object);
+            this.mapPool.put(ret, ret);
+            ret.referenceCount = 1;
+        } else {
+            if (ret.referenceCount != -1) {
+                ++ret.referenceCount;
+            }
+            current.add(object);
+        }
+
+        current.updateRemoveCache(object, ret);
+
+        decrementReferenceCount(current);
+        return ret;
+    }
+
+    public static final class PooledObjectLinkedOpenHashSet<E> implements Iterable<E> {
+
+        private static final WeakReference NULL_REFERENCE = new WeakReference(null);
+
+        final ObjectLinkedOpenHashSet<E> set;
+        int referenceCount; // -1 if special
+        int hash; // optimize hashcode
+
+        // add cache
+        WeakReference<E> lastAddObject = NULL_REFERENCE;
+        WeakReference<PooledObjectLinkedOpenHashSet<E>> lastAddMap = NULL_REFERENCE;
+
+        // remove cache
+        WeakReference<E> lastRemoveObject = NULL_REFERENCE;
+        WeakReference<PooledObjectLinkedOpenHashSet<E>> lastRemoveMap = NULL_REFERENCE;
+
+        public PooledObjectLinkedOpenHashSet() {
+            this.set = new ObjectLinkedOpenHashSet<>(2, 0.6f);
+        }
+
+        public PooledObjectLinkedOpenHashSet(final E single) {
+            this();
+            this.referenceCount = -1;
+            this.add(single);
+        }
+
+        public PooledObjectLinkedOpenHashSet(final PooledObjectLinkedOpenHashSet<E> other) {
+            this.set = other.set.clone();
+            this.hash = other.hash;
+        }
+
+        // from https://github.com/Spottedleaf/ConcurrentUtil/blob/master/src/main/java/ca/spottedleaf/concurrentutil/util/IntegerUtil.java
+        // generated by https://github.com/skeeto/hash-prospector
+        static int hash0(int x) {
+            x *= 0x36935555;
+            x ^= x >>> 16;
+            return x;
+        }
+
+        public PooledObjectLinkedOpenHashSet<E> getAddCache(final E element) {
+            final E currentAdd = this.lastAddObject.get();
+
+            if (currentAdd == null || !(currentAdd == element || currentAdd.equals(element))) {
+                return null;
+            }
+
+            final PooledObjectLinkedOpenHashSet<E> map = this.lastAddMap.get();
+            if (map == null || map.referenceCount == 0) {
+                // we need to ret null if ref count is zero as calling code will assume the map is in use
+                return null;
+            }
+
+            return map;
+        }
+
+        public PooledObjectLinkedOpenHashSet<E> getRemoveCache(final E element) {
+            final E currentRemove = this.lastRemoveObject.get();
+
+            if (currentRemove == null || !(currentRemove == element || currentRemove.equals(element))) {
+                return null;
+            }
+
+            final PooledObjectLinkedOpenHashSet<E> map = this.lastRemoveMap.get();
+            if (map == null || map.referenceCount == 0) {
+                // we need to ret null if ref count is zero as calling code will assume the map is in use
+                return null;
+            }
+
+            return map;
+        }
+
+        public void updateAddCache(final E element, final PooledObjectLinkedOpenHashSet<E> map) {
+            this.lastAddObject = new WeakReference<>(element);
+            this.lastAddMap = new WeakReference<>(map);
+        }
+
+        public void updateRemoveCache(final E element, final PooledObjectLinkedOpenHashSet<E> map) {
+            this.lastRemoveObject = new WeakReference<>(element);
+            this.lastRemoveMap = new WeakReference<>(map);
+        }
+
+        boolean add(final E element) {
+            boolean added =  this.set.add(element);
+
+            if (added) {
+                this.hash += hash0(element.hashCode());
+            }
+
+            return added;
+        }
+
+        boolean remove(Object element) {
+            boolean removed = this.set.remove(element);
+
+            if (removed) {
+                this.hash -= hash0(element.hashCode());
+            }
+
+            return removed;
+        }
+
+        @Override
+        public Iterator<E> iterator() {
+            return this.set.iterator();
+        }
+
+        @Override
+        public int hashCode() {
+            return this.hash;
+        }
+
+        @Override
+        public boolean equals(final Object other) {
+            if (!(other instanceof PooledObjectLinkedOpenHashSet)) {
+                return false;
+            }
+            if (this.referenceCount == 0) {
+                return other == this;
+            } else {
+                if (other == this) {
+                    // Unfortunately we are never equal to our own instance while in use!
+                    return false;
+                }
+                return this.hash == ((PooledObjectLinkedOpenHashSet)other).hash && this.set.equals(((PooledObjectLinkedOpenHashSet)other).set);
+            }
+        }
+
+        @Override
+        public String toString() {
+            return "PooledHashSet: size: " + this.set.size() + ", reference count: " + this.referenceCount + ", hash: " +
+                this.hashCode() + ", identity: " + System.identityHashCode(this) + " map: " + this.set.toString();
+        }
+    }
+}
diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java
index 707db4febac59a4d09d6420ea2add469cf54c2ec..1597b7a882769109f467d81ecbadc45ff6779b7e 100644
--- a/src/main/java/net/minecraft/server/ChunkProviderServer.java
+++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java
@@ -744,7 +744,22 @@ public class ChunkProviderServer extends IChunkProvider {
             this.world.getMethodProfiler().enter("naturalSpawnCount");
             this.world.timings.countNaturalMobs.startTiming(); // Paper - timings
             int l = this.chunkMapDistance.b();
-            SpawnerCreature.d spawnercreature_d = SpawnerCreature.a(l, this.world.z(), this::a);
+            // Paper start - per player mob spawning
+            SpawnerCreature.d spawnercreature_d; // moved down
+            if (this.playerChunkMap.playerMobDistanceMap != null) {
+                // update distance map
+                this.world.timings.playerMobDistanceMapUpdate.startTiming();
+                this.playerChunkMap.playerMobDistanceMap.update(this.world.players, this.playerChunkMap.viewDistance);
+                this.world.timings.playerMobDistanceMapUpdate.stopTiming();
+                // re-set mob counts
+                for (EntityPlayer player : this.world.players) {
+                    Arrays.fill(player.mobCounts, 0);
+                }
+                spawnercreature_d = SpawnerCreature.countMobs(l, this.world.z(), this::a, true);
+            } else {
+                spawnercreature_d = SpawnerCreature.countMobs(l, this.world.z(), this::a, false);
+            }
+            // Paper end
             this.world.timings.countNaturalMobs.stopTiming(); // Paper - timings
 
             this.p = spawnercreature_d;
diff --git a/src/main/java/net/minecraft/server/EntityPlayer.java b/src/main/java/net/minecraft/server/EntityPlayer.java
index 2f0759ebc6f7adb3dc0856e19f856b406a21845e..8965141ab85df736e6adad6a3cf728864f582949 100644
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
@@ -85,6 +85,11 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public boolean queueHealthUpdatePacket = false;
     public net.minecraft.server.PacketPlayOutUpdateHealth queuedHealthUpdatePacket;
     // Paper end
+    // Paper start - mob spawning rework
+    public static final int ENUMCREATURETYPE_TOTAL_ENUMS = EnumCreatureType.values().length;
+    public final int[] mobCounts = new int[ENUMCREATURETYPE_TOTAL_ENUMS]; // Paper
+    public final com.destroystokyo.paper.util.PooledHashSets.PooledObjectLinkedOpenHashSet<EntityPlayer> cachedSingleMobDistanceMap;
+    // Paper end
 
     // CraftBukkit start
     public String displayName;
@@ -120,6 +125,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
         this.displayName = this.getName();
         this.canPickUpLoot = true;
         this.maxHealthCache = this.getMaxHealth();
+        this.cachedSingleMobDistanceMap = new com.destroystokyo.paper.util.PooledHashSets.PooledObjectLinkedOpenHashSet<>(this); // Paper
     }
 
     // Yes, this doesn't match Vanilla, but it's the best we can do for now.
@@ -1929,6 +1935,7 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
 
     }
 
+    public final SectionPosition getPlayerMapSection() { return this.N(); } // Paper - OBFHELPER
     public SectionPosition N() {
         return this.cq;
     }
diff --git a/src/main/java/net/minecraft/server/EntityTypes.java b/src/main/java/net/minecraft/server/EntityTypes.java
index b9fe08301409bc1f0d61a7566c26e720ff720d80..18a806ebbf092b904983691529ce5edf2da4e6db 100644
--- a/src/main/java/net/minecraft/server/EntityTypes.java
+++ b/src/main/java/net/minecraft/server/EntityTypes.java
@@ -265,6 +265,7 @@ public class EntityTypes<T extends Entity> {
         return this.bk;
     }
 
+    public final EnumCreatureType getEnumCreatureType() { return this.e(); } // Paper - OBFHELPER
     public EnumCreatureType e() {
         return this.bf;
     }
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
index cacf60563826da0219754a67500fdc239b13f0cd..f06c41d06f853b625dbd46822126810da3c3264a 100644
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
@@ -80,7 +80,8 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
     public final Int2ObjectMap<PlayerChunkMap.EntityTracker> trackedEntities;
     private final Long2ByteMap z;
     private final Queue<Runnable> A; private final Queue<Runnable> getUnloadQueueTasks() { return this.A; } // Paper - OBFHELPER
-    private int viewDistance;
+    int viewDistance; // Paper - private -> package private
+    public final com.destroystokyo.paper.util.PlayerMobDistanceMap playerMobDistanceMap; // Paper
 
     // CraftBukkit start - recursion-safe executor for Chunk loadCallback() and unloadCallback()
     public final CallbackExecutor callbackExecutor = new CallbackExecutor();
@@ -159,6 +160,24 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
         this.l = supplier;
         this.m = new VillagePlace(new File(this.w, "poi"), datafixer, flag, this.world); // Paper
         this.setViewDistance(i);
+        this.playerMobDistanceMap = this.world.paperConfig.perPlayerMobSpawns ? new com.destroystokyo.paper.util.PlayerMobDistanceMap() : null; // Paper
+    }
+
+    public void updatePlayerMobTypeMap(Entity entity) {
+        if (!this.world.paperConfig.perPlayerMobSpawns) {
+            return;
+        }
+        int chunkX = (int)Math.floor(entity.locX()) >> 4;
+        int chunkZ = (int)Math.floor(entity.locZ()) >> 4;
+        int index = entity.getEntityType().getEnumCreatureType().ordinal();
+
+        for (EntityPlayer player : this.playerMobDistanceMap.getPlayersInRange(chunkX, chunkZ)) {
+            ++player.mobCounts[index];
+        }
+    }
+
+    public int getMobCountNear(EntityPlayer entityPlayer, EnumCreatureType enumCreatureType) {
+        return entityPlayer.mobCounts[enumCreatureType.ordinal()];
     }
 
     private static double a(ChunkCoordIntPair chunkcoordintpair, Entity entity) {
diff --git a/src/main/java/net/minecraft/server/SpawnerCreature.java b/src/main/java/net/minecraft/server/SpawnerCreature.java
index 8130e14b5d6c3e8b0a1234668d5c855e82f3a5dc..c5845013a79036704d084cfb903589cb9e8767cd 100644
--- a/src/main/java/net/minecraft/server/SpawnerCreature.java
+++ b/src/main/java/net/minecraft/server/SpawnerCreature.java
@@ -29,6 +29,11 @@ public final class SpawnerCreature {
     });
 
     public static SpawnerCreature.d a(int i, Iterable<Entity> iterable, SpawnerCreature.b spawnercreature_b) {
+        // Paper start - add countMobs parameter
+        return countMobs(i, iterable, spawnercreature_b, false);
+    }
+    public static SpawnerCreature.d countMobs(int i, Iterable<Entity> iterable, SpawnerCreature.b spawnercreature_b, boolean countMobs) {
+        // Paper end - add countMobs parameter
         SpawnerCreatureProbabilities spawnercreatureprobabilities = new SpawnerCreatureProbabilities();
         Object2IntOpenHashMap<EnumCreatureType> object2intopenhashmap = new Object2IntOpenHashMap();
         Iterator iterator = iterable.iterator();
@@ -67,6 +72,11 @@ public final class SpawnerCreature {
                     }
 
                     object2intopenhashmap.addTo(enumcreaturetype, 1);
+                    // Paper start
+                    if (countMobs) {
+                        ((WorldServer)chunk.world).getChunkProvider().playerChunkMap.updatePlayerMobTypeMap(entity);
+                    }
+                    // Paper end
                 });
             }
         }
@@ -125,13 +135,33 @@ public final class SpawnerCreature {
                 continue;
             }
 
+            // Paper start - only allow spawns upto the limit per chunk and update count afterwards
+            int currEntityCount = spawnercreature_d.getEntityCountsByType().getInt(enumcreaturetype);
+            int k1 = limit * spawnercreature_d.getSpawnerChunks() / SpawnerCreature.b;
+            int difference = k1 - currEntityCount;
+
+            if (worldserver.paperConfig.perPlayerMobSpawns) {
+                int minDiff = Integer.MAX_VALUE;
+                for (EntityPlayer entityplayer : worldserver.getChunkProvider().playerChunkMap.playerMobDistanceMap.getPlayersInRange(chunk.getPos())) {
+                    minDiff = Math.min(limit - worldserver.getChunkProvider().playerChunkMap.getMobCountNear(entityplayer, enumcreaturetype), minDiff);
+                }
+                difference = (minDiff == Integer.MAX_VALUE) ? 0 : minDiff;
+            }
+            // Paper end
+
+            if (difference > 0) { // Paper
             if ((flag || !enumcreaturetype.d()) && (flag1 || enumcreaturetype.d()) && (flag2 || !enumcreaturetype.e()) && spawnercreature_d.a(enumcreaturetype, limit)) {
                 // CraftBukkit end
-                a(enumcreaturetype, worldserver, chunk, (entitytypes, blockposition, ichunkaccess) -> {
+                int spawnCount = spawnMobs(enumcreaturetype, worldserver, chunk, (entitytypes, blockposition, ichunkaccess) -> {
                     return spawnercreature_d.a(entitytypes, blockposition, ichunkaccess);
                 }, (entityinsentient, ichunkaccess) -> {
                     spawnercreature_d.a(entityinsentient, ichunkaccess);
+                },
+                limit, worldserver.paperConfig.perPlayerMobSpawns ? worldserver.getChunkProvider().playerChunkMap::updatePlayerMobTypeMap : null);
+                spawnercreature_d.getEntityCountsByType().mergeInt(enumcreaturetype, 0, (keyInMap, valueInMap) -> {
+                    return Integer.valueOf(spawnCount + valueInMap.intValue());
                 });
+                } // Paper
             }
         }
 
@@ -140,22 +170,34 @@ public final class SpawnerCreature {
     }
 
     public static void a(EnumCreatureType enumcreaturetype, WorldServer worldserver, Chunk chunk, SpawnerCreature.c spawnercreature_c, SpawnerCreature.a spawnercreature_a) {
+        // Paper start - add parameters and int ret type
+        spawnMobs(enumcreaturetype, worldserver, chunk, spawnercreature_c, spawnercreature_a, Integer.MAX_VALUE, null);
+    }
+    public static int spawnMobs(EnumCreatureType enumcreaturetype, WorldServer worldserver, Chunk chunk, SpawnerCreature.c spawnercreature_c, SpawnerCreature.a spawnercreature_a, int maxSpawns, Consumer<Entity> trackEntity) {
+        // Paper end - add parameters and int ret type
         BlockPosition blockposition = getRandomPosition(worldserver, chunk);
 
         if (blockposition.getY() >= 1) {
-            a(enumcreaturetype, worldserver, (IChunkAccess) chunk, blockposition, spawnercreature_c, spawnercreature_a);
+            return spawnMobsInternal(enumcreaturetype, worldserver, (IChunkAccess) chunk, blockposition, spawnercreature_c, spawnercreature_a, maxSpawns, trackEntity);
         }
+        return 0; // Paper
     }
 
     public static void a(EnumCreatureType enumcreaturetype, WorldServer worldserver, IChunkAccess ichunkaccess, BlockPosition blockposition, SpawnerCreature.c spawnercreature_c, SpawnerCreature.a spawnercreature_a) {
+        // Paper start - add maxSpawns parameter and return spawned mobs
+        spawnMobsInternal(enumcreaturetype, worldserver, ichunkaccess, blockposition, spawnercreature_c, spawnercreature_a, Integer.MAX_VALUE, null);
+    }
+    public static int spawnMobsInternal(EnumCreatureType enumcreaturetype, WorldServer worldserver, IChunkAccess ichunkaccess, BlockPosition blockposition, SpawnerCreature.c spawnercreature_c, SpawnerCreature.a spawnercreature_a, int maxSpawns, Consumer<Entity> trackEntity) {
+        // Paper end - add maxSpawns parameter and return spawned mobs
         StructureManager structuremanager = worldserver.getStructureManager();
         ChunkGenerator chunkgenerator = worldserver.getChunkProvider().getChunkGenerator();
         int i = blockposition.getY();
         IBlockData iblockdata = worldserver.getTypeIfLoadedAndInBounds(blockposition); // Paper - don't load chunks for mob spawn
+        int j = 0; // Paper - moved up
 
         if (iblockdata != null && !iblockdata.isOccluding(ichunkaccess, blockposition)) { // Paper - don't load chunks for mob spawn
             BlockPosition.MutableBlockPosition blockposition_mutableblockposition = new BlockPosition.MutableBlockPosition();
-            int j = 0;
+            // Paper - moved up
             int k = 0;
 
             while (k < 3) {
@@ -195,13 +237,13 @@ public final class SpawnerCreature {
                                     // Paper start
                                     Boolean doSpawning = a(worldserver, enumcreaturetype, structuremanager, chunkgenerator, biomebase_biomemeta, blockposition_mutableblockposition, d2);
                                     if (doSpawning == null) {
-                                        return;
+                                        return j; // Paper
                                     }
                                     if (doSpawning.booleanValue() && spawnercreature_c.test(biomebase_biomemeta.c, blockposition_mutableblockposition, ichunkaccess)) { // Paper end
                                         EntityInsentient entityinsentient = a(worldserver, biomebase_biomemeta.c);
 
                                         if (entityinsentient == null) {
-                                            return;
+                                            return j; // Paper
                                         }
 
                                         entityinsentient.setPositionRotation(d0, (double) i, d1, worldserver.random.nextFloat() * 360.0F, 0.0F);
@@ -209,13 +251,18 @@ public final class SpawnerCreature {
                                             groupdataentity = entityinsentient.prepare(worldserver, worldserver.getDamageScaler(entityinsentient.getChunkCoordinates()), EnumMobSpawn.NATURAL, groupdataentity, (NBTTagCompound) null);
                                             // CraftBukkit start
                                             if (worldserver.addEntity(entityinsentient, SpawnReason.NATURAL)) {
-                                                ++j;
+                                                ++j; // Paper - force diff on name change - we expect this to be the total amount spawned
                                                 ++k1;
                                                 spawnercreature_a.run(entityinsentient, ichunkaccess);
+                                                // Paper start
+                                                if (trackEntity != null) {
+                                                    trackEntity.accept(entityinsentient);
+                                                }
+                                                // Paper end
                                             }
                                             // CraftBukkit end
-                                            if (j >= entityinsentient.getMaxSpawnGroup()) {
-                                                return;
+                                            if (j >= entityinsentient.getMaxSpawnGroup() || j >= maxSpawns) { // Paper
+                                                return j; // Paper
                                             }
 
                                             if (entityinsentient.c(k1)) {
@@ -237,6 +284,7 @@ public final class SpawnerCreature {
             }
 
         }
+        return j; // Paper
     }
 
     private static boolean a(WorldServer worldserver, IChunkAccess ichunkaccess, BlockPosition.MutableBlockPosition blockposition_mutableblockposition, double d0) {
@@ -476,8 +524,8 @@ public final class SpawnerCreature {
 
     public static class d {
 
-        private final int a;
-        private final Object2IntOpenHashMap<EnumCreatureType> b;
+        private final int a; final int getSpawnerChunks() { return this.a; } // Paper - OBFHELPER
+        private final Object2IntOpenHashMap<EnumCreatureType> b; final Object2IntMap<EnumCreatureType> getEntityCountsByType() { return this.b; } // Paper - OBFHELPER
         private final SpawnerCreatureProbabilities c;
         private final Object2IntMap<EnumCreatureType> d;
         @Nullable
@@ -540,7 +588,7 @@ public final class SpawnerCreature {
 
         // CraftBukkit start
         private boolean a(EnumCreatureType enumcreaturetype, int limit) {
-            int i = limit * this.a / SpawnerCreature.b;
+            int i = limit * this.a / SpawnerCreature.b; // Paper - diff on change, needed in the spawn method
             // CraftBukkit end
 
             return this.b.getInt(enumcreaturetype) < i;