Handle PlayerLoginEvent not being triggered in offline mode

This commit is contained in:
Vankka 2024-07-07 12:33:06 +03:00
parent ae60e7e423
commit a0a5dd01fa
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
3 changed files with 32 additions and 9 deletions

View File

@ -23,9 +23,12 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import java.util.UUID;
public class BukkitConnectionListener implements Listener { public class BukkitConnectionListener implements Listener {
private final BukkitDiscordSRV discordSRV; private final BukkitDiscordSRV discordSRV;
@ -33,6 +36,7 @@ public class BukkitConnectionListener implements Listener {
public BukkitConnectionListener(BukkitDiscordSRV discordSRV) { public BukkitConnectionListener(BukkitDiscordSRV discordSRV) {
this.discordSRV = discordSRV; this.discordSRV = discordSRV;
// Load players who joined before this listener was created
for (Player onlinePlayer : discordSRV.server().getOnlinePlayers()) { for (Player onlinePlayer : discordSRV.server().getOnlinePlayers()) {
discordSRV.profileManager().loadProfile(onlinePlayer.getUniqueId()); discordSRV.profileManager().loadProfile(onlinePlayer.getUniqueId());
} }
@ -55,6 +59,16 @@ public class BukkitConnectionListener implements Listener {
} }
} }
@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) @EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
discordSRV.profileManager().unloadProfile(event.getPlayer().getUniqueId()); discordSRV.profileManager().unloadProfile(event.getPlayer().getUniqueId());

View File

@ -64,7 +64,7 @@ public class BukkitPlayerProvider extends ServerPlayerProvider<BukkitPlayer, Buk
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
if (player(event.getPlayer()) == null) { if (player(event.getPlayer()) == null) {
// The player should already be added by this point, but just in case // The player wasn't loaded at PlayerLoginEvent (offline mode)
addPlayer(event.getPlayer(), false); addPlayer(event.getPlayer(), false);
} }
} }

View File

@ -20,7 +20,6 @@ package com.discordsrv.common.profile;
import com.discordsrv.api.profile.IProfileManager; import com.discordsrv.api.profile.IProfileManager;
import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.DiscordSRV;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -32,6 +31,7 @@ import java.util.concurrent.ConcurrentHashMap;
public class ProfileManager implements IProfileManager { public class ProfileManager implements IProfileManager {
private final DiscordSRV discordSRV; private final DiscordSRV discordSRV;
private final Map<UUID, CompletableFuture<Profile>> profileLookups = new ConcurrentHashMap<>();
private final Map<UUID, Profile> profiles = new ConcurrentHashMap<>(); private final Map<UUID, Profile> profiles = new ConcurrentHashMap<>();
private final Map<Long, Profile> discordUserMap = new ConcurrentHashMap<>(); private final Map<Long, Profile> discordUserMap = new ConcurrentHashMap<>();
@ -39,16 +39,25 @@ public class ProfileManager implements IProfileManager {
this.discordSRV = discordSRV; this.discordSRV = discordSRV;
} }
@Blocking public CompletableFuture<Profile> loadProfile(UUID playerUUID) {
public void loadProfile(UUID playerUUID) { CompletableFuture<Profile> lookup = lookupProfile(playerUUID)
Profile profile = lookupProfile(playerUUID).join(); .thenApply(profile -> {
profiles.put(playerUUID, profile); profiles.put(playerUUID, profile);
if (profile.isLinked()) { if (profile.isLinked()) {
discordUserMap.put(profile.userId(), profile); discordUserMap.put(profile.userId(), profile);
} }
return profile;
});
profileLookups.put(playerUUID, lookup);
return lookup;
} }
public void unloadProfile(UUID playerUUID) { public void unloadProfile(UUID playerUUID) {
CompletableFuture<Profile> lookup = profileLookups.remove(playerUUID);
if (lookup != null) {
lookup.cancel(false);
}
Profile profile = profiles.remove(playerUUID); Profile profile = profiles.remove(playerUUID);
if (profile == null) { if (profile == null) {
return; return;