187 lines
13 KiB
Diff
187 lines
13 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Helvetica Volubi <suisuroru@blue-millennium.fun>
|
|
Date: Wed, 3 Dec 2025 16:28:34 +0800
|
|
Subject: [PATCH] Global Entities Counter
|
|
|
|
|
|
diff --git a/io/papermc/paper/threadedregions/RegionizedWorldData.java b/io/papermc/paper/threadedregions/RegionizedWorldData.java
|
|
index 0cf5815672ac62d8caf7f18165be1f0f8e067aed..64d407c5a6140a7e5dc47a8e304b4c00602e2bdd 100644
|
|
--- a/io/papermc/paper/threadedregions/RegionizedWorldData.java
|
|
+++ b/io/papermc/paper/threadedregions/RegionizedWorldData.java
|
|
@@ -558,6 +558,9 @@ public final class RegionizedWorldData {
|
|
public final Map<Long, ChunkSectionItemEntityMovementTracker> itemEntityMovementTrackerMap = new it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap<>();
|
|
// Luminol end
|
|
|
|
+ public int uniqueId = -1; // Lophine - Global Entities Counter
|
|
+ public boolean underGlobalEntitiesCounter = false; // Lophine - Global Entities Counter
|
|
+
|
|
public final TickRegions.TickRegionData regionData;
|
|
|
|
public RegionizedWorldData(final ServerLevel world, final TickRegions.TickRegionData regionData) {
|
|
@@ -570,6 +573,15 @@ public final class RegionizedWorldData {
|
|
this.turbo = new io.papermc.paper.redstone.RedstoneWireTurbo((RedStoneWireBlock)Blocks.REDSTONE_WIRE);
|
|
this.regionData = regionData;
|
|
|
|
+ // Lophine start - Global Entities Counter
|
|
+ if (fun.bm.lophine.config.modules.experiment.GlobalEntitiesCounter.enabled) {
|
|
+ uniqueId = fun.bm.lophine.utils.EntitiesCounterUtil.generateUniqueId();
|
|
+ fun.bm.lophine.utils.EntitiesCounterUtil.reportAreaMap(world, spawnChunkTracker, uniqueId);
|
|
+ fun.bm.lophine.utils.EntitiesCounterUtil.addDataToLoaded(world, loadedEntities, uniqueId);
|
|
+ underGlobalEntitiesCounter = true;
|
|
+ }
|
|
+ // Lophine end - Global Entities Counter
|
|
+
|
|
// tasks may be drained before the region ticks, so we must set up the tick data early just in case
|
|
this.updateTickData();
|
|
}
|
|
diff --git a/net/minecraft/server/level/ServerChunkCache.java b/net/minecraft/server/level/ServerChunkCache.java
|
|
index 27389ff98942e27c2a877ea5b671caa31353d18c..e9e5f929d638d3ddf279423d3e23b3f824ad7df2 100644
|
|
--- a/net/minecraft/server/level/ServerChunkCache.java
|
|
+++ b/net/minecraft/server/level/ServerChunkCache.java
|
|
@@ -513,6 +513,25 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
|
|
this.chunkMap.tick(haveTime);
|
|
profiler.pop();
|
|
this.clearCache();
|
|
+ // Lophine start - Global Entities Counter
|
|
+ if (fun.bm.lophine.config.modules.experiment.GlobalEntitiesCounter.enabled) {
|
|
+ io.papermc.paper.threadedregions.RegionizedWorldData regionizedWorldData = this.level.getCurrentWorldData();
|
|
+ if (regionizedWorldData != null) {
|
|
+ if (regionizedWorldData.underGlobalEntitiesCounter) {
|
|
+ if (regionizedWorldData.getLocalPlayers().isEmpty()) {
|
|
+ fun.bm.lophine.utils.EntitiesCounterUtil.onWorldDataUnload(level, regionizedWorldData.uniqueId);
|
|
+ regionizedWorldData.underGlobalEntitiesCounter = false;
|
|
+ }
|
|
+ } else {
|
|
+ if (!regionizedWorldData.getLocalPlayers().isEmpty()) {
|
|
+ fun.bm.lophine.utils.EntitiesCounterUtil.reportAreaMap(level, regionizedWorldData.spawnChunkTracker, regionizedWorldData.uniqueId);
|
|
+ fun.bm.lophine.utils.EntitiesCounterUtil.addDataToLoaded(level, (ca.spottedleaf.moonrise.common.list.ReferenceList<Entity>) regionizedWorldData.getLoadedEntities(), regionizedWorldData.uniqueId);
|
|
+ regionizedWorldData.underGlobalEntitiesCounter = true;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ // Lophine end - Global Entities Counter
|
|
}
|
|
|
|
private void tickChunks() {
|
|
@@ -577,7 +596,13 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
|
|
}
|
|
spawnCookie = NaturalSpawner.createState(chunkCount, regionizedWorldData.getLoadedEntities(), this::getFullChunk, null, true); // Folia - region threading - note: function only cares about loaded entities, doesn't need all
|
|
} else {
|
|
- spawnCookie = NaturalSpawner.createState(chunkCount, regionizedWorldData.getLoadedEntities(), this::getFullChunk, !this.level.paperConfig().entities.spawning.perPlayerMobSpawns ? new LocalMobCapCalculator(this.chunkMap) : null, false); // Folia - region threading - note: function only cares about loaded entities, doesn't need all
|
|
+ // Lophine start - Global Entities Counter
|
|
+ if (fun.bm.lophine.config.modules.experiment.GlobalEntitiesCounter.enabled) {
|
|
+ spawnCookie = fun.bm.lophine.utils.EntitiesCounterUtil.runRemainingTasks(level, regionizedWorldData.getLoadedEntities(), this::getFullChunk, !this.level.paperConfig().entities.spawning.perPlayerMobSpawns ? new LocalMobCapCalculator(this.chunkMap) : null, false);
|
|
+ } else {
|
|
+ spawnCookie = NaturalSpawner.createState(chunkCount, regionizedWorldData.getLoadedEntities(), this::getFullChunk, !this.level.paperConfig().entities.spawning.perPlayerMobSpawns ? new LocalMobCapCalculator(this.chunkMap) : null, false); // Folia - region threading - note: function only cares about loaded entities, doesn't need all
|
|
+ }
|
|
+ // Lophine end - Global Entities Counter
|
|
}
|
|
} finally { foliaProfiler.stopTimer(ca.spottedleaf.leafprofiler.LProfilerRegistry.MOB_SPAWN_ENTITY_COUNT); } // Folia - profiler
|
|
// Paper end - Optional per player mob spawns
|
|
@@ -585,7 +610,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
|
|
boolean doMobSpawning = this.level.getGameRules().get(GameRules.SPAWN_MOBS) && !this.level.getLocalPlayers().isEmpty(); // CraftBukkit // Folia - region threading
|
|
int tickSpeed = this.level.getGameRules().get(GameRules.RANDOM_TICK_SPEED);
|
|
List<MobCategory> spawningCategories;
|
|
- if (doMobSpawning && (this.spawnEnemies || this.spawnFriendlies)) { // Paper
|
|
+ if (spawnCookie != null && doMobSpawning && (this.spawnEnemies || this.spawnFriendlies)) { // Paper // Lophine - Global Entities Counter
|
|
// Paper start - PlayerNaturallySpawnCreaturesEvent
|
|
for (ServerPlayer player : this.level.getLocalPlayers()) { // Folia - region threading
|
|
int chunkRange = Math.min(level.spigotConfig.mobSpawnRange, player.getBukkitEntity().getViewDistance());
|
|
@@ -645,7 +670,7 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
|
|
this.level.tickThunder(chunk);
|
|
}
|
|
|
|
- if (!spawningCategories.isEmpty()) {
|
|
+ if (spawnCookie != null && !spawningCategories.isEmpty()) { // Lophine - Global Entities Counter
|
|
if (this.level.getWorldBorder().isWithinBounds(chunkPos)) { // Paper - rewrite chunk system
|
|
NaturalSpawner.spawnForChunk(this.level, chunk, spawnCookie, spawningCategories);
|
|
}
|
|
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
|
|
index a7cebb797973cde399dc30c696b064efd40874f2..c0e62a69bb5c9e777265b5474d76ba8d06ccec4b 100644
|
|
--- a/net/minecraft/server/level/ServerLevel.java
|
|
+++ b/net/minecraft/server/level/ServerLevel.java
|
|
@@ -808,6 +808,11 @@ public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGet
|
|
}
|
|
|
|
public void tick(BooleanSupplier haveTime, final io.papermc.paper.threadedregions.TickRegions.TickRegionData region) { // Folia - regionised ticking
|
|
+ // Lophine start - Global Entities Counter
|
|
+ if (fun.bm.lophine.config.modules.experiment.GlobalEntitiesCounter.enabled) {
|
|
+ if (fun.bm.lophine.utils.EntitiesCounterUtil.canRunNewTask(this)) fun.bm.lophine.utils.EntitiesCounterUtil.tick(this);
|
|
+ }
|
|
+ // Lophine end - Global Entities Counter
|
|
final io.papermc.paper.threadedregions.RegionizedWorldData regionizedWorldData = this.getCurrentWorldData(); // Folia - regionised ticking
|
|
final ca.spottedleaf.leafprofiler.RegionizedProfiler.Handle foliaProfiler = io.papermc.paper.threadedregions.TickRegionScheduler.getProfiler(); // Folia - profiler
|
|
ProfilerFiller profiler = Profiler.get();
|
|
diff --git a/net/minecraft/world/entity/Mob.java b/net/minecraft/world/entity/Mob.java
|
|
index fc29654dd7ee3329edf1ccf705f45631d0ecc079..9b8ee2df5a0b6d76d9bfc747187f6cd51bb4a520 100644
|
|
--- a/net/minecraft/world/entity/Mob.java
|
|
+++ b/net/minecraft/world/entity/Mob.java
|
|
@@ -813,6 +813,44 @@ public abstract class Mob extends LivingEntity implements Targeting, EquipmentUs
|
|
}
|
|
}
|
|
} else {
|
|
+ // Lophine start - fix some bug if player is cross scheduler >> to remove monster if the region don't have any valid player
|
|
+ if (fun.bm.lophine.config.modules.experiment.GlobalEntitiesCounter.enabled) {
|
|
+ List<net.minecraft.server.level.ServerPlayer> playerList = this.level().getServer().getPlayerList().getPlayers();
|
|
+ if (!playerList.isEmpty()) {
|
|
+ double d = -1.0;
|
|
+ Vec3 vec3 = null;
|
|
+ for (net.minecraft.server.level.ServerPlayer player1 : playerList) {
|
|
+ if (player1.level() != this.level()) continue;
|
|
+ double d1 = player1.distanceToSqr(this.position());
|
|
+ if (d == -1.0 || d1 < d) {
|
|
+ d = d1;
|
|
+ vec3 = player1.position();
|
|
+ }
|
|
+ }
|
|
+ if (vec3 != null) {
|
|
+ // copy from PaperMC
|
|
+ // Paper start - Configurable despawn distances
|
|
+ final io.papermc.paper.configuration.WorldConfiguration.Entities.Spawning.DespawnRangePair despawnRangePair = this.level().paperConfig().entities.spawning.despawnRanges.get(this.getType().getCategory());
|
|
+ final io.papermc.paper.configuration.type.DespawnRange.Shape shape = this.level().paperConfig().entities.spawning.despawnRangeShape;
|
|
+ final double dy = Math.abs(vec3.y - this.getY());
|
|
+ final double dySqr = Mth.square(dy);
|
|
+ final double dxSqr = Mth.square(vec3.x - this.getX());
|
|
+ final double dzSqr = Mth.square(vec3.z - this.getZ());
|
|
+ final double distanceSquared = dxSqr + dzSqr + dySqr;
|
|
+ // Despawn if hard/soft limit is exceeded
|
|
+ if (despawnRangePair.hard().shouldDespawn(shape, dxSqr, dySqr, dzSqr, dy) && this.removeWhenFarAway(distanceSquared)) {
|
|
+ this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
|
|
+ }
|
|
+
|
|
+ if (despawnRangePair.soft().shouldDespawn(shape, dxSqr, dySqr, dzSqr, dy)) {
|
|
+ if (this.noActionTime > 600 && this.random.nextInt(800) == 0 && this.removeWhenFarAway(distanceSquared)) {
|
|
+ this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ // Lophine end - fix some bug if player is cross scheduler >> to remove monster if the region don't have any valid player
|
|
this.noActionTime = 0;
|
|
}
|
|
}
|
|
diff --git a/net/minecraft/world/level/NaturalSpawner.java b/net/minecraft/world/level/NaturalSpawner.java
|
|
index b3b8160927147468c1dd4a30fac51f96a9d60c9a..cd20d10ee4cd2fe98131c9a93367ae133f96b8d4 100644
|
|
--- a/net/minecraft/world/level/NaturalSpawner.java
|
|
+++ b/net/minecraft/world/level/NaturalSpawner.java
|
|
@@ -121,7 +121,7 @@ public final class NaturalSpawner {
|
|
return new NaturalSpawner.SpawnState(spawnableChunkCount, mobCounts, spawnPotential, localMobCapCalculator);
|
|
}
|
|
|
|
- private static Biome getRoughBiome(final BlockPos pos, final ChunkAccess chunk) {
|
|
+ public static Biome getRoughBiome(final BlockPos pos, final ChunkAccess chunk) { // Lophine - protected -> public
|
|
return chunk.getNoiseBiome(QuartPos.fromBlock(pos.getX()), QuartPos.fromBlock(pos.getY()), QuartPos.fromBlock(pos.getZ())).value();
|
|
}
|
|
|
|
@@ -628,7 +628,7 @@ public final class NaturalSpawner {
|
|
private @Nullable EntityType<?> lastCheckedType;
|
|
private double lastCharge;
|
|
|
|
- private SpawnState(
|
|
+ public SpawnState( // Lophine - protected -> public
|
|
final int spawnableChunkCount,
|
|
final Object2IntOpenHashMap<MobCategory> mobCategoryCounts,
|
|
final PotentialCalculator spawnPotential,
|