Skip to content
Permalink
Browse files

Refactor/economy (#3663)

* Mostly broken code

* Just messing with some ideas, have static EconomyManager and an interface. The latter is used for class specific items such as econ name, and the other is used for function that are class agnostic.

* This is a scrapped idea

* Refactored a bunch of code

* Made it so the account field is now implemented in most areas in towny.

* Code sucessfully compiles

* Added two new interfaces
  - Permissible
  - TownBlockOwner (A port from the original class)

*Since both of these interfaces are implemented, Resident and Town now both directly inherit from TownyObject.

* Fixed initialization bugs

* Nation now conforms to the Economy interface.

* Added javadoc

* Fixed compiler errors from nation transition.

* Changed Economy to EconomyHandler

* Restore legacy methods via deprecation

* Changed EconomyAccount.getEconomyName() to EconomyAccount.getName()

this limits verbosity and repetition in the method calls.

* Added deprecation documentation.

* Re-add support for economy logging.

* Cleaned up logging changes

* EconomyAccount now conforms to EconomyHandler

* Revert "Cleaned up logging changes"

* This reverts commit 3bf863a.

* Auto stash before revert of "Cleaned up logging changes"

* Fix econ logging (again)

* Fix econ logging (again)

* Fix NPE from null log values.

* remove debug code

* Remove unused imports and unused method.

* Create an alias for `TownyInviteSender` and `TownyInviteReceiver`

* Co-authored-by: LlmDl <LlmDlio@gmail.com>
  • Loading branch information
suneettipirneni committed Jan 29, 2020
1 parent 783952b commit 734a3f61682d06094a2825a947607cdb585055eb
Showing with 708 additions and 541 deletions.
  1. BIN .DS_Store
  2. +5 −7 src/com/palmergames/bukkit/towny/TownyFormatter.java
  3. +25 −20 src/com/palmergames/bukkit/towny/TownyLogger.java
  4. +2 −2 src/com/palmergames/bukkit/towny/TownyPlaceholderExpansion.java
  5. +7 −7 src/com/palmergames/bukkit/towny/command/NationCommand.java
  6. +2 −2 src/com/palmergames/bukkit/towny/command/PlotCommand.java
  7. +2 −2 src/com/palmergames/bukkit/towny/command/ResidentCommand.java
  8. +17 −15 src/com/palmergames/bukkit/towny/command/TownCommand.java
  9. +7 −7 src/com/palmergames/bukkit/towny/command/TownyCommand.java
  10. +17 −17 src/com/palmergames/bukkit/towny/db/TownyDatabaseHandler.java
  11. +2 −2 src/com/palmergames/bukkit/towny/event/NationPreTransactionEvent.java
  12. +2 −2 src/com/palmergames/bukkit/towny/event/TownPreTransactionEvent.java
  13. +45 −45 src/com/palmergames/bukkit/towny/listeners/TownyEntityMonitorListener.java
  14. +212 −201 src/com/palmergames/bukkit/towny/object/{TownyEconomyObject.java → EconomyAccount.java}
  15. +16 −0 src/com/palmergames/bukkit/towny/object/EconomyHandler.java
  16. +50 −21 src/com/palmergames/bukkit/towny/object/Nation.java
  17. +23 −0 src/com/palmergames/bukkit/towny/object/Permissible.java
  18. +79 −11 src/com/palmergames/bukkit/towny/object/Resident.java
  19. +76 −21 src/com/palmergames/bukkit/towny/object/Town.java
  20. +25 −66 src/com/palmergames/bukkit/towny/object/TownBlock.java
  21. +46 −51 src/com/palmergames/bukkit/towny/object/TownBlockOwner.java
  22. +6 −0 src/com/palmergames/bukkit/towny/object/TownyInviter.java
  23. +12 −12 src/com/palmergames/bukkit/towny/tasks/DailyTimerTask.java
  24. +2 −2 src/com/palmergames/bukkit/towny/tasks/OnPlayerLogin.java
  25. +8 −8 src/com/palmergames/bukkit/towny/tasks/PlotClaim.java
  26. +1 −1 src/com/palmergames/bukkit/towny/tasks/TeleportWarmupTimerTask.java
  27. +2 −2 src/com/palmergames/bukkit/towny/tasks/TownClaim.java
  28. +7 −7 src/com/palmergames/bukkit/towny/utils/SpawnUtil.java
  29. +1 −1 src/com/palmergames/bukkit/towny/war/eventwar/War.java
  30. +2 −2 src/com/palmergames/bukkit/towny/war/eventwar/WarSpoils.java
  31. +2 −2 src/com/palmergames/bukkit/towny/war/flagwar/TownyWar.java
  32. +5 −5 src/com/palmergames/bukkit/towny/war/flagwar/listeners/TownyWarCustomListener.java
BIN +6 KB .DS_Store
Binary file not shown.
@@ -9,7 +9,6 @@
import com.palmergames.bukkit.towny.object.ResidentList;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownBlock;
import com.palmergames.bukkit.towny.object.TownBlockOwner;
import com.palmergames.bukkit.towny.object.TownBlockType;
import com.palmergames.bukkit.towny.object.TownyObject;
import com.palmergames.bukkit.towny.object.TownyWorld;
@@ -113,7 +112,7 @@ public static String getTime() {
List<String> out = new ArrayList<String>();

try {
TownBlockOwner owner;
TownyObject owner;
Town town = townBlock.getTown();
TownyWorld world = townBlock.getWorld();

@@ -123,7 +122,6 @@ public static String getTime() {
owner = townBlock.getTown();
}



out.add(ChatTools.formatTitle(TownyFormatter.getFormattedName(owner) + ((BukkitTools.isOnline(owner.getName())) ? TownySettings.getLangString("online") : "")));
if (!townBlock.getType().equals(TownBlockType.RESIDENTIAL))
@@ -187,7 +185,7 @@ public static String getTime() {
// Bank: 534 coins
if (TownySettings.isUsingEconomy())
if (TownyEconomyHandler.isActive())
out.add(String.format(TownySettings.getLangString("status_bank"), resident.getHoldingFormattedBalance()));
out.add(String.format(TownySettings.getLangString("status_bank"), resident.getAccount().getHoldingFormattedBalance()));

// Town: Camelot
String line = TownySettings.getLangString("status_town");
@@ -397,7 +395,7 @@ public static String getTime() {
String bankString = "";
if (TownySettings.isUsingEconomy()) {
if (TownyEconomyHandler.isActive()) {
bankString = String.format(TownySettings.getLangString("status_bank"), town.getHoldingFormattedBalance());
bankString = String.format(TownySettings.getLangString("status_bank"), town.getAccount().getHoldingFormattedBalance());
if (town.hasUpkeep())
bankString += String.format(TownySettings.getLangString("status_bank_town2"), new BigDecimal(TownySettings.getTownUpkeepCost(town)).setScale(2, RoundingMode.HALF_UP).doubleValue());
if (TownySettings.getUpkeepPenalty() > 0 && town.isOverClaimed())
@@ -476,7 +474,7 @@ public static String getTime() {
String line = "";
if (TownySettings.isUsingEconomy())
if (TownyEconomyHandler.isActive()) {
line = String.format(TownySettings.getLangString("status_bank"), nation.getHoldingFormattedBalance());
line = String.format(TownySettings.getLangString("status_bank"), nation.getAccount().getHoldingFormattedBalance());

if (TownySettings.getNationUpkeepCost(nation) > 0)
line += String.format(TownySettings.getLangString("status_bank_town2"), TownySettings.getNationUpkeepCost(nation));
@@ -642,7 +640,7 @@ public static String getTime() {
out.add(TownySettings.getLangString("status_res_taxexempt"));
} else {
if (town.isTaxPercentage()) {
out.add(String.format(TownySettings.getLangString("status_res_tax"), resident.getHoldingBalance() * town.getTaxes() / 100));
out.add(String.format(TownySettings.getLangString("status_res_tax"), resident.getAccount().getHoldingBalance() * town.getTaxes() / 100));
} else {
out.add(String.format(TownySettings.getLangString("status_res_tax"), town.getTaxes()));

@@ -1,9 +1,6 @@
package com.palmergames.bukkit.towny;

import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyEconomyObject;
import com.palmergames.bukkit.towny.object.EconomyAccount;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -125,30 +122,38 @@ public void refreshDebugLogger() {
ctx.updateLoggers();
}

public void logMoneyTransaction(TownyEconomyObject a, double amount, TownyEconomyObject b, String reason) {
public void logMoneyTransaction(EconomyAccount a, double amount, EconomyAccount b, String reason) {

String sender;
String receiver;

if (a == null) {
sender = "None";
} else {
sender = a.getName();
}

if (b == null) {
receiver = "None";
} else {
receiver = b.getName();
}

if (reason == null) {
LOGGER_MONEY.info(String.format("%s,%s,%s,%s", "Unknown Reason", getObjectName(a), amount, getObjectName(b)));
LOGGER_MONEY.info(String.format("%s,%s,%s,%s", "Unknown Reason", sender, amount, receiver));
} else {
LOGGER_MONEY.info(String.format("%s,%s,%s,%s", reason, getObjectName(a), amount, getObjectName(b)));
LOGGER_MONEY.info(String.format("%s,%s,%s,%s", reason, sender, amount, receiver));
}
}

private String getObjectName(TownyEconomyObject obj) {
String type;
if (obj == null) {
type = "Server";
} else if (obj instanceof Resident) {
type = "Resident";
} else if (obj instanceof Town) {
type = "Town";
} else if (obj instanceof Nation) {
type = "Nation";
public void logMoneyTransaction(String a, double amount, String b, String reason) {
if (reason == null) {
LOGGER_MONEY.info(String.format("%s,%s,%s,%s", "Unknown Reason", a, amount, b));
} else {
type = "?";
LOGGER_MONEY.info(String.format("%s,%s,%s,%s", reason, a, amount, b));
}
return String.format("[%s] %s", type, obj != null ? obj.getName() : "");
}

public static TownyLogger getInstance() {
return instance;
}
@@ -146,13 +146,13 @@ public String onPlaceholderRequest(Player player, String identifier) {
return nation;
case "town_balance": // %townyadvanced_town_balance%
try {
balance = resident.getTown().getHoldingFormattedBalance();
balance = resident.getTown().getAccount().getHoldingFormattedBalance();
} catch (NotRegisteredException ignored) {
}
return balance;
case "nation_balance": // %townyadvanced_nation_balance%
try {
balance = resident.getTown().getNation().getHoldingFormattedBalance();
balance = resident.getTown().getNation().getAccount().getHoldingFormattedBalance();
} catch (NotRegisteredException ignored) {
}
return balance;
@@ -76,7 +76,7 @@
private static final Comparator<Nation> BY_NAME = (n1, n2) -> n1.getName().compareTo(n2.getName());
private static final Comparator<Nation> BY_BANK_BALANCE = (n1, n2) -> {
try {
return Double.compare(n2.getHoldingBalance(), n1.getHoldingBalance());
return Double.compare(n2.getAccount().getHoldingBalance(), n1.getAccount().getHoldingBalance());
} catch (EconomyException e) {
throw new RuntimeException("Failed to get balance. Aborting.");
}
@@ -874,7 +874,7 @@ private void nationDeposit(Player player, int amount) {

double bankcap = TownySettings.getNationBankCap();
if (bankcap > 0) {
if (amount + nation.getHoldingBalance() > bankcap)
if (amount + nation.getAccount().getHoldingBalance() > bankcap)
throw new TownyException(String.format(TownySettings.getLangString("msg_err_deposit_capped"), bankcap));
}

@@ -891,7 +891,7 @@ private void nationDeposit(Player player, int amount) {
return;
}

if (!resident.payTo(amount, nation, "Nation Deposit"))
if (!resident.getAccount().payTo(amount, nation, "Nation Deposit"))
throw new TownyException(TownySettings.getLangString("msg_insuf_funds"));

TownyMessaging.sendPrefixedNationMessage(nation, String.format(TownySettings.getLangString("msg_xx_deposited_xx"), resident.getName(), amount, "nation"));
@@ -1042,7 +1042,7 @@ public static void newNation(Player player, String name, String capitalName, boo
if ((filteredName == null) || universe.getDataSource().hasNation(filteredName))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_invalid_name"), name));

if (!noCharge && TownySettings.isUsingEconomy() && !town.pay(TownySettings.getNewNationPrice(), "New Nation Cost"))
if (!noCharge && TownySettings.isUsingEconomy() && !town.getAccount().pay(TownySettings.getNewNationPrice(), "New Nation Cost"))
throw new TownyException(String.format(TownySettings.getLangString("msg_no_funds_new_nation2"), TownySettings.getNewNationPrice()));

newNation(name, town);
@@ -1063,7 +1063,7 @@ public static Nation newNation(String name, Town town) throws AlreadyRegisteredE
nation.setRegistered(System.currentTimeMillis());
if (TownySettings.isUsingEconomy()) {
try {
nation.setBalance(0, "Deleting Nation");
nation.getAccount().setBalance(0, "Deleting Nation");
} catch (EconomyException e) {
e.printStackTrace();
}
@@ -2100,7 +2100,7 @@ else if (split[0].equalsIgnoreCase("taxes")) {

if(TownySettings.getNationRenameCost() > 0) {
try {
if (TownySettings.isUsingEconomy() && !nation.pay(TownySettings.getNationRenameCost(), String.format("Nation renamed to: %s", split[1])))
if (TownySettings.isUsingEconomy() && !nation.getAccount().pay(TownySettings.getNationRenameCost(), String.format("Nation renamed to: %s", split[1])))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_no_money"), TownyEconomyHandler.getFormattedBalance(TownySettings.getNationRenameCost())));
} catch (EconomyException e) {
throw new TownyException("Economy Error");
@@ -2264,7 +2264,7 @@ public static void nationToggle(Player player, String[] split, boolean admin, Na
boolean choice = !nation.isNeutral();
double cost = TownySettings.getNationNeutralityCost();

if (choice && TownySettings.isUsingEconomy() && !nation.pay(cost, "Peaceful Nation Cost"))
if (choice && TownySettings.isUsingEconomy() && !nation.getAccount().pay(cost, "Peaceful Nation Cost"))
throw new TownyException(TownySettings.getLangString("msg_nation_cant_peaceful"));

nation.setNeutral(choice);
@@ -195,7 +195,7 @@ public boolean parsePlotCommand(Player player, String[] split) throws TownyExcep
if (maxPlots >= 0 && resident.getTownBlocks().size() + selection.size() > maxPlots)
throw new TownyException(String.format(TownySettings.getLangString("msg_max_plot_own"), maxPlots));

if (TownySettings.isUsingEconomy() && (!resident.canPayFromHoldings(cost)))
if (TownySettings.isUsingEconomy() && (!resident.getAccount().canPayFromHoldings(cost)))
throw new TownyException(String.format(TownySettings.getLangString("msg_no_funds_claim"), selection.size(), TownyEconomyHandler.getFormattedBalance(cost)));

// Start the claim task
@@ -519,7 +519,7 @@ public boolean parsePlotCommand(Player player, String[] split) throws TownyExcep
Coord key = Coord.parseCoord(plugin.getCache(player).getLastLocation());

if (OutpostUtil.OutpostTests(town, resident, townyWorld, key, isAdmin, true)) {
if (TownySettings.isUsingEconomy() && !town.pay(TownySettings.getOutpostCost(), String.format("Plot Set Outpost")))
if (TownySettings.isUsingEconomy() && !town.getAccount().pay(TownySettings.getOutpostCost(), String.format("Plot Set Outpost")))
throw new TownyException(TownySettings.getLangString("msg_err_cannot_afford_to_set_outpost"));

TownyMessaging.sendMessage(player, String.format(TownySettings.getLangString("msg_plot_set_cost"), TownyEconomyHandler.getFormattedBalance(TownySettings.getOutpostCost()), TownySettings.getLangString("outpost")));
@@ -173,9 +173,9 @@ public void parseResidentCommand(final Player player, String[] split) {

if (split[1].equalsIgnoreCase("paybail")) {
Resident resident = townyUniverse.getDataSource().getResident(player.getName());
if (resident.canPayFromHoldings(TownySettings.getBailAmount())) {
if (resident.getAccount().canPayFromHoldings(TownySettings.getBailAmount())) {
Town JailTown = townyUniverse.getDataSource().getTown(resident.getJailTown());
resident.payTo(TownySettings.getBailAmount(), JailTown, "Bail");
resident.getAccount().payTo(TownySettings.getBailAmount(), JailTown, "Bail");
resident.setJailed(false);
resident.setJailSpawn(0);
resident.setJailTown("");
@@ -92,7 +92,7 @@
private static final Comparator<Town> BY_NAME = (t1, t2) -> t1.getName().compareTo(t2.getName());
private static final Comparator<Town> BY_BANK_BALANCE = (t1, t2) -> {
try {
return Double.compare(t2.getHoldingBalance(), t1.getHoldingBalance());
return Double.compare(t2.getAccount().getHoldingBalance(), t1.getAccount().getHoldingBalance());
} catch (EconomyException e) {
throw new RuntimeException("Failed to get balance. Aborting.");
}
@@ -1792,7 +1792,7 @@ public static void townSet(Player player, String[] split, boolean admin, Town to

if(TownySettings.getTownRenameCost() > 0) {
try {
if (TownySettings.isUsingEconomy() && !town.pay(TownySettings.getTownRenameCost(), String.format("Town renamed to: %s", split[1])))
if (TownySettings.isUsingEconomy() && !town.getAccount().pay(TownySettings.getTownRenameCost(), String.format("Town renamed to: %s", split[1])))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_no_money"), TownyEconomyHandler.getFormattedBalance(TownySettings.getTownRenameCost())));
} catch (EconomyException e) {
throw new TownyException("Economy Error");
@@ -2016,7 +2016,7 @@ public static int townBuyBonusTownBlocks(Town town, int inputN, Object player) t
return n;
double cost = town.getBonusBlockCostN(n);
try {
boolean pay = town.pay(cost, String.format("Town Buy Bonus (%d)", n));
boolean pay = town.getAccount().pay(cost, String.format("Town Buy Bonus (%d)", n));
if (TownySettings.isUsingEconomy() && !pay) {
throw new TownyException(String.format(TownySettings.getLangString("msg_no_funds_to_buy"), n, "bonus town blocks", TownyEconomyHandler.getFormattedBalance(cost)));
} else if (TownySettings.isUsingEconomy() && pay) {
@@ -2095,16 +2095,18 @@ public static void newTown(Player player, String name, String mayorName, boolean
if ((world.getMinDistanceFromOtherTowns(key) > TownySettings.getMaxDistanceBetweenHomeblocks()) && world.hasTowns())
throw new TownyException(TownySettings.getLangString("msg_too_far"));

if (!noCharge && TownySettings.isUsingEconomy() && !resident.pay(TownySettings.getNewTownPrice(), "New Town Cost"))
if (!noCharge && TownySettings.isUsingEconomy() && !resident.getAccount().pay(TownySettings.getNewTownPrice(), "New Town Cost"))
throw new TownyException(String.format(TownySettings.getLangString("msg_no_funds_new_town2"), (resident.getName().equals(player.getName()) ? "You" : resident.getName()), TownySettings.getNewTownPrice()));

newTown(world, name, resident, key, player.getLocation(), player);
TownyMessaging.sendGlobalMessage(TownySettings.getNewTownMsg(player.getName(), StringMgmt.remUnderscore(name)));
} catch (TownyException x) {
TownyMessaging.sendErrorMsg(player, x.getMessage());
// TODO: delete town data that might have been done
} catch (EconomyException x) {
TownyMessaging.sendErrorMsg(player, "No valid economy found, your server admin might need to install Vault.jar or set using_economy: false in the Towny config.yml");
} catch (Exception e) {
e.printStackTrace();
}
}

@@ -2145,7 +2147,7 @@ public static Town newTown(TownyWorld world, String name, Resident resident, Coo
TownyMessaging.sendDebugMsg("Creating new Town account: " + "town-" + name);
if (TownySettings.isUsingEconomy()) {
try {
town.setBalance(0, "Deleting Town");
town.getAccount().setBalance(0, "Deleting Town");
} catch (EconomyException e) {
e.printStackTrace();
}
@@ -2590,7 +2592,7 @@ public static void checkTownResidents(Town town, Resident removedResident) throw

if (TownySettings.isRefundNationDisbandLowResidents()) {
try {
town.pay(TownySettings.getNewNationPrice(), "nation refund");
town.getAccount().pay(TownySettings.getNewNationPrice(), "nation refund");
} catch (EconomyException e) {
e.printStackTrace();
}
@@ -3029,8 +3031,8 @@ else if (selection.size() == 1 && outpost)
else
blockCost = town.getTownBlockCostN(selection.size());

double missingAmount = blockCost - town.getHoldingBalance();
if (TownySettings.isUsingEconomy() && !town.pay(blockCost, String.format("Town Claim (%d)", selection.size())))
double missingAmount = blockCost - town.getAccount().getHoldingBalance();
if (TownySettings.isUsingEconomy() && !town.getAccount().pay(blockCost, String.format("Town Claim (%d)", selection.size())))
throw new TownyException(String.format(TownySettings.getLangString("msg_no_funds_claim2"), selection.size(), TownyEconomyHandler.getFormattedBalance(blockCost), TownyEconomyHandler.getFormattedBalance(missingAmount), new DecimalFormat("#").format(missingAmount)));
} catch (EconomyException e1) {
throw new TownyException("Economy Error");
@@ -3147,8 +3149,8 @@ public static void checkIfSelectionIsValid(TownBlockOwner owner, List<WorldCoord
else
blockCost = town.getTownBlockCostN(selection.size());

double missingAmount = blockCost - town.getHoldingBalance();
if (TownySettings.isUsingEconomy() && !owner.canPayFromHoldings(blockCost))
double missingAmount = blockCost - town.getAccount().getHoldingBalance();
if (TownySettings.isUsingEconomy() && !((Town) owner).getAccount().canPayFromHoldings(blockCost))
throw new TownyException(String.format(TownySettings.getLangString("msg_err_cant_afford_blocks2"), selection.size(), TownyEconomyHandler.getFormattedBalance(blockCost), TownyEconomyHandler.getFormattedBalance(missingAmount), new DecimalFormat("#").format(missingAmount)));
} catch (EconomyException e1) {
throw new TownyException("Economy Error");
@@ -3206,7 +3208,7 @@ private void townDeposit(Player player, int amount) {

double bankcap = TownySettings.getTownBankCap();
if (bankcap > 0) {
if (amount + town.getHoldingBalance() > bankcap)
if (amount + town.getAccount().getHoldingBalance() > bankcap)
throw new TownyException(String.format(TownySettings.getLangString("msg_err_deposit_capped"), bankcap));
}

@@ -3223,7 +3225,7 @@ private void townDeposit(Player player, int amount) {
return;
}

if (!resident.payTo(amount, town, "Town Deposit"))
if (!resident.getAccount().payTo(amount, town, "Town Deposit"))
throw new TownyException(TownySettings.getLangString("msg_insuf_funds"));

TownyMessaging.sendPrefixedTownMessage(town, String.format(TownySettings.getLangString("msg_xx_deposited_xx"), resident.getName(), amount, "town"));
@@ -3246,7 +3248,7 @@ public static void townDeposit(Player player, Town town, int amount) {

double bankcap = TownySettings.getTownBankCap();
if (bankcap > 0) {
if (amount + town.getHoldingBalance() > bankcap)
if (amount + town.getAccount().getHoldingBalance() > bankcap)
throw new TownyException(String.format(TownySettings.getLangString("msg_err_deposit_capped"), bankcap));
}

@@ -3263,7 +3265,7 @@ public static void townDeposit(Player player, Town town, int amount) {
return;
}

if (!resident.payTo(amount, town, "Town Deposit from Nation member"))
if (!resident.getAccount().payTo(amount, town, "Town Deposit from Nation member"))
throw new TownyException(TownySettings.getLangString("msg_insuf_funds"));

TownyMessaging.sendPrefixedNationMessage(resident.getTown().getNation(), String.format(TownySettings.getLangString("msg_xx_deposited_xx"), resident.getName(), amount, town + " town"));

0 comments on commit 734a3f6

Please sign in to comment.
You can’t perform that action at this time.