cat.nyaa.nyaacore.utils.OfflinePlayerUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rpgitems Show documentation
Show all versions of rpgitems Show documentation
The legacy RPGItems continues. (fork)
package cat.nyaa.nyaacore.utils;
import cat.nyaa.nyaacore.NyaaCoreLoader;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class OfflinePlayerUtils {
private static final String UNDASHED = "(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})";
private static final String DASHED = "$1-$2-$3-$4-$5";
private static final TypeToken>> typeTokenListMap =
new TypeToken<>() {
};
private static ConcurrentMap playerCache;
private static ConcurrentMap nameCache;
private OfflinePlayerUtils() {
}
public static void init() {
playerCache = Arrays.stream(Bukkit.getOfflinePlayers())
.filter(p -> p.getName() != null)
.collect(Collectors.toConcurrentMap(p -> p.getName().toLowerCase(Locale.ENGLISH), Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(OfflinePlayer::getLastPlayed))));
nameCache = playerCache.entrySet().stream().collect(
Collectors.toConcurrentMap(e -> e.getValue().getUniqueId(), Map.Entry::getKey));
}
public static OfflinePlayer lookupPlayer(String name) {
OfflinePlayer player = Bukkit.getPlayerExact(name);
if (player != null) return player;
return playerCache.get(name.toLowerCase(Locale.ENGLISH));
}
public static CompletableFuture> lookupPlayerNamesOnline(String... names) {
List nameList = new LinkedList<>(Arrays.asList(names));
BiMap ret = HashBiMap.create();
Iterator iterator = nameList.iterator();
while (iterator.hasNext()) {
String n = iterator.next();
OfflinePlayer player = playerCache.get(n.toLowerCase(Locale.ENGLISH));
if (player != null) {
iterator.remove();
ret.put(n, player.getUniqueId());
}
}
HttpClient client = HttpClient.newHttpClient();
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create("https://api.mojang.com/profiles/minecraft"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(List.of(names))))
.build();
return client.sendAsync(req, HttpResponse.BodyHandlers.ofString())
.thenApply((response) -> {
NyaaCoreLoader.getPlugin().getLogger().log(Level.FINER,
"request name -> uuid api " + response.statusCode());
if (response.statusCode() > 299 || response.body() == null) {
return HashBiMap.create();
}
List