From 3730eb97031ed31412c62f6425b38653225a5839 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 16 Jun 2019 15:06:35 +0200 Subject: [PATCH] Disable plugin when shops fail to load or reload --- src/main/java/de/epiceric/shopchest/ShopChest.java | 6 ++++++ .../epiceric/shopchest/command/ShopCommandExecutor.java | 8 ++++++++ .../epiceric/shopchest/listeners/ShopUpdateListener.java | 8 ++++++++ src/main/java/de/epiceric/shopchest/utils/ShopUtils.java | 8 ++++++-- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index 926f058..d235392 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -444,6 +444,12 @@ public class ShopChest extends JavaPlugin { getLogger().info("Initialized " + result + " Shops"); debug("Initialized " + result + " Shops"); } + + @Override + public void onError(Throwable throwable) { + // Database connection probably failed => disable plugin to prevent more errors + getServer().getPluginManager().disablePlugin(ShopChest.this); + } }); } diff --git a/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java b/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java index 97746e0..b8bf04d 100644 --- a/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java +++ b/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java @@ -198,6 +198,14 @@ class ShopCommandExecutor implements CommandExecutor { new Replacement(Placeholder.AMOUNT, String.valueOf(result)))); plugin.debug(sender.getName() + " has reloaded " + result + " shops"); } + + @Override + public void onError(Throwable throwable) { + // Database connection probably failed => disable plugin to prevent more errors + sender.sendMessage(LanguageUtils.getMessage(Message.ERROR_OCCURRED, + new Replacement(Placeholder.ERROR, "No database access: Disabling ShopChest"))); + Bukkit.getPluginManager().disablePlugin(plugin); + } }); } diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java index 5242615..e3505be 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java @@ -3,6 +3,8 @@ package de.epiceric.shopchest.listeners; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.utils.Callback; + +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -94,6 +96,12 @@ public class ShopUpdateListener implements Listener { plugin.getLogger().info(String.format("Reloaded %d shops because a new world '%s' was loaded", result, worldName)); plugin.debug(String.format("Reloaded %d shops because a new world '%s' was loaded", result, worldName)); } + + @Override + public void onError(Throwable throwable) { + // Database connection probably failed => disable plugin to prevent more errors + Bukkit.getPluginManager().disablePlugin(plugin); + } }); } } diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index bf76fd5..0dbb15a 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -3,6 +3,7 @@ package de.epiceric.shopchest.utils; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.shop.Shop; + import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.block.Chest; @@ -256,11 +257,14 @@ public class ShopUtils { @Override public void onError(Throwable throwable) { if (callback != null) callback.callSyncError(throwable); - plugin.debug("Error while adding shops"); - plugin.debug(throwable); } }); } + + @Override + public void onError(Throwable throwable) { + if (callback != null) callback.callSyncError(throwable); + } }); }