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
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
2024-09-07 21:49:28 +02:00
index 56f7a63e65cce587512b77aafdb4ced18b43d024..21370ed6c7d98d3f3546f0365ac50e5c26ba3bde 100644
2023-08-21 09:44:47 +02:00
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
2024-07-17 19:24:53 +02:00
@@ -240,8 +240,25 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
++(backingSet[i].mobCounts[index]);
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-06-15 19:24:11 +02:00
// Paper end - Optional per player mob spawns
2023-08-21 09:44:47 +02:00
}
2024-06-15 19:24:11 +02:00
// Paper end
2023-08-21 09:44:47 +02:00
diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2024-07-29 01:18:25 +02:00
index e18c3e08f9fbfe17c797c1f96dce1b86fa41fab6..4a5dc7fd4eb1a7ab1ec371f0f107de882f88149c 100644
2023-08-21 09:44:47 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerChunkCache.java
+++ b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2024-07-29 01:18:25 +02:00
@@ -456,7 +456,17 @@ public class ServerChunkCache extends ChunkSource implements ca.spottedleaf.moon
2023-12-06 17:34:54 +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
+
2023-12-06 17:34:54 +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;
2023-08-21 09:44:47 +02:00
+ }
2023-12-06 17:34:54 +01:00
+ // Paper end - per player mob spawning backoff
}
spawnercreature_d = NaturalSpawner.createState(naturalSpawnChunkCount, this.level.getAllEntities(), this::getFullChunk, null, true);
} else {
2023-08-21 09:44:47 +02:00
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2024-08-31 20:29:50 +02:00
index cb961d9051416626f499c1ca87107f1114433c94..8dc3ba983fd4c61e463867be8d224aa90424215a 100644
2023-08-21 09:44:47 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2024-07-18 10:13:20 +02:00
@@ -280,6 +280,7 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player imple
2023-08-21 09:44:47 +02:00
public static final int MOBCATEGORY_TOTAL_ENUMS = net.minecraft.world.entity.MobCategory.values().length;
public final int[] mobCounts = new int[MOBCATEGORY_TOTAL_ENUMS]; // Paper
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-04-25 11:42:10 +02:00
public CraftPlayer.TransferCookieConnection transferCookieConnection;
2023-08-21 09:44:47 +02:00
diff --git a/src/main/java/net/minecraft/world/level/NaturalSpawner.java b/src/main/java/net/minecraft/world/level/NaturalSpawner.java
2024-07-17 19:24:53 +02:00
index 364510c0d0667e67aa3b25099a021f5f856fc113..e524b27d185da3e88668f8ef107517272860bd66 100644
2023-08-21 09:44:47 +02:00
--- a/src/main/java/net/minecraft/world/level/NaturalSpawner.java
+++ b/src/main/java/net/minecraft/world/level/NaturalSpawner.java
2024-04-25 11:42:10 +02:00
@@ -272,6 +272,11 @@ public final class NaturalSpawner {
2023-08-21 09:44:47 +02:00
2024-01-22 18:04:55 +01:00
// Paper start - PreCreatureSpawnEvent
2023-08-21 09:44:47 +02:00
PreSpawnStatus doSpawning = isValidSpawnPostitionForType(world, group, structuremanager, chunkgenerator, biomesettingsmobs_c, blockposition_mutableblockposition, d2);
2024-01-14 16:31:39 +01:00
+ // Paper start - per player mob count backoff
2023-08-21 09:44:47 +02:00
+ if (doSpawning == PreSpawnStatus.ABORT || doSpawning == PreSpawnStatus.CANCELLED) {
+ world.getChunkSource().chunkMap.updateFailurePlayerMobTypeMap(blockposition_mutableblockposition.getX() >> 4, blockposition_mutableblockposition.getZ() >> 4, group);
+ }
2024-01-14 16:31:39 +01:00
+ // Paper end - per player mob count backoff
2023-08-21 09:44:47 +02:00
if (doSpawning == PreSpawnStatus.ABORT) {
2024-01-21 12:53:04 +01:00
return j; // Paper - Optional per player mob spawns
2023-08-21 09:44:47 +02:00
}