Fix errors when server economy account is not defined or invalid

This commit is contained in:
Phoenix616 2017-11-15 21:13:07 +01:00
parent 4075b2e38a
commit 29ef7f25aa
6 changed files with 43 additions and 54 deletions

View File

@ -7,6 +7,7 @@ import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyFormatEvent; import com.Acrobot.ChestShop.Events.Economy.CurrencyFormatEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent; import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign; import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
@ -18,12 +19,18 @@ import java.util.UUID;
* Economy management * Economy management
*/ */
public class Economy { public class Economy {
/**
* Get the name of the server conomy account
* @return The username of te server economy account
* @deprecated Use {@link NameManager#getServerEconomyAccount()} or {@link Properties#SERVER_ECONOMY_ACCOUNT}
*/
@Deprecated
public static String getServerAccountName() { public static String getServerAccountName() {
return Properties.SERVER_ECONOMY_ACCOUNT; return Properties.SERVER_ECONOMY_ACCOUNT;
} }
public static boolean isOwnerEconomicallyActive(Inventory inventory) { public static boolean isOwnerEconomicallyActive(Inventory inventory) {
return !ChestShopSign.isAdminShop(inventory) || !getServerAccountName().isEmpty(); return !ChestShopSign.isAdminShop(inventory) || NameManager.getServerEconomyAccount() != null;
} }
public static boolean add(UUID name, World world, double amount) { public static boolean add(UUID name, World world, double amount) {

View File

@ -11,8 +11,6 @@ import org.bukkit.event.Listener;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.UUID; import java.util.UUID;
import static com.Acrobot.ChestShop.Configuration.Properties.SERVER_ECONOMY_ACCOUNT;
/** /**
* @author Acrobot * @author Acrobot
*/ */
@ -22,20 +20,12 @@ public class ServerAccountCorrector implements Listener {
public static void onCurrencyAdd(CurrencyAddEvent event) { public static void onCurrencyAdd(CurrencyAddEvent event) {
UUID target = event.getTarget(); UUID target = event.getTarget();
if (!NameManager.isAdminShop(target) || SERVER_ECONOMY_ACCOUNT.equals(NameManager.getUsername(target))) { if (!NameManager.isAdminShop(target) || NameManager.isServerEconomyAccount(target)) {
return; return;
} }
if (SERVER_ECONOMY_ACCOUNT.isEmpty()) { Account account = NameManager.getServerEconomyAccount();
event.setAdded(true); target = account != null ? account.getUuid() : null;
return;
} else {
Account account = NameManager.getAccount(SERVER_ECONOMY_ACCOUNT);
if (account == null || account.getUuid() == null) {
return;
}
target = account.getUuid();
}
event.setAdded(true); event.setAdded(true);
if (target == null) { if (target == null) {
@ -50,21 +40,12 @@ public class ServerAccountCorrector implements Listener {
public static void onCurrencySubtract(CurrencySubtractEvent event) { public static void onCurrencySubtract(CurrencySubtractEvent event) {
UUID target = event.getTarget(); UUID target = event.getTarget();
if (!NameManager.isAdminShop(target) || SERVER_ECONOMY_ACCOUNT.equals(NameManager.getUsername(target))) { if (!NameManager.isAdminShop(target) || NameManager.isServerEconomyAccount(target)) {
return; return;
} }
if (SERVER_ECONOMY_ACCOUNT.isEmpty()) { Account account = NameManager.getServerEconomyAccount();
event.setSubtracted(true); target = account != null ? account.getUuid() : null;
return;
} else {
Account account = NameManager.getAccount(SERVER_ECONOMY_ACCOUNT);
if (account != null) {
target = account.getUuid();
} else {
target = null;
}
}
event.setSubtracted(true); event.setSubtracted(true);
if (target == null) { if (target == null) {
@ -79,20 +60,16 @@ public class ServerAccountCorrector implements Listener {
public static void onCurrencyCheck(CurrencyCheckEvent event) { public static void onCurrencyCheck(CurrencyCheckEvent event) {
UUID target = event.getAccount(); UUID target = event.getAccount();
if (!NameManager.isAdminShop(target) || SERVER_ECONOMY_ACCOUNT.equals(NameManager.getUsername(target))) { if (!NameManager.isAdminShop(target) || NameManager.isServerEconomyAccount(target)) {
return; return;
} }
if (SERVER_ECONOMY_ACCOUNT.isEmpty()) { Account account = NameManager.getServerEconomyAccount();
target = account != null ? account.getUuid() : null;
if (target == null) {
event.hasEnough(true); event.hasEnough(true);
return; return;
} else {
Account account = NameManager.getAccount(SERVER_ECONOMY_ACCOUNT);
if (account == null || account.getUuid() == null) {
event.hasEnough(true);
return;
}
target = account.getUuid();
} }
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(event.getAmount(), target, event.getWorld()); CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(event.getAmount(), target, event.getWorld());
@ -105,7 +82,7 @@ public class ServerAccountCorrector implements Listener {
public static void onCurrencyHoldCheck(CurrencyHoldEvent event) { public static void onCurrencyHoldCheck(CurrencyHoldEvent event) {
UUID target = event.getAccount(); UUID target = event.getAccount();
if (!NameManager.isAdminShop(target) || SERVER_ECONOMY_ACCOUNT.equals(NameManager.getUsername(target))) { if (!NameManager.isAdminShop(target) || NameManager.isServerEconomyAccount(target)) {
return; return;
} }
@ -121,15 +98,12 @@ public class ServerAccountCorrector implements Listener {
return; return;
} }
if (SERVER_ECONOMY_ACCOUNT.isEmpty()) { Account account = NameManager.getServerEconomyAccount();
target = account != null ? account.getUuid() : null;
if (target == null) {
event.setAmount(BigDecimal.valueOf(Double.MAX_VALUE)); event.setAmount(BigDecimal.valueOf(Double.MAX_VALUE));
return; return;
} else {
Account account = NameManager.getAccount(SERVER_ECONOMY_ACCOUNT);
if (account == null || account.getUuid() == null) {
return;
}
target = account.getUuid();
} }
CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(target, event.getWorld()); CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(target, event.getWorld());

View File

@ -33,7 +33,7 @@ public class TaxModule implements Listener {
UUID target = event.getTarget(); UUID target = event.getTarget();
if (Economy.getServerAccountName().equals(NameManager.getUsername(target))) { if (NameManager.isServerEconomyAccount(target)) {
return; return;
} }
@ -45,8 +45,11 @@ public class TaxModule implements Listener {
BigDecimal tax = getTax(event.getAmount(), taxAmount); BigDecimal tax = getTax(event.getAmount(), taxAmount);
if (!Economy.getServerAccountName().isEmpty()) { if (NameManager.getServerEconomyAccount() != null) {
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(tax, NameManager.getUUID(Economy.getServerAccountName()), event.getWorld()); CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(
tax,
NameManager.getServerEconomyAccount().getUuid(),
event.getWorld());
ChestShop.callEvent(currencyAddEvent); ChestShop.callEvent(currencyAddEvent);
} }

View File

@ -45,10 +45,10 @@ public class CreationFeeGetter implements Listener {
CurrencySubtractEvent subtractionEvent = new CurrencySubtractEvent(BigDecimal.valueOf(shopCreationPrice), player); CurrencySubtractEvent subtractionEvent = new CurrencySubtractEvent(BigDecimal.valueOf(shopCreationPrice), player);
ChestShop.callEvent(subtractionEvent); ChestShop.callEvent(subtractionEvent);
if (!Economy.getServerAccountName().isEmpty()) { if (NameManager.getServerEconomyAccount() != null) {
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent( CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(
BigDecimal.valueOf(shopCreationPrice), BigDecimal.valueOf(shopCreationPrice),
NameManager.getUUID(Economy.getServerAccountName()), NameManager.getServerEconomyAccount().getUuid(),
player.getWorld()); player.getWorld());
ChestShop.callEvent(currencyAddEvent); ChestShop.callEvent(currencyAddEvent);
} }

View File

@ -40,10 +40,10 @@ public class ShopRefundListener implements Listener {
CurrencyAddEvent currencyEvent = new CurrencyAddEvent(BigDecimal.valueOf(refundPrice), account.getUuid(), event.getSign().getWorld()); CurrencyAddEvent currencyEvent = new CurrencyAddEvent(BigDecimal.valueOf(refundPrice), account.getUuid(), event.getSign().getWorld());
ChestShop.callEvent(currencyEvent); ChestShop.callEvent(currencyEvent);
if (!Economy.getServerAccountName().isEmpty()) { if (NameManager.getServerEconomyAccount() != null) {
CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent( CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(
BigDecimal.valueOf(refundPrice), BigDecimal.valueOf(refundPrice),
NameManager.getUUID(Economy.getServerAccountName()), NameManager.getServerEconomyAccount().getUuid(),
event.getSign().getWorld()); event.getSign().getWorld());
ChestShop.callEvent(currencySubtractEvent); ChestShop.callEvent(currencySubtractEvent);
} }

View File

@ -285,6 +285,7 @@ public class NameManager {
if (!Properties.SERVER_ECONOMY_ACCOUNT.isEmpty()) { if (!Properties.SERVER_ECONOMY_ACCOUNT.isEmpty()) {
serverEconomyAccount = getAccount(Properties.SERVER_ECONOMY_ACCOUNT); serverEconomyAccount = getAccount(Properties.SERVER_ECONOMY_ACCOUNT);
if (serverEconomyAccount == null || serverEconomyAccount.getUuid() == null) { if (serverEconomyAccount == null || serverEconomyAccount.getUuid() == null) {
serverEconomyAccount = null;
ChestShop.getBukkitLogger().log(Level.WARNING, "Server economy account setting '" + Properties.SERVER_ECONOMY_ACCOUNT + "' doesn't seem to be the name of a known player! Please log in at least once in order for the server economy account to work."); ChestShop.getBukkitLogger().log(Level.WARNING, "Server economy account setting '" + Properties.SERVER_ECONOMY_ACCOUNT + "' doesn't seem to be the name of a known player! Please log in at least once in order for the server economy account to work.");
} }
} }
@ -293,6 +294,10 @@ public class NameManager {
} }
} }
public static Account getServerEconomyAccount() {
return serverEconomyAccount;
}
private static class SimpleLoadingCache<K, V> { private static class SimpleLoadingCache<K, V> {
private final LinkedHashMap<K, V> map; private final LinkedHashMap<K, V> map;