Skip to content

Commit

Permalink
Check whether we can get a library hook before returning it
Browse files Browse the repository at this point in the history
  • Loading branch information
Staartvin committed Nov 29, 2020
1 parent 1be6cb6 commit c01b114
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 15 deletions.
90 changes: 90 additions & 0 deletions PluginLibrary.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.spigotmc:spigot-api:1.14.4-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" name="Maven: net.md-5:bungeecord-chat:1.13-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.23" level="project" />
<orderEntry type="library" name="Maven: org.bukkit:bukkit:1.14.4-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: me.staartvin.Statz:Statz:1.5.3" level="project" />
<orderEntry type="library" name="Maven: com.savagefactions:SavageFactions:2.5" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:13.0" level="project" />
<orderEntry type="library" name="Maven: com.massivecraft.massivecore:MassiveCore:2.14.0" level="project" />
<orderEntry type="library" name="Maven: com.massivecraft.factions:Factions:2.14.0" level="project" />
<orderEntry type="library" name="Maven: net.milkbowl.vault:VaultAPI:1.7" level="project" />
<orderEntry type="library" name="Maven: Jobs:jobs:4.14.3" level="project" />
<orderEntry type="library" name="Maven: afkTerminator:afkTerminator:3.0.8" level="project" />
<orderEntry type="library" name="Maven: com.gmail.nossr50.mcMMO:mcMMO:2.1.111" level="project" />
<orderEntry type="library" name="Maven: org.apache.maven.scm:maven-scm-provider-gitexe:1.8.1" level="project" />
<orderEntry type="library" name="Maven: regexp:regexp:1.3" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.maven.scm:maven-scm-provider-git-commons:1.8.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.maven.scm:maven-scm-api:1.8.1" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.plexus:plexus-utils:3.0.7" level="project" />
<orderEntry type="library" name="Maven: org.bstats:bstats-bukkit:1.4" level="project" />
<orderEntry type="library" name="Maven: com.sk89q.worldguard:worldguard-core:7.0.1-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.sk89q.worldguard.worldguard-libs:core:7.0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.flywaydb:flyway-core:3.0" level="project" />
<orderEntry type="library" name="Maven: com.sk89q.worldguard:worldguard-legacy:7.0.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat:tomcat-jdbc:7.0.52" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat:tomcat-juli:7.0.52" level="project" />
<orderEntry type="library" name="Maven: com.wasteofplastic:acidisland:3.0.9.4" level="project" />
<orderEntry type="library" name="Maven: com.wasteofplastic:askyblock:3.0.9.4" level="project" />
<orderEntry type="library" name="Maven: BattleLevels:BattleLevels:8.2.2" level="project" />
<orderEntry type="library" name="Maven: net.ess3:EssentialsX:2.17.1" level="project" />
<orderEntry type="library" name="Maven: nl.lolmewn.stats:Stats:5.3.3" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.46" level="project" />
<orderEntry type="library" name="Maven: com.rabbitmq:amqp-client:5.3.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" name="Maven: io.reactivex.rxjava2:rxjava:2.2.0" level="project" />
<orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.2" level="project" />
<orderEntry type="library" name="Maven: com.github.akarnokd:rxjava2-extensions:0.20.0" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.1.0" level="project" />
<orderEntry type="library" name="Maven: RPGme:RPGme:1.0.2" level="project" />
<orderEntry type="library" name="Maven: RoyalCommands:RoyalCommands:4.1.1" level="project" />
<orderEntry type="library" name="Maven: me.staartvin.autorank:Autorank:4.2.6" level="project" />
<orderEntry type="library" name="Maven: me.ryanhamshire:GriefPrevention:16.12.0" level="project" />
<orderEntry type="library" name="Maven: OnTime:OnTime:4.1.4" level="project" />
<orderEntry type="library" name="Maven: com.hm.achievement:advanced-achievements-plugin:5.13.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldguard:worldguard-bukkit:7.0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.papermc:paperlib:1.0.2" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: com.sk89q:commandbook:2.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldedit:worldedit-bukkit:7.0.1" level="project" />
<orderEntry type="library" name="Maven: com.sk89q.worldedit:worldedit-core:7.0.1" level="project" />
<orderEntry type="library" name="Maven: com.sk89q.worldedit.worldedit-libs:core:7.0.1" level="project" />
<orderEntry type="library" name="Maven: de.schlichtherle:truezip:6.8.3" level="project" />
<orderEntry type="library" name="Maven: org.mozilla:rhino:1.7.11" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.sk89q.worldedit.worldedit-libs:bukkit:7.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.logging.log4j:log4j-slf4j-impl:2.8.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.logging.log4j:log4j-api:2.8.1" level="project" />
<orderEntry type="library" name="Maven: com.github.rlf:uSkyBlock-API:2.8.5" level="project" />
<orderEntry type="library" name="Maven: me.blackvein.quests:quests-main:3.8.1" level="project" />
<orderEntry type="library" name="Maven: com.leonardobishop:quests:2.6" level="project" />
<orderEntry type="library" name="Maven: org.black_ixx:PlayerPoints:2.1.5" level="project" />
<orderEntry type="library" name="Maven: com.vexsoftware:nuvotifier:2.5.3" level="project" />
<orderEntry type="library" name="Maven: com.Zrips.CMI:CMI:7.1.3.0" level="project" />
<orderEntry type="library" name="Maven: com.gmail.mezymc:UhcStats:1.4" level="project" />
<orderEntry type="library" name="Maven: com.github.Mezy:UhcCore:master-2a339b901c-1" level="project" />
<orderEntry type="library" name="Maven: com.github.Mezy.UhcCore:Support-WorldEdit-6:master-61bd19769b-1" level="project" />
<orderEntry type="library" name="Maven: com.github.Mezy.UhcCore:Support-WorldEdit-7:master-61bd19769b-1" level="project" />
<orderEntry type="library" name="Maven: com.github.Mezy.UhcCore:UhcCore:master-61bd19769b-1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:java-hamcrest:2.0.0.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:2.7.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.6.5" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.6.5" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.5" level="project" />
</component>
</module>
14 changes: 9 additions & 5 deletions src/me/staartvin/utils/pluginlibrary/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand Down Expand Up @@ -102,18 +104,20 @@ public String getInternalPluginName() {
return internalPluginName;
}

public LibraryHook getHook() {
public Optional<LibraryHook> 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() {
Expand Down
14 changes: 8 additions & 6 deletions src/me/staartvin/utils/pluginlibrary/PluginLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<LibraryHook> getLibrary(String pluginName) throws IllegalArgumentException {
return me.staartvin.utils.pluginlibrary.Library.getEnum(pluginName).getHook();
}

Expand All @@ -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<LibraryHook> getLibrary(me.staartvin.utils.pluginlibrary.Library lib) {
return lib.getHook();
}

Expand Down Expand Up @@ -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> 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();
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/me/staartvin/utils/pluginlibrary/hooks/McRPGHook.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -49,7 +48,7 @@ public Optional<McRPGPlayer> getPlayer(UUID uuid) {

try {
return Optional.ofNullable(PlayerManager.getPlayer(uuid));
} catch (McRPGPlayerNotFoundException e) {
} catch (Exception e) {
return Optional.of(new McRPGPlayer(uuid));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -41,7 +40,7 @@ public Optional<Resident> getResident(String playerName) {

try {
return Optional.ofNullable(TownyAPI.getInstance().getDataSource().getResident(playerName));
} catch (NotRegisteredException e) {
} catch (Exception e) {
return Optional.empty();
}
}
Expand Down

0 comments on commit c01b114

Please sign in to comment.