Skip to content

Commit

Permalink
Fix some issues with hooking into dependencies
Browse files Browse the repository at this point in the history
- Directly use list of dependencies from Dependency enum instead of the one defined in plugin.yml to be able to hook into plugins not depended on
- Add ability to check for correct version of a plugin via the author name as some people "steal" plugin names...
- Fixed that a plugin's "provides" declaration was only taken into account for plugins already loaded on startup, not when they enabled later
- Made the message when a dependency was hooked into less confusing
  • Loading branch information
Phoenix616 committed Aug 3, 2024
1 parent 1523e5d commit 7c2edc9
Showing 1 changed file with 41 additions and 15 deletions.
56 changes: 41 additions & 15 deletions src/main/java/com/Acrobot/ChestShop/Dependencies.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ private static void initializePlugin(String name, Plugin plugin) { //Really mess
public static boolean loadPlugins() {
PluginManager pluginManager = Bukkit.getPluginManager();

for (String dependency : ChestShop.getDependencies()) {
Plugin plugin = pluginManager.getPlugin(dependency);
for (Dependency dependency : Dependency.values()) {
Plugin plugin = pluginManager.getPlugin(dependency.name());

if (plugin != null && plugin.isEnabled()) {
try {
loadPlugin(dependency, plugin);
loadPlugin(dependency.name(), plugin);
} catch (Exception e) {
plugin.getLogger().log(Level.WARNING, "Unable to hook into " + plugin.getName() + " " + plugin.getDescription().getVersion(), e);
}
Expand Down Expand Up @@ -118,13 +118,19 @@ private static boolean loadEconomy() {
return true;
}

private static void loadPlugin(String name, Plugin plugin) { //Really messy, right? But it's short and fast :)
private static boolean loadPlugin(String name, Plugin plugin) { //Really messy, right? But it's short and fast :)
Dependency dependency;

try {
dependency = Dependency.valueOf(name);

if (dependency.author != null && !plugin.getDescription().getAuthors().contains(dependency.author)) {
ChestShop.getBukkitLogger().info("You are not using the supported variant of " + name + " by " + dependency.author + "."
+ " This variant of " + name + " seems to be made by " + plugin.getDescription().getAuthors().get(0) + " which isn't supported!");
return false;
}
} catch (IllegalArgumentException exception) {
return;
return false;
}

Listener listener = null;
Expand Down Expand Up @@ -165,7 +171,7 @@ private static void loadPlugin(String name, Plugin plugin) { //Really messy, rig
boolean inUse = Properties.WORLDGUARD_USE_PROTECTION || Properties.WORLDGUARD_INTEGRATION;

if (!inUse) {
return;
return false;
}

if (Properties.WORLDGUARD_USE_PROTECTION) {
Expand All @@ -180,14 +186,14 @@ private static void loadPlugin(String name, Plugin plugin) { //Really messy, rig

case GriefPrevention:
if (!Properties.GRIEFPREVENTION_INTEGRATION) {
return;
return false;
}
listener = new GriefPrevenentionBuilding(plugin);
break;

case RedProtect:
if (!Properties.REDPROTECT_INTEGRATION) {
return;
return false;
}
listener = new RedProtectBuilding(plugin);
break;
Expand All @@ -197,7 +203,7 @@ private static void loadPlugin(String name, Plugin plugin) { //Really messy, rig
Heroes heroes = Heroes.getHeroes(plugin);

if (heroes == null) {
return;
return false;
}

listener = heroes;
Expand All @@ -216,12 +222,14 @@ private static void loadPlugin(String name, Plugin plugin) { //Really messy, rig

PluginDescriptionFile description = plugin.getDescription();
versions.put(description.getName(), description.getVersion());
ChestShop.getBukkitLogger().info(description.getName() + " version " + description.getVersion() + " loaded.");
ChestShop.getBukkitLogger().info(description.getName() + " version " + description.getVersion() + " hooked.");

return true;
}

private static enum Dependency {
private enum Dependency {
LWC,
Lockette,
Lockette("Acru"),
LockettePro,
Deadbolt,
SimpleChestLock,
Expand All @@ -236,14 +244,32 @@ private static enum Dependency {

ItemBridge,

ShowItem
ShowItem;

private final String author;

Dependency() {
this.author = null;
}

Dependency(String author) {
this.author = author;
}
}

@EventHandler(priority = EventPriority.MONITOR)
public void onEnable(PluginEnableEvent event) {
Plugin plugin = event.getPlugin();
if (ChestShop.getDependencies().contains(plugin.getName())) {
loadPlugin(plugin.getName(), plugin);
try {
if (!loadPlugin(plugin.getName(), plugin)) {
for (String pluginAlias : plugin.getDescription().getProvides()) {
if (loadPlugin(pluginAlias, plugin)) {
break;
}
}
}
} catch (Exception e) {
plugin.getLogger().log(Level.WARNING, "Unable to hook into " + plugin.getName() + " " + plugin.getDescription().getVersion(), e);
}
}
}

0 comments on commit 7c2edc9

Please sign in to comment.