From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers Date: Sun, 25 Dec 2022 20:29:03 +0100 Subject: [PATCH] Skip unnecessary mob spawning computations Removed since Leaf 1.21.5, can be replaced by other optimizations License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) Gale - https://galemc.org This patch is based on the following patch: "Only create a spawner state when we are actually spawning mobs" By: PureGero As part of: MultiPaper (https://github.com/MultiPaper/MultiPaper) Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) diff --git a/net/minecraft/server/level/ServerChunkCache.java b/net/minecraft/server/level/ServerChunkCache.java index 5c8d76b80c6cefd87088282e59f5f0f72a4f1611..7a18b11782b2524280fddf20e6b1cabdddf07c49 100644 --- a/net/minecraft/server/level/ServerChunkCache.java +++ b/net/minecraft/server/level/ServerChunkCache.java @@ -550,9 +550,15 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon } private void tickChunks(long timeInhabited, List chunks) { // Gale - Purpur - remove vanilla profiler + // Gale start - MultiPaper - skip unnecessary mob spawning computations + NaturalSpawner.SpawnState spawnState; // moved down + final boolean _boolean = this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING) && !this.level.players().isEmpty(); // CraftBukkit + boolean flagAndHasNaturalSpawn = _boolean && this.anySpawnCategoryIsSpawnedThisTick(); + + if (flagAndHasNaturalSpawn) { + // Gale end - MultiPaper - skip unnecessary mob spawning computations int naturalSpawnChunkCount = this.distanceManager.getNaturalSpawnChunkCount(); // Paper start - Optional per player mob spawns - NaturalSpawner.SpawnState spawnState; if ((this.spawnFriendlies || this.spawnEnemies) && this.level.paperConfig().entities.spawning.perPlayerMobSpawns) { // don't count mobs when animals and monsters are disabled // re-set mob counts for (ServerPlayer player : this.level.players) { @@ -574,10 +580,14 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon } // Paper end - Optional per player mob spawns this.lastSpawnState = spawnState; - boolean _boolean = this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING) && !this.level.players().isEmpty(); // CraftBukkit + // Gale start - MultiPaper - skip unnecessary mob spawning computations + } else { + spawnState = null; + } + // Gale end - MultiPaper - skip unnecessary mob spawning computations int _int = this.level.getGameRules().getInt(GameRules.RULE_RANDOMTICKING); List filteredSpawningCategories; - if (_boolean && (this.spawnEnemies || this.spawnFriendlies)) { + if (flagAndHasNaturalSpawn && (this.spawnEnemies || this.spawnFriendlies)) { // Gale - MultiPaper - skip unnecessary mob spawning computations // Paper start - PlayerNaturallySpawnCreaturesEvent for (ServerPlayer entityPlayer : this.level.players()) { int chunkRange = Math.min(level.spigotConfig.mobSpawnRange, entityPlayer.getBukkitEntity().getViewDistance()); @@ -604,11 +614,25 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon } } - if (_boolean) { + if (flagAndHasNaturalSpawn) { // Gale - MultiPaper - skip unnecessary mob spawning computations this.level.tickCustomSpawners(this.spawnEnemies, this.spawnFriendlies); } } + // Gale start - MultiPaper - skip unnecessary mob spawning computations + private boolean anySpawnCategoryIsSpawnedThisTick() { + long gameTime = this.level.getLevelData().getGameTime(); + + for (long ticksForSpawnCategory : this.level.ticksPerSpawnCategory.values()) { + if (ticksForSpawnCategory != 0L && gameTime % ticksForSpawnCategory == 0L) { + return true; + } + } + + return false; + } + // Gale end - MultiPaper - skip unnecessary mob spawning computations + private void getFullChunk(long chunkPos, Consumer fullChunkGetter) { // Paper start - rewrite chunk system // note: bypass currentlyLoaded from getChunkNow