mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 14:38:51 +00:00
Player saving done right
This commit is contained in:
1 parent
3736377fd7
commit
9305da7d2d
21 files changed
+314
-257
No files matched your search
@@ -3,6 +3,7 @@ package net.draycia.carbon.bukkit;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Singleton;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import io.papermc.lib.PaperLib;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Set;
|
||||
@@ -15,7 +16,6 @@ import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.api.util.SourcedAudience;
|
||||
import net.draycia.carbon.bukkit.listeners.BukkitChatListener;
|
||||
import net.draycia.carbon.bukkit.listeners.BukkitPlayerJoinListener;
|
||||
import net.draycia.carbon.bukkit.users.CarbonPlayerBukkit;
|
||||
import net.draycia.carbon.bukkit.util.BukkitMessageRenderer;
|
||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||
import net.draycia.carbon.common.command.commands.ContinueCommand;
|
||||
@@ -25,7 +25,8 @@ import net.draycia.carbon.common.command.commands.WhisperCommand;
|
||||
import net.draycia.carbon.common.listeners.DeafenHandler;
|
||||
import net.draycia.carbon.common.listeners.MuteHandler;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.message.IMessageRenderer;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -49,7 +50,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
private static final int BSTATS_PLUGIN_ID = 8720;
|
||||
private final CarbonEventHandler eventHandler = new CarbonEventHandler();
|
||||
private @MonotonicNonNull Injector injector;
|
||||
private @MonotonicNonNull UserManager userManager;
|
||||
private @MonotonicNonNull UserManager<CarbonPlayerCommon> userManager;
|
||||
private @MonotonicNonNull Logger logger;
|
||||
private @MonotonicNonNull CarbonServerBukkit carbonServerBukkit;
|
||||
private @MonotonicNonNull CarbonMessageService messageService;
|
||||
@@ -77,7 +78,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
this.messageService = this.injector.getInstance(CarbonMessageService.class);
|
||||
this.channelRegistry = this.injector.getInstance(ChannelRegistry.class);
|
||||
this.carbonServerBukkit = this.injector.getInstance(CarbonServerBukkit.class);
|
||||
this.userManager = this.injector.getInstance(UserManager.class);
|
||||
this.userManager = this.injector.getInstance(com.google.inject.Key.get(new TypeLiteral<UserManager<CarbonPlayerCommon>>() {}));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -115,16 +116,24 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
}
|
||||
|
||||
private void savePlayers() {
|
||||
// TODO: move to common or CarbonServer?
|
||||
for (final var player : this.server().players()) {
|
||||
this.userManager().savePlayer(((CarbonPlayerBukkit) player).carbonPlayer()).thenAccept(result -> {
|
||||
final var saveResult = this.userManager().savePlayer(((WrappedCarbonPlayer) player).carbonPlayerCommon());
|
||||
|
||||
saveResult.thenAccept(result -> {
|
||||
if (result.player() == null) {
|
||||
this.server().console().sendMessage(result.reason());
|
||||
}
|
||||
});
|
||||
|
||||
saveResult.exceptionally(exception -> {
|
||||
exception.getCause().printStackTrace();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public UserManager userManager() {
|
||||
public UserManager<CarbonPlayerCommon> userManager() {
|
||||
return this.userManager;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,12 +29,12 @@ import static net.kyori.adventure.text.Component.text;
|
||||
public final class CarbonServerBukkit implements CarbonServer, ForwardingAudience.Single {
|
||||
|
||||
private final CarbonChatBukkit chatBukkitEntry;
|
||||
private final UserManager userManager;
|
||||
private final UserManager<CarbonPlayerCommon> userManager;
|
||||
|
||||
private final Map<UUID, CarbonPlayerBukkit> userCache = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
private CarbonServerBukkit(final CarbonChatBukkit chatBukkitEntry, final UserManager userManager) {
|
||||
private CarbonServerBukkit(final CarbonChatBukkit chatBukkitEntry, final UserManager<CarbonPlayerCommon> userManager) {
|
||||
this.chatBukkitEntry = chatBukkitEntry;
|
||||
this.userManager = userManager;
|
||||
}
|
||||
@@ -54,7 +54,7 @@ public final class CarbonServerBukkit implements CarbonServer, ForwardingAudienc
|
||||
final var players = new ArrayList<CarbonPlayer>();
|
||||
|
||||
for (final var player : this.chatBukkitEntry.getServer().getOnlinePlayers()) {
|
||||
final @Nullable ComponentPlayerResult result = this.player(player).join();
|
||||
final @Nullable ComponentPlayerResult<CarbonPlayer> result = this.player(player).join();
|
||||
|
||||
if (result.player() != null) {
|
||||
players.add(result.player());
|
||||
@@ -64,47 +64,46 @@ public final class CarbonServerBukkit implements CarbonServer, ForwardingAudienc
|
||||
return players;
|
||||
}
|
||||
|
||||
private CompletableFuture<ComponentPlayerResult> wrapPlayer(final UUID uuid) {
|
||||
private CompletableFuture<ComponentPlayerResult<CarbonPlayer>> wrapPlayer(final UUID uuid) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
final @Nullable CarbonPlayerBukkit cachedPlayer = this.userCache.get(uuid);
|
||||
|
||||
if (cachedPlayer != null) {
|
||||
return new ComponentPlayerResult(cachedPlayer, Component.empty());
|
||||
return new ComponentPlayerResult<>(cachedPlayer, Component.empty());
|
||||
}
|
||||
|
||||
final ComponentPlayerResult result = this.userManager.carbonPlayer(uuid).join();
|
||||
final ComponentPlayerResult<CarbonPlayerCommon> result = this.userManager.carbonPlayer(uuid).join();
|
||||
|
||||
if (result.player() != null) {
|
||||
final CarbonPlayerBukkit carbonPlayerBukkit = new CarbonPlayerBukkit(result.player());
|
||||
|
||||
this.userCache.put(uuid, carbonPlayerBukkit);
|
||||
|
||||
return new ComponentPlayerResult(carbonPlayerBukkit, Component.empty());
|
||||
return new ComponentPlayerResult<>(carbonPlayerBukkit, Component.empty());
|
||||
}
|
||||
|
||||
final @Nullable String name = this.resolveName(uuid).join();
|
||||
|
||||
if (name != null) {
|
||||
final CarbonPlayerCommon player = new CarbonPlayerCommon(null,
|
||||
null, name, uuid);
|
||||
final CarbonPlayerCommon player = new CarbonPlayerCommon(name, uuid);
|
||||
final CarbonPlayerBukkit carbonPlayerBukkit = new CarbonPlayerBukkit(player);
|
||||
|
||||
this.userCache.put(uuid, carbonPlayerBukkit);
|
||||
|
||||
return new ComponentPlayerResult(carbonPlayerBukkit, Component.empty());
|
||||
return new ComponentPlayerResult<>(carbonPlayerBukkit, Component.empty());
|
||||
}
|
||||
|
||||
return new ComponentPlayerResult(null, text("Name not found for uuid!"));
|
||||
return new ComponentPlayerResult<>(null, text("Name not found for uuid!"));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ComponentPlayerResult> player(final UUID uuid) {
|
||||
public CompletableFuture<ComponentPlayerResult<CarbonPlayer>> player(final UUID uuid) {
|
||||
return this.wrapPlayer(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<ComponentPlayerResult> player(final String username) {
|
||||
public CompletableFuture<ComponentPlayerResult<CarbonPlayer>> player(final String username) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
final @Nullable UUID uuid = this.resolveUUID(username).join();
|
||||
|
||||
@@ -112,11 +111,11 @@ public final class CarbonServerBukkit implements CarbonServer, ForwardingAudienc
|
||||
return this.player(uuid).join();
|
||||
}
|
||||
|
||||
return new ComponentPlayerResult(null, text("No UUID found for name."));
|
||||
return new ComponentPlayerResult<>(null, text("No UUID found for name."));
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<ComponentPlayerResult> player(final Player player) {
|
||||
public CompletableFuture<ComponentPlayerResult<CarbonPlayer>> player(final Player player) {
|
||||
return this.player(player.getUniqueId());
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.ArrayList;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.bukkit.CarbonChatBukkit;
|
||||
@@ -15,6 +16,7 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static java.util.Objects.requireNonNullElse;
|
||||
@@ -37,7 +39,7 @@ public final class BukkitChatListener implements Listener {
|
||||
@EventHandler
|
||||
public void onPlayerChat(final @NonNull AsyncChatEvent event) {
|
||||
final var playerResult = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
final var sender = playerResult.player();
|
||||
final @Nullable CarbonPlayer sender = playerResult.player();
|
||||
|
||||
if (sender == null) {
|
||||
return;
|
||||
|
||||
+8
-5
@@ -10,7 +10,11 @@ import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class BukkitPlayerJoinListener implements Listener {
|
||||
|
||||
private final CarbonChat carbonChat;
|
||||
@@ -27,14 +31,13 @@ public class BukkitPlayerJoinListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerLogin(final PlayerJoinEvent event) {
|
||||
final ComponentPlayerResult result = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
final ComponentPlayerResult<CarbonPlayer> result = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
final @Nullable CarbonPlayer player = result.player();
|
||||
|
||||
if (result.player() == null) {
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final CarbonPlayer player = result.player();
|
||||
|
||||
// Don't show join messages when muted
|
||||
if (this.primaryConfig.hideMutedJoinLeaveQuit() && player.muted()) {
|
||||
event.joinMessage(null);
|
||||
@@ -48,7 +51,7 @@ public class BukkitPlayerJoinListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
final ComponentPlayerResult result = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
final ComponentPlayerResult<CarbonPlayer> result = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
|
||||
if (result.player() == null) {
|
||||
return;
|
||||
|
||||
@@ -4,14 +4,11 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.bukkit.util.BukkitCapabilities;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -23,17 +20,37 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonPlayerBukkit extends CarbonPlayerCommon implements ForwardingAudience.Single {
|
||||
public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements ForwardingAudience.Single {
|
||||
|
||||
private final CarbonPlayer carbonPlayer;
|
||||
private final CarbonPlayerCommon carbonPlayerCommon;
|
||||
|
||||
public CarbonPlayerBukkit(final CarbonPlayer carbonPlayer) {
|
||||
this.carbonPlayer = carbonPlayer;
|
||||
public CarbonPlayerBukkit(final CarbonPlayerCommon carbonPlayerCommon) {
|
||||
this.carbonPlayerCommon = carbonPlayerCommon;
|
||||
}
|
||||
|
||||
private @Nullable Player player() {
|
||||
return Bukkit.getPlayer(this.carbonPlayerCommon.uuid());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarbonPlayerCommon carbonPlayerCommon() {
|
||||
return this.carbonPlayerCommon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience audience() {
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
if (player == null) {
|
||||
return Audience.empty();
|
||||
}
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayName(final @Nullable Component displayName) {
|
||||
this.carbonPlayer.displayName(displayName);
|
||||
this.carbonPlayerCommon.displayName(displayName);
|
||||
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
@@ -49,7 +66,7 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon implements Forw
|
||||
|
||||
@Override
|
||||
public void temporaryDisplayName(final @Nullable Component displayName, final long expirationEpoch) {
|
||||
this.carbonPlayer.temporaryDisplayName(displayName, expirationEpoch);
|
||||
this.carbonPlayerCommon.temporaryDisplayName(displayName, expirationEpoch);
|
||||
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
@@ -64,42 +81,6 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon implements Forw
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String username() {
|
||||
return this.carbonPlayer.username();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomDisplayName() {
|
||||
return this.carbonPlayer.hasCustomDisplayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Component displayName() {
|
||||
return this.carbonPlayer.displayName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID uuid() {
|
||||
return this.carbonPlayer.uuid();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience audience() {
|
||||
final @Nullable Player player = this.player();
|
||||
|
||||
if (player == null) {
|
||||
return Audience.empty();
|
||||
}
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CarbonPlayer carbonPlayer() {
|
||||
return this.carbonPlayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component createItemHoverComponent() {
|
||||
final @Nullable Player player = this.player(); // This is temporary (it's not)
|
||||
@@ -174,36 +155,6 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon implements Forw
|
||||
return List.of("default"); // TODO: implement
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean muted() {
|
||||
return this.carbonPlayer.muted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void muted(final boolean muted) {
|
||||
this.carbonPlayer.muted(muted);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deafened() {
|
||||
return this.carbonPlayer.deafened();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deafened(final boolean deafened) {
|
||||
this.carbonPlayer.deafened(deafened);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean spying() {
|
||||
return this.carbonPlayer.spying();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spying(final boolean spying) {
|
||||
this.carbonPlayer.spying(spying);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Locale locale() {
|
||||
final @Nullable Player player = this.player();
|
||||
@@ -215,25 +166,6 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon implements Forw
|
||||
}
|
||||
}
|
||||
|
||||
private @Nullable Player player() {
|
||||
return Bukkit.getPlayer(this.carbonPlayer.uuid());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ChatChannel selectedChannel() {
|
||||
return this.carbonPlayer.selectedChannel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectedChannel(final ChatChannel chatChannel) {
|
||||
this.carbonPlayer.selectedChannel(chatChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identity identity() {
|
||||
return this.carbonPlayer.identity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessageAsPlayer(String message) {
|
||||
Bukkit.getScheduler().runTask(Bukkit.getPluginManager().getPlugin("CarbonChat"), () -> {
|
||||
@@ -246,24 +178,4 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon implements Forw
|
||||
return this.player() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void whisperReplyTarget(@Nullable UUID uuid) {
|
||||
this.carbonPlayer.whisperReplyTarget(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable UUID whisperReplyTarget() {
|
||||
return this.carbonPlayer.whisperReplyTarget();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lastWhisperTarget(@Nullable UUID uuid) {
|
||||
this.carbonPlayer.lastWhisperTarget(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable UUID lastWhisperTarget() {
|
||||
return this.carbonPlayer.lastWhisperTarget();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user