Changed how dependencies are loaded

This commit is contained in:
Acrobot 2012-06-25 17:16:24 +02:00
parent c31e902b4a
commit f67990ea48
7 changed files with 183 additions and 62 deletions

View File

@ -2,103 +2,159 @@ package com.Acrobot.ChestShop;
import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.Config.Config; import com.Acrobot.ChestShop.Config.Config;
import com.Acrobot.ChestShop.Config.Property;
import com.Acrobot.ChestShop.Economy.Economy; import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Economy.NoProvider; import com.Acrobot.ChestShop.Economy.NoProvider;
import com.Acrobot.ChestShop.Economy.Register; import com.Acrobot.ChestShop.Economy.Register;
import com.Acrobot.ChestShop.Economy.Vault; import com.Acrobot.ChestShop.Economy.Vault;
import com.Acrobot.ChestShop.Plugins.*; import com.Acrobot.ChestShop.Plugins.*;
import com.griefcraft.lwc.LWC;
import com.nijikokun.register.payment.forChestShop.Method; import com.nijikokun.register.payment.forChestShop.Method;
import com.nijikokun.register.payment.forChestShop.Methods; import com.nijikokun.register.payment.forChestShop.Methods;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.webkonsept.bukkit.simplechestlock.SCL;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.List;
import static com.Acrobot.ChestShop.Config.Property.WORLDGUARD_INTEGRATION;
import static com.Acrobot.ChestShop.Config.Property.WORLDGUARD_USE_PROTECTION;
/** /**
* @author Acrobot * @author Acrobot
*/ */
public class Dependencies { public class Dependencies {
public static void load() { public static void load() {
initializeSecurity();
PluginManager pluginManager = Bukkit.getPluginManager(); PluginManager pluginManager = Bukkit.getPluginManager();
for (Object dependency : ChestShop.getDependencies()) { for (String dependency : (List<String>) ChestShop.getDependencies()) {
Plugin plugin = pluginManager.getPlugin((String) dependency); Plugin plugin = pluginManager.getPlugin(dependency);
if (plugin != null) { if (plugin != null) {
initializePlugin((String) dependency, plugin); initializePlugin(dependency, plugin);
} }
} }
loadRegister();
}
private static void initializeSecurity() { if (!Economy.isLoaded()) {
ChestShop.registerListener(new com.Acrobot.ChestShop.Plugins.ChestShop()); loadRegister();
}
} }
private static void loadRegister() { private static void loadRegister() {
if (Economy.economy != null) { Method method = Methods.load();
if (method == null) {
Economy.setPlugin(new NoProvider());
return; return;
} }
Method method = Methods.load(); Economy.setPlugin(new Register(method));
if (method == null) {
Economy.economy = new NoProvider();
return;
}
Economy.economy = new Register(method);
ChestShop.getBukkitLogger().info(method.getName() + " version " + method.getVersion() + " loaded."); ChestShop.getBukkitLogger().info(method.getName() + " version " + method.getVersion() + " loaded.");
} }
private static void initializePlugin(String name, Plugin plugin) { //Really messy, right? But it's short and fast :) private static void initializePlugin(String name, Plugin plugin) { //Really messy, right? But it's short and fast :)
if (name.equals("LWC")) { Dependency dependency;
ChestShop.registerListener(new LightweightChestProtection(LWC.getInstance()));
} else if (name.equals("Lockette")) { try {
ChestShop.registerListener(new Lockette()); dependency = Dependency.valueOf(name);
} else if (name.equals("Deadbolt")) { } catch (IllegalArgumentException exception) {
ChestShop.registerListener(new Deadbolt());
} else if (name.equals("OddItem")) {
MaterialUtil.Odd.initialize();
} else if (name.equals("Towny")) {
if (!Config.getBoolean(Property.TOWNY_INTEGRATION)) {
return; return;
} }
ChestShop.registerListener(new Towny());
} else if (name.equals("WorldGuard")) { Listener listener = null;
switch(dependency) {
//Protection plugins
case LWC:
listener = new LightweightChestProtection();
break;
case Lockette:
listener = new Lockette();
break;
case Deadbolt:
listener = new Deadbolt();
break;
case SimpleChestLock:
listener = SimpleChestLock.getSimpleChestLock(plugin);
break;
//Terrain protection plugins
case Towny:
Towny towny = Towny.getTowny();
if (towny == null) {
return;
}
listener = towny;
break;
case WorldGuard:
WorldGuardPlugin worldGuard = (WorldGuardPlugin) plugin; WorldGuardPlugin worldGuard = (WorldGuardPlugin) plugin;
if (Config.getBoolean(Property.WORLDGUARD_USE_PROTECTION)) { boolean inUse = Config.getBoolean(WORLDGUARD_USE_PROTECTION) || Config.getBoolean(WORLDGUARD_INTEGRATION);
if (!inUse) {
return;
}
if (Config.getBoolean(WORLDGUARD_USE_PROTECTION)) {
ChestShop.registerListener(new WorldGuardProtection(worldGuard)); ChestShop.registerListener(new WorldGuardProtection(worldGuard));
} }
if (!Config.getBoolean(Property.WORLDGUARD_INTEGRATION)) {
if (Config.getBoolean(WORLDGUARD_INTEGRATION)) {
listener = new WorldGuardBuilding(worldGuard);
}
break;
//Other plugins
case Vault:
Vault vault = Vault.getVault();
if (vault == null) {
return; return;
} }
ChestShop.registerListener(new WorldGuardBuilding(worldGuard));
} else if (name.equals("Vault")) {
if (Economy.economy != null) return;
RegisteredServiceProvider<net.milkbowl.vault.economy.Economy> rsp = ChestShop.getBukkitServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); Economy.setPlugin(vault);
if (rsp == null) return;
Vault.economy = rsp.getProvider(); ChestShop.getBukkitLogger().info("Vault loaded - using " + Vault.getPluginName());
if (Vault.economy == null) return;
Economy.economy = new Vault();
ChestShop.getBukkitLogger().info("Vault loaded - using " + Vault.economy.getName());
return; return;
} else if (name.equals("Heroes")) { case Heroes:
ChestShop.registerListener(new Heroes((com.herocraftonline.heroes.Heroes) plugin)); Heroes heroes = Heroes.getHeroes(plugin);
} else if (name.equals("SimpleChestLock")) {
ChestShop.registerListener(new SimpleChestLock((SCL) plugin)); if (heroes == null) {
} else {
return; return;
} }
listener = heroes;
break;
case OddItem:
MaterialUtil.Odd.initialize();
break;
}
if (listener != null) {
ChestShop.registerListener(listener);
}
PluginDescriptionFile description = plugin.getDescription(); PluginDescriptionFile description = plugin.getDescription();
ChestShop.getBukkitLogger().info(description.getName() + " version " + description.getVersion() + " loaded."); ChestShop.getBukkitLogger().info(description.getName() + " version " + description.getVersion() + " loaded.");
} }
private static enum Dependency {
LWC,
Lockette,
Deadbolt,
SimpleChestLock,
OddItem,
Towny,
WorldGuard,
Vault,
Heroes
}
} }

View File

@ -11,7 +11,7 @@ import static com.Acrobot.Breeze.Utils.NumberUtil.roundUp;
* Economy management * Economy management
*/ */
public class Economy { public class Economy {
public static EcoPlugin economy; private static EcoPlugin economy;
public static boolean hasAccount(String p) { public static boolean hasAccount(String p) {
return !p.isEmpty() && economy.hasAccount(uName.getName(p)); return !p.isEmpty() && economy.hasAccount(uName.getName(p));
@ -63,4 +63,16 @@ public class Economy {
public static String formatBalance(double amount) { public static String formatBalance(double amount) {
return economy.format(roundUp(amount)); return economy.format(roundUp(amount));
} }
public static void setPlugin(EcoPlugin plugin) {
economy = plugin;
}
public static EcoPlugin getPlugin() {
return economy;
}
public static boolean isLoaded() {
return economy != null;
}
} }

View File

@ -1,10 +1,13 @@
package com.Acrobot.ChestShop.Economy; package com.Acrobot.ChestShop.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
/** /**
* @author Acrobot * @author Acrobot
*/ */
public class Vault implements EcoPlugin { public class Vault implements EcoPlugin {
public static net.milkbowl.vault.economy.Economy economy; private static net.milkbowl.vault.economy.Economy economy;
public boolean hasAccount(String player) { public boolean hasAccount(String player) {
return economy.hasAccount(player); return economy.hasAccount(player);
@ -29,4 +32,28 @@ public class Vault implements EcoPlugin {
public String format(double amount) { public String format(double amount) {
return economy.format(amount); return economy.format(amount);
} }
public static String getPluginName() {
if (economy == null) {
return "";
} else {
return economy.getName();
}
}
public static Vault getVault() {
RegisteredServiceProvider<net.milkbowl.vault.economy.Economy> rsp = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
if (rsp == null) {
return null;
}
economy = rsp.getProvider();
if (economy == null) {
return null;
} else {
return new Vault();
}
}
} }

View File

@ -7,6 +7,7 @@ import com.herocraftonline.heroes.characters.Hero;
import com.herocraftonline.heroes.characters.classes.HeroClass; import com.herocraftonline.heroes.characters.classes.HeroClass;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
/** /**
* @author Acrobot * @author Acrobot
@ -34,4 +35,12 @@ public class Heroes implements Listener {
hero.gainExp(heroExp, HeroClass.ExperienceType.EXTERNAL); hero.gainExp(heroExp, HeroClass.ExperienceType.EXTERNAL);
} }
} }
public static Heroes getHeroes(Plugin plugin) {
if (!(plugin instanceof com.herocraftonline.heroes.Heroes)) {
return null;
}
return new Heroes((com.herocraftonline.heroes.Heroes) plugin);
}
} }

View File

@ -29,8 +29,8 @@ public class LightweightChestProtection implements Listener {
private LWC lwc; private LWC lwc;
private LimitsV2 limitsModule; private LimitsV2 limitsModule;
public LightweightChestProtection(LWC lwc) { public LightweightChestProtection() {
this.lwc = lwc; this.lwc = LWC.getInstance();
limitsModule = new LimitsV2(); limitsModule = new LimitsV2();
} }

View File

@ -7,6 +7,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
/** /**
* @author Acrobot * @author Acrobot
@ -37,4 +38,12 @@ public class SimpleChestLock implements Listener {
event.setResult(Event.Result.DENY); event.setResult(Event.Result.DENY);
} }
} }
public static SimpleChestLock getSimpleChestLock(Plugin plugin) {
if (!(plugin instanceof SCL)) {
return null;
}
return new SimpleChestLock((SCL) plugin);
}
} }

View File

@ -101,4 +101,12 @@ public class Towny implements Listener {
return true; return true;
} }
public static Towny getTowny() {
if (!Config.getBoolean(Property.TOWNY_INTEGRATION)) {
return null;
}
return new Towny();
}
} }