Saber-Factions/src/com/massivecraft/factions/integration/Econ.java

277 lines
7.5 KiB
Java
Raw Normal View History

package com.massivecraft.factions.integration;
import org.bukkit.event.Event;
import org.bukkit.plugin.Plugin;
import com.massivecraft.factions.listeners.FactionsServerListener;
Some tweaking of the faction bank code... The new help page with bank related commands will now only be shown if banks are enabled and the Economy is enabled and hooked in. Shortened a couple of command descriptions to fit on one line. Made Deposit, Pay, and Withdraw commands additionally log to the server console/log. When bank is given to person disbanding a faction, it now lets them know and logs it to the server log. Added commands to commandDisable list in plugin.yml, along with "permanent" command which I'd missed adding before Added new permission node factions.viewAnyFactionBalance (granted by default if using superperms), which is required to view the bank balance of other factions For reference, about the faction bank addition as a whole... New conf.json settings: "bankEnabled": true, - enable faction banks "bankMembersCanWithdraw": false, - have to be at least moderator to withdraw or pay money to another faction, unless this is set to true "bankFactionPaysCosts": true, - if true, payments for faction command costs are charged to faction bank "bankFactionPaysLandCosts": true, - if true, payments for land claims are charged to faction bank New commands: /f balance *<faction tag> - Shows the bank balance of a specified faction (if permission checks out), or the player's faction if none is specified /f deposit <amount> - Deposit money into your faction's bank /f pay <faction tag> <amount> - Pay money from your faction bank to another faction (requires moderator or admin status) /f withdraw <amount> - Withdraw money from your faction's bank (requires moderator or admin status, unless "bankMembersCanWithdraw" is set to true) New permission node: factions.viewAnyFactionBalance - Allows the player to view the faction bank balance of all factions (default)
2011-10-01 12:59:09 +02:00
import com.massivecraft.factions.commands.FCommandHelp;
import com.earth2me.essentials.api.Economy;
import com.nijikokun.register.payment.Methods;
import com.nijikokun.register.payment.Method.MethodAccount;
import com.iConomy.*;
import com.iConomy.system.*;
import com.massivecraft.factions.Conf;
import com.massivecraft.factions.Factions;
public class Econ {
private static boolean registerUse = false;
private static boolean iConomyUse = false;
private static boolean essEcoUse = false;
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_DISABLE, new FactionsServerListener(), Event.Priority.Monitor, Factions.instance);
}
public static void setup(Factions factions) {
if (enabled()) {
return;
}
2011-08-20 02:41:28 +02:00
if (!registerHooked()) {
Plugin plug = factions.getServer().getPluginManager().getPlugin("Register");
if (plug != null && plug.getClass().getName().equals("com.nijikokun.register.Register") && plug.isEnabled()) {
registerSet(true);
}
}
2011-08-20 02:41:28 +02:00
if (!iConomyHooked()) {
Plugin plug = factions.getServer().getPluginManager().getPlugin("iConomy");
if (plug != null && plug.getClass().getName().equals("com.iConomy.iConomy") && plug.isEnabled()) {
iConomySet(true);
}
}
if (!essentialsEcoHooked()) {
Plugin plug = factions.getServer().getPluginManager().getPlugin("Essentials");
if (plug != null && plug.isEnabled()) {
essentialsEcoSet(true);
}
2011-08-20 02:41:28 +02:00
}
}
public static void registerSet(boolean enable) {
registerUse = enable;
if (enable) {
Factions.log("Register hook available, "+(Conf.econRegisterEnabled ? "and interface is enabled" : "but disabled (\"econRegisterEnabled\": false)")+".");
}
else {
Factions.log("Un-hooked from Register.");
}
Some tweaking of the faction bank code... The new help page with bank related commands will now only be shown if banks are enabled and the Economy is enabled and hooked in. Shortened a couple of command descriptions to fit on one line. Made Deposit, Pay, and Withdraw commands additionally log to the server console/log. When bank is given to person disbanding a faction, it now lets them know and logs it to the server log. Added commands to commandDisable list in plugin.yml, along with "permanent" command which I'd missed adding before Added new permission node factions.viewAnyFactionBalance (granted by default if using superperms), which is required to view the bank balance of other factions For reference, about the faction bank addition as a whole... New conf.json settings: "bankEnabled": true, - enable faction banks "bankMembersCanWithdraw": false, - have to be at least moderator to withdraw or pay money to another faction, unless this is set to true "bankFactionPaysCosts": true, - if true, payments for faction command costs are charged to faction bank "bankFactionPaysLandCosts": true, - if true, payments for land claims are charged to faction bank New commands: /f balance *<faction tag> - Shows the bank balance of a specified faction (if permission checks out), or the player's faction if none is specified /f deposit <amount> - Deposit money into your faction's bank /f pay <faction tag> <amount> - Pay money from your faction bank to another faction (requires moderator or admin status) /f withdraw <amount> - Withdraw money from your faction's bank (requires moderator or admin status, unless "bankMembersCanWithdraw" is set to true) New permission node: factions.viewAnyFactionBalance - Allows the player to view the faction bank balance of all factions (default)
2011-10-01 12:59:09 +02:00
FCommandHelp.updateHelp();
}
public static void iConomySet(boolean enable) {
iConomyUse = enable;
if (enable && !registerUse) {
2011-08-20 02:41:28 +02:00
Factions.log("iConomy hook available, "+(Conf.econIConomyEnabled ? "and interface is enabled" : "but disabled (\"econIConomyEnabled\": false)")+".");
}
else {
Factions.log("Un-hooked from iConomy.");
}
Some tweaking of the faction bank code... The new help page with bank related commands will now only be shown if banks are enabled and the Economy is enabled and hooked in. Shortened a couple of command descriptions to fit on one line. Made Deposit, Pay, and Withdraw commands additionally log to the server console/log. When bank is given to person disbanding a faction, it now lets them know and logs it to the server log. Added commands to commandDisable list in plugin.yml, along with "permanent" command which I'd missed adding before Added new permission node factions.viewAnyFactionBalance (granted by default if using superperms), which is required to view the bank balance of other factions For reference, about the faction bank addition as a whole... New conf.json settings: "bankEnabled": true, - enable faction banks "bankMembersCanWithdraw": false, - have to be at least moderator to withdraw or pay money to another faction, unless this is set to true "bankFactionPaysCosts": true, - if true, payments for faction command costs are charged to faction bank "bankFactionPaysLandCosts": true, - if true, payments for land claims are charged to faction bank New commands: /f balance *<faction tag> - Shows the bank balance of a specified faction (if permission checks out), or the player's faction if none is specified /f deposit <amount> - Deposit money into your faction's bank /f pay <faction tag> <amount> - Pay money from your faction bank to another faction (requires moderator or admin status) /f withdraw <amount> - Withdraw money from your faction's bank (requires moderator or admin status, unless "bankMembersCanWithdraw" is set to true) New permission node: factions.viewAnyFactionBalance - Allows the player to view the faction bank balance of all factions (default)
2011-10-01 12:59:09 +02:00
FCommandHelp.updateHelp();
}
public static void essentialsEcoSet(boolean enable) {
essEcoUse = enable;
if (enable && !registerUse) {
2011-08-20 02:41:28 +02:00
Factions.log("EssentialsEco hook available, "+(Conf.econEssentialsEcoEnabled ? "and interface is enabled" : "but disabled (\"econEssentialsEcoEnabled\": false)")+".");
}
else {
Factions.log("Un-hooked from EssentialsEco.");
}
Some tweaking of the faction bank code... The new help page with bank related commands will now only be shown if banks are enabled and the Economy is enabled and hooked in. Shortened a couple of command descriptions to fit on one line. Made Deposit, Pay, and Withdraw commands additionally log to the server console/log. When bank is given to person disbanding a faction, it now lets them know and logs it to the server log. Added commands to commandDisable list in plugin.yml, along with "permanent" command which I'd missed adding before Added new permission node factions.viewAnyFactionBalance (granted by default if using superperms), which is required to view the bank balance of other factions For reference, about the faction bank addition as a whole... New conf.json settings: "bankEnabled": true, - enable faction banks "bankMembersCanWithdraw": false, - have to be at least moderator to withdraw or pay money to another faction, unless this is set to true "bankFactionPaysCosts": true, - if true, payments for faction command costs are charged to faction bank "bankFactionPaysLandCosts": true, - if true, payments for land claims are charged to faction bank New commands: /f balance *<faction tag> - Shows the bank balance of a specified faction (if permission checks out), or the player's faction if none is specified /f deposit <amount> - Deposit money into your faction's bank /f pay <faction tag> <amount> - Pay money from your faction bank to another faction (requires moderator or admin status) /f withdraw <amount> - Withdraw money from your faction's bank (requires moderator or admin status, unless "bankMembersCanWithdraw" is set to true) New permission node: factions.viewAnyFactionBalance - Allows the player to view the faction bank balance of all factions (default)
2011-10-01 12:59:09 +02:00
FCommandHelp.updateHelp();
}
public static boolean registerHooked() {
return registerUse;
}
public static boolean iConomyHooked() {
return iConomyUse;
}
public static boolean essentialsEcoHooked() {
return essEcoUse;
}
public static boolean registerAvailable() {
return Conf.econRegisterEnabled && registerUse && Methods.hasMethod();
}
// If economy is enabled in conf.json, and we're successfully hooked into an economy plugin
public static boolean enabled() {
return (Conf.econRegisterEnabled && registerUse && Methods.hasMethod())
|| (Conf.econIConomyEnabled && iConomyUse)
|| (Conf.econEssentialsEcoEnabled && essEcoUse);
}
// mainly for internal use, for a little less code repetition
public static Holdings getIconomyHoldings(String playerName) {
if (!enabled()) {
return null;
}
Account account = iConomy.getAccount(playerName);
if (account == null) {
return null;
}
Holdings holdings = account.getHoldings();
return holdings;
}
public static MethodAccount getRegisterAccount(String playerName) {
if (!enabled()) {
return null;
}
if (!Methods.getMethod().hasAccount(playerName)) {
return null;
}
MethodAccount account = Methods.getMethod().getAccount(playerName);
return account;
}
// format money string based on server's set currency type, like "24 gold" or "$24.50"
public static String moneyString(double amount) {
return registerAvailable() ? Methods.getMethod().format(amount)
: (iConomyUse ? iConomy.format(amount) : Economy.format(amount));
}
// whether a player can afford specified amount
public static boolean canAfford(String playerName, double amount) {
// if Economy support is not enabled, they can certainly afford to pay nothing
if (!enabled()) {
return true;
}
if (registerAvailable()) {
MethodAccount holdings = getRegisterAccount(playerName);
if (holdings == null) {
return false;
}
return holdings.hasEnough(amount);
}
else if (iConomyUse) {
Holdings holdings = getIconomyHoldings(playerName);
if (holdings == null) {
return false;
}
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
public static boolean deductMoney(String playerName, double amount) {
if (!enabled()) {
return true;
}
if (registerAvailable()) {
MethodAccount holdings = getRegisterAccount(playerName);
if (holdings == null || !holdings.hasEnough(amount)) {
return false;
}
return holdings.subtract(amount);
}
else if (iConomyUse) {
Holdings holdings = getIconomyHoldings(playerName);
if (holdings == null || !holdings.hasEnough(amount)) {
return false;
}
holdings.subtract(amount);
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
public static boolean addMoney(String playerName, double amount) {
if (!enabled()) {
return true;
}
if (registerAvailable()) {
MethodAccount holdings = getRegisterAccount(playerName);
if (holdings == null) {
return false;
}
return holdings.add(amount);
}
else if (iConomyUse) {
Holdings holdings = getIconomyHoldings(playerName);
if (holdings == null) {
return false;
}
holdings.add(amount);
return true;
}
else {
try {
Economy.add(playerName, amount);
return true;
}
catch (Exception ex) {
return false;
}
}
}
// calculate the cost for claiming land
public static double calculateClaimCost(int ownedLand, boolean takingFromAnotherFaction) {
if (!enabled()) {
return 0.0;
}
// basic claim cost, plus land inflation cost, minus the potential bonus given for claiming from another faction
return Conf.econCostClaimWilderness
+ (Conf.econCostClaimWilderness * Conf.econClaimAdditionalMultiplier * ownedLand)
- (takingFromAnotherFaction ? Conf.econCostClaimFromFactionBonus: 0);
}
// calculate refund amount for unclaiming land
public static double calculateClaimRefund(int ownedLand) {
return calculateClaimCost(ownedLand - 1, false) * Conf.econClaimRefundMultiplier;
}
// calculate value of all owned land
public static double calculateTotalLandValue(int ownedLand) {
double amount = 0;
for (int x = 0; x < ownedLand; x++) {
amount += calculateClaimCost(x, false);
}
return amount;
}
// calculate refund amount for all owned land
public static double calculateTotalLandRefund(int ownedLand) {
return calculateTotalLandValue(ownedLand) * Conf.econClaimRefundMultiplier;
}
}