Give item fix for offline players

This commit is contained in:
Shaun Bennett 2016-01-14 08:06:27 -05:00
parent b1c6af4d1a
commit 404e049c31
4 changed files with 46 additions and 11 deletions

View File

@ -213,6 +213,11 @@ public class CoreClientManager extends MiniPlugin
_clientsConnecting.decrementAndGet();
}
}
public void loadAccountIdFromUUID(UUID uuid, Callback<Integer> callback)
{
_repository.getAccountId(uuid, callback);
}
public void loadClientByName(final String playerName, final Runnable runnable)
{

View File

@ -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<Integer> 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)
{

View File

@ -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<ClientInventory>
{
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<Integer>()
{
@Override
public void run(Integer id)
{
if (id > 0)
{
addItemToInventoryForOffline(callback, id, item, count);
}
else
{
runSync(() -> callback.run(false));
}
}
});
}
}
});

View File

@ -55,19 +55,27 @@ public class GiveItemCommand extends CommandBase<InventoryManager>
}
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<Boolean>()
{
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);
}