spigot/CraftBukkit-Patches/0006-Async-Operation-Catching.patch

262 lines
14 KiB
Diff
Raw Normal View History

2023-10-03 07:37:55 +11:00
From e761eaa4630a5fc89569fc13f464cf2ce41fcfba Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Tue, 25 Mar 2014 16:10:01 +1100
Subject: [PATCH] Async Operation Catching
Catch and throw an exception when a potentially unsafe operation occurs on a thread other than the main server thread.
2021-03-16 09:00:00 +11:00
diff --git a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
2023-09-25 08:07:32 +10:00
index 72e1ebb2e..d2f717803 100644
2021-03-16 09:00:00 +11:00
--- a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
2023-09-22 02:40:00 +10:00
@@ -1299,6 +1299,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
}
2019-04-23 12:00:00 +10:00
protected void addEntity(Entity entity) {
+ org.spigotmc.AsyncCatcher.catchOp("entity track"); // Spigot
2019-04-23 12:00:00 +10:00
if (!(entity instanceof EntityComplexPart)) {
2021-11-22 09:00:00 +11:00
EntityTypes<?> entitytypes = entity.getType();
int i = entitytypes.clientTrackingRange() * 16;
2023-09-22 02:40:00 +10:00
@@ -1334,6 +1335,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
}
2019-04-23 12:00:00 +10:00
protected void removeEntity(Entity entity) {
+ org.spigotmc.AsyncCatcher.catchOp("entity untrack"); // Spigot
if (entity instanceof EntityPlayer) {
EntityPlayer entityplayer = (EntityPlayer) entity;
2023-09-22 02:40:00 +10:00
@@ -1557,6 +1559,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2019-04-23 12:00:00 +10:00
}
2021-11-22 09:00:00 +11:00
public void removePlayer(EntityPlayer entityplayer) {
+ org.spigotmc.AsyncCatcher.catchOp("player tracker clear"); // Spigot
2021-06-11 15:00:00 +10:00
if (this.seenBy.remove(entityplayer.connection)) {
2021-11-22 09:00:00 +11:00
this.serverEntity.removePairing(entityplayer);
2019-04-23 12:00:00 +10:00
}
2023-09-22 02:40:00 +10:00
@@ -1564,6 +1567,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
2019-04-23 12:00:00 +10:00
}
2019-04-23 12:00:00 +10:00
public void updatePlayer(EntityPlayer entityplayer) {
+ org.spigotmc.AsyncCatcher.catchOp("player tracker update"); // Spigot
2021-06-11 15:00:00 +10:00
if (entityplayer != this.entity) {
2022-06-08 02:00:00 +10:00
Vec3D vec3d = entityplayer.position().subtract(this.entity.position());
2023-09-22 02:40:00 +10:00
int i = PlayerChunkMap.this.getPlayerViewDistance(entityplayer);
2021-03-16 09:00:00 +11:00
diff --git a/src/main/java/net/minecraft/server/level/WorldServer.java b/src/main/java/net/minecraft/server/level/WorldServer.java
2023-09-25 08:07:32 +10:00
index 046e1483d..2437fc257 100644
2021-03-16 09:00:00 +11:00
--- a/src/main/java/net/minecraft/server/level/WorldServer.java
+++ b/src/main/java/net/minecraft/server/level/WorldServer.java
2023-09-22 02:40:00 +10:00
@@ -1050,6 +1050,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2019-04-23 12:00:00 +10:00
// CraftBukkit start
2021-11-22 09:00:00 +11:00
private boolean addEntity(Entity entity, CreatureSpawnEvent.SpawnReason spawnReason) {
+ org.spigotmc.AsyncCatcher.catchOp("entity add"); // Spigot
2021-06-11 15:00:00 +10:00
if (entity.isRemoved()) {
2021-11-22 09:00:00 +11:00
// WorldServer.LOGGER.warn("Tried to add entity {} but it was marked as removed already", EntityTypes.getKey(entity.getType())); // CraftBukkit
2018-12-17 12:35:20 +11:00
return false;
2023-09-22 02:40:00 +10:00
@@ -1848,6 +1849,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2021-06-11 15:00:00 +10:00
@Override
public LevelEntityGetter<Entity> getEntities() {
+ org.spigotmc.AsyncCatcher.catchOp("Chunk getEntities call"); // Spigot
2021-11-22 09:00:00 +11:00
return this.entityManager.getEntityGetter();
}
2023-09-22 02:40:00 +10:00
@@ -1934,6 +1936,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2021-06-11 15:00:00 +10:00
}
2021-11-22 09:00:00 +11:00
public void onTrackingStart(Entity entity) {
2021-06-11 15:00:00 +10:00
+ org.spigotmc.AsyncCatcher.catchOp("entity register"); // Spigot
2021-11-22 09:00:00 +11:00
WorldServer.this.getChunkSource().addEntity(entity);
2021-06-11 15:00:00 +10:00
if (entity instanceof EntityPlayer) {
2021-12-11 00:00:00 +11:00
EntityPlayer entityplayer = (EntityPlayer) entity;
2023-09-22 02:40:00 +10:00
@@ -1971,6 +1974,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2021-06-11 15:00:00 +10:00
}
2021-11-22 09:00:00 +11:00
public void onTrackingEnd(Entity entity) {
2021-06-11 15:00:00 +10:00
+ org.spigotmc.AsyncCatcher.catchOp("entity unregister"); // Spigot
2021-11-22 09:00:00 +11:00
WorldServer.this.getChunkSource().removeEntity(entity);
2021-06-11 15:00:00 +10:00
if (entity instanceof EntityPlayer) {
EntityPlayer entityplayer = (EntityPlayer) entity;
2021-03-16 09:00:00 +11:00
diff --git a/src/main/java/net/minecraft/world/entity/EntityLiving.java b/src/main/java/net/minecraft/world/entity/EntityLiving.java
2023-09-25 08:07:32 +10:00
index 6c7ffcc49..49a1697ab 100644
2021-03-16 09:00:00 +11:00
--- a/src/main/java/net/minecraft/world/entity/EntityLiving.java
+++ b/src/main/java/net/minecraft/world/entity/EntityLiving.java
2023-06-08 01:30:00 +10:00
@@ -1102,6 +1102,7 @@ public abstract class EntityLiving extends Entity implements Attackable {
2021-03-16 09:00:00 +11:00
}
2021-06-11 15:00:00 +10:00
public boolean addEffect(MobEffect mobeffect, @Nullable Entity entity, EntityPotionEffectEvent.Cause cause) {
2021-03-16 09:00:00 +11:00
+ org.spigotmc.AsyncCatcher.catchOp("effect add"); // Spigot
if (isTickingEffects) {
effectsToProcess.add(new ProcessableEffect(mobeffect, cause));
return true;
diff --git a/src/main/java/net/minecraft/world/item/crafting/CraftingManager.java b/src/main/java/net/minecraft/world/item/crafting/CraftingManager.java
2023-09-25 08:07:32 +10:00
index 7917e442c..1efa5da7a 100644
2021-03-16 09:00:00 +11:00
--- a/src/main/java/net/minecraft/world/item/crafting/CraftingManager.java
+++ b/src/main/java/net/minecraft/world/item/crafting/CraftingManager.java
2023-09-22 02:40:00 +10:00
@@ -89,6 +89,7 @@ public class CraftingManager extends ResourceDataJson {
2021-03-16 09:00:00 +11:00
// CraftBukkit start
2023-09-22 02:40:00 +10:00
public void addRecipe(RecipeHolder<?> irecipe) {
2021-03-16 09:00:00 +11:00
+ org.spigotmc.AsyncCatcher.catchOp("Recipe Add"); // Spigot
2023-09-22 02:40:00 +10:00
Object2ObjectLinkedOpenHashMap<MinecraftKey, RecipeHolder<?>> map = this.recipes.get(irecipe.value().getType()); // CraftBukkit
2021-03-16 09:00:00 +11:00
2023-09-22 02:40:00 +10:00
if (byName.containsKey(irecipe.id()) || map.containsKey(irecipe.id())) {
2021-03-16 09:00:00 +11:00
diff --git a/src/main/java/net/minecraft/world/level/block/state/BlockBase.java b/src/main/java/net/minecraft/world/level/block/state/BlockBase.java
2023-09-25 08:07:32 +10:00
index 1a8d0e568..148c01954 100644
2021-03-16 09:00:00 +11:00
--- a/src/main/java/net/minecraft/world/level/block/state/BlockBase.java
+++ b/src/main/java/net/minecraft/world/level/block/state/BlockBase.java
2023-03-15 03:30:00 +11:00
@@ -145,11 +145,14 @@ public abstract class BlockBase implements FeatureElement {
2021-03-16 09:00:00 +11:00
2021-11-25 08:00:00 +11:00
/** @deprecated */
2021-03-16 09:00:00 +11:00
@Deprecated
- public void onPlace(IBlockData iblockdata, World world, BlockPosition blockposition, IBlockData iblockdata1, boolean flag) {}
+ public void onPlace(IBlockData iblockdata, World world, BlockPosition blockposition, IBlockData iblockdata1, boolean flag) {
+ org.spigotmc.AsyncCatcher.catchOp("block onPlace"); // Spigot
+ }
2021-11-25 08:00:00 +11:00
/** @deprecated */
2021-03-16 09:00:00 +11:00
@Deprecated
2021-11-22 09:00:00 +11:00
public void onRemove(IBlockData iblockdata, World world, BlockPosition blockposition, IBlockData iblockdata1, boolean flag) {
2021-03-16 09:00:00 +11:00
+ org.spigotmc.AsyncCatcher.catchOp("block remove"); // Spigot
2021-11-22 09:00:00 +11:00
if (iblockdata.hasBlockEntity() && !iblockdata.is(iblockdata1.getBlock())) {
world.removeBlockEntity(blockposition);
2021-03-16 09:00:00 +11:00
}
2018-07-18 12:36:24 +10:00
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
2023-09-25 08:07:32 +10:00
index 42049a0ad..4563cbc38 100644
2018-07-18 12:36:24 +10:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
2023-09-25 08:07:32 +10:00
@@ -854,6 +854,7 @@ public final class CraftServer implements Server {
2018-07-18 12:36:24 +10:00
public boolean dispatchCommand(CommandSender sender, String commandLine) {
2023-06-12 19:41:07 +10:00
Preconditions.checkArgument(sender != null, "sender cannot be null");
Preconditions.checkArgument(commandLine != null, "commandLine cannot be null");
+ org.spigotmc.AsyncCatcher.catchOp("command dispatch"); // Spigot
2018-07-18 12:36:24 +10:00
if (commandMap.dispatch(sender, commandLine)) {
return true;
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2023-09-25 08:07:32 +10:00
index 90c826fe3..8a23c7441 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2023-06-12 19:41:07 +10:00
@@ -273,6 +273,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {
2019-04-28 11:41:06 +10:00
@Override
2019-04-23 12:00:00 +10:00
public boolean unloadChunkRequest(int x, int z) {
+ org.spigotmc.AsyncCatcher.catchOp("chunk unload"); // Spigot
2020-06-06 19:28:03 +10:00
if (isChunkLoaded(x, z)) {
2021-11-22 09:00:00 +11:00
world.getChunkSource().removeRegionTicket(TicketType.PLUGIN, new ChunkCoordIntPair(x, z), 1, Unit.INSTANCE);
2020-06-06 19:28:03 +10:00
}
2023-06-12 19:41:07 +10:00
@@ -281,6 +282,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {
}
2019-04-23 12:00:00 +10:00
private boolean unloadChunk0(int x, int z, boolean save) {
+ org.spigotmc.AsyncCatcher.catchOp("chunk unload"); // Spigot
2020-06-06 19:28:03 +10:00
if (!isChunkLoaded(x, z)) {
2019-04-23 12:00:00 +10:00
return true;
2020-06-06 19:28:03 +10:00
}
2023-06-12 19:41:07 +10:00
@@ -295,6 +297,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {
2019-04-28 11:41:06 +10:00
@Override
public boolean regenerateChunk(int x, int z) {
+ org.spigotmc.AsyncCatcher.catchOp("chunk regenerate"); // Spigot
2019-04-23 12:00:00 +10:00
throw new UnsupportedOperationException("Not supported in this Minecraft version! Unless you can fix it, this is not a bug :)");
/*
if (!unloadChunk0(x, z, false)) {
2023-06-12 19:41:07 +10:00
@@ -347,6 +350,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {
2019-04-28 11:41:06 +10:00
@Override
public boolean loadChunk(int x, int z, boolean generate) {
+ org.spigotmc.AsyncCatcher.catchOp("chunk load"); // Spigot
2021-11-22 09:00:00 +11:00
IChunkAccess chunk = world.getChunkSource().getChunk(x, z, generate ? ChunkStatus.FULL : ChunkStatus.EMPTY, true);
2019-04-23 12:00:00 +10:00
// If generate = false, but the chunk already exists, we will get this back.
2023-06-12 19:41:07 +10:00
@@ -841,6 +845,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {
@Override
public Collection<Entity> getNearbyEntities(BoundingBox boundingBox, Predicate<Entity> filter) {
+ org.spigotmc.AsyncCatcher.catchOp("getNearbyEntities"); // Spigot
2023-06-12 19:41:07 +10:00
Preconditions.checkArgument(boundingBox != null, "BoundingBox cannot be null");
AxisAlignedBB bb = new AxisAlignedBB(boundingBox.getMinX(), boundingBox.getMinY(), boundingBox.getMinZ(), boundingBox.getMaxX(), boundingBox.getMaxY(), boundingBox.getMaxZ());
2023-06-12 19:41:07 +10:00
@@ -995,6 +1000,7 @@ public class CraftWorld extends CraftRegionAccessor implements World {
2019-08-13 20:38:28 +10:00
@Override
public void save() {
+ org.spigotmc.AsyncCatcher.catchOp("world save"); // Spigot
this.server.checkSaveState();
2021-06-11 15:00:00 +10:00
boolean oldSave = world.noSave;
2020-06-25 10:00:00 +10:00
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
2023-10-03 07:37:55 +11:00
index e89ef7b7a..efd781c34 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
2023-10-03 07:37:55 +11:00
@@ -567,6 +567,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
@Override
public List<org.bukkit.entity.Entity> getNearbyEntities(double x, double y, double z) {
2021-08-15 08:08:25 +10:00
Preconditions.checkState(!entity.generation, "Cannot get nearby entities during world generation");
+ org.spigotmc.AsyncCatcher.catchOp("getNearbyEntities"); // Spigot
2021-08-15 08:08:25 +10:00
2023-06-08 01:30:00 +10:00
List<Entity> notchEntityList = entity.level().getEntities(entity, entity.getBoundingBox().inflate(x, y, z), Predicates.alwaysTrue());
List<org.bukkit.entity.Entity> bukkitEntityList = new java.util.ArrayList<org.bukkit.entity.Entity>(notchEntityList.size());
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2023-09-25 08:07:32 +10:00
index a11dfb26b..f91e28d75 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2023-09-22 02:40:00 +10:00
@@ -388,6 +388,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2013-12-01 14:40:53 +11:00
@Override
public void kickPlayer(String message) {
+ org.spigotmc.AsyncCatcher.catchOp("player kick"); // Spigot
2021-06-11 15:00:00 +10:00
if (getHandle().connection == null) return;
2021-06-11 15:00:00 +10:00
getHandle().connection.disconnect(message == null ? "" : message);
diff --git a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboardManager.java b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboardManager.java
2023-09-25 08:07:32 +10:00
index 1713b8698..a9acf1d06 100644
--- a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboardManager.java
+++ b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboardManager.java
2019-04-28 11:41:06 +10:00
@@ -41,6 +41,7 @@ public final class CraftScoreboardManager implements ScoreboardManager {
2019-04-28 11:41:06 +10:00
@Override
public CraftScoreboard getNewScoreboard() {
+ org.spigotmc.AsyncCatcher.catchOp("scoreboard creation"); // Spigot
CraftScoreboard scoreboard = new CraftScoreboard(new ScoreboardServer(server));
scoreboards.add(scoreboard);
return scoreboard;
2017-06-19 19:56:13 +10:00
diff --git a/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java b/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java
2023-09-25 08:07:32 +10:00
index 2e057fd4c..ddef523ea 100644
2017-06-19 19:56:13 +10:00
--- a/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java
2019-04-23 12:00:00 +10:00
@@ -12,6 +12,7 @@ public class ServerShutdownThread extends Thread {
2017-06-19 19:56:13 +10:00
@Override
public void run() {
try {
+ org.spigotmc.AsyncCatcher.enabled = false; // Spigot
2019-04-23 12:00:00 +10:00
server.close();
} finally {
try {
diff --git a/src/main/java/org/spigotmc/AsyncCatcher.java b/src/main/java/org/spigotmc/AsyncCatcher.java
new file mode 100644
2023-09-25 08:07:32 +10:00
index 000000000..aeed76972
--- /dev/null
+++ b/src/main/java/org/spigotmc/AsyncCatcher.java
@@ -0,0 +1,17 @@
+package org.spigotmc;
+
+import net.minecraft.server.MinecraftServer;
+
+public class AsyncCatcher
+{
+
+ public static boolean enabled = true;
+
+ public static void catchOp(String reason)
+ {
2019-04-23 12:00:00 +10:00
+ if ( enabled && Thread.currentThread() != MinecraftServer.getServer().serverThread )
+ {
+ throw new IllegalStateException( "Asynchronous " + reason + "!" );
+ }
+ }
+}
--
2023-09-22 02:40:00 +10:00
2.42.0