From 4692e66389b9bba01cce6bdb13d263b6b13e6c6d Mon Sep 17 00:00:00 2001 From: cpw Date: Fri, 15 Aug 2014 16:16:07 -0400 Subject: [PATCH] Allow a clean way to exit the game without big ugly warnings, but with logging information available if needed. Closes #496 --- .../cpw/mods/fml/common/FMLCommonHandler.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/fml/src/main/java/cpw/mods/fml/common/FMLCommonHandler.java b/fml/src/main/java/cpw/mods/fml/common/FMLCommonHandler.java index 4c7403920..3999a5622 100644 --- a/fml/src/main/java/cpw/mods/fml/common/FMLCommonHandler.java +++ b/fml/src/main/java/cpw/mods/fml/common/FMLCommonHandler.java @@ -594,4 +594,37 @@ public class FMLCommonHandler { return sidedDelegate.shouldAllowPlayerLogins(); } + + + /** + * Used to exit from java, with system exit preventions in place. Will be tidy about it and just log a message, + * unless debugging is enabled + * + * @param exitCode The exit code + * @param hardExit Perform a halt instead of an exit (only use when the world is unsavable) + */ + public void exitJava(int exitCode, boolean hardExit) + { + FMLLog.log(Level.INFO, "Java has been asked to exit (code %d) by %s.", exitCode, Thread.currentThread().getStackTrace()[1]); + if (hardExit) + { + FMLLog.log(Level.INFO, "This is a forced exit"); + } + if (Boolean.parseBoolean(System.getProperty("fml.debugExit", "false"))) + { + FMLLog.log(Level.INFO, new Throwable(), "Exit trace"); + } + else + { + FMLLog.log(Level.INFO, "If this was an unexpected exit, use -Dfml.debugExit=true to find out where it was called"); + } + if (hardExit) + { + Runtime.getRuntime().halt(exitCode); + } + else + { + System.exit(exitCode); + } + } }