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 ISaneEconomy saneEconomy;
private final Currency currency; private final Currency currency;
private final EconomyStorageBackend backend; 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.saneEconomy = saneEconomy;
this.currency = currency; this.currency = currency;
this.backend = backend; this.backend = backend;
this.serverAccountName = serverAccountName;
} }
/** /**
@ -196,4 +198,12 @@ public class EconomyManager {
public EconomyStorageBackend getBackend() { public EconomyStorageBackend getBackend() {
return backend; 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() { public boolean isSenderAffected() {
if (sender == Economable.CONSOLE) {
return false;
}
return (reason.getAffectedParties() == AffectedParties.SENDER) || (reason.getAffectedParties() == AffectedParties.BOTH); return (reason.getAffectedParties() == AffectedParties.SENDER) || (reason.getAffectedParties() == AffectedParties.BOTH);
} }
public boolean isReceiverAffected() { public boolean isReceiverAffected() {
if (receiver == Economable.CONSOLE) {
return false;
}
return (reason.getAffectedParties() == AffectedParties.RECEIVER) || (reason.getAffectedParties() == AffectedParties.BOTH); return (reason.getAffectedParties() == AffectedParties.RECEIVER) || (reason.getAffectedParties() == AffectedParties.BOTH);
} }
} }

View File

@ -65,7 +65,7 @@ public class SaneEconomyConfiguration {
rootConfig.set("old-backend", null); 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 @Override
public boolean hasAccount(String playerName) { public boolean hasAccount(String target) {
Economable economable; return SaneEconomy.getInstance().getEconomyManager().accountExists(makeEconomable(target));
if (validatePlayer(playerName)) {
economable = Economable.wrap(PlayerUtils.getOfflinePlayer(playerName));
} else {
economable = Economable.wrap(playerName);
}
return SaneEconomy.getInstance().getEconomyManager().accountExists(economable);
} }
@Override @Override
@ -72,8 +64,8 @@ public class EconomySaneEconomy implements Economy {
} }
@Override @Override
public boolean hasAccount(String playerName, String worldName) { public boolean hasAccount(String target, String worldName) {
return hasAccount(playerName); return hasAccount(target);
} }
@Override @Override
@ -82,15 +74,8 @@ public class EconomySaneEconomy implements Economy {
} }
@Override @Override
public double getBalance(String playerName) { public double getBalance(String target) {
Economable economable; return SaneEconomy.getInstance().getEconomyManager().getBalance(makeEconomable(target));
if (validatePlayer(playerName)) {
economable = Economable.wrap(PlayerUtils.getOfflinePlayer(playerName));
} else {
economable = Economable.wrap(playerName);
}
return SaneEconomy.getInstance().getEconomyManager().getBalance(economable);
} }
@Override @Override
@ -99,8 +84,8 @@ public class EconomySaneEconomy implements Economy {
} }
@Override @Override
public double getBalance(String playerName, String worldName) { public double getBalance(String target, String worldName) {
return getBalance(playerName); return getBalance(target);
} }
@Override @Override
@ -109,62 +94,48 @@ public class EconomySaneEconomy implements Economy {
} }
@Override @Override
public boolean has(String playerName, double v) { public boolean has(String target, double amount) {
Economable economable; return SaneEconomy.getInstance().getEconomyManager().hasBalance(makeEconomable(target), amount);
if (validatePlayer(playerName)) {
economable = Economable.wrap(PlayerUtils.getOfflinePlayer(playerName));
} else {
economable = Economable.wrap(playerName);
}
return SaneEconomy.getInstance().getEconomyManager().hasBalance(economable, v);
} }
@Override @Override
public boolean has(OfflinePlayer offlinePlayer, double v) { public boolean has(OfflinePlayer offlinePlayer, double amount) {
return SaneEconomy.getInstance().getEconomyManager().hasBalance(Economable.wrap(offlinePlayer), v); return SaneEconomy.getInstance().getEconomyManager().hasBalance(Economable.wrap(offlinePlayer), amount);
} }
@Override @Override
public boolean has(String playerName, String worldName, double v) { public boolean has(String target, String worldName, double amount) {
return has(playerName, v); return has(target, amount);
} }
@Override @Override
public boolean has(OfflinePlayer offlinePlayer, String worldName, double v) { public boolean has(OfflinePlayer offlinePlayer, String worldName, double amount) {
return has(offlinePlayer, v); return has(offlinePlayer, amount);
} }
@Override @Override
public EconomyResponse withdrawPlayer(String playerName, double v) { public EconomyResponse withdrawPlayer(String target, double amount) {
if (v == 0) { if (amount == 0) {
return new EconomyResponse(v, getBalance(playerName), EconomyResponse.ResponseType.SUCCESS, ""); return new EconomyResponse(amount, getBalance(target), EconomyResponse.ResponseType.SUCCESS, "");
}
Economable economable;
if (validatePlayer(playerName)) {
economable = Economable.wrap(PlayerUtils.getOfflinePlayer(playerName));
} else {
economable = Economable.wrap(playerName);
} }
return transact(new Transaction( return transact(new Transaction(
economable, Economable.PLUGIN, v, TransactionReason.PLUGIN_TAKE makeEconomable(target), Economable.PLUGIN, amount, TransactionReason.PLUGIN_TAKE
)); ));
} }
@Override @Override
public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, double v) { public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, double amount) {
if (v == 0) { if (amount == 0) {
return new EconomyResponse(v, getBalance(offlinePlayer), EconomyResponse.ResponseType.SUCCESS, ""); return new EconomyResponse(amount, getBalance(offlinePlayer), EconomyResponse.ResponseType.SUCCESS, "");
} }
if (!has(offlinePlayer, v)) { if (!has(offlinePlayer, amount)) {
return new EconomyResponse(v, getBalance(offlinePlayer), EconomyResponse.ResponseType.FAILURE, "Insufficient funds."); return new EconomyResponse(amount, getBalance(offlinePlayer), EconomyResponse.ResponseType.FAILURE, "Insufficient funds.");
} }
return transact(new Transaction( 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 @Override
public EconomyResponse depositPlayer(String playerName, double v) { public EconomyResponse depositPlayer(String target, double amount) {
if (v == 0) { if (amount == 0) {
return new EconomyResponse(v, getBalance(playerName), EconomyResponse.ResponseType.SUCCESS, ""); return new EconomyResponse(amount, getBalance(target), EconomyResponse.ResponseType.SUCCESS, "");
}
Economable economable;
if (validatePlayer(playerName)) {
economable = Economable.wrap(PlayerUtils.getOfflinePlayer(playerName));
} else {
economable = Economable.wrap(playerName);
} }
return transact(new Transaction( 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; 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) { private EconomyResponse transact(Transaction transaction) {
TransactionResult result = SaneEconomy.getInstance().getEconomyManager().transact(transaction); TransactionResult result = SaneEconomy.getInstance().getEconomyManager().transact(transaction);

View File

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

View File

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