papermc/CraftBukkit-Patches/0040-Save-entity-counts-for-randomly-spawned-creatures-to.patch

74 lines
2.7 KiB
Diff
Raw Normal View History

From b59089bc2de520bc295712f1320a568dfb218bdd Mon Sep 17 00:00:00 2001
From: Ammar Askar <ammar@ammaraskar.com>
Date: Sat, 20 Apr 2013 12:26:20 +0500
Subject: [PATCH] Save entity counts for randomly spawned creatures to avoid
repeatedly traversing over the entity slices and chunks
diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
index 7f1c155..7dd931c 100644
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
@@ -560,6 +560,13 @@ public class Chunk {
entity.ak = k;
entity.al = this.z;
this.entitySlices[k].add(entity);
+ // Spigot start - increment creature type count
+ for (EnumCreatureType creatureType : EnumCreatureType.values()) {
+ if (creatureType.a().isAssignableFrom(entity.getClass())) {
+ world.entityCount.adjustOrPutValue(creatureType.a(), 1, 1);
+ }
+ }
+ // Spigot end
}
public void b(Entity entity) {
@@ -576,6 +583,13 @@ public class Chunk {
}
this.entitySlices[i].remove(entity);
+ // Spigot start - decrement creature type count
+ for (EnumCreatureType creatureType : EnumCreatureType.values()) {
+ if (creatureType.a().isAssignableFrom(entity.getClass())) {
+ world.entityCount.adjustValue(creatureType.a(), -1);
+ }
+ }
+ // Spigot end
}
public boolean d(int i, int j, int k) {
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index 7e10318..3b23a58 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -76,6 +76,7 @@ public abstract class World implements IBlockAccess {
int[] H;
public boolean isStatic;
// Spigot start
+ protected gnu.trove.map.hash.TObjectIntHashMap<Class> entityCount = new gnu.trove.map.hash.TObjectIntHashMap<Class>();
public static long chunkToKey(int x, int z) {
long k = ((((long)x) & 0xFFFF0000L) << 16) | ((((long)x) & 0x0000FFFFL) << 0);
@@ -2456,6 +2457,9 @@ public abstract class World implements IBlockAccess {
}
public int a(Class oclass) {
+ // Spigot start - commented out inefficient chunk traversing in favour of keeping entity counts in a map
+ return this.entityCount.get(oclass);
+ /*
int i = 0;
for (int j = 0; j < this.entityList.size(); ++j) {
@@ -2467,6 +2471,8 @@ public abstract class World implements IBlockAccess {
}
return i;
+ */
+ // Spigot end
}
public void a(List list) {
--
1.7.11.msysgit.0