From 5f11690d01d5c9edcb1ce23d539ea6cbe9e61328 Mon Sep 17 00:00:00 2001 From: md_5 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. diff --git a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java index fe8992bb48..e1353632bf 100644 --- a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java +++ b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java @@ -1292,6 +1292,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.e { } protected void addEntity(Entity entity) { + org.spigotmc.AsyncCatcher.catchOp("entity track"); // Spigot if (!(entity instanceof EntityComplexPart)) { EntityTypes entitytypes = entity.getType(); int i = entitytypes.clientTrackingRange() * 16; @@ -1327,6 +1328,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.e { } protected void removeEntity(Entity entity) { + org.spigotmc.AsyncCatcher.catchOp("entity untrack"); // Spigot if (entity instanceof EntityPlayer) { EntityPlayer entityplayer = (EntityPlayer) entity; @@ -1579,6 +1581,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.e { } public void removePlayer(EntityPlayer entityplayer) { + org.spigotmc.AsyncCatcher.catchOp("player tracker clear"); // Spigot if (this.seenBy.remove(entityplayer.connection)) { this.serverEntity.removePairing(entityplayer); } @@ -1586,6 +1589,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.e { } public void updatePlayer(EntityPlayer entityplayer) { + org.spigotmc.AsyncCatcher.catchOp("player tracker update"); // Spigot if (entityplayer != this.entity) { Vec3D vec3d = entityplayer.position().subtract(this.entity.position()); double d0 = (double) Math.min(this.getEffectiveRange(), (PlayerChunkMap.this.viewDistance - 1) * 16); diff --git a/src/main/java/net/minecraft/server/level/WorldServer.java b/src/main/java/net/minecraft/server/level/WorldServer.java index db82616d9c..6e38b63bf9 100644 --- a/src/main/java/net/minecraft/server/level/WorldServer.java +++ b/src/main/java/net/minecraft/server/level/WorldServer.java @@ -1034,6 +1034,7 @@ public class WorldServer extends World implements GeneratorAccessSeed { // CraftBukkit start private boolean addEntity(Entity entity, CreatureSpawnEvent.SpawnReason spawnReason) { + org.spigotmc.AsyncCatcher.catchOp("entity add"); // Spigot if (entity.isRemoved()) { // WorldServer.LOGGER.warn("Tried to add entity {} but it was marked as removed already", EntityTypes.getKey(entity.getType())); // CraftBukkit return false; @@ -1841,6 +1842,7 @@ public class WorldServer extends World implements GeneratorAccessSeed { @Override public LevelEntityGetter getEntities() { + org.spigotmc.AsyncCatcher.catchOp("Chunk getEntities call"); // Spigot return this.entityManager.getEntityGetter(); } @@ -1919,6 +1921,7 @@ public class WorldServer extends World implements GeneratorAccessSeed { } public void onTrackingStart(Entity entity) { + org.spigotmc.AsyncCatcher.catchOp("entity register"); // Spigot WorldServer.this.getChunkSource().addEntity(entity); if (entity instanceof EntityPlayer) { EntityPlayer entityplayer = (EntityPlayer) entity; @@ -1956,6 +1959,7 @@ public class WorldServer extends World implements GeneratorAccessSeed { } public void onTrackingEnd(Entity entity) { + org.spigotmc.AsyncCatcher.catchOp("entity unregister"); // Spigot WorldServer.this.getChunkSource().removeEntity(entity); if (entity instanceof EntityPlayer) { EntityPlayer entityplayer = (EntityPlayer) entity; diff --git a/src/main/java/net/minecraft/world/entity/EntityLiving.java b/src/main/java/net/minecraft/world/entity/EntityLiving.java index c07d54bd72..2a733f8eb9 100644 --- a/src/main/java/net/minecraft/world/entity/EntityLiving.java +++ b/src/main/java/net/minecraft/world/entity/EntityLiving.java @@ -1084,6 +1084,7 @@ public abstract class EntityLiving extends Entity implements Attackable { } public boolean addEffect(MobEffect mobeffect, @Nullable Entity entity, EntityPotionEffectEvent.Cause cause) { + 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 index c7912e8ec5..6751a30592 100644 --- a/src/main/java/net/minecraft/world/item/crafting/CraftingManager.java +++ b/src/main/java/net/minecraft/world/item/crafting/CraftingManager.java @@ -86,6 +86,7 @@ public class CraftingManager extends ResourceDataJson { // CraftBukkit start public void addRecipe(IRecipe irecipe) { + org.spigotmc.AsyncCatcher.catchOp("Recipe Add"); // Spigot Object2ObjectLinkedOpenHashMap> map = this.recipes.get(irecipe.getType()); // CraftBukkit if (byName.containsKey(irecipe.getId()) || map.containsKey(irecipe.getId())) { 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 index 39a88e44fa..39b18e2f73 100644 --- a/src/main/java/net/minecraft/world/level/block/state/BlockBase.java +++ b/src/main/java/net/minecraft/world/level/block/state/BlockBase.java @@ -145,11 +145,14 @@ public abstract class BlockBase implements FeatureElement { /** @deprecated */ @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 + } /** @deprecated */ @Deprecated public void onRemove(IBlockData iblockdata, World world, BlockPosition blockposition, IBlockData iblockdata1, boolean flag) { + org.spigotmc.AsyncCatcher.catchOp("block remove"); // Spigot if (iblockdata.hasBlockEntity() && !iblockdata.is(iblockdata1.getBlock())) { world.removeBlockEntity(blockposition); } diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index a6401095e2..ad925597db 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -817,6 +817,7 @@ public final class CraftServer implements Server { public boolean dispatchCommand(CommandSender sender, String commandLine) { Validate.notNull(sender, "Sender cannot be null"); Validate.notNull(commandLine, "CommandLine cannot be null"); + org.spigotmc.AsyncCatcher.catchOp("command dispatch"); // Spigot 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 index ef20f970cb..6153d1ee9b 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -272,6 +272,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { @Override public boolean unloadChunkRequest(int x, int z) { + org.spigotmc.AsyncCatcher.catchOp("chunk unload"); // Spigot if (isChunkLoaded(x, z)) { world.getChunkSource().removeRegionTicket(TicketType.PLUGIN, new ChunkCoordIntPair(x, z), 1, Unit.INSTANCE); } @@ -280,6 +281,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { } private boolean unloadChunk0(int x, int z, boolean save) { + org.spigotmc.AsyncCatcher.catchOp("chunk unload"); // Spigot if (!isChunkLoaded(x, z)) { return true; } @@ -294,6 +296,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { @Override public boolean regenerateChunk(int x, int z) { + org.spigotmc.AsyncCatcher.catchOp("chunk regenerate"); // Spigot throw new UnsupportedOperationException("Not supported in this Minecraft version! Unless you can fix it, this is not a bug :)"); /* if (!unloadChunk0(x, z, false)) { @@ -346,6 +349,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { @Override public boolean loadChunk(int x, int z, boolean generate) { + org.spigotmc.AsyncCatcher.catchOp("chunk load"); // Spigot IChunkAccess chunk = world.getChunkSource().getChunk(x, z, generate ? ChunkStatus.FULL : ChunkStatus.EMPTY, true); // If generate = false, but the chunk already exists, we will get this back. @@ -834,6 +838,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { @Override public Collection getNearbyEntities(BoundingBox boundingBox, Predicate filter) { + org.spigotmc.AsyncCatcher.catchOp("getNearbyEntities"); // Spigot Validate.notNull(boundingBox, "Bounding box is null!"); AxisAlignedBB bb = new AxisAlignedBB(boundingBox.getMinX(), boundingBox.getMinY(), boundingBox.getMinZ(), boundingBox.getMaxX(), boundingBox.getMaxY(), boundingBox.getMaxZ()); @@ -988,6 +993,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { @Override public void save() { + org.spigotmc.AsyncCatcher.catchOp("world save"); // Spigot this.server.checkSaveState(); boolean oldSave = world.noSave; diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java index 49f6038f5a..e90064b5e7 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java @@ -563,6 +563,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity { @Override public List getNearbyEntities(double x, double y, double z) { Preconditions.checkState(!entity.generation, "Cannot get nearby entities during world generation"); + org.spigotmc.AsyncCatcher.catchOp("getNearbyEntities"); // Spigot List notchEntityList = entity.level.getEntities(entity, entity.getBoundingBox().inflate(x, y, z), Predicates.alwaysTrue()); List bukkitEntityList = new java.util.ArrayList(notchEntityList.size()); diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 18d0a726b0..11c5d2aadf 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -373,6 +373,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player { @Override public void kickPlayer(String message) { + org.spigotmc.AsyncCatcher.catchOp("player kick"); // Spigot if (getHandle().connection == null) return; 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 index 659c1b272a..329b2fbe07 100644 --- a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboardManager.java +++ b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboardManager.java @@ -41,6 +41,7 @@ public final class CraftScoreboardManager implements ScoreboardManager { @Override public CraftScoreboard getNewScoreboard() { + org.spigotmc.AsyncCatcher.catchOp("scoreboard creation"); // Spigot CraftScoreboard scoreboard = new CraftScoreboard(new ScoreboardServer(server)); scoreboards.add(scoreboard); return scoreboard; diff --git a/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java b/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java index 2e057fd4c0..ddef523ea8 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java +++ b/src/main/java/org/bukkit/craftbukkit/util/ServerShutdownThread.java @@ -12,6 +12,7 @@ public class ServerShutdownThread extends Thread { @Override public void run() { try { + org.spigotmc.AsyncCatcher.enabled = false; // Spigot 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 index 0000000000..aeed769725 --- /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) + { + if ( enabled && Thread.currentThread() != MinecraftServer.getServer().serverThread ) + { + throw new IllegalStateException( "Asynchronous " + reason + "!" ); + } + } +} -- 2.40.1