mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 22:16:22 +00:00
MojangProfileResolver changes & wiring
This commit is contained in:
3 files changed
+58
-79
No files matched your search
@@ -19,80 +19,54 @@
|
||||
*/
|
||||
package net.draycia.carbon.paper.users;
|
||||
|
||||
import com.destroystokyo.paper.profile.PlayerProfile;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import net.draycia.carbon.common.users.MojangProfileResolver;
|
||||
import net.draycia.carbon.common.users.ProfileResolver;
|
||||
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
// TODO caching impl using Mojang api. Need to account for rate limits as well
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class PaperProfileResolver implements ProfileResolver {
|
||||
|
||||
private final Server server;
|
||||
private final Logger logger;
|
||||
private final ExecutorService executor;
|
||||
private final ProfileResolver mojang;
|
||||
|
||||
@Inject
|
||||
private PaperProfileResolver(final Server server, final Logger logger) {
|
||||
private PaperProfileResolver(final Server server, final MojangProfileResolver mojang) {
|
||||
this.server = server;
|
||||
this.logger = logger;
|
||||
this.executor = Executors.newFixedThreadPool(2, ConcurrentUtil.carbonThreadFactory(logger, "PaperProfileResolver"));
|
||||
this.mojang = mojang;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<@Nullable UUID> resolveUUID(final String username) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
final @Nullable Player online = this.server.getPlayer(username);
|
||||
if (online != null) {
|
||||
return online.getUniqueId();
|
||||
}
|
||||
final PlayerProfile profile = this.server.createProfile(username);
|
||||
profile.complete(true);
|
||||
if (profile.isComplete()) {
|
||||
return profile.getId();
|
||||
}
|
||||
return null;
|
||||
}, this.executor).exceptionally(thr -> {
|
||||
this.logger.warn("Failed to resolve UUID for player with name {}", username, thr);
|
||||
return null;
|
||||
});
|
||||
final @Nullable Player online = this.server.getPlayer(username);
|
||||
if (online != null) {
|
||||
return CompletableFuture.completedFuture(online.getUniqueId());
|
||||
}
|
||||
|
||||
return this.mojang.resolveUUID(username);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<@Nullable String> resolveName(final UUID uuid) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
final @Nullable Player online = this.server.getPlayer(uuid);
|
||||
if (online != null) {
|
||||
return online.getName();
|
||||
}
|
||||
final PlayerProfile profile = this.server.createProfile(uuid);
|
||||
profile.complete(true);
|
||||
if (profile.isComplete()) {
|
||||
return profile.getName();
|
||||
}
|
||||
return null;
|
||||
}, this.executor).exceptionally(thr -> {
|
||||
this.logger.warn("Failed to resolve player name for uuid {}", uuid, thr);
|
||||
return null;
|
||||
});
|
||||
final @Nullable Player online = this.server.getPlayer(uuid);
|
||||
if (online != null) {
|
||||
return CompletableFuture.completedFuture(online.getName());
|
||||
}
|
||||
|
||||
return this.mojang.resolveName(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
ConcurrentUtil.shutdownExecutor(this.executor, TimeUnit.MILLISECONDS, 50);
|
||||
this.mojang.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user