diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java index 698b1c15c..e9dbfc916 100644 --- a/src/main/java/net/minestom/server/MinecraftServer.java +++ b/src/main/java/net/minestom/server/MinecraftServer.java @@ -9,6 +9,7 @@ import net.minestom.server.data.SerializableData; import net.minestom.server.entity.EntityType; import net.minestom.server.entity.Player; import net.minestom.server.event.GlobalEventHandler; +import net.minestom.server.exception.ExceptionManager; import net.minestom.server.extensions.Extension; import net.minestom.server.extensions.ExtensionManager; import net.minestom.server.fluids.Fluid; @@ -99,6 +100,8 @@ public final class MinecraftServer { private static int nettyThreadCount = Runtime.getRuntime().availableProcessors(); private static boolean processNettyErrors = false; + private static ExceptionManager exceptionManager; + // In-Game Manager private static ConnectionManager connectionManager; private static InstanceManager instanceManager; @@ -140,6 +143,10 @@ public final class MinecraftServer { public static MinecraftServer init() { if (minecraftServer != null) // don't init twice return minecraftServer; + + // Initialize the ExceptionManager at first + exceptionManager = new ExceptionManager(); + extensionManager = new ExtensionManager(); // warmup/force-init registries @@ -401,6 +408,16 @@ public final class MinecraftServer { return benchmarkManager; } + /** + * Gets the exception manager for exception handling. + * + * @return the exception manager + */ + public static ExceptionManager getExceptionManager() { + checkInitStatus(exceptionManager); + return exceptionManager; + } + /** * Gets the manager handling server connections. * diff --git a/src/main/java/net/minestom/server/UpdateManager.java b/src/main/java/net/minestom/server/UpdateManager.java index 3cd810856..db7ae1947 100644 --- a/src/main/java/net/minestom/server/UpdateManager.java +++ b/src/main/java/net/minestom/server/UpdateManager.java @@ -80,7 +80,7 @@ public final class UpdateManager { doTickCallback(tickEndCallbacks, tickTime / 1000000L); } catch (Exception e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } }, 0, MinecraftServer.TICK_MS, TimeUnit.MILLISECONDS); } @@ -103,7 +103,7 @@ public final class UpdateManager { try { future.get(); } catch (Throwable e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } } diff --git a/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java b/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java index 12b5a78f2..715cd7cdf 100644 --- a/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java +++ b/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java @@ -67,7 +67,7 @@ public final class BenchmarkManager { try { Thread.sleep(time); } catch (InterruptedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index e42bbd0d9..d6bf08e13 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -2,6 +2,7 @@ package net.minestom.server.command; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntList; +import net.minestom.server.MinecraftServer; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandDispatcher; import net.minestom.server.command.builder.CommandSyntax; @@ -63,7 +64,7 @@ public final class CommandManager { execute(consoleSender, command); } } catch (IOException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); continue; } @@ -71,14 +72,14 @@ public final class CommandManager { try { Thread.sleep(200); } catch (InterruptedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } try { bi.close(); } catch (IOException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } }, "ConsoleCommand-Thread"); consoleThread.setDaemon(true); diff --git a/src/main/java/net/minestom/server/data/Data.java b/src/main/java/net/minestom/server/data/Data.java index 3b8309f5f..55237cc77 100644 --- a/src/main/java/net/minestom/server/data/Data.java +++ b/src/main/java/net/minestom/server/data/Data.java @@ -1,5 +1,6 @@ package net.minestom.server.data; +import net.minestom.server.MinecraftServer; import net.minestom.server.utils.clone.PublicCloneable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -126,7 +127,7 @@ public abstract class Data implements PublicCloneable { try { return (Data) super.clone(); } catch (CloneNotSupportedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); throw new IllegalStateException("Weird thing happened"); } } diff --git a/src/main/java/net/minestom/server/data/SerializableDataImpl.java b/src/main/java/net/minestom/server/data/SerializableDataImpl.java index 0c04ba47f..256283764 100644 --- a/src/main/java/net/minestom/server/data/SerializableDataImpl.java +++ b/src/main/java/net/minestom/server/data/SerializableDataImpl.java @@ -183,7 +183,7 @@ public class SerializableDataImpl extends SerializableData { try { return Class.forName(className); } catch (ClassNotFoundException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } }); diff --git a/src/main/java/net/minestom/server/exception/ExceptionHandler.java b/src/main/java/net/minestom/server/exception/ExceptionHandler.java new file mode 100644 index 000000000..ce22931ec --- /dev/null +++ b/src/main/java/net/minestom/server/exception/ExceptionHandler.java @@ -0,0 +1,17 @@ +package net.minestom.server.exception; + +/** + * Used when you want to implement your own exception handling, instead of just printing the stack trace. + *

+ * Sets with {@link ExceptionManager#setExceptionHandler(ExceptionHandler)}. + */ +@FunctionalInterface +public interface ExceptionHandler { + + /** + * Called when a exception was caught. + * + * @param e the thrown exception + */ + void handleException(Throwable e); +} diff --git a/src/main/java/net/minestom/server/exception/ExceptionManager.java b/src/main/java/net/minestom/server/exception/ExceptionManager.java new file mode 100644 index 000000000..e8964bf34 --- /dev/null +++ b/src/main/java/net/minestom/server/exception/ExceptionManager.java @@ -0,0 +1,38 @@ +package net.minestom.server.exception; + +import org.jetbrains.annotations.Nullable; + +/** + * Manages the handling of exceptions. + */ +public final class ExceptionManager { + + private ExceptionHandler exceptionHandler; + + /** + * Handles an exception, if no {@link ExceptionHandler} is set, it just prints the stack trace. + * + * @param e the occurred exception + */ + public void handleException(Throwable e) { + this.getExceptionHandler().handleException(e); + } + + /** + * Changes the exception handler, to allow custom exception handling. + * + * @param exceptionHandler the new {@link ExceptionHandler}, can be set to null to apply the default provider + */ + public void setExceptionHandler(@Nullable ExceptionHandler exceptionHandler) { + this.exceptionHandler = exceptionHandler; + } + + /** + * Retrieves the current {@link ExceptionHandler}, can be the default one if none is defined. + * + * @return the current {@link ExceptionHandler} + */ + public ExceptionHandler getExceptionHandler() { + return this.exceptionHandler == null ? exceptionHandler = Throwable::printStackTrace : this.exceptionHandler; + } +} diff --git a/src/main/java/net/minestom/server/extensions/ExtensionManager.java b/src/main/java/net/minestom/server/extensions/ExtensionManager.java index f80f6072f..e7f81be86 100644 --- a/src/main/java/net/minestom/server/extensions/ExtensionManager.java +++ b/src/main/java/net/minestom/server/extensions/ExtensionManager.java @@ -3,6 +3,7 @@ package net.minestom.server.extensions; import com.google.gson.Gson; import net.minestom.dependencies.DependencyGetter; import net.minestom.dependencies.maven.MavenRepository; +import net.minestom.server.MinecraftServer; import net.minestom.server.extras.selfmodification.MinestomExtensionClassLoader; import net.minestom.server.extras.selfmodification.MinestomRootClassLoader; import net.minestom.server.ping.ResponseDataConsumer; @@ -98,7 +99,7 @@ public class ExtensionManager { setupClassLoader(discoveredExtension); } catch (Exception e) { discoveredExtension.loadStatus = DiscoveredExtension.LoadStatus.FAILED_TO_SETUP_CLASSLOADER; - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); LOGGER.error("Failed to load extension {}", discoveredExtension.getName()); LOGGER.error("Failed to load extension", e); } @@ -113,9 +114,8 @@ public class ExtensionManager { attemptSingleLoad(discoveredExtension); } catch (Exception e) { discoveredExtension.loadStatus = DiscoveredExtension.LoadStatus.LOAD_FAILED; - e.printStackTrace(); LOGGER.error("Failed to load extension {}", discoveredExtension.getName()); - LOGGER.error("Failed to load extension", e); + MinecraftServer.getExceptionManager().handleException(e); } } } @@ -210,7 +210,7 @@ public class ExtensionManager { loggerField.set(extension, LoggerFactory.getLogger(extensionClass)); } catch (IllegalAccessException e) { // We made it accessible, should not occur - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } catch (NoSuchFieldException e) { // This should also not occur (unless someone changed the logger in Extension superclass). LOGGER.error("Main class '{}' in '{}' has no logger field.", mainClass, extensionName, e); @@ -265,7 +265,7 @@ public class ExtensionManager { extensions.add(extension); } } catch (IOException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } return extensions; @@ -284,7 +284,7 @@ public class ExtensionManager { return extension; } catch (IOException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } } @@ -497,7 +497,7 @@ public class ExtensionManager { LOGGER.info("Found mixin in extension {}: {}", extension.getName(), mixinConfigFile); } } catch (Exception e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); LOGGER.error("Failed to load code modifier for extension in files: " + extension.files .stream() @@ -531,7 +531,7 @@ public class ExtensionManager { // close resources classloader.close(); } catch (IOException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } MinestomRootClassLoader.getInstance().removeChildInHierarchy(classloader); } diff --git a/src/main/java/net/minestom/server/extras/mojangAuth/MojangCrypt.java b/src/main/java/net/minestom/server/extras/mojangAuth/MojangCrypt.java index c0f08a2f3..c040b55d9 100644 --- a/src/main/java/net/minestom/server/extras/mojangAuth/MojangCrypt.java +++ b/src/main/java/net/minestom/server/extras/mojangAuth/MojangCrypt.java @@ -1,5 +1,6 @@ package net.minestom.server.extras.mojangAuth; +import net.minestom.server.MinecraftServer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; @@ -20,7 +21,7 @@ public final class MojangCrypt { keyGen.initialize(1024); return keyGen.generateKeyPair(); } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); LOGGER.error("Key pair generation failed!"); return null; } @@ -31,7 +32,7 @@ public final class MojangCrypt { try { return digestData("SHA-1", data.getBytes("ISO_8859_1"), secretKey.getEncoded(), publicKey.getEncoded()); } catch (UnsupportedEncodingException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } } @@ -47,7 +48,7 @@ public final class MojangCrypt { return digest.digest(); } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } } @@ -64,7 +65,7 @@ public final class MojangCrypt { try { return setupCipher(mode, key.getAlgorithm(), key).doFinal(data); } catch (IllegalBlockSizeException | BadPaddingException var4) { - var4.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(var4); } LOGGER.error("Cipher data failed!"); @@ -77,7 +78,7 @@ public final class MojangCrypt { cipher4.init(mode, key); return cipher4; } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException var4) { - var4.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(var4); } LOGGER.error("Cipher creation failed!"); diff --git a/src/main/java/net/minestom/server/extras/selfmodification/MinestomRootClassLoader.java b/src/main/java/net/minestom/server/extras/selfmodification/MinestomRootClassLoader.java index e0ab08857..bcb9d4232 100644 --- a/src/main/java/net/minestom/server/extras/selfmodification/MinestomRootClassLoader.java +++ b/src/main/java/net/minestom/server/extras/selfmodification/MinestomRootClassLoader.java @@ -1,5 +1,6 @@ package net.minestom.server.extras.selfmodification; +import net.minestom.server.MinecraftServer; import org.jetbrains.annotations.NotNull; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassWriter; @@ -260,7 +261,7 @@ public class MinestomRootClassLoader extends HierarchyClassLoader { } } } catch (MalformedURLException | ClassNotFoundException | InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } diff --git a/src/main/java/net/minestom/server/gamedata/loottables/LootTableManager.java b/src/main/java/net/minestom/server/gamedata/loottables/LootTableManager.java index f130474fa..c96d10384 100644 --- a/src/main/java/net/minestom/server/gamedata/loottables/LootTableManager.java +++ b/src/main/java/net/minestom/server/gamedata/loottables/LootTableManager.java @@ -3,6 +3,7 @@ package net.minestom.server.gamedata.loottables; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializer; +import net.minestom.server.MinecraftServer; import net.minestom.server.gamedata.Condition; import net.minestom.server.registry.ResourceGatherer; import net.minestom.server.utils.NamespaceID; @@ -84,7 +85,7 @@ public final class LootTableManager { try (reader) { return cache.computeIfAbsent(name, _name -> create(reader)); } catch (IOException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } } diff --git a/src/main/java/net/minestom/server/gamedata/loottables/entries/TagType.java b/src/main/java/net/minestom/server/gamedata/loottables/entries/TagType.java index 73996ac54..6aad1f812 100644 --- a/src/main/java/net/minestom/server/gamedata/loottables/entries/TagType.java +++ b/src/main/java/net/minestom/server/gamedata/loottables/entries/TagType.java @@ -20,7 +20,7 @@ public class TagType implements LootTableEntryType { try { return new TagEntry(this, MinecraftServer.getTagManager().load(NamespaceID.from(name), "items"), expand, weight, quality, conditions); } catch (FileNotFoundException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } } diff --git a/src/main/java/net/minestom/server/gamedata/tags/TagManager.java b/src/main/java/net/minestom/server/gamedata/tags/TagManager.java index 2059458f0..d028d7c2e 100644 --- a/src/main/java/net/minestom/server/gamedata/tags/TagManager.java +++ b/src/main/java/net/minestom/server/gamedata/tags/TagManager.java @@ -2,6 +2,7 @@ package net.minestom.server.gamedata.tags; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import net.minestom.server.MinecraftServer; import net.minestom.server.network.packet.server.play.TagsPacket; import net.minestom.server.registry.ResourceGatherer; import net.minestom.server.utils.NamespaceID; @@ -288,7 +289,7 @@ public class TagManager { try { return load(name, type); } catch (FileNotFoundException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return Tag.EMPTY; } } diff --git a/src/main/java/net/minestom/server/instance/IChunkLoader.java b/src/main/java/net/minestom/server/instance/IChunkLoader.java index a11fb2546..d8b004b14 100644 --- a/src/main/java/net/minestom/server/instance/IChunkLoader.java +++ b/src/main/java/net/minestom/server/instance/IChunkLoader.java @@ -59,7 +59,7 @@ public interface IChunkLoader { parallelSavingThreadPool.awaitTermination(1L, java.util.concurrent.TimeUnit.DAYS); OptionalCallback.execute(callback); } catch (InterruptedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } else { AtomicInteger counter = new AtomicInteger(); diff --git a/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java b/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java index 13232f64b..5075f2a65 100644 --- a/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java +++ b/src/main/java/net/minestom/server/instance/palette/PaletteStorage.java @@ -2,6 +2,7 @@ package net.minestom.server.instance.palette; import it.unimi.dsi.fastutil.shorts.Short2ShortLinkedOpenHashMap; import it.unimi.dsi.fastutil.shorts.Short2ShortOpenHashMap; +import net.minestom.server.MinecraftServer; import net.minestom.server.instance.Chunk; import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.chunk.ChunkUtils; @@ -171,7 +172,7 @@ public class PaletteStorage implements PublicCloneable { paletteStorage.blockPaletteMaps = blockPaletteMaps.clone(); return paletteStorage; } catch (CloneNotSupportedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); throw new IllegalStateException("Weird thing happened"); } } diff --git a/src/main/java/net/minestom/server/item/ItemStack.java b/src/main/java/net/minestom/server/item/ItemStack.java index b11e94e4e..0e176eedf 100644 --- a/src/main/java/net/minestom/server/item/ItemStack.java +++ b/src/main/java/net/minestom/server/item/ItemStack.java @@ -2,6 +2,7 @@ package net.minestom.server.item; import it.unimi.dsi.fastutil.objects.Object2ShortMap; import it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap; +import net.minestom.server.MinecraftServer; import net.minestom.server.chat.JsonMessage; import net.minestom.server.data.Data; import net.minestom.server.data.DataContainer; @@ -596,7 +597,7 @@ public class ItemStack implements DataContainer, PublicCloneable { return itemStack; } catch (CloneNotSupportedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } } @@ -787,4 +788,4 @@ public class ItemStack implements DataContainer, PublicCloneable { public void onInventoryClick(@NotNull Player player, @NotNull ClickType clickType, int slot, boolean playerInventory) { } -} \ No newline at end of file +} diff --git a/src/main/java/net/minestom/server/item/metadata/ItemMeta.java b/src/main/java/net/minestom/server/item/metadata/ItemMeta.java index d68a6d3e1..4e200e331 100644 --- a/src/main/java/net/minestom/server/item/metadata/ItemMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/ItemMeta.java @@ -1,5 +1,6 @@ package net.minestom.server.item.metadata; +import net.minestom.server.MinecraftServer; import net.minestom.server.item.ItemStack; import net.minestom.server.utils.clone.PublicCloneable; import org.jetbrains.annotations.NotNull; @@ -53,7 +54,7 @@ public abstract class ItemMeta implements PublicCloneable { try { return (ItemMeta) super.clone(); } catch (CloneNotSupportedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); throw new IllegalStateException("Weird thing happened"); } } diff --git a/src/main/java/net/minestom/server/item/metadata/MapMeta.java b/src/main/java/net/minestom/server/item/metadata/MapMeta.java index e16aef362..991c21716 100644 --- a/src/main/java/net/minestom/server/item/metadata/MapMeta.java +++ b/src/main/java/net/minestom/server/item/metadata/MapMeta.java @@ -1,5 +1,6 @@ package net.minestom.server.item.metadata; +import net.minestom.server.MinecraftServer; import net.minestom.server.chat.ChatColor; import net.minestom.server.utils.clone.CloneUtils; import net.minestom.server.utils.clone.PublicCloneable; @@ -271,7 +272,7 @@ public class MapMeta extends ItemMeta { try { return (MapDecoration) super.clone(); } catch (CloneNotSupportedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); throw new IllegalStateException("Something weird happened"); } } diff --git a/src/main/java/net/minestom/server/map/MapColors.java b/src/main/java/net/minestom/server/map/MapColors.java index 4871bde02..9dc58107b 100644 --- a/src/main/java/net/minestom/server/map/MapColors.java +++ b/src/main/java/net/minestom/server/map/MapColors.java @@ -1,5 +1,6 @@ package net.minestom.server.map; +import net.minestom.server.MinecraftServer; import net.minestom.server.utils.thread.MinestomThread; import java.util.concurrent.ConcurrentHashMap; @@ -104,7 +105,7 @@ public enum MapColors { reduction = Integer.parseInt(reductionStr); } catch (NumberFormatException e) { System.err.println("Invalid integer in reduction argument: " + reductionStr); - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } if (reduction < 0 || reduction >= 255) { @@ -195,7 +196,7 @@ public enum MapColors { threads.shutdown(); threads.awaitTermination(100, TimeUnit.MINUTES); } catch (Throwable t) { - t.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(t); } } diff --git a/src/main/java/net/minestom/server/network/PacketProcessor.java b/src/main/java/net/minestom/server/network/PacketProcessor.java index db31fe08d..71935f876 100644 --- a/src/main/java/net/minestom/server/network/PacketProcessor.java +++ b/src/main/java/net/minestom/server/network/PacketProcessor.java @@ -176,7 +176,7 @@ public final class PacketProcessor { LOGGER.warn("Connection {} ({}) sent an unexpected packet.", connection.getRemoteAddress(), username); - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } } diff --git a/src/main/java/net/minestom/server/network/netty/NettyServer.java b/src/main/java/net/minestom/server/network/netty/NettyServer.java index 05a5e9a31..a84419434 100644 --- a/src/main/java/net/minestom/server/network/netty/NettyServer.java +++ b/src/main/java/net/minestom/server/network/netty/NettyServer.java @@ -219,7 +219,7 @@ public final class NettyServer { this.serverChannel = (ServerSocketChannel) cf.channel(); } catch (InterruptedException ex) { - ex.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(ex); } } @@ -263,7 +263,7 @@ public final class NettyServer { this.worker.shutdownGracefully(); this.boss.shutdownGracefully(); } catch (InterruptedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } this.trafficScheduler.shutdown(); diff --git a/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java b/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java index 4e0e4c14b..519c34a95 100644 --- a/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java +++ b/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java @@ -34,7 +34,7 @@ public class ClientChannel extends SimpleChannelInboundHandler { try { packetProcessor.process(ctx, packet); } catch (Exception e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } finally { // Check remaining final ByteBuf body = packet.getBody(); @@ -74,7 +74,7 @@ public class ClientChannel extends SimpleChannelInboundHandler { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { if (MinecraftServer.shouldProcessNettyErrors()) { LOGGER.info(cause.getMessage()); - cause.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(cause); } ctx.close(); } diff --git a/src/main/java/net/minestom/server/network/packet/client/login/EncryptionResponsePacket.java b/src/main/java/net/minestom/server/network/packet/client/login/EncryptionResponsePacket.java index 17d0dfc40..0b6917e91 100644 --- a/src/main/java/net/minestom/server/network/packet/client/login/EncryptionResponsePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/login/EncryptionResponsePacket.java @@ -57,7 +57,7 @@ public class EncryptionResponsePacket implements ClientPreplayPacket { CONNECTION_MANAGER.startPlayState(connection, gameProfile.getId(), gameProfile.getName(), true); } } catch (AuthenticationUnavailableException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } }); } diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java index 0021ae8eb..ef78b7af7 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -150,7 +150,7 @@ public class NettyPlayerConnection extends PlayerConnection { if (MinecraftServer.shouldProcessNettyErrors()) { return channelFuture.addListener(future -> { if (!future.isSuccess()) { - future.cause().printStackTrace(); + MinecraftServer.getExceptionManager().handleException(future.cause()); } }); } else { @@ -165,7 +165,7 @@ public class NettyPlayerConnection extends PlayerConnection { if (MinecraftServer.shouldProcessNettyErrors()) { return channelFuture.addListener(future -> { if (!future.isSuccess()) { - future.cause().printStackTrace(); + MinecraftServer.getExceptionManager().handleException(future.cause()); } }); } else { diff --git a/src/main/java/net/minestom/server/potion/CustomPotionEffect.java b/src/main/java/net/minestom/server/potion/CustomPotionEffect.java index d84daa1f1..6faa1ab91 100644 --- a/src/main/java/net/minestom/server/potion/CustomPotionEffect.java +++ b/src/main/java/net/minestom/server/potion/CustomPotionEffect.java @@ -1,5 +1,6 @@ package net.minestom.server.potion; +import net.minestom.server.MinecraftServer; import net.minestom.server.utils.clone.PublicCloneable; import org.jetbrains.annotations.NotNull; @@ -72,7 +73,7 @@ public class CustomPotionEffect implements PublicCloneable { try { return (CustomPotionEffect) super.clone(); } catch (CloneNotSupportedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); throw new IllegalStateException("Weird thing happened"); } } diff --git a/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java b/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java index 96191f43f..98b226d46 100644 --- a/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java +++ b/src/main/java/net/minestom/server/storage/systems/FileStorageSystem.java @@ -1,5 +1,6 @@ package net.minestom.server.storage.systems; +import net.minestom.server.MinecraftServer; import net.minestom.server.storage.StorageOptions; import net.minestom.server.storage.StorageSystem; import org.jetbrains.annotations.NotNull; @@ -41,7 +42,7 @@ public class FileStorageSystem implements StorageSystem { try { this.rocksDB = RocksDB.open(options, location); } catch (RocksDBException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } @@ -50,7 +51,7 @@ public class FileStorageSystem implements StorageSystem { try { return rocksDB.get(getKey(key)); } catch (RocksDBException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } } @@ -60,7 +61,7 @@ public class FileStorageSystem implements StorageSystem { try { this.rocksDB.put(getKey(key), data); } catch (RocksDBException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } @@ -69,7 +70,7 @@ public class FileStorageSystem implements StorageSystem { try { this.rocksDB.delete(getKey(key)); } catch (RocksDBException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } @@ -78,7 +79,7 @@ public class FileStorageSystem implements StorageSystem { try { this.rocksDB.closeE(); } catch (RocksDBException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } diff --git a/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java b/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java index f54c73083..22178cbef 100644 --- a/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java +++ b/src/main/java/net/minestom/server/thread/PerGroupChunkProvider.java @@ -4,6 +4,7 @@ import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.LongArraySet; import it.unimi.dsi.fastutil.longs.LongSet; +import net.minestom.server.MinecraftServer; import net.minestom.server.instance.Instance; import net.minestom.server.utils.chunk.ChunkUtils; import org.jetbrains.annotations.NotNull; @@ -150,7 +151,7 @@ public class PerGroupChunkProvider extends ThreadProvider { try { countDownLatch.await(); } catch (InterruptedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } // Tick all this chunk group diff --git a/src/main/java/net/minestom/server/timer/SchedulerManager.java b/src/main/java/net/minestom/server/timer/SchedulerManager.java index d389178ba..40d3f6f00 100644 --- a/src/main/java/net/minestom/server/timer/SchedulerManager.java +++ b/src/main/java/net/minestom/server/timer/SchedulerManager.java @@ -106,7 +106,7 @@ public final class SchedulerManager { try { batchesPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } diff --git a/src/main/java/net/minestom/server/timer/Task.java b/src/main/java/net/minestom/server/timer/Task.java index eccc8cc71..b92acd984 100644 --- a/src/main/java/net/minestom/server/timer/Task.java +++ b/src/main/java/net/minestom/server/timer/Task.java @@ -1,6 +1,7 @@ package net.minestom.server.timer; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import net.minestom.server.MinecraftServer; import org.jetbrains.annotations.NotNull; import java.util.Objects; @@ -65,7 +66,7 @@ public class Task implements Runnable { this.id, e.getMessage() ); - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } finally { if (this.repeat == 0) this.finish(); this.currentThreadTask = null; diff --git a/src/main/java/net/minestom/server/utils/BlockPosition.java b/src/main/java/net/minestom/server/utils/BlockPosition.java index 5f77bdb73..e04770e0c 100644 --- a/src/main/java/net/minestom/server/utils/BlockPosition.java +++ b/src/main/java/net/minestom/server/utils/BlockPosition.java @@ -1,5 +1,6 @@ package net.minestom.server.utils; +import net.minestom.server.MinecraftServer; import net.minestom.server.instance.block.BlockFace; import net.minestom.server.utils.clone.PublicCloneable; import org.jetbrains.annotations.NotNull; @@ -229,7 +230,7 @@ public class BlockPosition implements PublicCloneable { try { return (BlockPosition) super.clone(); } catch (CloneNotSupportedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } } diff --git a/src/main/java/net/minestom/server/utils/NBTUtils.java b/src/main/java/net/minestom/server/utils/NBTUtils.java index 8aae3569c..f1e91d941 100644 --- a/src/main/java/net/minestom/server/utils/NBTUtils.java +++ b/src/main/java/net/minestom/server/utils/NBTUtils.java @@ -118,7 +118,7 @@ public final class NBTUtils { loadDataIntoItem(item, nbt); } } catch (IOException | NBTException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } return item; diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index b274d5e78..b22e0eb0f 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -165,7 +165,7 @@ public final class PacketUtils { try { packet.write(writer); } catch (Exception e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } return writer.getBuffer(); diff --git a/src/main/java/net/minestom/server/utils/Position.java b/src/main/java/net/minestom/server/utils/Position.java index 3a79a167b..6d3666bb1 100644 --- a/src/main/java/net/minestom/server/utils/Position.java +++ b/src/main/java/net/minestom/server/utils/Position.java @@ -1,5 +1,6 @@ package net.minestom.server.utils; +import net.minestom.server.MinecraftServer; import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.clone.PublicCloneable; import org.jetbrains.annotations.NotNull; @@ -203,7 +204,7 @@ public class Position implements PublicCloneable { try { return (Position) super.clone(); } catch (CloneNotSupportedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } } diff --git a/src/main/java/net/minestom/server/utils/Vector.java b/src/main/java/net/minestom/server/utils/Vector.java index e00f587e6..dc7b87f6d 100644 --- a/src/main/java/net/minestom/server/utils/Vector.java +++ b/src/main/java/net/minestom/server/utils/Vector.java @@ -1,5 +1,6 @@ package net.minestom.server.utils; +import net.minestom.server.MinecraftServer; import net.minestom.server.utils.clone.PublicCloneable; import org.jetbrains.annotations.NotNull; @@ -272,7 +273,7 @@ public class Vector implements PublicCloneable { try { return (Vector) super.clone(); } catch (CloneNotSupportedException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); throw new IllegalStateException("Weird thing happened"); } } diff --git a/src/main/java/net/minestom/server/utils/async/AsyncUtils.java b/src/main/java/net/minestom/server/utils/async/AsyncUtils.java index c04d410c6..d280d467f 100644 --- a/src/main/java/net/minestom/server/utils/async/AsyncUtils.java +++ b/src/main/java/net/minestom/server/utils/async/AsyncUtils.java @@ -1,5 +1,6 @@ package net.minestom.server.utils.async; +import net.minestom.server.MinecraftServer; import org.jetbrains.annotations.NotNull; import java.util.concurrent.CompletableFuture; @@ -11,7 +12,7 @@ public final class AsyncUtils { try { runnable.run(); } catch (Exception e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } }); } diff --git a/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java b/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java index b5f79f561..b7f1c430f 100644 --- a/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java +++ b/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java @@ -2,6 +2,7 @@ package net.minestom.server.utils.binary; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import net.minestom.server.MinecraftServer; import net.minestom.server.item.ItemStack; import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.NBTUtils; @@ -248,7 +249,7 @@ public class BinaryWriter extends OutputStream { nbtWriter.writeNamed(name, tag); } catch (IOException e) { // should not throw, as nbtWriter points to this PacketWriter - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); } } diff --git a/src/main/java/net/minestom/server/utils/mojang/MojangUtils.java b/src/main/java/net/minestom/server/utils/mojang/MojangUtils.java index c38d14ae2..76bdee3b2 100644 --- a/src/main/java/net/minestom/server/utils/mojang/MojangUtils.java +++ b/src/main/java/net/minestom/server/utils/mojang/MojangUtils.java @@ -2,6 +2,7 @@ package net.minestom.server.utils.mojang; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import net.minestom.server.MinecraftServer; import net.minestom.server.utils.url.URLUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -20,7 +21,7 @@ public final class MojangUtils { final String response = URLUtils.getText(url); return JsonParser.parseString(response).getAsJsonObject(); } catch (IOException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } } @@ -33,7 +34,7 @@ public final class MojangUtils { final String response = URLUtils.getText(url); return JsonParser.parseString(response).getAsJsonObject(); } catch (IOException e) { - e.printStackTrace(); + MinecraftServer.getExceptionManager().handleException(e); return null; } }