2023-08-21 09:44:47 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: kickash32 <kickash32@gmail.com>
Date: Mon, 5 Apr 2021 01:42:35 -0400
Subject: [PATCH] Improve cancelling PreCreatureSpawnEvent with per player mob
spawns
2024-12-19 12:03:16 +01:00
diff --git a/net/minecraft/server/level/ChunkMap.java b/net/minecraft/server/level/ChunkMap.java
2024-12-20 23:18:34 +01:00
index 9eed24939fc09f00a9dbce1be2ab9c34d024fd29..b3f498558614243cf633dcd71e3c49c2c55e6e0f 100644
2024-12-19 12:03:16 +01:00
--- a/net/minecraft/server/level/ChunkMap.java
+++ b/net/minecraft/server/level/ChunkMap.java
2024-12-20 23:18:34 +01:00
@@ -255,8 +255,25 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
2023-08-21 09:44:47 +02:00
}
}
2024-12-19 12:03:16 +01:00
2023-08-21 09:44:47 +02:00
+ // Paper start - per player mob count backoff
+ public void updateFailurePlayerMobTypeMap(int chunkX, int chunkZ, net.minecraft.world.entity.MobCategory mobCategory) {
+ if (!this.level.paperConfig().entities.spawning.perPlayerMobSpawns) {
+ return;
+ }
+ int idx = mobCategory.ordinal();
2024-07-17 19:24:53 +02:00
+ final ca.spottedleaf.moonrise.common.list.ReferenceList<ServerPlayer> inRange =
+ this.level.moonrise$getNearbyPlayers().getPlayersByChunk(chunkX, chunkZ, ca.spottedleaf.moonrise.common.misc.NearbyPlayers.NearbyMapType.TICK_VIEW_DISTANCE);
2023-09-24 06:43:10 +02:00
+ if (inRange == null) {
+ return;
+ }
2024-07-17 19:24:53 +02:00
+ final ServerPlayer[] backingSet = inRange.getRawDataUnchecked();
2023-09-24 06:43:10 +02:00
+ for (int i = 0, len = inRange.size(); i < len; i++) {
2024-07-17 19:24:53 +02:00
+ ++(backingSet[i].mobBackoffCounts[idx]);
2023-08-21 09:44:47 +02:00
+ }
+ }
+ // Paper end - per player mob count backoff
2024-01-23 18:01:39 +01:00
public int getMobCountNear(final ServerPlayer player, final net.minecraft.world.entity.MobCategory mobCategory) {
- return player.mobCounts[mobCategory.ordinal()];
+ return player.mobCounts[mobCategory.ordinal()] + player.mobBackoffCounts[mobCategory.ordinal()]; // Paper - per player mob count backoff
2024-12-19 12:03:16 +01:00
}
// Paper end - Optional per player mob spawns
diff --git a/net/minecraft/server/level/ServerChunkCache.java b/net/minecraft/server/level/ServerChunkCache.java
2024-12-20 23:18:34 +01:00
index 5576bf1d1d70ab7a010653d3207909b5de867e70..6540b2d6a1062d883811ce240c49d30d1925b291 100644
2024-12-19 12:03:16 +01:00
--- a/net/minecraft/server/level/ServerChunkCache.java
+++ b/net/minecraft/server/level/ServerChunkCache.java
2024-12-20 23:18:34 +01:00
@@ -576,7 +576,17 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
2024-10-27 12:02:26 +01:00
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) {
- Arrays.fill(player.mobCounts, 0);
+ // Paper start - per player mob spawning backoff
+ for (int ii = 0; ii < ServerPlayer.MOBCATEGORY_TOTAL_ENUMS; ii++) {
+ player.mobCounts[ii] = 0;
2023-08-21 09:44:47 +02:00
+
2024-10-27 12:02:26 +01:00
+ int newBackoff = player.mobBackoffCounts[ii] - 1; // TODO make configurable bleed // TODO use nonlinear algorithm?
+ if (newBackoff < 0) {
+ newBackoff = 0;
+ }
+ player.mobBackoffCounts[ii] = newBackoff;
+ }
+ // Paper end - per player mob spawning backoff
}
2024-12-19 12:03:16 +01:00
spawnState = NaturalSpawner.createState(naturalSpawnChunkCount, this.level.getAllEntities(), this::getFullChunk, null, true);
2024-10-27 12:02:26 +01:00
} else {
2024-12-19 12:03:16 +01:00
diff --git a/net/minecraft/server/level/ServerPlayer.java b/net/minecraft/server/level/ServerPlayer.java
2024-12-21 13:45:04 +01:00
index a63702dd7e86fc8b9f78c2ae23e23b65b6b2ee24..6d75a641431c7deb7e8ddbf02cdc919015a3a7dc 100644
2024-12-19 12:03:16 +01:00
--- a/net/minecraft/server/level/ServerPlayer.java
+++ b/net/minecraft/server/level/ServerPlayer.java
2024-12-20 23:18:34 +01:00
@@ -372,6 +372,7 @@ public class ServerPlayer extends Player implements ca.spottedleaf.moonrise.patc
2023-08-21 09:44:47 +02:00
public static final int MOBCATEGORY_TOTAL_ENUMS = net.minecraft.world.entity.MobCategory.values().length;
2024-12-19 12:03:16 +01:00
public final int[] mobCounts = new int[MOBCATEGORY_TOTAL_ENUMS];
2024-01-21 12:53:04 +01:00
// Paper end - Optional per player mob spawns
2023-08-21 09:44:47 +02:00
+ public final int[] mobBackoffCounts = new int[MOBCATEGORY_TOTAL_ENUMS]; // Paper - per player mob count backoff
2023-09-24 06:43:10 +02:00
// CraftBukkit start
2024-12-19 12:03:16 +01:00
public org.bukkit.craftbukkit.entity.CraftPlayer.TransferCookieConnection transferCookieConnection;
public String displayName;
diff --git a/net/minecraft/world/level/NaturalSpawner.java b/net/minecraft/world/level/NaturalSpawner.java
2024-12-20 23:18:34 +01:00
index ef8bacbbb43a9b80281a313ca43b7efff5a93e03..17ce115e887cbbb06ad02ab7ddb488e27342c0e4 100644
2024-12-19 12:03:16 +01:00
--- a/net/minecraft/world/level/NaturalSpawner.java
+++ b/net/minecraft/world/level/NaturalSpawner.java
@@ -285,6 +285,11 @@ public final class NaturalSpawner {
2023-08-21 09:44:47 +02:00
2024-12-19 12:03:16 +01:00
// Paper start - PreCreatureSpawnEvent
PreSpawnStatus doSpawning = isValidSpawnPostitionForType(level, category, structureManager, generator, spawnerData, mutableBlockPos, d2);
+ // Paper start - per player mob count backoff
+ if (doSpawning == PreSpawnStatus.ABORT || doSpawning == PreSpawnStatus.CANCELLED) {
+ level.getChunkSource().chunkMap.updateFailurePlayerMobTypeMap(mutableBlockPos.getX() >> 4, mutableBlockPos.getZ() >> 4, category);
+ }
+ // Paper end - per player mob count backoff
if (doSpawning == PreSpawnStatus.ABORT) {
return;
}