From 854c2f5f10fd7616ea651fda85cc55f688dfd34f Mon Sep 17 00:00:00 2001 From: AppleDash Date: Sun, 2 Feb 2020 08:53:31 -0500 Subject: [PATCH] Experimental baltop fixes and actually use the value of the last name. --- SaneEconomyCore/pom.xml | 2 +- .../command/BalanceTopCommand.java | 10 ++++++-- .../backend/EconomyStorageBackend.java | 8 ++++++ .../type/EconomyStorageBackendCaching.java | 5 ++++ .../type/EconomyStorageBackendJSON.java | 1 + .../type/EconomyStorageBackendMySQL.java | 2 ++ .../listeners/JoinQuitListener.java | 2 +- SaneEconomyMobKills/pom.xml | 2 +- .../listeners/EntityDamageListener.java | 5 ++-- SaneEconomyOnlineTime/pom.xml | 2 +- .../saneeconomy/onlinetime/Payout.java | 7 +++--- .../onlinetime/SaneEconomyOnlineTime.java | 9 ++++--- SaneEconomySignShop/pom.xml | 2 +- .../listeners/InteractListener.java | 3 ++- .../signshop/ShopTransaction.java | 8 +++--- .../signshop/SignShop.java | 25 ++++++++++--------- 16 files changed, 61 insertions(+), 32 deletions(-) diff --git a/SaneEconomyCore/pom.xml b/SaneEconomyCore/pom.xml index 93dbcf9..866f379 100644 --- a/SaneEconomyCore/pom.xml +++ b/SaneEconomyCore/pom.xml @@ -9,7 +9,7 @@ 0 SaneEconomyCore - 0.17.0-SNAPSHOT + 0.17.1-SNAPSHOT diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/BalanceTopCommand.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/BalanceTopCommand.java index 93192c1..a17ba35 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/BalanceTopCommand.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/command/BalanceTopCommand.java @@ -48,7 +48,7 @@ public class BalanceTopCommand extends SaneCommand { try { page = Math.abs(Integer.parseInt(args[0])); } catch (NumberFormatException e) { - this.saneEconomy.getMessenger().sendMessage(sender, "{1} is not a valid number."); + this.saneEconomy.getMessenger().sendMessage(sender, "{1} is not a valid number.", args[0]); return; } } @@ -65,6 +65,12 @@ public class BalanceTopCommand extends SaneCommand { AtomicInteger index = new AtomicInteger(offset + 1); /* I know it's stupid, but you can't do some_int++ from within the lambda. */ this.saneEconomy.getMessenger().sendMessage(sender, "Top {1} players on page {2}:", topBalances.size(), page); - topBalances.forEach((player, balance) -> this.saneEconomy.getMessenger().sendMessage(sender, "[{1:02d}] {2} - {3}", index.getAndIncrement(), player == null ? "" : player, this.saneEconomy.getEconomyManager().getCurrency().formatAmount(balance))); + + topBalances.forEach((player, balance) -> + this.saneEconomy.getMessenger().sendMessage(sender, "[{1:02d}] {2} - {3}", + index.getAndIncrement(), + player == null ? "" : player, + this.saneEconomy.getEconomyManager().getCurrency().formatAmount(balance)) + ); } } diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/EconomyStorageBackend.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/EconomyStorageBackend.java index 90d371a..8bc4ead 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/EconomyStorageBackend.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/EconomyStorageBackend.java @@ -67,6 +67,14 @@ public interface EconomyStorageBackend { */ void waitUntilFlushed(); + /** + * Get the last name associated with a unique ID. + * + * @param uuid Unique ID. + * @return Last name, or null if none. + */ + String getLastName(String uuid); + enum EconomableReloadReason { CROSS_SERVER_SYNC, PLAYER_JOIN } diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendCaching.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendCaching.java index 9cf17bc..6899f70 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendCaching.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendCaching.java @@ -64,4 +64,9 @@ public abstract class EconomyStorageBackendCaching implements EconomyStorageBack this.reloadDatabase(); } + + @Override + public String getLastName(String uuid) { + return this.uuidToName.get(uuid); + } } diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendJSON.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendJSON.java index 2eb52c6..d85cfc6 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendJSON.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendJSON.java @@ -25,6 +25,7 @@ public class EconomyStorageBackendJSON extends EconomyStorageBackendCaching { @Override public void setBalance(Economable economable, BigDecimal newBalance) { this.balances.put(economable.getUniqueIdentifier(), newBalance); + this.uuidToName.put(economable.getUniqueIdentifier(), economable.getName()); this.saveDatabase(); } diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendMySQL.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendMySQL.java index 777687d..78f252e 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendMySQL.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendMySQL.java @@ -105,6 +105,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching { while (rs.next()) { this.balances.put(rs.getString("unique_identifier"), new BigDecimal(rs.getString("balance"))); + this.uuidToName.put(rs.getString("unique_identifier"), rs.getString("last_name")); } } catch (SQLException e) { throw new RuntimeException("Failed to reload data from SQL.", e); @@ -115,6 +116,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching { public void setBalance(Economable economable, BigDecimal newBalance) { BigDecimal oldBalance = this.getBalance(economable); this.balances.put(economable.getUniqueIdentifier(), newBalance); + this.uuidToName.put(economable.getUniqueIdentifier(), economable.getName()); this.dbConn.executeAsyncOperation("set_balance_" + economable.getUniqueIdentifier(), (conn) -> { try { diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/listeners/JoinQuitListener.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/listeners/JoinQuitListener.java index e417c06..1ec9041 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/listeners/JoinQuitListener.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/listeners/JoinQuitListener.java @@ -31,7 +31,7 @@ public class JoinQuitListener implements Listener { public void onPlayerJoin(PlayerJoinEvent evt) { Player player = evt.getPlayer(); Economable economable = Economable.wrap((OfflinePlayer) player); - BigDecimal startBalance = new BigDecimal(this.plugin.getConfig().getDouble("economy.start-balance", 0.0D)); + BigDecimal startBalance = BigDecimal.valueOf(this.plugin.getConfig().getDouble("economy.start-balance", 0.0D)); /* A starting balance is configured AND they haven't been given it yet. */ if ((startBalance.compareTo(BigDecimal.ZERO) > 0) && !this.plugin.getEconomyManager().accountExists(economable)) { diff --git a/SaneEconomyMobKills/pom.xml b/SaneEconomyMobKills/pom.xml index c5cd33e..15d7502 100644 --- a/SaneEconomyMobKills/pom.xml +++ b/SaneEconomyMobKills/pom.xml @@ -16,7 +16,7 @@ org.appledash SaneEconomyCore - 0.17.0-SNAPSHOT + 0.17.1-SNAPSHOT diff --git a/SaneEconomyMobKills/src/main/java/org/appledash/saneeconomymobkills/listeners/EntityDamageListener.java b/SaneEconomyMobKills/src/main/java/org/appledash/saneeconomymobkills/listeners/EntityDamageListener.java index 5737cfc..22e8680 100644 --- a/SaneEconomyMobKills/src/main/java/org/appledash/saneeconomymobkills/listeners/EntityDamageListener.java +++ b/SaneEconomyMobKills/src/main/java/org/appledash/saneeconomymobkills/listeners/EntityDamageListener.java @@ -12,6 +12,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDeathEvent; +import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -83,11 +84,11 @@ public class EntityDamageListener implements Listener { if (offlinePlayer.isOnline()) { Player player = Bukkit.getServer().getPlayer(offlinePlayer.getUniqueId()); - this.plugin.getMessenger().sendMessage(player, "You have been awarded {1} for doing {2:.2f}% of the damage required to kill that {3}!", this.plugin.getSaneEconomy().getEconomyManager().getCurrency().formatAmount(thisAmount), thisPercent, entity.getName()); + this.plugin.getMessenger().sendMessage(player, "You have been awarded {1} for doing {2:.2f}% of the damage required to kill that {3}!", this.plugin.getSaneEconomy().getEconomyManager().getCurrency().formatAmount(BigDecimal.valueOf(thisAmount)), thisPercent, entity.getName()); } this.plugin.getSaneEconomy().getEconomyManager().transact(new Transaction( - this.plugin.getSaneEconomy().getEconomyManager().getCurrency(), Economable.PLUGIN, Economable.wrap(offlinePlayer), thisAmount, TransactionReason.PLUGIN_GIVE + this.plugin.getSaneEconomy().getEconomyManager().getCurrency(), Economable.PLUGIN, Economable.wrap(offlinePlayer), BigDecimal.valueOf(thisAmount), TransactionReason.PLUGIN_GIVE )); } diff --git a/SaneEconomyOnlineTime/pom.xml b/SaneEconomyOnlineTime/pom.xml index aaa4c43..40bbb9f 100644 --- a/SaneEconomyOnlineTime/pom.xml +++ b/SaneEconomyOnlineTime/pom.xml @@ -17,7 +17,7 @@ org.appledash SaneEconomyCore - 0.17.0-SNAPSHOT + 0.17.1-SNAPSHOT diff --git a/SaneEconomyOnlineTime/src/main/java/org/appledash/saneeconomy/onlinetime/Payout.java b/SaneEconomyOnlineTime/src/main/java/org/appledash/saneeconomy/onlinetime/Payout.java index e7650d7..055df18 100644 --- a/SaneEconomyOnlineTime/src/main/java/org/appledash/saneeconomy/onlinetime/Payout.java +++ b/SaneEconomyOnlineTime/src/main/java/org/appledash/saneeconomy/onlinetime/Payout.java @@ -1,5 +1,6 @@ package org.appledash.saneeconomy.onlinetime; +import java.math.BigDecimal; import java.util.Map; /** @@ -8,14 +9,14 @@ import java.util.Map; */ public class Payout { private final int secondsInterval; - private final double amount; + private final BigDecimal amount; private final String message; private String permission; private final long reportInterval; public Payout(int secondsInterval, double amount, String message, long reportInterval) { this.secondsInterval = secondsInterval; - this.amount = amount; + this.amount = BigDecimal.valueOf(amount); this.message = message; this.reportInterval = reportInterval; } @@ -24,7 +25,7 @@ public class Payout { return this.secondsInterval; } - public double getAmount() { + public BigDecimal getAmount() { return this.amount; } diff --git a/SaneEconomyOnlineTime/src/main/java/org/appledash/saneeconomy/onlinetime/SaneEconomyOnlineTime.java b/SaneEconomyOnlineTime/src/main/java/org/appledash/saneeconomy/onlinetime/SaneEconomyOnlineTime.java index 8f87d32..7ff8239 100644 --- a/SaneEconomyOnlineTime/src/main/java/org/appledash/saneeconomy/onlinetime/SaneEconomyOnlineTime.java +++ b/SaneEconomyOnlineTime/src/main/java/org/appledash/saneeconomy/onlinetime/SaneEconomyOnlineTime.java @@ -10,6 +10,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; +import java.math.BigDecimal; import java.util.*; /** @@ -18,7 +19,7 @@ import java.util.*; */ public class SaneEconomyOnlineTime extends SanePlugin implements Listener { private final Map onlineSeconds = new HashMap<>(); - private final Map reportingAmounts = new HashMap<>(); + private final Map reportingAmounts = new HashMap<>(); private final List payouts = new ArrayList<>(); private SaneEconomy saneEconomy; @@ -47,7 +48,7 @@ public class SaneEconomyOnlineTime extends SanePlugin implements Listener { if ((onlineSeconds % payout.getSecondsInterval()) == 0) { if (this.reportingAmounts.containsKey(player.getUniqueId())) { - this.reportingAmounts.put(player.getUniqueId(), this.reportingAmounts.get(player.getUniqueId()) + payout.getAmount()); + this.reportingAmounts.put(player.getUniqueId(), this.reportingAmounts.get(player.getUniqueId()).add(payout.getAmount())); } else { this.reportingAmounts.put(player.getUniqueId(), payout.getAmount()); } @@ -56,8 +57,8 @@ public class SaneEconomyOnlineTime extends SanePlugin implements Listener { } if ((onlineSeconds % payout.getReportInterval()) == 0) { - this.getMessenger().sendMessage(player, payout.getMessage(), this.saneEconomy.getEconomyManager().getCurrency().formatAmount(this.reportingAmounts.getOrDefault(player.getUniqueId(), 0.0D)), payout.getReportInterval()); - this.reportingAmounts.put(player.getUniqueId(), 0.0D); + this.getMessenger().sendMessage(player, payout.getMessage(), this.saneEconomy.getEconomyManager().getCurrency().formatAmount(this.reportingAmounts.getOrDefault(player.getUniqueId(), BigDecimal.ZERO)), payout.getReportInterval()); + this.reportingAmounts.put(player.getUniqueId(), BigDecimal.ZERO); } } diff --git a/SaneEconomySignShop/pom.xml b/SaneEconomySignShop/pom.xml index cd308f3..aef295c 100644 --- a/SaneEconomySignShop/pom.xml +++ b/SaneEconomySignShop/pom.xml @@ -16,7 +16,7 @@ org.appledash SaneEconomyCore - 0.17.0-SNAPSHOT + 0.17.1-SNAPSHOT diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/InteractListener.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/InteractListener.java index 911ddc1..d4c44a9 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/InteractListener.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/InteractListener.java @@ -16,6 +16,7 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; +import java.math.BigDecimal; import java.util.Optional; import java.util.logging.Logger; @@ -114,7 +115,7 @@ public class InteractListener implements Listener { private void doSell(SignShop shop, Player player) { // TODO: Selling enchanted items EconomyManager ecoMan = this.plugin.getSaneEconomy().getEconomyManager(); int quantity = player.isSneaking() ? 1 : shop.getQuantity(); - double price = shop.getSellPrice(quantity); + BigDecimal price = shop.getSellPrice(quantity); if (!player.getInventory().containsAtLeast(new ItemStack(shop.getItemStack()), quantity)) { this.plugin.getMessenger().sendMessage(player, "You do not have {1} {2}!", quantity, shop.getItemStack().getType().name()); diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/ShopTransaction.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/ShopTransaction.java index 5b29f56..c4d942d 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/ShopTransaction.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/ShopTransaction.java @@ -7,6 +7,8 @@ import org.appledash.saneeconomy.economy.transaction.TransactionReason; import org.appledash.saneeconomysignshop.util.ItemInfo; import org.bukkit.entity.Player; +import java.math.BigDecimal; + /** * Created by appledash on 1/1/17. * Blackjack is still best pony. @@ -18,9 +20,9 @@ public class ShopTransaction { private final Player player; private final ItemInfo item; private final int quantity; - private final double price; + private final BigDecimal price; - public ShopTransaction(Currency currency, TransactionDirection direction, Player player, ItemInfo item, int quantity, double price) { + public ShopTransaction(Currency currency, TransactionDirection direction, Player player, ItemInfo item, int quantity, BigDecimal price) { this.currency = currency; this.direction = direction; this.player = player; @@ -45,7 +47,7 @@ public class ShopTransaction { return this.quantity; } - public double getPrice() { + public BigDecimal getPrice() { return this.price; } diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java index 14b299a..eb2e011 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java @@ -9,6 +9,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import java.io.Serializable; +import java.math.BigDecimal; import java.util.UUID; /** @@ -20,8 +21,8 @@ public class SignShop implements Serializable { private final SerializableLocation location; private final ItemInfo item; private final int quantity; - private final double buyPrice; - private final double sellPrice; + private final BigDecimal buyPrice; + private final BigDecimal sellPrice; public SignShop(UUID ownerUuid, Location location, ItemStack item, int quantity, double buyPrice, double sellPrice) { if ((ownerUuid == null) || (location == null) || (item == null)) { @@ -36,8 +37,8 @@ public class SignShop implements Serializable { this.location = new SerializableLocation(location); this.item = new ItemInfo(item); this.quantity = quantity; - this.buyPrice = buyPrice; - this.sellPrice = sellPrice; + this.buyPrice = BigDecimal.valueOf(buyPrice); + this.sellPrice = BigDecimal.valueOf(sellPrice); } /** @@ -68,7 +69,7 @@ public class SignShop implements Serializable { * Get the price that the player can buy this item from the server for * @return Buy price for this.getQuantity() items */ - public double getBuyPrice() { + public BigDecimal getBuyPrice() { return this.buyPrice; } @@ -76,7 +77,7 @@ public class SignShop implements Serializable { * Get the price that the player can sell this item to the server for * @return Buy price for this.getQuantity() items */ - public double getSellPrice() { + public BigDecimal getSellPrice() { return this.sellPrice; } @@ -86,8 +87,8 @@ public class SignShop implements Serializable { * @param quantity Quantity of items to price * @return Price to buy that number of items at this shop */ - public double getBuyPrice(int quantity) { - return this.buyPrice * ((float)quantity / (float)this.quantity); // TODO: Is this okay? + public BigDecimal getBuyPrice(int quantity) { + return this.buyPrice.multiply(BigDecimal.valueOf((double) quantity / this.quantity)); // TODO: Is this okay? } /** @@ -96,8 +97,8 @@ public class SignShop implements Serializable { * @param quantity Quantity of items to price * @return Price to sell that number of items at this shop */ - public double getSellPrice(int quantity) { - return this.sellPrice * ((float)quantity / (float)this.quantity); // TODO: Is this okay? + public BigDecimal getSellPrice(int quantity) { + return this.sellPrice.multiply(BigDecimal.valueOf((double) quantity / this.quantity)); // TODO: Is this okay? } /** @@ -105,7 +106,7 @@ public class SignShop implements Serializable { * @return True if they can, false if they can't */ public boolean canBuy() { - return this.buyPrice >= 0; + return this.buyPrice.compareTo(BigDecimal.ZERO) >= 0; } /** @@ -113,7 +114,7 @@ public class SignShop implements Serializable { * @return True if they can, false if they can't */ public boolean canSell() { - return this.sellPrice >= 0; + return this.sellPrice.compareTo(BigDecimal.ZERO) >= 0; } /**