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,6 +78,7 @@ public class Econ {
return true; return true;
} }
if (iConomyUse) {
Holdings holdings = getIconomyHoldings(playerName); Holdings holdings = getIconomyHoldings(playerName);
if (holdings == null) { if (holdings == null) {
return false; return false;
@ -63,6 +86,15 @@ public class Econ {
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
public static boolean deductMoney(String playerName, double amount) { public static boolean deductMoney(String playerName, double amount) {
@ -70,6 +102,7 @@ public class Econ {
return true; return true;
} }
if (iConomyUse) {
Holdings holdings = getIconomyHoldings(playerName); Holdings holdings = getIconomyHoldings(playerName);
if (holdings == null || !holdings.hasEnough(amount)) { if (holdings == null || !holdings.hasEnough(amount)) {
return false; return false;
@ -78,6 +111,19 @@ public class Econ {
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
public static boolean addMoney(String playerName, double amount) { public static boolean addMoney(String playerName, double amount) {
@ -85,6 +131,7 @@ public class Econ {
return true; return true;
} }
if (iConomyUse) {
Holdings holdings = getIconomyHoldings(playerName); Holdings holdings = getIconomyHoldings(playerName);
if (holdings == null) { if (holdings == null) {
return false; return false;
@ -93,6 +140,16 @@ public class Econ {
holdings.add(amount); holdings.add(amount);
return true; return true;
} }
else {
try {
Economy.add(playerName, amount);
return true;
}
catch (Exception ex) {
return false;
}
}
}
// calculate the cost for claiming land // calculate the cost for claiming land

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);
}
}
} }
} }
} }