From 27212f0496fe913bbe6c4d71a92b9579049fd1bf Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 4 Jun 2019 16:12:55 +0200 Subject: [PATCH] Prevent NPEs when disabling plugin in onEnable --- .../java/de/epiceric/shopchest/ShopChest.java | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index 6bf4c19..09e895c 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -180,12 +180,9 @@ public class ShopChest extends JavaPlugin { getLogger().warning("Plugin may still work, but more errors are expected!"); } - loadExternalPlugins(); - - debug("Loading utils and extras..."); - LanguageUtils.load(); - + shopUtils = new ShopUtils(this); saveResource("item_names.txt", true); + LanguageUtils.load(); File hologramFormatFile = new File(getDataFolder(), "hologram-format.yml"); if (!hologramFormatFile.exists()) { @@ -193,16 +190,14 @@ public class ShopChest extends JavaPlugin { } hologramFormat = new HologramFormat(this); - - loadMetrics(); - initDatabase(); - checkForUpdates(); - - shopUtils = new ShopUtils(this); shopCommand = new ShopCommand(this); shopCreationThreadPool = new ThreadPoolExecutor(0, 8, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue()); - + + loadExternalPlugins(); + loadMetrics(); + initDatabase(); + checkForUpdates(); registerListeners(); registerExternalListeners(); initializeShops(); @@ -215,6 +210,20 @@ public class ShopChest extends JavaPlugin { public void onDisable() { debug("Disabling ShopChest..."); + if (shopUtils == null) { + // Plugin has not been fully enabled (probably due to errors), + // so only close file writer. + if (fw != null && Config.enableDebugLog) { + try { + fw.close(); + } catch (IOException e) { + getLogger().severe("Failed to close FileWriter"); + e.printStackTrace(); + } + } + return; + } + ClickType.clear(); if (updater != null) {