Use UUID in enjin commands instead of name

This commit is contained in:
cnr 2016-12-28 13:37:02 -07:00
parent 844052c0c6
commit 347cddccc0
2 changed files with 65 additions and 49 deletions

View File

@ -31,6 +31,7 @@ import com.google.common.collect.Sets;
import com.google.gson.Gson;
import mineplex.cache.player.PlayerCache;
import mineplex.cache.player.PlayerInfo;
import mineplex.core.MiniPlugin;
import mineplex.core.account.command.TestRank;
import mineplex.core.account.command.UpdateRank;
@ -260,6 +261,53 @@ public class CoreClientManager extends MiniPlugin
loadClientByName(playerName, client -> runnable.run());
}
public void loadClientByUUID(UUID uuid, Consumer<CoreClient> loadedClient)
{
runAsync(() ->
{
AtomicReference<CoreClient> loaded = new AtomicReference<>();
try
{
Gson gson = new Gson();
String response = _repository.getClientByUUID(uuid);
ClientToken token = gson.fromJson(response, ClientToken.class);
if (token.Name == null || token.Rank == null)
{
loaded.set(null);
return;
}
CoreClient client = Add(token.Name, uuid);
client.SetRank(Rank.valueOf(token.Rank), false);
client.setAccountId(_repository.login(_loginProcessors, uuid, client.getName()));
Bukkit.getServer().getPluginManager().callEvent(new ClientWebResponseEvent(response, uuid));
if (client.getAccountId() > 0)
{
PlayerInfo playerInfo = PlayerCache.getInstance().getPlayer(uuid);
if (playerInfo != null)
{
PlayerCache.getInstance().updateAccountId(uuid, client.getAccountId());
}
}
loaded.set(client);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
UtilTasks.onMainThread(() -> loadedClient.accept(loaded.get())).run();
}
});
}
public void loadClientByName(String playerName, Consumer<CoreClient> loadedClient)
{
runAsync(() ->

View File

@ -2,17 +2,13 @@ package mineplex.enjinTranslator;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.AbstractMap;
import java.util.Date;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import mineplex.core.MiniPlugin;
import mineplex.core.account.CoreClient;
@ -20,15 +16,12 @@ import mineplex.core.account.CoreClientManager;
import mineplex.core.common.Rank;
import mineplex.core.common.currency.GlobalCurrency;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.donation.DonationManager;
import mineplex.core.inventory.InventoryManager;
import mineplex.core.powerplayclub.PowerPlayClubRepository;
import mineplex.core.punish.Category;
import mineplex.core.punish.Punish;
import mineplex.core.server.util.TransactionResponse;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.enjinTranslator.purchase.PurchaseManager;
public class Enjin extends MiniPlugin implements CommandExecutor
@ -40,7 +33,6 @@ public class Enjin extends MiniPlugin implements CommandExecutor
private PowerPlayClubRepository _powerPlayClubRepository;
private Punish _punish;
private NautHashMap<String, Entry<UUID, Long>> _cachedUUIDs = new NautHashMap<String, Entry<UUID, Long>>();
private static Object _commandLock = new Object();
public long _lastPoll = System.currentTimeMillis() - 120000;
@ -63,21 +55,6 @@ public class Enjin extends MiniPlugin implements CommandExecutor
plugin.getCommand("pull").setExecutor(this);
}
@EventHandler
public void expireCachedUUIDs(UpdateEvent event)
{
if (event.getType() != UpdateType.MIN_01)
return;
for (Iterator<Entry<String, Entry<UUID, Long>>> iterator = _cachedUUIDs.entrySet().iterator(); iterator.hasNext(); )
{
Entry<String, Entry<UUID, Long>> entry = iterator.next();
if (System.currentTimeMillis() > entry.getValue().getValue())
iterator.remove();
}
}
@Override
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args)
{
@ -90,35 +67,26 @@ public class Enjin extends MiniPlugin implements CommandExecutor
if (label.equalsIgnoreCase("enjin_mineplex"))
{
final String name = args[1];
final UUID uuid;
try
{
uuid = UUID.fromString(args[1]);
}
catch (IllegalArgumentException e)
{
System.out.println("[" + _dateFormat.format(new Date()) + "] ERROR processing " + args[1] + "; invalid UUID.");
return true;
}
_clientManager.loadClientByName(name, client ->
_clientManager.loadClientByUUID(uuid, client ->
{
if (client == null)
{
System.out.println("[" + _dateFormat.format(new Date()) + "] ERROR processing " + name + ", isn't in our database.");
System.out.println("[" + _dateFormat.format(new Date()) + "] ERROR processing " + uuid + ", isn't in our database.");
}
else
{
UUID uuid = null;
if (_cachedUUIDs.containsKey(name))
uuid = _cachedUUIDs.get(name).getKey();
else
{
// Fails if not in DB and if duplicate.
uuid = _clientManager.loadUUIDFromDB(name);
}
if (uuid == null)
{
System.out.println("[" + _dateFormat.format(new Date()) + "] ERROR processing " + name + ", no UUID.");
return;
}
final UUID playerUUID = uuid;
_cachedUUIDs.put(name, new AbstractMap.SimpleEntry<UUID, Long>(playerUUID, System.currentTimeMillis() + 240000));
final String name = client.getName();
if (args[0].equalsIgnoreCase("chargeback"))
{
@ -129,15 +97,15 @@ public class Enjin extends MiniPlugin implements CommandExecutor
if (!checkForClansPurchase(args, name, client))
{
if (!checkForBoosterPurchase(args, name, playerUUID, client))
if (!checkForBoosterPurchase(args, name, uuid, client))
{
if (!checkForCoinPurchase(args, name, playerUUID, client))
if (!checkForCoinPurchase(args, name, uuid, client))
{
if (!checkForRankPurchase(args, name, playerUUID, client))
if (!checkForRankPurchase(args, name, uuid, client))
{
if (!checkForPurchase(args, name, client))
{
if (!checkForPowerPlayClub(args, name, playerUUID, client))
if (!checkForPowerPlayClub(args, name, uuid, client))
{
StringBuilder sb = new StringBuilder();