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-01-16 19:27:39 +01:00
index a9c94645a4bf3837bce5622b76f0d24a5a4ef065..cc1b903111730d4042fdbf4d2dae88ae72ac4bc8 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
2023-12-25 23:51:56 +01:00
@@ -386,8 +386,26 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
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();
2023-09-24 06:43:10 +02:00
+ final com.destroystokyo.paper.util.maplist.ReferenceList<ServerPlayer> inRange =
+ this.getNearbyPlayers().getPlayersByChunk(chunkX, chunkZ, io.papermc.paper.util.player.NearbyPlayers.NearbyMapType.TICK_VIEW_DISTANCE);
+ if (inRange == null) {
+ return;
+ }
+ final Object[] backingSet = inRange.getRawData();
+ for (int i = 0, len = inRange.size(); i < len; i++) {
+ ++((ServerPlayer)backingSet[i]).mobBackoffCounts[idx];
2023-08-21 09:44:47 +02:00
+ }
+ }
+ // Paper end - per player mob count backoff
+
public int getMobCountNear(ServerPlayer entityPlayer, net.minecraft.world.entity.MobCategory mobCategory) {
- return entityPlayer.mobCounts[mobCategory.ordinal()];
+ return entityPlayer.mobCounts[mobCategory.ordinal()] + entityPlayer.mobBackoffCounts[mobCategory.ordinal()]; // Paper - per player mob count backoff
}
// Paper end
diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
2023-12-07 21:27:28 +01:00
index 1483007b79e18107e41037c279e048f04f666d1d..5152979bf81345fc365e7b16028c7d970d2f5856 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
2023-12-06 17:34:54 +01:00
@@ -539,7 +539,17 @@ public class ServerChunkCache extends ChunkSource {
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-01-18 18:52:00 +01:00
index 6bf33688882e3dca8868590454a9d25567a6a780..b63cbff6e42791bb9e490c0fb5e4e719abb4f202 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
2023-12-06 17:34:54 +01:00
@@ -260,6 +260,7 @@ public class ServerPlayer extends Player {
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
2023-09-24 06:43:10 +02:00
// Paper end - mob spawning rework
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
public String displayName;
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-01-14 16:31:39 +01:00
index d08c7b0b52065980f1f13c5533ff6355028322db..dc211c6aedc178ac50b7d05aab3662c422211cbd 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
2023-09-24 06:43:10 +02:00
@@ -277,6 +277,11 @@ public final class NaturalSpawner {
2023-08-21 09:44:47 +02:00
// Paper start
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) {
return j; // Paper
}