From 28f9f08987f37f177806620bd784e27b19bf0658 Mon Sep 17 00:00:00 2001 From: Andrzej Pomirski Date: Wed, 11 Mar 2015 18:02:30 +0100 Subject: [PATCH] Fix money being transferred to the old username --- .../Economy/Plugins/VaultListener.java | 16 ++-- .../Acrobot/ChestShop/UUIDs/NameManager.java | 86 +++++++++++++------ 2 files changed, 66 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/VaultListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/VaultListener.java index 22e3be4..e27a125 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/VaultListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Economy/Plugins/VaultListener.java @@ -61,7 +61,7 @@ public class VaultListener implements Listener { return; } - double balance = provider.getBalance(NameManager.getUsername(event.getAccount()), event.getWorld().getName()); + double balance = provider.getBalance(NameManager.getLastSeenName(event.getAccount()), event.getWorld().getName()); if (balance > Double.MAX_VALUE) { balance = Double.MAX_VALUE; @@ -78,7 +78,7 @@ public class VaultListener implements Listener { World world = event.getWorld(); - if (provider.has(NameManager.getUsername(event.getAccount()), world.getName(), event.getDoubleAmount())) { + if (provider.has(NameManager.getLastSeenName(event.getAccount()), world.getName(), event.getDoubleAmount())) { event.hasEnough(true); } } @@ -91,7 +91,7 @@ public class VaultListener implements Listener { World world = event.getWorld(); - if (!provider.hasAccount(NameManager.getUsername(event.getAccount()), world.getName())) { + if (!provider.hasAccount(NameManager.getLastSeenName(event.getAccount()), world.getName())) { event.hasAccount(false); } } @@ -115,7 +115,7 @@ public class VaultListener implements Listener { World world = event.getWorld(); - provider.depositPlayer(NameManager.getUsername(event.getTarget()), world.getName(), event.getDoubleAmount()); + provider.depositPlayer(NameManager.getLastSeenName(event.getTarget()), world.getName(), event.getDoubleAmount()); } @EventHandler @@ -126,7 +126,7 @@ public class VaultListener implements Listener { World world = event.getWorld(); - provider.withdrawPlayer(NameManager.getUsername(event.getTarget()), world.getName(), event.getDoubleAmount()); + provider.withdrawPlayer(NameManager.getLastSeenName(event.getTarget()), world.getName(), event.getDoubleAmount()); } @EventHandler @@ -152,18 +152,18 @@ public class VaultListener implements Listener { return; } - if (!provider.hasAccount(NameManager.getUsername(event.getAccount()), event.getWorld().getName())) { + if (!provider.hasAccount(NameManager.getLastSeenName(event.getAccount()), event.getWorld().getName())) { event.canHold(false); return; } - EconomyResponse response = provider.depositPlayer(NameManager.getUsername(event.getAccount()), event.getWorld().getName(), event.getDoubleAmount()); + EconomyResponse response = provider.depositPlayer(NameManager.getLastSeenName(event.getAccount()), event.getWorld().getName(), event.getDoubleAmount()); if (!response.transactionSuccess()) { event.canHold(false); return; } - provider.withdrawPlayer(NameManager.getUsername(event.getAccount()), event.getWorld().getName(), event.getDoubleAmount()); + provider.withdrawPlayer(NameManager.getLastSeenName(event.getAccount()), event.getWorld().getName(), event.getDoubleAmount()); } } diff --git a/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java b/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java index a0fa3b9..a32abfd 100644 --- a/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java +++ b/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java @@ -31,9 +31,37 @@ import java.util.UUID; public class NameManager { private static Dao accounts; + private static Map lastSeenName = new HashMap(); private static BiMap usernameToUUID = HashBiMap.create(); private static Map shortToLongName = new HashMap(); + public static String getLastSeenName(UUID uuid) { + if (lastSeenName.containsKey(uuid)) { + return lastSeenName.get(uuid); + } + + Account account = null; + + try { + account = accounts.queryBuilder().selectColumns("lastSeenName", "name").where().eq("uuid", uuid).queryForFirst(); + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + + if (account == null) { + return ""; + } + + if (account.getLastSeenName() != null) { + lastSeenName.put(uuid, account.getLastSeenName()); + } else if (account.getName() != null) { + lastSeenName.put(uuid, account.getName()); + } + + return account.getLastSeenName(); + } + public static UUID getUUID(String username) { if (usernameToUUID.containsKey(username)) { return usernameToUUID.get(username); @@ -51,10 +79,7 @@ public class NameManager { } if (account == null) { - UUID uuid = Bukkit.getOfflinePlayer(username).getUniqueId(); - usernameToUUID.put(username, uuid); - - return uuid; + return Bukkit.getOfflinePlayer(username).getUniqueId(); } UUID uuid = account.getUuid(); @@ -136,35 +161,40 @@ public class NameManager { public static void storeUsername(final Player player) { final UUID uuid = player.getUniqueId(); + Account account = null; + + try { + account = accounts.queryBuilder().where().eq("uuid", uuid).queryForFirst(); + } catch (SQLException e) { + e.printStackTrace(); + return; + } + + if (account != null) { + account.setLastSeenName(player.getName()); + + try { + accounts.createOrUpdate(account); + } catch (SQLException e) { + e.printStackTrace(); + } + + return; + } + + account = new Account(player.getName(), player.getUniqueId()); + if (!usernameToUUID.inverse().containsKey(uuid)) { usernameToUUID.inverse().put(uuid, player.getName()); } - Bukkit.getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() { - @Override - public void run() { - Account account = null; + lastSeenName.put(uuid, player.getName()); - try { - account = accounts.queryBuilder().selectColumns("name").where().eq("uuid", uuid).queryForFirst(); - } catch (SQLException e) { - e.printStackTrace(); - return; - } - - if (account != null) { - return; - } - - account = new Account(player.getName(), player.getUniqueId()); - - try { - accounts.createOrUpdate(account); - } catch (SQLException e) { - e.printStackTrace(); - } - } - }); + try { + accounts.createOrUpdate(account); + } catch (SQLException e) { + e.printStackTrace(); + } } public static void dropUsername(final Player player) {