From c01b114a089d98de55c9876323d8d31dbfb5ca55 Mon Sep 17 00:00:00 2001 From: Vincent Bolta Date: Sun, 29 Nov 2020 21:56:24 +0100 Subject: [PATCH] Check whether we can get a library hook before returning it --- PluginLibrary.iml | 90 +++++++++++++++++++ .../utils/pluginlibrary/Library.java | 14 +-- .../utils/pluginlibrary/PluginLibrary.java | 14 +-- .../utils/pluginlibrary/hooks/McRPGHook.java | 3 +- .../hooks/TownyAdvancedHook.java | 3 +- 5 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 PluginLibrary.iml diff --git a/PluginLibrary.iml b/PluginLibrary.iml new file mode 100644 index 0000000..2a565e3 --- /dev/null +++ b/PluginLibrary.iml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/me/staartvin/utils/pluginlibrary/Library.java b/src/me/staartvin/utils/pluginlibrary/Library.java index c783876..00ed37e 100644 --- a/src/me/staartvin/utils/pluginlibrary/Library.java +++ b/src/me/staartvin/utils/pluginlibrary/Library.java @@ -3,6 +3,8 @@ import me.staartvin.utils.pluginlibrary.hooks.*; import org.bukkit.Bukkit; +import java.util.Optional; + /** * This class holds all libraries PluginLibrary has. *

@@ -102,18 +104,20 @@ public String getInternalPluginName() { return internalPluginName; } - public LibraryHook getHook() { + public Optional getHook() { // Check if hook is not initialized yet. if (hook == null) { try { - hook = libraryClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - e.printStackTrace(); + hook = libraryClass.getDeclaredConstructor().newInstance(); + } catch (Exception | NoClassDefFoundError exception) { + Bukkit.getConsoleSender().sendMessage("Could not grab hook of " + this.getHumanPluginName()); + exception.printStackTrace(); + return Optional.empty(); } } - return hook; + return Optional.of(hook); } public String getAuthor() { diff --git a/src/me/staartvin/utils/pluginlibrary/PluginLibrary.java b/src/me/staartvin/utils/pluginlibrary/PluginLibrary.java index cf4d668..967ce8a 100644 --- a/src/me/staartvin/utils/pluginlibrary/PluginLibrary.java +++ b/src/me/staartvin/utils/pluginlibrary/PluginLibrary.java @@ -28,7 +28,7 @@ public class PluginLibrary { * @return {@link Library} class or an error. * @throws IllegalArgumentException When no plugin with the given name was found. */ - public static LibraryHook getLibrary(String pluginName) throws IllegalArgumentException { + public static Optional getLibrary(String pluginName) throws IllegalArgumentException { return me.staartvin.utils.pluginlibrary.Library.getEnum(pluginName).getHook(); } @@ -39,7 +39,7 @@ public static LibraryHook getLibrary(String pluginName) throws IllegalArgumentEx * @return {@link Library} class or an error. * @see #getLibrary(String) */ - public static LibraryHook getLibrary(me.staartvin.utils.pluginlibrary.Library lib) { + public static Optional getLibrary(me.staartvin.utils.pluginlibrary.Library lib) { return lib.getHook(); } @@ -124,15 +124,17 @@ public int loadLibraries() { for (me.staartvin.utils.pluginlibrary.Library l : me.staartvin.utils.pluginlibrary.Library.values()) { if (LibraryHook.isPluginAvailable(l)) { try { - LibraryHook libraryHook = l.getHook(); - if (libraryHook.hook()) { + Optional libraryHook = l.getHook(); + if (libraryHook.isPresent() && libraryHook.get().hook()) { loadedLibraries.add(l); count++; + } else { + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Obtained error when " + + "loading " + l.getHumanPluginName()); } } catch (NoClassDefFoundError error) { Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Obtained error when " + - "loading " + - l.getHumanPluginName()); + "loading " + l.getHumanPluginName()); error.printStackTrace(); } } diff --git a/src/me/staartvin/utils/pluginlibrary/hooks/McRPGHook.java b/src/me/staartvin/utils/pluginlibrary/hooks/McRPGHook.java index 14dc792..4df32a2 100644 --- a/src/me/staartvin/utils/pluginlibrary/hooks/McRPGHook.java +++ b/src/me/staartvin/utils/pluginlibrary/hooks/McRPGHook.java @@ -1,7 +1,6 @@ package me.staartvin.utils.pluginlibrary.hooks; import me.staartvin.utils.pluginlibrary.Library; -import us.eunoians.mcrpg.api.exceptions.McRPGPlayerNotFoundException; import us.eunoians.mcrpg.players.McRPGPlayer; import us.eunoians.mcrpg.players.PlayerManager; import us.eunoians.mcrpg.types.Skills; @@ -49,7 +48,7 @@ public Optional getPlayer(UUID uuid) { try { return Optional.ofNullable(PlayerManager.getPlayer(uuid)); - } catch (McRPGPlayerNotFoundException e) { + } catch (Exception e) { return Optional.of(new McRPGPlayer(uuid)); } } diff --git a/src/me/staartvin/utils/pluginlibrary/hooks/TownyAdvancedHook.java b/src/me/staartvin/utils/pluginlibrary/hooks/TownyAdvancedHook.java index 775fabd..3492e0f 100644 --- a/src/me/staartvin/utils/pluginlibrary/hooks/TownyAdvancedHook.java +++ b/src/me/staartvin/utils/pluginlibrary/hooks/TownyAdvancedHook.java @@ -1,7 +1,6 @@ package me.staartvin.utils.pluginlibrary.hooks; import com.palmergames.bukkit.towny.TownyAPI; -import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.Resident; import me.staartvin.utils.pluginlibrary.Library; @@ -41,7 +40,7 @@ public Optional getResident(String playerName) { try { return Optional.ofNullable(TownyAPI.getInstance().getDataSource().getResident(playerName)); - } catch (NotRegisteredException e) { + } catch (Exception e) { return Optional.empty(); } }