Add support for server account

This commit is contained in:
AppleDash 2017-02-14 11:28:04 -05:00
parent 6352cce5f3
commit c621824b82
6 changed files with 65 additions and 70 deletions

View File

@ -23,11 +23,13 @@ public class EconomyManager {
private final ISaneEconomy saneEconomy;
private final Currency currency;
private final EconomyStorageBackend backend;
private final String serverAccountName;
public EconomyManager(ISaneEconomy saneEconomy, Currency currency, EconomyStorageBackend backend) {
public EconomyManager(ISaneEconomy saneEconomy, Currency currency, EconomyStorageBackend backend, String serverAccountName) {
this.saneEconomy = saneEconomy;
this.currency = currency;
this.backend = backend;
this.serverAccountName = serverAccountName;
}
/**
@ -196,4 +198,12 @@ public class EconomyManager {
public EconomyStorageBackend getBackend() {
return backend;
}
/**
* Get the name of the "Server" economy account. This account has an infinite balance and deposits do nothing.
* @return Server economy account, or null if none.
*/
public String getServerAccountName() {
return serverAccountName;
}
}

View File

@ -41,10 +41,18 @@ public class Transaction {
}
public boolean isSenderAffected() {
if (sender == Economable.CONSOLE) {
return false;
}
return (reason.getAffectedParties() == AffectedParties.SENDER) || (reason.getAffectedParties() == AffectedParties.BOTH);
}
public boolean isReceiverAffected() {
if (receiver == Economable.CONSOLE) {
return false;
}
return (reason.getAffectedParties() == AffectedParties.RECEIVER) || (reason.getAffectedParties() == AffectedParties.BOTH);
}
}

View File

@ -65,7 +65,7 @@ public class SaneEconomyConfiguration {
rootConfig.set("old-backend", null);
}
return new EconomyManager(saneEconomy, currency, backend);
return new EconomyManager(saneEconomy, currency, backend, rootConfig.getString("economy.server-account", null));
}
/**

View File

@ -54,16 +54,8 @@ public class EconomySaneEconomy implements Economy {
}
@Override
public boolean hasAccount(String playerName) {
Economable economable;
if (validatePlayer(playerName)) {
economable = Economable.wrap(PlayerUtils.getOfflinePlayer(playerName));
} else {
economable = Economable.wrap(playerName);
}
return SaneEconomy.getInstance().getEconomyManager().accountExists(economable);
public boolean hasAccount(String target) {
return SaneEconomy.getInstance().getEconomyManager().accountExists(makeEconomable(target));
}
@Override
@ -72,8 +64,8 @@ public class EconomySaneEconomy implements Economy {
}
@Override
public boolean hasAccount(String playerName, String worldName) {
return hasAccount(playerName);
public boolean hasAccount(String target, String worldName) {
return hasAccount(target);
}
@Override
@ -82,15 +74,8 @@ public class EconomySaneEconomy implements Economy {
}
@Override
public double getBalance(String playerName) {
Economable economable;
if (validatePlayer(playerName)) {
economable = Economable.wrap(PlayerUtils.getOfflinePlayer(playerName));
} else {
economable = Economable.wrap(playerName);
}
return SaneEconomy.getInstance().getEconomyManager().getBalance(economable);
public double getBalance(String target) {
return SaneEconomy.getInstance().getEconomyManager().getBalance(makeEconomable(target));
}
@Override
@ -99,8 +84,8 @@ public class EconomySaneEconomy implements Economy {
}
@Override
public double getBalance(String playerName, String worldName) {
return getBalance(playerName);
public double getBalance(String target, String worldName) {
return getBalance(target);
}
@Override
@ -109,62 +94,48 @@ public class EconomySaneEconomy implements Economy {
}
@Override
public boolean has(String playerName, double v) {
Economable economable;
if (validatePlayer(playerName)) {
economable = Economable.wrap(PlayerUtils.getOfflinePlayer(playerName));
} else {
economable = Economable.wrap(playerName);
}
return SaneEconomy.getInstance().getEconomyManager().hasBalance(economable, v);
public boolean has(String target, double amount) {
return SaneEconomy.getInstance().getEconomyManager().hasBalance(makeEconomable(target), amount);
}
@Override
public boolean has(OfflinePlayer offlinePlayer, double v) {
return SaneEconomy.getInstance().getEconomyManager().hasBalance(Economable.wrap(offlinePlayer), v);
public boolean has(OfflinePlayer offlinePlayer, double amount) {
return SaneEconomy.getInstance().getEconomyManager().hasBalance(Economable.wrap(offlinePlayer), amount);
}
@Override
public boolean has(String playerName, String worldName, double v) {
return has(playerName, v);
public boolean has(String target, String worldName, double amount) {
return has(target, amount);
}
@Override
public boolean has(OfflinePlayer offlinePlayer, String worldName, double v) {
return has(offlinePlayer, v);
public boolean has(OfflinePlayer offlinePlayer, String worldName, double amount) {
return has(offlinePlayer, amount);
}
@Override
public EconomyResponse withdrawPlayer(String playerName, double v) {
if (v == 0) {
return new EconomyResponse(v, getBalance(playerName), EconomyResponse.ResponseType.SUCCESS, "");
}
Economable economable;
if (validatePlayer(playerName)) {
economable = Economable.wrap(PlayerUtils.getOfflinePlayer(playerName));
} else {
economable = Economable.wrap(playerName);
public EconomyResponse withdrawPlayer(String target, double amount) {
if (amount == 0) {
return new EconomyResponse(amount, getBalance(target), EconomyResponse.ResponseType.SUCCESS, "");
}
return transact(new Transaction(
economable, Economable.PLUGIN, v, TransactionReason.PLUGIN_TAKE
makeEconomable(target), Economable.PLUGIN, amount, TransactionReason.PLUGIN_TAKE
));
}
@Override
public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, double v) {
if (v == 0) {
return new EconomyResponse(v, getBalance(offlinePlayer), EconomyResponse.ResponseType.SUCCESS, "");
public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, double amount) {
if (amount == 0) {
return new EconomyResponse(amount, getBalance(offlinePlayer), EconomyResponse.ResponseType.SUCCESS, "");
}
if (!has(offlinePlayer, v)) {
return new EconomyResponse(v, getBalance(offlinePlayer), EconomyResponse.ResponseType.FAILURE, "Insufficient funds.");
if (!has(offlinePlayer, amount)) {
return new EconomyResponse(amount, getBalance(offlinePlayer), EconomyResponse.ResponseType.FAILURE, "Insufficient funds.");
}
return transact(new Transaction(
Economable.wrap(offlinePlayer), Economable.PLUGIN, v, TransactionReason.PLUGIN_TAKE
Economable.wrap(offlinePlayer), Economable.PLUGIN, amount, TransactionReason.PLUGIN_TAKE
));
}
@ -179,20 +150,13 @@ public class EconomySaneEconomy implements Economy {
}
@Override
public EconomyResponse depositPlayer(String playerName, double v) {
if (v == 0) {
return new EconomyResponse(v, getBalance(playerName), EconomyResponse.ResponseType.SUCCESS, "");
}
Economable economable;
if (validatePlayer(playerName)) {
economable = Economable.wrap(PlayerUtils.getOfflinePlayer(playerName));
} else {
economable = Economable.wrap(playerName);
public EconomyResponse depositPlayer(String target, double amount) {
if (amount == 0) {
return new EconomyResponse(amount, getBalance(target), EconomyResponse.ResponseType.SUCCESS, "");
}
return transact(new Transaction(
Economable.PLUGIN, economable, v, TransactionReason.PLUGIN_GIVE
Economable.PLUGIN, makeEconomable(target), amount, TransactionReason.PLUGIN_GIVE
));
}
@ -301,6 +265,18 @@ public class EconomySaneEconomy implements Economy {
return PlayerUtils.getOfflinePlayer(playerName) != null;
}
private Economable makeEconomable(String input) {
if (input.equals(SaneEconomy.getInstance().getEconomyManager().getServerAccountName())) {
return Economable.CONSOLE;
}
if (validatePlayer(input)) {
return Economable.wrap(PlayerUtils.getOfflinePlayer(input));
}
return Economable.wrap(input);
}
private EconomyResponse transact(Transaction transaction) {
TransactionResult result = SaneEconomy.getInstance().getEconomyManager().transact(transaction);

View File

@ -15,5 +15,6 @@ chat:
economy:
start-balance: 0.0
notify-start-balance: true
server-account: '$SERVER$'
debug: false

View File

@ -362,7 +362,7 @@ sell:
gain: 64
- item: WOOL:5
limit: 1600
Gain: 64
gain: 64
- item: WOOL:13
limit: 1600
gain: 64
@ -438,7 +438,7 @@ sell:
- item: STAINED_CLAY:0
limit: 1600
gain: 80
- item: STAINED_CLAY8:
- item: STAINED_CLAY:8
limit: 1600
gain: 80
- item: STAINED_CLAY:7