mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 06:28:54 +00:00
Initial work on user rewrite
Only JSONUserManager and Paper platform work currently
This commit is contained in:
22 files changed
+456
-305
No files matched your search
@@ -25,7 +25,6 @@ import github.scarsz.discordsrv.DiscordSRV;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
@@ -37,6 +36,8 @@ import net.draycia.carbon.common.listeners.RadiusListener;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.messaging.MessagingManager;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.ProfileResolver;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
import net.draycia.carbon.common.util.CloudUtils;
|
||||
import net.draycia.carbon.common.util.ListenerUtils;
|
||||
import net.draycia.carbon.common.util.PlayerUtils;
|
||||
@@ -73,7 +74,7 @@ public final class CarbonChatPaper implements CarbonChat {
|
||||
private @MonotonicNonNull Logger logger;
|
||||
private @MonotonicNonNull Path dataDirectory;
|
||||
private @MonotonicNonNull Injector injector;
|
||||
private @MonotonicNonNull UserManager<CarbonPlayerCommon> userManager;
|
||||
private @MonotonicNonNull UserManagerInternal<CarbonPlayerCommon> userManager;
|
||||
private @MonotonicNonNull CarbonServer carbonServer;
|
||||
private @MonotonicNonNull CarbonMessages carbonMessages;
|
||||
private @MonotonicNonNull ChannelRegistry channelRegistry;
|
||||
@@ -100,7 +101,7 @@ public final class CarbonChatPaper implements CarbonChat {
|
||||
this.carbonMessages = carbonMessages;
|
||||
this.channelRegistry = channelRegistry;
|
||||
this.carbonServer = carbonServer;
|
||||
this.userManager = userManager;
|
||||
this.userManager = (UserManagerInternal<CarbonPlayerCommon>) userManager;
|
||||
this.dataDirectory = dataDirectory;
|
||||
this.packetService();
|
||||
}
|
||||
@@ -129,7 +130,7 @@ public final class CarbonChatPaper implements CarbonChat {
|
||||
final long saveDelay = 5 * 60 * 20;
|
||||
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(this.plugin,
|
||||
() -> PlayerUtils.saveLoggedInPlayers(this.carbonServer, this.userManager), saveDelay, saveDelay);
|
||||
() -> PlayerUtils.saveLoggedInPlayers(this.carbonServer, this.userManager, this.logger), saveDelay, saveDelay);
|
||||
|
||||
// Load channels
|
||||
((CarbonChannelRegistry) this.channelRegistry()).loadConfigChannels(this.carbonMessages);
|
||||
@@ -151,7 +152,8 @@ public final class CarbonChatPaper implements CarbonChat {
|
||||
}
|
||||
|
||||
void onDisable() {
|
||||
PlayerUtils.saveLoggedInPlayers(this.carbonServer, this.userManager).forEach(CompletableFuture::join);
|
||||
this.injector.getInstance(ProfileResolver.class).shutdown();
|
||||
this.userManager.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -37,10 +37,12 @@ import net.draycia.carbon.common.ForCarbon;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.users.ProfileResolver;
|
||||
import net.draycia.carbon.common.util.CloudUtils;
|
||||
import net.draycia.carbon.paper.command.PaperCommander;
|
||||
import net.draycia.carbon.paper.command.PaperPlayerCommander;
|
||||
import net.draycia.carbon.paper.messages.PaperMessageRenderer;
|
||||
import net.draycia.carbon.paper.users.PaperProfileResolver;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.message.IMessageRenderer;
|
||||
@@ -131,6 +133,7 @@ public final class CarbonChatPaperModule extends AbstractModule {
|
||||
this.bind(Path.class).annotatedWith(ForCarbon.class).toInstance(this.bootstrap.getDataFolder().toPath());
|
||||
this.bind(CarbonServer.class).to(CarbonServerPaper.class);
|
||||
this.bind(PlayerSuggestions.class).toInstance(new PlayerArgument.PlayerParser<Commander>()::suggestions);
|
||||
this.bind(ProfileResolver.class).to(PaperProfileResolver.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,20 +21,18 @@ package net.draycia.carbon.paper;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.ProfileResolver;
|
||||
import net.draycia.carbon.paper.users.CarbonPlayerPaper;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Server;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
@@ -46,11 +44,13 @@ public final class CarbonServerPaper implements CarbonServer, ForwardingAudience
|
||||
|
||||
private final Server server;
|
||||
private final UserManager<CarbonPlayerPaper> userManager;
|
||||
private final ProfileResolver profileResolver;
|
||||
|
||||
@Inject
|
||||
private CarbonServerPaper(final Server server, final UserManager<CarbonPlayerCommon> userManager) {
|
||||
private CarbonServerPaper(final Server server, final UserManager<CarbonPlayerCommon> userManager, final ProfileResolver profileResolver) {
|
||||
this.server = server;
|
||||
this.userManager = new PaperUserManager(userManager);
|
||||
this.profileResolver = profileResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,17 +65,10 @@ public final class CarbonServerPaper implements CarbonServer, ForwardingAudience
|
||||
|
||||
@Override
|
||||
public List<? extends CarbonPlayer> players() {
|
||||
final var players = new ArrayList<CarbonPlayer>();
|
||||
|
||||
for (final var player : this.server.getOnlinePlayers()) {
|
||||
final ComponentPlayerResult<CarbonPlayerPaper> result = this.userManager.carbonPlayer(player.getUniqueId()).join();
|
||||
|
||||
if (result.player() != null) {
|
||||
players.add(result.player());
|
||||
}
|
||||
}
|
||||
|
||||
return players;
|
||||
return this.server.getOnlinePlayers().stream()
|
||||
.map(bukkit -> this.userManager.user(bukkit.getUniqueId()).getNow(null))
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,22 +78,12 @@ public final class CarbonServerPaper implements CarbonServer, ForwardingAudience
|
||||
|
||||
@Override
|
||||
public CompletableFuture<@Nullable UUID> resolveUUID(final String username) {
|
||||
// TODO: user cache?
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username);
|
||||
|
||||
if (!offlinePlayer.hasPlayedBefore()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return offlinePlayer.getUniqueId();
|
||||
});
|
||||
return this.profileResolver.resolveUUID(username);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<@Nullable String> resolveName(final UUID uuid) {
|
||||
// TODO: user cache?
|
||||
return CompletableFuture.supplyAsync(() -> Bukkit.getOfflinePlayer(uuid).getName());
|
||||
return this.profileResolver.resolveName(uuid);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.SaveOnChange;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
import net.draycia.carbon.paper.users.CarbonPlayerPaper;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -33,7 +34,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class PaperUserManager implements UserManager<CarbonPlayerPaper>, SaveOnChange {
|
||||
public class PaperUserManager implements UserManagerInternal<CarbonPlayerPaper>, SaveOnChange {
|
||||
|
||||
protected final UserManager<CarbonPlayerCommon> proxiedUserManager;
|
||||
|
||||
@@ -43,35 +44,30 @@ public class PaperUserManager implements UserManager<CarbonPlayerPaper>, SaveOnC
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ComponentPlayerResult<CarbonPlayerPaper>> carbonPlayer(final UUID uuid) {
|
||||
return this.proxiedUserManager.carbonPlayer(uuid).thenApply(result -> {
|
||||
if (result.player() == null) {
|
||||
return new ComponentPlayerResult<>(null, result.reason());
|
||||
}
|
||||
return this.proxiedUserManager.carbonPlayer(uuid).thenApply(PaperUserManager::wrapResult);
|
||||
}
|
||||
|
||||
return new ComponentPlayerResult<>(new CarbonPlayerPaper(result.player()), result.reason());
|
||||
});
|
||||
@Override
|
||||
public CompletableFuture<CarbonPlayerPaper> user(UUID uuid) {
|
||||
return this.proxiedUserManager.user(uuid).thenApply(CarbonPlayerPaper::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ComponentPlayerResult<CarbonPlayerPaper>> savePlayer(final CarbonPlayerPaper player) {
|
||||
return this.proxiedUserManager.savePlayer(player.carbonPlayerCommon()).thenApply(result -> {
|
||||
if (result.player() == null) {
|
||||
return new ComponentPlayerResult<>(null, result.reason());
|
||||
}
|
||||
|
||||
return new ComponentPlayerResult<>(new CarbonPlayerPaper(result.player()), result.reason());
|
||||
});
|
||||
return this.proxiedUserManager.savePlayer(player.carbonPlayerCommon()).thenApply(PaperUserManager::wrapResult);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ComponentPlayerResult<CarbonPlayerPaper>> saveAndInvalidatePlayer(final CarbonPlayerPaper player) {
|
||||
return this.proxiedUserManager.saveAndInvalidatePlayer(player.carbonPlayerCommon()).thenApply(result -> {
|
||||
if (result.player() == null) {
|
||||
return new ComponentPlayerResult<>(null, result.reason());
|
||||
}
|
||||
return this.proxiedUserManager.saveAndInvalidatePlayer(player.carbonPlayerCommon()).thenApply(PaperUserManager::wrapResult);
|
||||
}
|
||||
|
||||
return new ComponentPlayerResult<>(new CarbonPlayerPaper(result.player()), result.reason());
|
||||
});
|
||||
private static ComponentPlayerResult<CarbonPlayerPaper> wrapResult(final ComponentPlayerResult<CarbonPlayerCommon> result) {
|
||||
if (result.player() == null) {
|
||||
return new ComponentPlayerResult<>(null, result.reason());
|
||||
}
|
||||
|
||||
return new ComponentPlayerResult<>(new CarbonPlayerPaper(result.player()), result.reason());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -173,4 +169,18 @@ public class PaperUserManager implements UserManager<CarbonPlayerPaper>, SaveOnC
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
((UserManagerInternal<?>) this.proxiedUserManager).shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> save(CarbonPlayerPaper player) {
|
||||
return ((UserManagerInternal<CarbonPlayerCommon>) this.proxiedUserManager).save(player.carbonPlayerCommon());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> loggedOut(final UUID uuid) {
|
||||
return ((UserManagerInternal<?>) this.proxiedUserManager).loggedOut(uuid);
|
||||
}
|
||||
}
|
||||
+15
-13
@@ -22,10 +22,9 @@ package net.draycia.carbon.paper.listeners;
|
||||
import com.google.inject.Inject;
|
||||
import java.util.Optional;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.util.PlayerUtils;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
import net.draycia.carbon.paper.users.CarbonPlayerPaper;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
@@ -39,15 +38,15 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
public class PaperPlayerJoinListener implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
private final UserManager<CarbonPlayerCommon> userManager;
|
||||
private final Logger logger;
|
||||
|
||||
@Inject
|
||||
public PaperPlayerJoinListener(
|
||||
final CarbonChat carbonChat,
|
||||
final UserManager<CarbonPlayerCommon> userManager
|
||||
final Logger logger
|
||||
) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.userManager = userManager;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
@@ -62,18 +61,21 @@ public class PaperPlayerJoinListener implements Listener {
|
||||
player.displayName(displayName);
|
||||
player.playerListName(displayName);
|
||||
});
|
||||
}).exceptionally(thr -> {
|
||||
this.carbonChat.logger().warn("Exception handling player join", thr);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onQuit(final PlayerQuitEvent event) {
|
||||
this.carbonChat.server().userManager().carbonPlayer(event.getPlayer().getUniqueId()).thenAccept(result -> {
|
||||
if (result.player() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
PlayerUtils.saveAndInvalidatePlayer(this.carbonChat.server(), this.userManager, (CarbonPlayerPaper) result.player());
|
||||
});
|
||||
((UserManagerInternal<CarbonPlayerPaper>) this.carbonChat.server().userManager())
|
||||
.loggedOut(event.getPlayer().getUniqueId())
|
||||
.exceptionally(thr -> {
|
||||
this.logger.warn("Exception saving data for player " + event.getPlayer().getName() + " with uuid " + event.getPlayer().getUniqueId());
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
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.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;
|
||||
|
||||
@Inject
|
||||
private PaperProfileResolver(final Server server, final Logger logger) {
|
||||
this.server = server;
|
||||
this.logger = logger;
|
||||
this.executor = Executors.newFixedThreadPool(2, ConcurrentUtil.carbonThreadFactory(logger, "PaperProfileResolver"));
|
||||
}
|
||||
|
||||
@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;
|
||||
});
|
||||
}
|
||||
|
||||
@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;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
ConcurrentUtil.shutdownExecutor(this.executor, TimeUnit.MILLISECONDS, 50);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user