From 404e049c31df6d4278ee3ed46e7f0b8e2f92ee16 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Thu, 14 Jan 2016 08:06:27 -0500 Subject: [PATCH] Give item fix for offline players --- .../core/account/CoreClientManager.java | 5 ++++ .../account/repository/AccountRepository.java | 10 +++++++ .../core/inventory/InventoryManager.java | 28 +++++++++++++------ .../inventory/command/GiveItemCommand.java | 14 ++++++++-- 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/account/CoreClientManager.java b/Plugins/Mineplex.Core/src/mineplex/core/account/CoreClientManager.java index d92f4e5b7..2d92b75aa 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/account/CoreClientManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/account/CoreClientManager.java @@ -213,6 +213,11 @@ public class CoreClientManager extends MiniPlugin _clientsConnecting.decrementAndGet(); } } + + public void loadAccountIdFromUUID(UUID uuid, Callback callback) + { + _repository.getAccountId(uuid, callback); + } public void loadClientByName(final String playerName, final Runnable runnable) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java index d11b9a081..658c984ed 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/account/repository/AccountRepository.java @@ -41,6 +41,7 @@ public class AccountRepository extends RepositoryBase private static String UPDATE_ACCOUNT_NULL_RANK = "UPDATE accounts SET rank=?, donorRank=?, rankPerm=?, rankExpire=? WHERE uuid = ? AND rank IS NULL;"; private static String SELECT_ACCOUNT_UUID_BY_NAME = "SELECT uuid FROM accounts WHERE name = ? ORDER BY lastLogin DESC;"; + private static String SELECT_ACCOUNT_ID_BY_UUID = "SELECT id FROM accounts WHERE accounts.uuid = ? LIMIT 1"; private String _webAddress; @@ -160,6 +161,15 @@ public class AccountRepository extends RepositoryBase return accountId; } + + public void getAccountId(UUID uuid, Callback callback) + { + executeQuery(SELECT_ACCOUNT_ID_BY_UUID, resultSet -> { + int accountId = -1; + while (resultSet.next()) accountId = resultSet.getInt(1); + callback.run(accountId); + }, new ColumnVarChar("uuid", 100, uuid.toString())); + } public String GetClient(String name, UUID uuid, String ipAddress) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/inventory/InventoryManager.java b/Plugins/Mineplex.Core/src/mineplex/core/inventory/InventoryManager.java index a10dd5e73..4d3431275 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/inventory/InventoryManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/inventory/InventoryManager.java @@ -23,6 +23,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; import mineplex.cache.player.PlayerCache; +import mineplex.cache.player.PlayerInfo; import mineplex.core.MiniDbClientPlugin; import mineplex.core.account.CoreClientManager; import mineplex.core.common.util.Callback; @@ -138,17 +139,28 @@ public class InventoryManager extends MiniDbClientPlugin { public void run() { - try + PlayerInfo playerInfo = PlayerCache.getInstance().getPlayer(uuid); + if (playerInfo != null) { - addItemToInventoryForOffline(callback, PlayerCache.getInstance().getPlayer(uuid).getAccountId(), item, count); + addItemToInventoryForOffline(callback, playerInfo.getAccountId(), item, count); } - catch (Exception e) + else { - e.printStackTrace(); - - System.out.println("-----DEBUG-----"); - System.out.println("PlayerCache Instance: " + (PlayerCache.getInstance() == null)); - System.out.println("PlayerCache Player Object: " + (PlayerCache.getInstance().getPlayer(uuid) == null)); + ClientManager.loadAccountIdFromUUID(uuid, new Callback() + { + @Override + public void run(Integer id) + { + if (id > 0) + { + addItemToInventoryForOffline(callback, id, item, count); + } + else + { + runSync(() -> callback.run(false)); + } + } + }); } } }); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/inventory/command/GiveItemCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/inventory/command/GiveItemCommand.java index ce6804e86..94e726ad8 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/inventory/command/GiveItemCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/inventory/command/GiveItemCommand.java @@ -55,19 +55,27 @@ public class GiveItemCommand extends CommandBase } else { - Plugin.getClientManager().loadClientByName(playerName, new Runnable() + Plugin.runAsync(new Runnable() { + @Override public void run() { UUID uuid = Plugin.getClientManager().loadUUIDFromDB(playerName); - + if (uuid != null) { Plugin.addItemToInventoryForOffline(new Callback() { public void run (Boolean success) { - UtilPlayer.message(caller, F.main("Item", "You gave " + F.elem(amount + " " + itemName) + " to offline player " + F.name(playerName))); + if (success) + { + UtilPlayer.message(caller, F.main("Item", "You gave " + F.elem(amount + " " + itemName) + " to offline player " + F.name(playerName))); + } + else + { + UtilPlayer.message(caller, F.main("Item", "An error occured while trying to give item to " + F.name(playerName) + "!")); + } } }, uuid, item.Name, amount); }