summaryrefslogtreecommitdiffstats
path: root/nms-patches/SpawnerCreature.patch
blob: ce90948fb3ce653e34751820e345a2efea8211df (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
--- a/net/minecraft/server/SpawnerCreature.java
+++ b/net/minecraft/server/SpawnerCreature.java
@@ -6,10 +6,16 @@
 import java.util.Random;
 import java.util.Set;
 
+// CraftBukkit start
+import org.bukkit.craftbukkit.util.LongHash;
+import org.bukkit.craftbukkit.util.LongHashSet;
+import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
+// CraftBukkit end
+
 public final class SpawnerCreature {
 
     private static final int a = (int) Math.pow(17.0D, 2.0D);
-    private final Set<ChunkCoordIntPair> b = Sets.newHashSet();
+    private final LongHashSet b = new LongHashSet(); // CraftBukkit
 
     public SpawnerCreature() {}
 
@@ -38,13 +44,16 @@
                             boolean flag4 = i1 == -8 || i1 == 8 || k == -8 || k == 8;
                             ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i1 + l, k + j);
 
-                            if (!this.b.contains(chunkcoordintpair)) {
+                            // CraftBukkit start - use LongHash and LongHashSet
+                            long chunkCoords = LongHash.toLong(chunkcoordintpair.x, chunkcoordintpair.z);
+                            if (!this.b.contains(chunkCoords)) {
                                 ++i;
                                 if (!flag4 && worldserver.getWorldBorder().isInBounds(chunkcoordintpair)) {
                                     PlayerChunk playerchunk = worldserver.getPlayerChunkMap().getChunk(chunkcoordintpair.x, chunkcoordintpair.z);
 
                                     if (playerchunk != null && playerchunk.e()) {
-                                        this.b.add(chunkcoordintpair);
+                                        this.b.add(chunkCoords);
+                                        // CraftBukkit end
                                     }
                                 }
                             }
@@ -62,9 +71,31 @@
             for (int k1 = 0; k1 < j; ++k1) {
                 EnumCreatureType enumcreaturetype = aenumcreaturetype[k1];
 
+               // CraftBukkit start - Use per-world spawn limits
+                int limit = enumcreaturetype.b();
+                switch (enumcreaturetype) {
+                    case MONSTER:
+                        limit = worldserver.getWorld().getMonsterSpawnLimit();
+                        break;
+                    case CREATURE:
+                        limit = worldserver.getWorld().getAnimalSpawnLimit();
+                        break;
+                    case WATER_CREATURE:
+                        limit = worldserver.getWorld().getWaterAnimalSpawnLimit();
+                        break;
+                    case AMBIENT:
+                        limit = worldserver.getWorld().getAmbientSpawnLimit();
+                        break;
+                }
+
+                if (limit == 0) {
+                    continue;
+                }
+                // CraftBukkit end
+
                 if ((!enumcreaturetype.d() || flag1) && (enumcreaturetype.d() || flag) && (!enumcreaturetype.e() || flag2)) {
                     k = worldserver.a(enumcreaturetype.a());
-                    int l1 = enumcreaturetype.b() * i / SpawnerCreature.a;
+                    int l1 = limit * i / a; // CraftBukkit - use per-world limits
 
                     if (k <= l1) {
                         BlockPosition.MutableBlockPosition blockposition_mutableblockposition = new BlockPosition.MutableBlockPosition();
@@ -72,8 +103,10 @@
 
                         label120:
                         while (iterator1.hasNext()) {
-                            ChunkCoordIntPair chunkcoordintpair1 = (ChunkCoordIntPair) iterator1.next();
-                            BlockPosition blockposition1 = getRandomPosition(worldserver, chunkcoordintpair1.x, chunkcoordintpair1.z);
+                            // CraftBukkit start = use LongHash and LongObjectHashMap
+                            long key = ((Long) iterator1.next()).longValue();
+                            BlockPosition blockposition1 = getRandomPosition(worldserver, LongHash.msw(key), LongHash.lsw(key));
+                            // CraftBukkit
                             int i2 = blockposition1.getX();
                             int j2 = blockposition1.getY();
                             int k2 = blockposition1.getZ();
@@ -125,8 +158,11 @@
                                                         if (entityinsentient.P() && entityinsentient.canSpawn()) {
                                                             groupdataentity = entityinsentient.prepare(worldserver.D(new BlockPosition(entityinsentient)), groupdataentity);
                                                             if (entityinsentient.canSpawn()) {
-                                                                ++l2;
-                                                                worldserver.addEntity(entityinsentient);
+                                                                // CraftBukkit start
+                                                                if (worldserver.addEntity(entityinsentient, SpawnReason.NATURAL)) {
+                                                                    ++l2;
+                                                                }
+                                                                // CraftBukkit end
                                                             } else {
                                                                 entityinsentient.die();
                                                             }
@@ -226,8 +262,10 @@
                             }
 
                             entityinsentient.setPositionRotation((double) ((float) j1 + 0.5F), (double) blockposition.getY(), (double) ((float) k1 + 0.5F), random.nextFloat() * 360.0F, 0.0F);
-                            world.addEntity(entityinsentient);
+                            // CraftBukkit start - Added a reason for spawning this creature, moved entityinsentient.prepare(groupdataentity) up
                             groupdataentity = entityinsentient.prepare(world.D(new BlockPosition(entityinsentient)), groupdataentity);
+                            world.addEntity(entityinsentient, SpawnReason.CHUNK_GEN);
+                            // CraftBukkit end
                             flag = true;
                         }