2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Joseph Hirschfeld <joe@ibj.io>
Date: Thu, 3 Mar 2016 03:15:41 -0600
Subject: [PATCH] Add exception reporting event
diff --git a/src/main/java/com/destroystokyo/paper/ServerSchedulerReportingWrapper.java b/src/main/java/com/destroystokyo/paper/ServerSchedulerReportingWrapper.java
new file mode 100644
index 0000000000000000000000000000000000000000..f699ce18ca044f813e194ef2786b7ea853ea86e7
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/ServerSchedulerReportingWrapper.java
@@ -0,0 +1,38 @@
+package com.destroystokyo.paper;
+
+import com.google.common.base.Preconditions;
+import org.bukkit.craftbukkit.scheduler.CraftTask;
+import com.destroystokyo.paper.event.server.ServerExceptionEvent;
+import com.destroystokyo.paper.exception.ServerSchedulerException;
+
+/**
+ * Reporting wrapper to catch exceptions not natively
+ */
+public class ServerSchedulerReportingWrapper implements Runnable {
+
+ private final CraftTask internalTask;
+
+ public ServerSchedulerReportingWrapper(CraftTask internalTask) {
+ this.internalTask = Preconditions.checkNotNull(internalTask, "internalTask");
+ }
+
+ @Override
+ public void run() {
+ try {
+ internalTask.run();
+ } catch (RuntimeException e) {
+ internalTask.getOwner().getServer().getPluginManager().callEvent(
+ new ServerExceptionEvent(new ServerSchedulerException(e, internalTask))
+ );
+ throw e;
+ } catch (Throwable t) {
+ internalTask.getOwner().getServer().getPluginManager().callEvent(
+ new ServerExceptionEvent(new ServerSchedulerException(t, internalTask))
+ ); //Do not rethrow, since it is not permitted with Runnable#run
+ }
+ }
+
+ public CraftTask getInternalTask() {
+ return internalTask;
+ }
+}
diff --git a/src/main/java/net/minecraft/server/players/OldUsersConverter.java b/src/main/java/net/minecraft/server/players/OldUsersConverter.java
2024-04-24 03:25:14 +02:00
index 68551947f5b7d3471f15bd74ccd86519ab34c1c1..a0b0614ac7d2009db5c6c10ab4a5f09dd447c635 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/players/OldUsersConverter.java
+++ b/src/main/java/net/minecraft/server/players/OldUsersConverter.java
2024-04-24 03:25:14 +02:00
@@ -356,7 +356,11 @@ public class OldUsersConverter {
try {
2023-12-05 20:54:55 +01:00
root = NbtIo.readCompressed(new java.io.FileInputStream(file5), NbtAccounter.unlimitedHeap());
2021-06-11 14:02:28 +02:00
} catch (Exception exception) {
2024-04-24 03:25:14 +02:00
- io.papermc.paper.util.TraceUtil.printStackTrace(exception); // Paper
+ // Paper start
+ io.papermc.paper.util.StacktraceDeobfuscator.INSTANCE.deobfuscateThrowable(exception);
+ exception.printStackTrace();
+ com.destroystokyo.paper.exception.ServerInternalException.reportInternalException(exception);
+ // Paper end
2021-06-11 14:02:28 +02:00
}
if (root != null) {
2024-04-24 03:25:14 +02:00
@@ -369,7 +373,11 @@ public class OldUsersConverter {
try {
2021-06-11 14:02:28 +02:00
NbtIo.writeCompressed(root, new java.io.FileOutputStream(file2));
} catch (Exception exception) {
2024-04-24 03:25:14 +02:00
- io.papermc.paper.util.TraceUtil.printStackTrace(exception); // Paper
+ // Paper start
+ io.papermc.paper.util.StacktraceDeobfuscator.INSTANCE.deobfuscateThrowable(exception);
+ exception.printStackTrace();
+ com.destroystokyo.paper.exception.ServerInternalException.reportInternalException(exception);
+ // Paper end
2021-06-11 14:02:28 +02:00
}
}
// CraftBukkit end
diff --git a/src/main/java/net/minecraft/world/entity/ai/village/VillageSiege.java b/src/main/java/net/minecraft/world/entity/ai/village/VillageSiege.java
2024-10-22 20:04:31 +02:00
index a5b18a04f482d05d3ca74918a580499b21c2fc3c..bd3f71c3eaa33258ff56062ea3a2099cef310b7a 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/ai/village/VillageSiege.java
+++ b/src/main/java/net/minecraft/world/entity/ai/village/VillageSiege.java
2024-04-24 03:25:14 +02:00
@@ -117,6 +117,7 @@ public class VillageSiege implements CustomSpawner {
2024-10-22 20:04:31 +02:00
entityzombie.finalizeSpawn(world, world.getCurrentDifficultyAt(entityzombie.blockPosition()), EntitySpawnReason.EVENT, (SpawnGroupData) null);
2021-06-11 14:02:28 +02:00
} catch (Exception exception) {
VillageSiege.LOGGER.warn("Failed to create zombie for village siege at {}", vec3d, exception);
2024-01-23 14:34:17 +01:00
+ com.destroystokyo.paper.exception.ServerInternalException.reportInternalException(exception); // Paper - ServerExceptionEvent
2021-06-11 14:02:28 +02:00
return;
}
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
2024-10-22 20:04:31 +02:00
index 5fe8726af73eb5334f47046c93096341e987269b..574d8d311001ce15c2a2391caaec0a0b0b1653b5 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
2024-10-22 20:04:31 +02:00
@@ -735,6 +735,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
2024-01-24 11:45:17 +01:00
// Paper start - Prevent block entity and entity crashes
2023-06-07 23:46:56 +02:00
final String msg = String.format("Entity threw exception at %s:%s,%s,%s", entity.level().getWorld().getName(), entity.getX(), entity.getY(), entity.getZ());
2021-06-21 10:09:18 +02:00
MinecraftServer.LOGGER.error(msg, throwable);
2024-02-01 10:15:57 +01:00
+ getCraftServer().getPluginManager().callEvent(new com.destroystokyo.paper.event.server.ServerExceptionEvent(new com.destroystokyo.paper.exception.ServerInternalException(msg, throwable))); // Paper - ServerExceptionEvent
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#10277)
Upstream has released updates that appear to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing
Bukkit Changes:
9a80d38c SPIGOT-336, SPIGOT-3366, SPIGOT-5768, SPIGOT-6409, SPIGOT-6861, PR-722: Add EntityRemoveEvent
258086d9 SPIGOT-7417, PR-967: Add Sign#getTargetSide and Sign#getAllowedEditor
ffaba051 SPIGOT-7584: Add missing Tag.ITEMS_NON_FLAMMABLE_WOOD
CraftBukkit Changes:
98b6c1ac7 SPIGOT-7589 Fix NullPointerException when bans expire
a2736ddb0 SPIGOT-336, SPIGOT-3366, SPIGOT-5768, SPIGOT-6409, SPIGOT-6861, PR-1008: Add EntityRemoveEvent
5bf12cb89 SPIGOT-7565: Throw a more descriptive error message when a developer tries to spawn an entity from a CraftBukkit class
76d95fe7e SPIGOT-7417, PR-1343: Add Sign#getTargetSide and Sign#getAllowedEditor
Spigot Changes:
e9ec5485 Rebuild patches
f1b62e0c Rebuild patches
2024-02-23 14:37:33 +01:00
entity.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DISCARD);
2024-01-24 11:45:17 +01:00
// Paper end - Prevent block entity and entity crashes
2021-06-12 02:57:04 +02:00
}
2021-06-11 14:02:28 +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-10-22 20:04:31 +02:00
index 1fe93e01c5e37397aded5d1f99214bf1bffe70b7..400166ad0199dd4b96684904ef4748cdb72381bb 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/NaturalSpawner.java
+++ b/src/main/java/net/minecraft/world/level/NaturalSpawner.java
2024-10-22 20:04:31 +02:00
@@ -296,6 +296,7 @@ public final class NaturalSpawner {
2022-12-07 19:32:25 +01:00
NaturalSpawner.LOGGER.warn("Can't spawn entity of type: {}", BuiltInRegistries.ENTITY_TYPE.getKey(type));
2021-06-11 14:02:28 +02:00
} catch (Exception exception) {
NaturalSpawner.LOGGER.warn("Failed to create mob", exception);
2024-01-23 14:34:17 +01:00
+ com.destroystokyo.paper.exception.ServerInternalException.reportInternalException(exception); // Paper - ServerExceptionEvent
2021-06-11 14:02:28 +02:00
}
2022-12-07 19:32:25 +01:00
return null;
2024-10-22 20:04:31 +02:00
@@ -384,6 +385,7 @@ public final class NaturalSpawner {
entity = biomesettingsmobs_c.type.create(world.getLevel(), EntitySpawnReason.NATURAL);
2021-06-12 02:57:04 +02:00
} catch (Exception exception) {
NaturalSpawner.LOGGER.warn("Failed to create mob", exception);
2024-01-23 14:34:17 +01:00
+ com.destroystokyo.paper.exception.ServerInternalException.reportInternalException(exception); // Paper - ServerExceptionEvent
2021-06-12 02:57:04 +02:00
continue;
}
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
2024-10-22 20:04:31 +02:00
index e1cd7497b1355030bf44b53aa30400604dff9aca..401f18deb9e9f019ea17ad684f2d5c4ea5e7ef97 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
+++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
2024-10-22 20:04:31 +02:00
@@ -496,8 +496,13 @@ public class LevelChunk extends ChunkAccess {
2024-06-13 19:30:39 +02:00
BlockState iblockdata = this.getBlockState(blockposition);
2021-06-11 14:02:28 +02:00
2024-06-13 19:30:39 +02:00
if (!iblockdata.hasBlockEntity()) {
- LevelChunk.LOGGER.warn("Trying to set block entity {} at position {}, but state {} does not allow it", new Object[]{blockEntity, blockposition, iblockdata});
- new Exception().printStackTrace(); // CraftBukkit
2024-01-23 14:34:17 +01:00
+ // Paper start - ServerExceptionEvent
2024-06-13 19:30:39 +02:00
+ com.destroystokyo.paper.exception.ServerInternalException e = new com.destroystokyo.paper.exception.ServerInternalException(
+ "Trying to set block entity %s at position %s, but state %s does not allow it".formatted(blockEntity, blockposition, iblockdata)
+ );
2021-06-11 14:02:28 +02:00
+ e.printStackTrace();
2024-06-13 19:30:39 +02:00
+ com.destroystokyo.paper.exception.ServerInternalException.reportInternalException(e);
2024-01-23 14:34:17 +01:00
+ // Paper end - ServerExceptionEvent
2024-06-13 19:30:39 +02:00
} else {
BlockState iblockdata1 = blockEntity.getBlockState();
2024-10-22 20:04:31 +02:00
@@ -989,6 +994,7 @@ public class LevelChunk extends ChunkAccess {
2024-01-24 11:45:17 +01:00
// Paper start - Prevent block entity and entity crashes
2021-06-21 10:09:18 +02:00
final String msg = String.format("BlockEntity threw exception at %s:%s,%s,%s", LevelChunk.this.getLevel().getWorld().getName(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ());
net.minecraft.server.MinecraftServer.LOGGER.error(msg, throwable);
2024-06-14 14:11:52 +02:00
+ net.minecraft.world.level.chunk.LevelChunk.this.level.getCraftServer().getPluginManager().callEvent(new com.destroystokyo.paper.event.server.ServerExceptionEvent(new com.destroystokyo.paper.exception.ServerInternalException(msg, throwable))); // Paper - ServerExceptionEvent
2021-06-12 02:57:04 +02:00
LevelChunk.this.removeBlockEntity(this.getPos());
2024-01-24 11:45:17 +01:00
// Paper end - Prevent block entity and entity crashes
2021-06-12 02:57:04 +02:00
// Spigot start
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
2024-04-24 03:25:14 +02:00
index 15f273aa592828719de6e092d79a407dc8652dfe..b24e8255ab18eb5b2e4968aa62aa3d72ef33f0eb 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
+++ b/src/main/java/net/minecraft/world/level/chunk/storage/RegionFile.java
2024-04-24 03:25:14 +02:00
@@ -296,6 +296,7 @@ public class RegionFile implements AutoCloseable {
2021-06-11 14:02:28 +02:00
return true;
}
} catch (IOException ioexception) {
2024-01-23 14:34:17 +01:00
+ com.destroystokyo.paper.exception.ServerInternalException.reportInternalException(ioexception); // Paper - ServerExceptionEvent
2021-06-11 14:02:28 +02:00
return false;
}
}
2024-04-24 03:25:14 +02:00
@@ -377,6 +378,7 @@ public class RegionFile implements AutoCloseable {
2022-11-12 21:57:41 +01:00
((java.nio.Buffer) buf).position(5); // CraftBukkit - decompile error
filechannel.write(buf);
2021-06-12 02:57:04 +02:00
} catch (Throwable throwable) {
2024-01-23 14:34:17 +01:00
+ com.destroystokyo.paper.exception.ServerInternalException.reportInternalException(throwable); // Paper - ServerExceptionEvent
2021-06-11 14:02:28 +02:00
if (filechannel != null) {
2021-06-12 02:57:04 +02:00
try {
filechannel.close();
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java
2024-10-22 20:04:31 +02:00
index 4c376f67ae311b4fedea27b3475f9fb56054aec2..22ddc74d85efb4e80e6f06acdf93341a122804fc 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java
+++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java
2024-07-18 16:50:16 +02:00
@@ -416,20 +416,25 @@ public class CraftScheduler implements BukkitScheduler {
try {
task.run();
} catch (final Throwable throwable) {
+ // Paper start
+ final String logMessage = String.format(
+ "Task #%s for %s generated an exception",
+ task.getTaskId(),
+ task.getOwner().getDescription().getFullName());
task.getOwner().getLogger().log(
Level.WARNING,
- String.format(
- "Task #%s for %s generated an exception",
- task.getTaskId(),
- task.getOwner().getDescription().getFullName()),
+ logMessage,
2021-06-11 14:02:28 +02:00
throwable);
+ org.bukkit.Bukkit.getServer().getPluginManager().callEvent(
2024-07-18 16:50:16 +02:00
+ new com.destroystokyo.paper.event.server.ServerExceptionEvent(new com.destroystokyo.paper.exception.ServerSchedulerException(logMessage, throwable, task)));
+ // Paper end
2021-06-11 14:02:28 +02:00
} finally {
2021-06-12 02:57:04 +02:00
this.currentTask = null;
2024-07-18 16:50:16 +02:00
}
2021-06-12 02:57:04 +02:00
this.parsePending();
2021-06-11 14:02:28 +02:00
} else {
2024-10-22 20:04:31 +02:00
this.debugTail = this.debugTail.setNext(new CraftAsyncDebugger(this.currentTick + CraftScheduler.RECENT_TICKS, task.getOwner(), task.getTaskClass()));
2021-06-12 02:57:04 +02:00
- this.executor.execute(task);
2023-06-13 01:51:45 +02:00
+ this.executor.execute(new com.destroystokyo.paper.ServerSchedulerReportingWrapper(task)); // Paper
2021-06-11 14:02:28 +02:00
// We don't need to parse pending
// (async tasks must live with race-conditions if they attempt to cancel between these few lines of code)
}