Merge profile loading into PlayerProvider

This commit is contained in:
Vankka 2024-12-30 00:16:12 +02:00
parent b588dc185a
commit 824a392414
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
6 changed files with 26 additions and 82 deletions

View File

@ -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<DiscordSRVBukkitBootstr
server().getPluginManager().registerEvents(BukkitChatForwarder.get(this), plugin());
server().getPluginManager().registerEvents(new BukkitDeathListener(this), plugin());
server().getPluginManager().registerEvents(new BukkitStatusMessageListener(this), plugin());
// Connection listener
server().getPluginManager().registerEvents(new BukkitConnectionListener(this), plugin());
}
@Override

View File

@ -1,76 +0,0 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2024 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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());
}
}

View File

@ -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)) {

View File

@ -70,6 +70,8 @@ public abstract class AbstractPlayerProvider<T extends IPlayer, DT extends Disco
this.allPlayers.add(player);
discordSRV.scheduler().run(() -> 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<T extends IPlayer, DT extends Disco
allPlayers.remove(player);
discordSRV.scheduler().run(() -> discordSRV.eventBus().publish(new PlayerDisconnectedEvent(player)));
}
discordSRV.profileManager().unloadProfile(uuid);
}
@Override
@ -103,6 +107,13 @@ public abstract class AbstractPlayerProvider<T extends IPlayer, DT extends Disco
return allPlayers;
}
@Override
public void loadAllProfilesAsync() {
for (T player : allPlayers()) {
discordSRV.profileManager().loadProfile(player.uniqueId());
}
}
@Override
public CompletableFuture<UUID> lookupUUIDForUsername(String username) {
IPlayer player = player(username);

View File

@ -54,6 +54,8 @@ public interface PlayerProvider<T extends IPlayer> extends IPlayerProvider {
@NotNull
Collection<T> allPlayers();
void loadAllProfilesAsync();
CompletableFuture<UUID> lookupUUIDForUsername(String username);
default CompletableFuture<IOfflinePlayer> lookupOfflinePlayer(String username) {

View File

@ -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<Profile> 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<Profile> 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));
}