Fix money being transferred to the old username

This commit is contained in:
Andrzej Pomirski 2015-03-11 18:02:30 +01:00
parent d25dbd5cce
commit 28f9f08987
2 changed files with 66 additions and 36 deletions

View File

@ -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());
}
}

View File

@ -31,9 +31,37 @@ import java.util.UUID;
public class NameManager {
private static Dao<Account, String> accounts;
private static Map<UUID, String> lastSeenName = new HashMap<UUID, String>();
private static BiMap<String, UUID> usernameToUUID = HashBiMap.create();
private static Map<String, String> shortToLongName = new HashMap<String, String>();
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) {