Make econ work with Factions again

This commit is contained in:
Magnus Ulf 2019-01-22 16:11:09 +01:00
parent 2204cd2321
commit 4f27928e85
2 changed files with 42 additions and 13 deletions

View File

@ -25,7 +25,6 @@ public class Money
if (account == null) return null; if (account == null) return null;
// ... but if something is supplied we must manage to extract an id. // ... but if something is supplied we must manage to extract an id.
// NOTE: This ID is the name for now, later all money plugins will probably support UUIDs.
String ret = MUtil.extract(String.class, "accountId", account); String ret = MUtil.extract(String.class, "accountId", account);
if (ret == null) throw new IllegalArgumentException("extraction of accountId from object failed: " + account.toString()); if (ret == null) throw new IllegalArgumentException("extraction of accountId from object failed: " + account.toString());
return ret; return ret;

View File

@ -115,7 +115,13 @@ public class MoneyMixinVault extends MoneyMixinAbstract
@Override @Override
public boolean exists(String accountId) public boolean exists(String accountId)
{ {
return this.getEconomy().hasAccount(getOfflinePlayer(accountId)); Object econObject = getEconomyObject(accountId);
OfflinePlayer op = econObject instanceof OfflinePlayer ? (OfflinePlayer) econObject : null;
String name = econObject instanceof String ? (String) econObject : null;
if (op != null) return this.getEconomy().hasAccount(op);
if (name != null) return this.getEconomy().hasAccount(name);
throw new RuntimeException();
} }
@Override @Override
@ -132,14 +138,28 @@ public class MoneyMixinVault extends MoneyMixinAbstract
public double get(String accountId) public double get(String accountId)
{ {
this.ensureExists(accountId); this.ensureExists(accountId);
return this.getEconomy().getBalance(getOfflinePlayer(accountId));
Object econObject = getEconomyObject(accountId);
OfflinePlayer op = econObject instanceof OfflinePlayer ? (OfflinePlayer) econObject : null;
String name = econObject instanceof String ? (String) econObject : null;
if (op != null) return this.getEconomy().getBalance(op);
if (name != null) return this.getEconomy().getBalance(name);
throw new RuntimeException();
} }
@Override @Override
public boolean has(String accountId, double amount) public boolean has(String accountId, double amount)
{ {
this.ensureExists(accountId); this.ensureExists(accountId);
return this.getEconomy().has(getOfflinePlayer(accountId), amount);
Object econObject = getEconomyObject(accountId);
OfflinePlayer op = econObject instanceof OfflinePlayer ? (OfflinePlayer) econObject : null;
String name = econObject instanceof String ? (String) econObject : null;
if (op != null) return this.getEconomy().has(op, amount);
if (name != null) return this.getEconomy().has(name, amount);
throw new RuntimeException();
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -201,23 +221,33 @@ public class MoneyMixinVault extends MoneyMixinAbstract
{ {
Economy economy = this.getEconomy(); Economy economy = this.getEconomy();
if (economy.hasAccount(getOfflinePlayer(accountId))) return true; Object econObject = getEconomyObject(accountId);
OfflinePlayer op = econObject instanceof OfflinePlayer ? (OfflinePlayer) econObject : null;
String name = econObject instanceof String ? (String) econObject : null;
if (!economy.createPlayerAccount(getOfflinePlayer(accountId))) return false; if (op != null && economy.hasAccount(op)) return true;
if (name != null && economy.hasAccount(name)) return true;
if (op != null && !economy.createPlayerAccount(op)) return false;
if (name != null && !economy.createPlayerAccount(name)) return false;
if (MUtil.isUuid(accountId)) return true; if (MUtil.isUuid(accountId)) return true;
double balance = economy.getBalance(getOfflinePlayer(accountId)); // What is this for???
economy.withdrawPlayer(getOfflinePlayer(accountId), balance); //double balance = economy.getBalance(getOfflinePlayer(accountId));
//economy.withdrawPlayer(getOfflinePlayer(accountId), balance);
return true; return false;
} }
public static OfflinePlayer getOfflinePlayer(String accountId) public static Object getEconomyObject(String accountId)
{ {
// Because of Factions we have this crazy-workaround.
// Where offlineplayers will be used when possible
// but otherwise names will.
OfflinePlayer ret = IdUtil.getOfflinePlayer(accountId); OfflinePlayer ret = IdUtil.getOfflinePlayer(accountId);
if (ret == null) throw new RuntimeException("couldn't get offlineplayer for: " + accountId); if (ret != null) return ret;
return ret; else return accountId;
} }
} }