Economy integration now works for EssentialsEco as well

This commit is contained in:
Brettflan 2011-08-04 02:25:12 -05:00
parent 100a1ffb1e
commit e6ac1c0f98
5 changed files with 115 additions and 42 deletions

BIN
lib/Essentials.jar Normal file

Binary file not shown.

View File

@ -149,6 +149,7 @@ public class Conf {
// Economy settings // Economy settings
public static boolean econIConomyEnabled = false; public static boolean econIConomyEnabled = false;
public static boolean econEssentialsEcoEnabled = false;
public static double econCostClaimWilderness = 30.0; public static double econCostClaimWilderness = 30.0;
public static double econCostClaimFromFactionBonus = 30.0; public static double econCostClaimFromFactionBonus = 30.0;
public static double econClaimAdditionalMultiplier = 0.5; public static double econClaimAdditionalMultiplier = 0.5;

View File

@ -4,29 +4,51 @@ import org.bukkit.event.Event;
import com.massivecraft.factions.listeners.FactionsServerListener; import com.massivecraft.factions.listeners.FactionsServerListener;
import com.earth2me.essentials.api.Economy;
import com.iConomy.*; import com.iConomy.*;
import com.iConomy.system.*; import com.iConomy.system.*;
public class Econ { public class Econ {
private static iConomy iConomyPlugin; private static boolean iConomyUse = false;
private static boolean essEcoUse = false;
public static void monitorPlugins() { public static void monitorPlugins() {
Factions.instance.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_ENABLE, new FactionsServerListener(), Event.Priority.Monitor, Factions.instance); Factions.instance.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_ENABLE, new FactionsServerListener(), Event.Priority.Monitor, Factions.instance);
Factions.instance.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_DISABLE, new FactionsServerListener(), Event.Priority.Monitor, Factions.instance); Factions.instance.getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_DISABLE, new FactionsServerListener(), Event.Priority.Monitor, Factions.instance);
} }
public static void iConomySet(iConomy instance) { public static void iConomySet(boolean enable) {
iConomyPlugin = instance; iConomyUse = enable;
if (enable) {
Factions.log("Hooked into iConomy, "+(Conf.econIConomyEnabled ? "and interface is enabled" : "but interface is currently disabled (\"econIConomyEnabled\": false)")+".");
}
else {
Factions.log("Un-hooked from iConomy.");
}
}
public static void essentialsEcoSet(boolean enable) {
essEcoUse = enable;
if (enable) {
Factions.log("Hooked into EssentialsEco, "+(Conf.econEssentialsEcoEnabled ? "and interface is enabled" : "but interface is currently disabled (\"econEssentialsEcoEnabled\": false)")+".");
}
else {
Factions.log("Un-hooked from EssentialsEco.");
}
} }
public static boolean iConomyHooked() { public static boolean iConomyHooked() {
return iConomyPlugin != null; return iConomyUse;
}
public static boolean essentialsEcoHooked() {
return essEcoUse;
} }
// If economy is enabled in conf.json, and we're successfully hooked into an economy plugin // If economy is enabled in conf.json, and we're successfully hooked into an economy plugin
public static boolean enabled() { public static boolean enabled() {
return Conf.econIConomyEnabled && iConomyPlugin != null; return (Conf.econIConomyEnabled && iConomyUse) || (Conf.econEssentialsEcoEnabled && essEcoUse);
} }
// mainly for internal use, for a little less code repetition // mainly for internal use, for a little less code repetition
@ -46,7 +68,7 @@ public class Econ {
// format money string based on server's set currency type, like "24 gold" or "$24.50" // format money string based on server's set currency type, like "24 gold" or "$24.50"
public static String moneyString(double amount) { public static String moneyString(double amount) {
return iConomy.format(amount); return iConomyUse ? iConomy.format(amount) : Economy.format(amount);
} }
// whether a player can afford specified amount // whether a player can afford specified amount
@ -56,12 +78,22 @@ public class Econ {
return true; return true;
} }
Holdings holdings = getIconomyHoldings(playerName); if (iConomyUse) {
if (holdings == null) { Holdings holdings = getIconomyHoldings(playerName);
return false; if (holdings == null) {
} return false;
}
return holdings.hasEnough(amount); return holdings.hasEnough(amount);
}
else {
try {
return Economy.hasEnough(playerName, amount);
}
catch (Exception ex) {
return false;
}
}
} }
// deduct money from their account; returns true if successful // deduct money from their account; returns true if successful
@ -70,13 +102,27 @@ public class Econ {
return true; return true;
} }
Holdings holdings = getIconomyHoldings(playerName); if (iConomyUse) {
if (holdings == null || !holdings.hasEnough(amount)) { Holdings holdings = getIconomyHoldings(playerName);
return false; if (holdings == null || !holdings.hasEnough(amount)) {
} return false;
}
holdings.subtract(amount); holdings.subtract(amount);
return true; return true;
}
else {
try {
if (!Economy.hasEnough(playerName, amount)) {
return false;
}
Economy.subtract(playerName, amount);
return true;
}
catch (Exception ex) {
return false;
}
}
} }
// add money to their account; returns true if successful // add money to their account; returns true if successful
@ -85,13 +131,24 @@ public class Econ {
return true; return true;
} }
Holdings holdings = getIconomyHoldings(playerName); if (iConomyUse) {
if (holdings == null) { Holdings holdings = getIconomyHoldings(playerName);
return false; if (holdings == null) {
} return false;
}
holdings.add(amount); holdings.add(amount);
return true; return true;
}
else {
try {
Economy.add(playerName, amount);
return true;
}
catch (Exception ex) {
return false;
}
}
} }

View File

@ -146,6 +146,7 @@ public class Factions extends JavaPlugin {
setupPermissions(); setupPermissions();
integrateEssentialsChat(); integrateEssentialsChat();
setupEcon();
Econ.monitorPlugins(); Econ.monitorPlugins();
@ -213,6 +214,25 @@ public class Factions extends JavaPlugin {
} }
} }
private void setupEcon() {
if (Econ.enabled()) {
return;
}
if (!Econ.iConomyHooked()) {
Plugin plug = this.getServer().getPluginManager().getPlugin("iConomy");
if (plug != null && plug.getClass().getName().equals("com.iConomy.iConomy")) {
Econ.iConomySet(true);
}
}
if (!Econ.essentialsEcoHooked()) {
Plugin plug = this.getServer().getPluginManager().getPlugin("Essentials");
if (plug != null) {
Econ.essentialsEcoSet(true);
}
}
}
private void integrateEssentialsChat() { private void integrateEssentialsChat() {
if (essChat != null) { if (essChat != null) {
return; return;

View File

@ -5,35 +5,30 @@ import org.bukkit.event.server.ServerListener;
import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.event.server.PluginEnableEvent;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.Econ; import com.massivecraft.factions.Econ;
import com.massivecraft.factions.Factions;
import com.iConomy.*;
public class FactionsServerListener extends ServerListener { public class FactionsServerListener extends ServerListener {
@Override @Override
public void onPluginDisable(PluginDisableEvent event) { public void onPluginDisable(PluginDisableEvent event) {
if (Econ.iConomyHooked()) { String name = event.getPlugin().getDescription().getName();
if (event.getPlugin().getDescription().getName().equals("iConomy")) { if (Econ.iConomyHooked() && name.equals("iConomy")) {
Econ.iConomySet(null); Econ.iConomySet(false);
Factions.log("Un-hooked from iConomy.");
}
} }
} if (Econ.essentialsEcoHooked() && name.equals("Essentials")) {
Econ.essentialsEcoSet(false);
}
}
@Override @Override
public void onPluginEnable(PluginEnableEvent event) { public void onPluginEnable(PluginEnableEvent event) {
if (!Econ.iConomyHooked()) { Plugin plug = event.getPlugin();
Plugin iConomy = Factions.instance.getServer().getPluginManager().getPlugin("iConomy"); String name = plug.getDescription().getName();
if (!Econ.iConomyHooked() && name.equals("iConomy") && plug.getClass().getName().equals("com.iConomy.iConomy")) {
if (iConomy != null) { Econ.iConomySet(true);
if (iConomy.isEnabled() && iConomy.getClass().getName().equals("com.iConomy.iConomy")) { }
Econ.iConomySet((iConomy)iConomy); else if (!Econ.essentialsEcoHooked() && name.equals("Essentials")) {
Factions.log("Hooked into iConomy, "+(Conf.econIConomyEnabled ? "and interface is enabled" : "but interface is currently disabled (\"econIConomyEnabled\": false)")+"."); Econ.essentialsEcoSet(true);
}
}
} }
} }
} }