Skip to content

Commit

Permalink
Only lazily load libraries when their classes are available to preven…
Browse files Browse the repository at this point in the history
…t ClassNotFoundExceptions.
  • Loading branch information
Staartvin committed May 28, 2020
1 parent 3441ada commit 1be6cb6
Show file tree
Hide file tree
Showing 32 changed files with 202 additions and 500 deletions.
99 changes: 61 additions & 38 deletions src/me/staartvin/utils/pluginlibrary/Library.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.staartvin.utils.pluginlibrary;

import me.staartvin.utils.pluginlibrary.hooks.*;
import org.bukkit.Bukkit;

/**
* This class holds all libraries PluginLibrary has.
Expand All @@ -11,68 +12,70 @@
*/
public enum Library {

AUTORANK("Autorank", new AutorankHook(), "Staartvin"),
MCMMO("mcMMO", new McMMOHook(), "t00thpick1"),
FACTIONS("Factions", new FactionsHook(), "Cayorion", "com.massivecraft.factions.Factions"),
ONTIME("OnTime", new OnTimeHook(), "Edge209"),
AFKTERMINATOR("afkTerminator", new AFKTerminatorHook(), "Edge209"),
ROYALCOMMANDS("RoyalCommands", new RoyalCommandsHook(), "WizardCM"),
ULTIMATECORE("UltimateCore", new UltimateCoreHook(), "Bammerbom"),
STATZ("Statz", new StatzHook(), "Staartvin"),
ACIDISLAND("AcidIsland", new AcidIslandHook(), "tastybento"),
ADVANCEDACHIEVEMENTS("AdvancedAchievements", new AdvancedAchievementsHook(), "DarkPyves"),
ASKYBLOCK("ASkyBlock", new ASkyBlockHook(), "tastybento"),
BATTLELEVELS("BattleLevels", new BattleLevelsHook(), "RobiRami"),
GRIEFPREVENTION("GriefPrevention", new GriefPreventionHook(), "RoboMWM"),
JOBS("Jobs", new JobsHook(), "Zrips"),
RPGME("RPGme", new RPGmeHook(), "Flamedek"),
USKYBLOCK("uSkyBlock", new uSkyBlockHook(), "R4zorax"),
VAULT("Vault", new VaultHook(), "Kainzo"),
WORLDGUARD("WorldGuard", new WorldGuardHook(), "sk89q"),
ESSENTIALSX("Essentials", "EssentialsX", new EssentialsXHook(), "drtshock"),
QUESTS("Quests", new QuestsHook(), "PikaMug", "me.blackvein.quests.Quests"),
STATS("Stats", new StatsHook(), "Lolmewn"),
QUESTS_ALTERNATIVE("Quests", new QuestsAlternative(), "LMBishop", "com.leonardobishop.quests.Quests"),
SAVAGE_FACTIONS("Factions", "SavageFactions", new SavageFactionsHook(), "ProSavage", "com.massivecraft.factions" +
AUTORANK("Autorank", AutorankHook.class, "Staartvin"),
MCMMO("mcMMO", McMMOHook.class, "t00thpick1"),
FACTIONS("Factions", FactionsHook.class, "Cayorion", "com.massivecraft.factions.Factions"),
ONTIME("OnTime", OnTimeHook.class, "Edge209"),
AFKTERMINATOR("afkTerminator", AFKTerminatorHook.class, "Edge209"),
ROYALCOMMANDS("RoyalCommands", RoyalCommandsHook.class, "WizardCM"),
ULTIMATECORE("UltimateCore", UltimateCoreHook.class, "Bammerbom"),
STATZ("Statz", StatzHook.class, "Staartvin"),
ACIDISLAND("AcidIsland", AcidIslandHook.class, "tastybento"),
ADVANCEDACHIEVEMENTS("AdvancedAchievements", AdvancedAchievementsHook.class, "DarkPyves"),
ASKYBLOCK("ASkyBlock", ASkyBlockHook.class, "tastybento"),
BATTLELEVELS("BattleLevels", BattleLevelsHook.class, "RobiRami"),
GRIEFPREVENTION("GriefPrevention", GriefPreventionHook.class, "RoboMWM"),
JOBS("Jobs", JobsHook.class, "Zrips"),
RPGME("RPGme", RPGmeHook.class, "Flamedek"),
USKYBLOCK("uSkyBlock", uSkyBlockHook.class, "R4zorax"),
VAULT("Vault", VaultHook.class, "Kainzo"),
WORLDGUARD("WorldGuard", WorldGuardHook.class, "sk89q"),
ESSENTIALSX("Essentials", "EssentialsX", EssentialsXHook.class, "drtshock"),
QUESTS("Quests", QuestsHook.class, "PikaMug", "me.blackvein.quests.Quests"),
STATS("Stats", StatsHook.class, "Lolmewn"),
QUESTS_ALTERNATIVE("Quests", QuestsAlternative.class, "LMBishop", "com.leonardobishop.quests.Quests"),
SAVAGE_FACTIONS("Factions", "SavageFactions", SavageFactionsHook.class, "ProSavage", "com.massivecraft.factions" +
".SavageFactions"),
PLAYERPOINTS("PlayerPoints", new PlayerPointsHook(), "Blackixx"),
NUVOTIFIER("Votifier", "NuVotifier", new NuVotifierHook(), "Ichbinjoe", "com.vexsoftware.votifier" +
PLAYERPOINTS("PlayerPoints", PlayerPointsHook.class, "Blackixx"),
NUVOTIFIER("Votifier", "NuVotifier", NuVotifierHook.class, "Ichbinjoe", "com.vexsoftware.votifier" +
".NuVotifierBukkit"),
CMI("CMI", new CMIHook(), "Zrips"),
UHCSTATS("UhcStats", new UHCStatsHook(), "Mezy"),
TOWNY_ADVANCED("Towny", new TownyAdvancedHook(), "Shade"),
MCRPG("McRPG", new McRPGHook(), "Eunoians");
CMI("CMI", CMIHook.class, "Zrips"),
UHCSTATS("UhcStats", UHCStatsHook.class, "Mezy"),
TOWNY_ADVANCED("Towny", TownyAdvancedHook.class, "Shade"),
MCRPG("McRPG", McRPGHook.class, "Eunoians");

private final String internalPluginName;
private final String authorName;
private final LibraryHook hook;
private final Class<? extends LibraryHook> libraryClass;
private LibraryHook hook;
private String humanPluginName;
private String mainClass;

Library(String pluginName, String humanPluginName, LibraryHook hook, String authorName) {
Library(String pluginName, String humanPluginName, Class<? extends LibraryHook> libraryClass, String authorName) {
this.internalPluginName = pluginName;
this.humanPluginName = humanPluginName;
this.hook = hook;
this.libraryClass = libraryClass;
this.authorName = authorName;
}

Library(String pluginName, LibraryHook hook, String authorName) {
Library(String pluginName, Class<? extends LibraryHook> libraryClass, String authorName) {
this.internalPluginName = pluginName;
this.hook = hook;
this.libraryClass = libraryClass;
this.authorName = authorName;
}

Library(String pluginName, LibraryHook hook, String authorName, String mainClass) {
Library(String pluginName, Class<? extends LibraryHook> libraryClass, String authorName, String mainClass) {
this.internalPluginName = pluginName;
this.hook = hook;
this.libraryClass = libraryClass;
this.authorName = authorName;
this.mainClass = mainClass;
}

Library(String pluginName, String humanPluginName, LibraryHook hook, String authorName, String mainClass) {
Library(String pluginName, String humanPluginName, Class<? extends LibraryHook> libraryClass, String authorName,
String mainClass) {
this.internalPluginName = pluginName;
this.humanPluginName = humanPluginName;
this.hook = hook;
this.libraryClass = libraryClass;
this.authorName = authorName;
this.mainClass = mainClass;
}
Expand Down Expand Up @@ -100,6 +103,16 @@ public String getInternalPluginName() {
}

public LibraryHook getHook() {

// Check if hook is not initialized yet.
if (hook == null) {
try {
hook = libraryClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}

return hook;
}

Expand Down Expand Up @@ -132,4 +145,14 @@ public boolean hasMainClass() {
public void setMainClass(String mainClass) {
this.mainClass = mainClass;
}

/**
* Check if this plugin is installed on the server.
*
* @return true if the plugin is enabled, false otherwise.
*/
public boolean isPluginInstalled() {
return Bukkit.getPluginManager().isPluginEnabled(Library.MCRPG.getInternalPluginName());
}

}
5 changes: 2 additions & 3 deletions src/me/staartvin/utils/pluginlibrary/PluginLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ public int loadLibraries() {
int count = 0;

for (me.staartvin.utils.pluginlibrary.Library l : me.staartvin.utils.pluginlibrary.Library.values()) {
LibraryHook libraryHook = l.getHook();

if (LibraryHook.isPluginAvailable(l) && libraryHook.isAvailable()) {
if (LibraryHook.isPluginAvailable(l)) {
try {
LibraryHook libraryHook = l.getHook();
if (libraryHook.hook()) {
loadedLibraries.add(l);
count++;
Expand Down
18 changes: 4 additions & 14 deletions src/me/staartvin/utils/pluginlibrary/hooks/AFKTerminatorHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,9 @@
*/
public class AFKTerminatorHook extends LibraryHook implements AFKManager {

/*
* (non-Javadoc)
*
* @see me.staartvin.utils.pluginlibrary.hooks.LibraryHook#isAvailable()
*/
@Override
public boolean isAvailable() {
return this.getServer().getPluginManager().isPluginEnabled(Library.AFKTERMINATOR.getInternalPluginName());
}

@Override
public boolean isHooked() {
return isAvailable();
return isPluginAvailable(Library.AFKTERMINATOR);
}

/*
Expand All @@ -40,10 +30,10 @@ public boolean isHooked() {
*/
@Override
public boolean hook() {
// All api calls are done static, so there is no need to get the plugin
// class.
// All api calls are done static, so there is no need to get the plugin
// class.
// We only check if the plugin is available.
return isAvailable();
return isPluginAvailable(Library.AFKTERMINATOR);
}

/**
Expand Down
11 changes: 1 addition & 10 deletions src/me/staartvin/utils/pluginlibrary/hooks/ASkyBlockHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ public class ASkyBlockHook extends LibraryHook {

private ASkyBlock aSkyBlock;

/*
* (non-Javadoc)
*
* @see me.staartvin.utils.pluginlibrary.hooks.LibraryHook#isAvailable()
*/
@Override
public boolean isAvailable() {
return this.getServer().getPluginManager().isPluginEnabled(Library.ASKYBLOCK.getInternalPluginName());
}

@Override
public boolean isHooked() {
Expand All @@ -41,7 +32,7 @@ public boolean isHooked() {
@Override
public boolean hook() {

if (!isAvailable())
if (!isPluginAvailable(Library.ASKYBLOCK))
return false;

aSkyBlock = (ASkyBlock) this.getServer().getPluginManager()
Expand Down
13 changes: 1 addition & 12 deletions src/me/staartvin/utils/pluginlibrary/hooks/AcidIslandHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ public class AcidIslandHook extends LibraryHook {

private ASkyBlock acidIsland;

/*
* (non-Javadoc)
*
* @see me.staartvin.utils.pluginlibrary.hooks.LibraryHook#isAvailable()
*/
@Override
public boolean isAvailable() {
return this.getServer().getPluginManager().isPluginEnabled(Library.ACIDISLAND
.getInternalPluginName());
}

@Override
public boolean isHooked() {
return acidIsland != null;
Expand All @@ -42,7 +31,7 @@ public boolean isHooked() {
@Override
public boolean hook() {

if (!isAvailable())
if (!isPluginAvailable(Library.ACIDISLAND))
return false;

acidIsland = (ASkyBlock) this.getServer().getPluginManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ public class AdvancedAchievementsHook extends LibraryHook {

private AdvancedAchievements advancedAchievements;

/*
* (non-Javadoc)
*
* @see me.staartvin.utils.pluginlibrary.hooks.LibraryHook#isAvailable()
*/
@Override
public boolean isAvailable() {
return this.getServer().getPluginManager().isPluginEnabled(Library.ADVANCEDACHIEVEMENTS
.getInternalPluginName());
}

@Override
public boolean isHooked() {
return advancedAchievements != null;
Expand All @@ -41,7 +30,7 @@ public boolean isHooked() {
@Override
public boolean hook() {

if (!isAvailable())
if (!isPluginAvailable(Library.ADVANCEDACHIEVEMENTS))
return false;

advancedAchievements = (AdvancedAchievements) this.getServer().getPluginManager()
Expand Down
11 changes: 1 addition & 10 deletions src/me/staartvin/utils/pluginlibrary/hooks/AutorankHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ public class AutorankHook extends LibraryHook {

private Autorank autorank;

/*
* (non-Javadoc)
*
* @see me.staartvin.utils.pluginlibrary.LibraryHook#isAvailable()
*/
@Override
public boolean isAvailable() {
return true;
}

@Override
public boolean isHooked() {
Expand All @@ -45,7 +36,7 @@ public boolean isHooked() {
*/
@Override
public boolean hook() {
if (!isAvailable())
if (!isPluginAvailable(Library.AUTORANK))
return false;

autorank = (Autorank) this.getServer().getPluginManager()
Expand Down
13 changes: 2 additions & 11 deletions src/me/staartvin/utils/pluginlibrary/hooks/BattleLevelsHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,10 @@
*/
public class BattleLevelsHook extends LibraryHook {

/*
* (non-Javadoc)
*
* @see me.staartvin.utils.pluginlibrary.hooks.LibraryHook#isAvailable()
*/
@Override
public boolean isAvailable() {
return this.getServer().getPluginManager().isPluginEnabled(Library.BATTLELEVELS.getInternalPluginName());
}

@Override
public boolean isHooked() {
return isAvailable();
return isPluginAvailable(Library.BATTLELEVELS);
}

/*
Expand All @@ -37,7 +28,7 @@ public boolean isHooked() {
*/
@Override
public boolean hook() {
return isAvailable();
return isPluginAvailable(Library.BATTLELEVELS);
}

/**
Expand Down
17 changes: 4 additions & 13 deletions src/me/staartvin/utils/pluginlibrary/hooks/CMIHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,10 @@
*/
public class CMIHook extends me.staartvin.utils.pluginlibrary.hooks.LibraryHook implements AFKManager {

/*
* (non-Javadoc)
*
* @see me.staartvin.plugins.pluginlibrary.hooks.LibraryHook#isAvailable()
*/
@Override
public boolean isAvailable() {
return this.getServer().getPluginManager().isPluginEnabled(Library.CMI.getInternalPluginName());
}

@Override
public boolean isHooked() {
return isAvailable();
return isPluginAvailable(Library.CMI);
}

/*
Expand All @@ -38,10 +29,10 @@ public boolean isHooked() {
*/
@Override
public boolean hook() {
// All api calls are done static, so there is no need to get the plugin
// class.
// All api calls are done static, so there is no need to get the plugin
// class.
// We only check if the plugin is available.
return isAvailable();
return isPluginAvailable(Library.CMI);
}

/**
Expand Down
11 changes: 1 addition & 10 deletions src/me/staartvin/utils/pluginlibrary/hooks/EssentialsXHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ public class EssentialsXHook extends LibraryHook implements AFKManager {

private Essentials essentials;

/*
* (non-Javadoc)
*
* @see me.staartvin.plugins.pluginlibrary.LibraryHook#isAvailable()
*/
@Override
public boolean isAvailable() {
return this.getServer().getPluginManager().isPluginEnabled(Library.ESSENTIALSX.getInternalPluginName());
}

@Override
public boolean isHooked() {
Expand All @@ -41,7 +32,7 @@ public boolean isHooked() {
*/
@Override
public boolean hook() {
if (!isAvailable())
if (!isPluginAvailable(Library.ESSENTIALSX))
return false;

essentials = (Essentials) this.getServer().getPluginManager()
Expand Down
Loading

0 comments on commit 1be6cb6

Please sign in to comment.