2021-01-14 01:04:51 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
Date: Wed, 18 Nov 2020 20:52:25 -0800
Subject: [PATCH] Entity load/save limit per chunk
Adds a config option to limit the number of entities saved and loaded
to a chunk. The default values of -1 disable the limit. Although
defaults are only included for certain entites, this allows setting
limits for any entity type.
diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
2021-03-16 14:04:28 +01:00
@@ -0,0 +0,0 @@ import java.util.stream.Collectors;
import com.destroystokyo.paper.antixray.ChunkPacketBlockControllerAntiXray.EngineMode;
import net.minecraft.world.EnumDifficulty;
+import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.monster.EntityVindicator;
import net.minecraft.world.entity.monster.EntityZombie;
2021-01-14 01:04:51 +01:00
import org.bukkit.Bukkit;
@@ -0,0 +0,0 @@ public class PaperWorldConfig {
EnumDifficulty.class
);
}
+
+ public Map<EntityTypes<?>, Integer> entityPerChunkSaveLimits = new HashMap<>();
+ private void entityPerChunkSaveLimits() {
+ getInt("entity-per-chunk-save-limit.experience_orb", -1);
+ getInt("entity-per-chunk-save-limit.snowball", -1);
+ getInt("entity-per-chunk-save-limit.ender_pearl", -1);
+ getInt("entity-per-chunk-save-limit.arrow", -1);
+ EntityTypes.getEntityNameList().forEach(name -> {
+ final EntityTypes<?> type = EntityTypes.getByName(name.getKey()).orElseThrow(() -> new IllegalStateException("Unknown Entity Type: " + name.toString()));
+ final String path = ".entity-per-chunk-save-limit." + name.getKey();
+ final int value = config.getInt("world-settings." + worldName + path, config.getInt("world-settings.default" + path, -1)); // get without setting defaults
+ if (value != -1) entityPerChunkSaveLimits.put(type, value);
+ });
+ }
}
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkRegionLoader.java b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkRegionLoader.java
2021-01-14 01:04:51 +01:00
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
2021-03-16 08:19:45 +01:00
--- a/src/main/java/net/minecraft/world/level/chunk/storage/ChunkRegionLoader.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/ChunkRegionLoader.java
2021-01-14 01:04:51 +01:00
@@ -0,0 +0,0 @@ public class ChunkRegionLoader {
chunk.d(false);
+ // Paper start
+ final Map<EntityTypes<?>, Integer> savedEntityCounts = Maps.newHashMap();
for (int j = 0; j < chunk.getEntitySlices().length; ++j) {
Iterator iterator1 = chunk.getEntitySlices()[j].iterator();
while (iterator1.hasNext()) {
Entity entity = (Entity) iterator1.next();
+ final EntityTypes<?> entityType = entity.getEntityType();
+ final int saveLimit = worldserver.paperConfig.entityPerChunkSaveLimits.getOrDefault(entityType, -1);
+ if (saveLimit > -1) {
+ if (savedEntityCounts.getOrDefault(entityType, 0) >= saveLimit) {
+ continue;
+ }
+ savedEntityCounts.merge(entityType, 1, Integer::sum);
+ }
+ // Paper end
NBTTagCompound nbttagcompound4 = new NBTTagCompound();
// Paper start
if (asyncsavedata == null && !entity.dead && (int) Math.floor(entity.locX()) >> 4 != chunk.getPos().x || (int) Math.floor(entity.locZ()) >> 4 != chunk.getPos().z) {
@@ -0,0 +0,0 @@ public class ChunkRegionLoader {
NBTTagList nbttaglist = nbttagcompound.getList("Entities", 10);
World world = chunk.getWorld();
+ // Paper start
+ final Map<EntityTypes<?>, Integer> loadedEntityCounts = Maps.newHashMap();
for (int i = 0; i < nbttaglist.size(); ++i) {
NBTTagCompound nbttagcompound1 = nbttaglist.getCompound(i);
EntityTypes.a(nbttagcompound1, world, (entity) -> {
+ final EntityTypes<?> entityType = entity.getEntityType();
+ final int saveLimit = world.paperConfig.entityPerChunkSaveLimits.getOrDefault(entityType, -1);
+ if (saveLimit > -1) {
+ if (loadedEntityCounts.getOrDefault(entityType, 0) >= saveLimit) {
2021-01-17 01:05:33 +01:00
+ return null;
2021-01-14 01:04:51 +01:00
+ }
+ loadedEntityCounts.merge(entityType, 1, Integer::sum);
+ }
+ // Paper end
chunk.a(entity);
return entity;
});