From 824a392414fe0d4ce5c9514415f5be05d3052ec4 Mon Sep 17 00:00:00 2001 From: Vankka Date: Mon, 30 Dec 2024 00:16:12 +0200 Subject: [PATCH] Merge profile loading into PlayerProvider --- .../discordsrv/bukkit/BukkitDiscordSRV.java | 4 - .../listener/BukkitConnectionListener.java | 76 ------------------- .../discordsrv/common/AbstractDiscordSRV.java | 5 ++ .../provider/AbstractPlayerProvider.java | 11 +++ .../player/provider/PlayerProvider.java | 2 + .../feature/profile/ProfileManager.java | 10 ++- 6 files changed, 26 insertions(+), 82 deletions(-) delete mode 100644 bukkit/src/main/java/com/discordsrv/bukkit/listener/BukkitConnectionListener.java diff --git a/bukkit/src/main/java/com/discordsrv/bukkit/BukkitDiscordSRV.java b/bukkit/src/main/java/com/discordsrv/bukkit/BukkitDiscordSRV.java index e24abf77..22018f4a 100644 --- a/bukkit/src/main/java/com/discordsrv/bukkit/BukkitDiscordSRV.java +++ b/bukkit/src/main/java/com/discordsrv/bukkit/BukkitDiscordSRV.java @@ -31,7 +31,6 @@ import com.discordsrv.bukkit.config.manager.BukkitConfigManager; import com.discordsrv.bukkit.config.manager.BukkitConnectionConfigManager; import com.discordsrv.bukkit.config.manager.BukkitMessagesConfigManager; import com.discordsrv.bukkit.console.BukkitConsole; -import com.discordsrv.bukkit.listener.BukkitConnectionListener; import com.discordsrv.bukkit.listener.BukkitDeathListener; import com.discordsrv.bukkit.listener.BukkitStatusMessageListener; import com.discordsrv.bukkit.listener.award.BukkitAwardForwarder; @@ -222,9 +221,6 @@ public class BukkitDiscordSRV extends AbstractDiscordSRV. - */ - -package com.discordsrv.bukkit.listener; - -import com.discordsrv.bukkit.BukkitDiscordSRV; -import org.bukkit.entity.Player; -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.PlayerLoginEvent; -import org.bukkit.event.player.PlayerQuitEvent; - -import java.util.UUID; - -public class BukkitConnectionListener implements Listener { - - private final BukkitDiscordSRV discordSRV; - - public BukkitConnectionListener(BukkitDiscordSRV discordSRV) { - this.discordSRV = discordSRV; - - // Load players who joined before this listener was created - for (Player onlinePlayer : discordSRV.server().getOnlinePlayers()) { - discordSRV.profileManager().loadProfile(onlinePlayer.getUniqueId()); - } - } - - @EventHandler(priority = EventPriority.NORMAL) - public void onPlayerLoginNormal(PlayerLoginEvent event) { - if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) { - return; - } - - discordSRV.profileManager().loadProfile(event.getPlayer().getUniqueId()); - } - - @EventHandler(priority = EventPriority.MONITOR) - public void onPlayerLoginMonitor(PlayerLoginEvent event) { - if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) { - // Unload in case got blocked after NORMAL - discordSRV.profileManager().unloadProfile(event.getPlayer().getUniqueId()); - } - } - - @EventHandler(priority = EventPriority.LOWEST) - public void onPlayerJoin(PlayerJoinEvent event) { - UUID playerUUID = event.getPlayer().getUniqueId(); - if (discordSRV.profileManager().getProfile(playerUUID) == null) { - // Not loaded in yet (offline mode) - // No blocking since this runs on main thread - discordSRV.profileManager().loadProfile(playerUUID); - } - } - - @EventHandler(priority = EventPriority.MONITOR) - public void onPlayerQuit(PlayerQuitEvent event) { - discordSRV.profileManager().unloadProfile(event.getPlayer().getUniqueId()); - } -} diff --git a/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java b/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java index 358fab4c..054d8f5e 100644 --- a/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java +++ b/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java @@ -850,6 +850,7 @@ public abstract class AbstractDiscordSRV< if (flags.contains(ReloadFlag.LINKED_ACCOUNT_PROVIDER)) { LinkedAccountConfig linkedAccountConfig = config().linkedAccounts; + boolean linkProviderMissing = linkProvider == null; if (linkedAccountConfig != null && linkedAccountConfig.enabled) { LinkedAccountConfig.Provider provider = linkedAccountConfig.provider; boolean permitMinecraftAuth = connectionConfig().minecraftAuth.allow; @@ -883,6 +884,10 @@ public abstract class AbstractDiscordSRV< linkProvider = null; logger().info("Linked accounts are disabled"); } + + if (linkProviderMissing && linkProvider != null) { + playerProvider().loadAllProfilesAsync(); + } } if (flags.contains(ReloadFlag.DISCORD_CONNECTION)) { diff --git a/common/src/main/java/com/discordsrv/common/abstraction/player/provider/AbstractPlayerProvider.java b/common/src/main/java/com/discordsrv/common/abstraction/player/provider/AbstractPlayerProvider.java index 1485ebe7..3fb44a68 100644 --- a/common/src/main/java/com/discordsrv/common/abstraction/player/provider/AbstractPlayerProvider.java +++ b/common/src/main/java/com/discordsrv/common/abstraction/player/provider/AbstractPlayerProvider.java @@ -70,6 +70,8 @@ public abstract class AbstractPlayerProvider discordSRV.eventBus().publish(new PlayerConnectedEvent(player, initial))); + discordSRV.profileManager().loadProfile(player.uniqueId()); + if (UUIDUtil.isOffline(uuid)) { anyOffline.set(true); } @@ -81,6 +83,8 @@ public abstract class AbstractPlayerProvider discordSRV.eventBus().publish(new PlayerDisconnectedEvent(player))); } + + discordSRV.profileManager().unloadProfile(uuid); } @Override @@ -103,6 +107,13 @@ public abstract class AbstractPlayerProvider lookupUUIDForUsername(String username) { IPlayer player = player(username); diff --git a/common/src/main/java/com/discordsrv/common/abstraction/player/provider/PlayerProvider.java b/common/src/main/java/com/discordsrv/common/abstraction/player/provider/PlayerProvider.java index d7e5e17d..9650d372 100644 --- a/common/src/main/java/com/discordsrv/common/abstraction/player/provider/PlayerProvider.java +++ b/common/src/main/java/com/discordsrv/common/abstraction/player/provider/PlayerProvider.java @@ -54,6 +54,8 @@ public interface PlayerProvider extends IPlayerProvider { @NotNull Collection allPlayers(); + void loadAllProfilesAsync(); + CompletableFuture lookupUUIDForUsername(String username); default CompletableFuture lookupOfflinePlayer(String username) { diff --git a/common/src/main/java/com/discordsrv/common/feature/profile/ProfileManager.java b/common/src/main/java/com/discordsrv/common/feature/profile/ProfileManager.java index 7adbace6..c058be90 100644 --- a/common/src/main/java/com/discordsrv/common/feature/profile/ProfileManager.java +++ b/common/src/main/java/com/discordsrv/common/feature/profile/ProfileManager.java @@ -20,6 +20,7 @@ package com.discordsrv.common.feature.profile; import com.discordsrv.api.profile.IProfileManager; import com.discordsrv.common.DiscordSRV; +import com.discordsrv.common.feature.linking.LinkProvider; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -49,6 +50,7 @@ public class ProfileManager implements IProfileManager { return profile; }); profileLookups.put(playerUUID, lookup); + lookup.whenComplete((__, ___) -> profileLookups.remove(playerUUID)); return lookup; } @@ -70,7 +72,9 @@ public class ProfileManager implements IProfileManager { @Override public @NotNull CompletableFuture lookupProfile(UUID playerUUID) { - return discordSRV.linkProvider().getUserId(playerUUID) + LinkProvider linkProvider = discordSRV.linkProvider(); + if (linkProvider == null) return CompletableFuture.completedFuture(null); + return linkProvider.getUserId(playerUUID) .thenApply(opt -> new Profile(playerUUID, opt.orElse(null))); } @@ -81,7 +85,9 @@ public class ProfileManager implements IProfileManager { @Override public @NotNull CompletableFuture lookupProfile(long userId) { - return discordSRV.linkProvider().getPlayerUUID(userId) + LinkProvider linkProvider = discordSRV.linkProvider(); + if (linkProvider == null) return CompletableFuture.completedFuture(null); + return linkProvider.getPlayerUUID(userId) .thenApply(opt -> new Profile(opt.orElse(null), userId)); }