MySQL now works on Bukkit

This commit is contained in:
Draycia committed 2022-06-08 15:16:20 -07:00
1 parent 751ca43f15
commit b871adef8f
32 files changed
+593 -116

No files matched your search

@@ -23,7 +23,7 @@ import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.ComponentPlayerResult;
import net.draycia.carbon.api.users.UserManager;
import net.kyori.adventure.audience.Audience;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -54,22 +54,12 @@ public interface CarbonServer extends Audience {
List<? extends CarbonPlayer> players();
/**
* Obtains a {@link CarbonPlayer} instance for the specified uuid.
* Manager used to load/obtain and save {@link CarbonPlayer CarbonPlayers}.
*
* @param uuid the player's uuid
* @return the result
* @since 2.0.0
* @return the user manager
* @since 2.1.0
*/
CompletableFuture<ComponentPlayerResult<CarbonPlayer>> player(final UUID uuid);
/**
* Obtains a {@link CarbonPlayer} instance for the specified username.
*
* @param username the player's username
* @return the result
* @since 2.0.0
*/
CompletableFuture<ComponentPlayerResult<CarbonPlayer>> player(final String username);
UserManager<? extends CarbonPlayer> userManager();
/**
* Obtains the desired user's UUID.
@@ -0,0 +1,158 @@
/*
* CarbonChat
*
* Copyright (c) 2021 Josua Parks (Vicarious)
* 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 net.draycia.carbon.bukkit;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.draycia.carbon.api.users.ComponentPlayerResult;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.bukkit.users.CarbonPlayerBukkit;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.SaveOnChange;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
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 BukkitUserManager implements UserManager<CarbonPlayerBukkit>, SaveOnChange {
protected final UserManager<CarbonPlayerCommon> proxiedUserManager;
public BukkitUserManager(final UserManager<CarbonPlayerCommon> proxiedUserManager) {
this.proxiedUserManager = proxiedUserManager;
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerBukkit>> carbonPlayer(final UUID uuid) {
return this.proxiedUserManager.carbonPlayer(uuid).thenApply(result -> {
if (result.player() == null) {
return new ComponentPlayerResult<>(null, result.reason());
}
return new ComponentPlayerResult<>(new CarbonPlayerBukkit(result.player()), result.reason());
});
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerBukkit>> savePlayer(final CarbonPlayerBukkit player) {
return this.proxiedUserManager.savePlayer(player.carbonPlayerCommon()).thenApply(result -> {
if (result.player() == null) {
return new ComponentPlayerResult<>(null, result.reason());
}
return new ComponentPlayerResult<>(new CarbonPlayerBukkit(result.player()), result.reason());
});
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerBukkit>> saveAndInvalidatePlayer(final CarbonPlayerBukkit player) {
return this.proxiedUserManager.saveAndInvalidatePlayer(player.carbonPlayerCommon()).thenApply(result -> {
if (result.player() == null) {
return new ComponentPlayerResult<>(null, result.reason());
}
return new ComponentPlayerResult<>(new CarbonPlayerBukkit(result.player()), result.reason());
});
}
@Override
public int saveDisplayName(final UUID id, final @Nullable Component component) {
if (this.proxiedUserManager instanceof SaveOnChange saveOnChange) {
return saveOnChange.saveDisplayName(id, component);
}
return -1;
}
@Override
public int saveMuted(final UUID id, final boolean muted) {
if (this.proxiedUserManager instanceof SaveOnChange saveOnChange) {
return saveOnChange.saveMuted(id, muted);
}
return -1;
}
@Override
public int saveDeafened(final UUID id, final boolean deafened) {
if (this.proxiedUserManager instanceof SaveOnChange saveOnChange) {
return saveOnChange.saveDeafened(id, deafened);
}
return -1;
}
@Override
public int saveSpying(final UUID id, final boolean spying) {
if (this.proxiedUserManager instanceof SaveOnChange saveOnChange) {
return saveOnChange.saveSpying(id, spying);
}
return -1;
}
@Override
public int saveSelectedChannel(final UUID id, final @Nullable Key selectedChannel) {
if (this.proxiedUserManager instanceof SaveOnChange saveOnChange) {
return saveOnChange.saveSelectedChannel(id, selectedChannel);
}
return -1;
}
@Override
public int saveLastWhisperTarget(final UUID id, final @Nullable UUID lastWhisperTarget) {
if (this.proxiedUserManager instanceof SaveOnChange saveOnChange) {
return saveOnChange.saveLastWhisperTarget(id, lastWhisperTarget);
}
return -1;
}
@Override
public int saveWhisperReplyTarget(final UUID id, final @Nullable UUID whisperReplyTarget) {
if (this.proxiedUserManager instanceof SaveOnChange saveOnChange) {
return saveOnChange.saveWhisperReplyTarget(id, whisperReplyTarget);
}
return -1;
}
@Override
public int addIgnore(final UUID id, final UUID ignoredPlayer) {
if (this.proxiedUserManager instanceof SaveOnChange saveOnChange) {
return saveOnChange.addIgnore(id, ignoredPlayer);
}
return -1;
}
@Override
public int removeIgnore(final UUID id, final UUID ignoredPlayer) {
if (this.proxiedUserManager instanceof SaveOnChange saveOnChange) {
return saveOnChange.removeIgnore(id, ignoredPlayer);
}
return -1;
}
}
@@ -23,10 +23,8 @@ import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import net.draycia.carbon.api.CarbonServer;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.ComponentPlayerResult;
@@ -35,28 +33,22 @@ import net.draycia.carbon.bukkit.users.CarbonPlayerBukkit;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
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;
import static net.kyori.adventure.text.Component.text;
@Singleton
@DefaultQualifier(NonNull.class)
public final class CarbonServerBukkit implements CarbonServer, ForwardingAudience.Single {
private final CarbonChatBukkit chatBukkitEntry;
private final UserManager<CarbonPlayerCommon> userManager;
private final Map<UUID, CarbonPlayerBukkit> userCache = new ConcurrentHashMap<>();
private final UserManager<CarbonPlayerBukkit> userManager;
@Inject
private CarbonServerBukkit(final CarbonChatBukkit chatBukkitEntry, final UserManager<CarbonPlayerCommon> userManager) {
this.chatBukkitEntry = chatBukkitEntry;
this.userManager = userManager;
this.userManager = new BukkitUserManager(userManager);
}
@Override
@@ -74,7 +66,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<CarbonPlayer> result = this.player(player).join();
final @Nullable ComponentPlayerResult<CarbonPlayerBukkit> result = this.userManager.carbonPlayer(player.getUniqueId()).join();
if (result.player() != null) {
players.add(result.player());
@@ -84,59 +76,9 @@ public final class CarbonServerBukkit implements CarbonServer, ForwardingAudienc
return players;
}
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());
}
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());
}
final @Nullable String name = this.resolveName(uuid).join();
if (name != null) {
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<>(null, text("Name not found for uuid!"));
});
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayer>> player(final UUID uuid) {
return this.wrapPlayer(uuid);
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayer>> player(final String username) {
return CompletableFuture.supplyAsync(() -> {
final @Nullable UUID uuid = this.resolveUUID(username).join();
if (uuid != null) {
return this.player(uuid).join();
}
return new ComponentPlayerResult<>(null, text("No UUID found for name."));
});
}
public CompletableFuture<ComponentPlayerResult<CarbonPlayer>> player(final Player player) {
return this.player(player.getUniqueId());
public UserManager<CarbonPlayerBukkit> userManager() {
return this.userManager;
}
@Override
@@ -46,7 +46,7 @@ public record BukkitPlayerCommander(
@Override
public @NonNull CarbonPlayer carbonPlayer() {
return requireNonNull(this.carbon.server().player(this.player.getUniqueId()).join().player(), "No CarbonPlayer for logged in Player!");
return requireNonNull(this.carbon.server().userManager().carbonPlayer(this.player.getUniqueId()).join().player(), "No CarbonPlayer for logged in Player!");
}
}
@@ -31,6 +31,7 @@ import net.draycia.carbon.api.users.ComponentPlayerResult;
import net.draycia.carbon.api.util.KeyedRenderer;
import net.draycia.carbon.api.util.RenderedMessage;
import net.draycia.carbon.bukkit.CarbonChatBukkit;
import net.draycia.carbon.bukkit.users.CarbonPlayerBukkit;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.TextReplacementConfig;
@@ -69,7 +70,7 @@ public final class BukkitChatListener implements Listener {
return;
}
final var playerResult = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
final var playerResult = this.carbonChat.server().userManager().carbonPlayer(event.getPlayer().getUniqueId()).join();
final @Nullable CarbonPlayer sender = playerResult.player();
if (sender == null) {
@@ -135,7 +136,7 @@ public final class BukkitChatListener implements Listener {
for (final var renderer : chatEvent.renderers()) {
try {
if (recipient instanceof Player player) {
final ComponentPlayerResult<CarbonPlayer> targetPlayer = this.carbonChat.server().player(player).join();
final ComponentPlayerResult<CarbonPlayerBukkit> targetPlayer = this.carbonChat.server().userManager().carbonPlayer(player.getUniqueId()).join();
renderedMessage = renderer.render(sender, targetPlayer.player(), renderedMessage.component(), chatEvent.message());
} else {
@@ -161,7 +162,7 @@ public final class BukkitChatListener implements Listener {
for (final var renderer : chatEvent.renderers()) {
try {
if (viewer instanceof Player player) {
final ComponentPlayerResult<CarbonPlayer> targetPlayer = this.carbonChat.server().player(player).join();
final ComponentPlayerResult<CarbonPlayerBukkit> targetPlayer = this.carbonChat.server().userManager().carbonPlayer(player.getUniqueId()).join();
renderedMessage = renderer.render(sender, targetPlayer.player(), renderedMessage.component(), message);
} else {
renderedMessage = renderer.render(sender, viewer, renderedMessage.component(), message);
@@ -49,7 +49,7 @@ public class BukkitPlayerJoinListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
public void onQuit(final PlayerQuitEvent event) {
this.carbonChat.server().player(event.getPlayer().getUniqueId()).thenAccept(result -> {
this.carbonChat.server().userManager().carbonPlayer(event.getPlayer().getUniqueId()).thenAccept(result -> {
if (result.player() == null) {
return;
}
@@ -157,21 +157,29 @@ public final class CarbonPlayerArgument extends CommandArgument<Commander, Carbo
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NoInputProvidedException(
CarbonPlayerParser.class,
commandContext
));
}
final var playerResult = CarbonChatProvider.carbonChat().server().player(input).join();
if (playerResult.player() == null) {
return ArgumentParseResult.failure(new CarbonPlayerParseException(input, commandContext, this.messageService));
}
return CarbonChatProvider.carbonChat().server().resolveUUID(input).thenApply(uuid -> {
if (uuid == null) {
return ArgumentParseResult.<CarbonPlayer>failure(new CarbonPlayerParseException(input, commandContext, this.messageService));
}
inputQueue.remove();
final var playerResult = CarbonChatProvider.carbonChat().server().userManager().carbonPlayer(uuid).join();
return ArgumentParseResult.success(playerResult.player());
if (playerResult.player() == null) {
return ArgumentParseResult.<CarbonPlayer>failure(new CarbonPlayerParseException(input, commandContext, this.messageService));
}
inputQueue.remove();
return ArgumentParseResult.<CarbonPlayer>success(playerResult.player());
}).join();
}
@Override
@@ -94,8 +94,8 @@ public class ContinueCommand extends CarbonCommand {
return;
}
final ComponentPlayerResult<@NonNull CarbonPlayer> result = this.carbonChat.server()
.player(whisperTarget).join();
final ComponentPlayerResult<@NonNull ? extends CarbonPlayer> result = carbonChat.server()
.userManager().carbonPlayer(whisperTarget).join();
final @MonotonicNonNull CarbonPlayer recipient = result.player();
if (sender.equals(recipient)) {
@@ -25,7 +25,6 @@ import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import java.util.Objects;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.command.CarbonCommand;
@@ -90,7 +89,7 @@ public class IgnoreCommand extends CarbonCommand {
if (handler.contains("player")) {
target = handler.get("player");
} else if (handler.flags().contains("uuid")) {
final var result = this.carbonChat.server().player(handler.<UUID>get("uuid")).join();
final var result = carbonChat.server().userManager().carbonPlayer(handler.get("uuid")).join();
target = Objects.requireNonNull(result.player(), "No player found for UUID.");
} else {
this.messageService.ignoreTargetInvalid(sender);
@@ -25,7 +25,6 @@ import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import java.util.Objects;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.command.CarbonCommand;
@@ -90,7 +89,7 @@ public class MuteCommand extends CarbonCommand {
if (handler.contains("player")) {
target = handler.get("player");
} else if (handler.flags().contains("uuid")) {
final var result = this.carbonChat.server().player(handler.<UUID>get("uuid")).join();
final var result = carbonChat.server().userManager().carbonPlayer(handler.get("uuid")).join();
target = Objects.requireNonNull(result.player(), "No player found for UUID.");
} else {
this.messageService.muteNoTarget(sender);
@@ -25,7 +25,6 @@ import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import java.util.Objects;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.command.CarbonCommand;
@@ -90,7 +89,7 @@ public class MuteInfoCommand extends CarbonCommand {
if (handler.contains("player")) {
target = handler.get("player");
} else if (handler.flags().contains("uuid")) {
final var result = this.carbonChat.server().player(handler.<UUID>get("uuid")).join();
final var result = carbonChat.server().userManager().carbonPlayer(handler.get("uuid")).join();
target = Objects.requireNonNull(result.player(), "No player found for UUID.");
} else {
target = sender;
@@ -95,7 +95,7 @@ public class ReplyCommand extends CarbonCommand {
return;
}
final ComponentPlayerResult<CarbonPlayer> result = this.carbonChat.server().player(replyTarget).join();
final ComponentPlayerResult<? extends CarbonPlayer> result = carbonChat.server().userManager().carbonPlayer(replyTarget).join();
final @MonotonicNonNull CarbonPlayer recipient = result.player();
if (sender.equals(recipient)) {
@@ -25,7 +25,6 @@ import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import java.util.Objects;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.command.CarbonCommand;
@@ -91,7 +90,7 @@ public class UnignoreCommand extends CarbonCommand {
if (handler.contains("player")) {
target = handler.get("player");
} else if (handler.flags().contains("uuid")) {
final var result = this.carbonChat.server().player(handler.<UUID>get("uuid")).join();
final var result = carbonChat.server().userManager().carbonPlayer(handler.get("uuid")).join();
target = Objects.requireNonNull(result.player(), "No player found for UUID.");
} else {
this.messageService.ignoreTargetInvalid(sender);
@@ -25,7 +25,6 @@ import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import java.util.Objects;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.command.CarbonCommand;
@@ -90,7 +89,7 @@ public class UnmuteCommand extends CarbonCommand {
if (handler.contains("player")) {
target = handler.get("player");
} else if (handler.flags().contains("uuid")) {
final var result = this.carbonChat.server().player(handler.<UUID>get("uuid")).join();
final var result = carbonChat.server().userManager().carbonPlayer(handler.get("uuid")).join();
target = Objects.requireNonNull(result.player(), "No player found for UUID.");
} else {
this.messageService.unmuteNoTarget(sender);
@@ -24,6 +24,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
@@ -42,6 +43,8 @@ import org.jetbrains.annotations.NotNull;
@DefaultQualifier(NonNull.class)
public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Single {
private final CarbonChat carbonChat = CarbonChatProvider.carbonChat();
protected boolean muted = false;
protected boolean deafened = false;
@@ -124,6 +127,10 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override
public void displayName(final @Nullable Component displayName) {
this.displayName = displayName;
if (this.carbonChat.server().userManager() instanceof SaveOnChange userManager) {
userManager.saveDisplayName(this.uuid(), displayName);
}
}
@Override
@@ -149,6 +156,10 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override
public void muted(final boolean muted) {
this.muted = muted;
if (this.carbonChat.server().userManager() instanceof SaveOnChange userManager) {
userManager.saveMuted(this.uuid(), muted);
}
}
public List<UUID> ignoredPlayers() {
@@ -174,6 +185,14 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
} else {
this.ignoredPlayers.remove(player);
}
if (this.carbonChat.server().userManager() instanceof SaveOnChange userManager) {
if (nowIgnoring) {
userManager.addIgnore(this.uuid, player);
} else {
userManager.removeIgnore(this.uuid, player);
}
}
}
@Override
@@ -189,6 +208,10 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override
public void deafened(final boolean deafened) {
this.deafened = deafened;
if (this.carbonChat.server().userManager() instanceof SaveOnChange userManager) {
userManager.saveDeafened(this.uuid(), deafened);
}
}
@Override
@@ -199,6 +222,10 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override
public void spying(final boolean spying) {
this.spying = spying;
if (this.carbonChat.server().userManager() instanceof SaveOnChange userManager) {
userManager.saveSpying(this.uuid(), spying);
}
}
@Override
@@ -222,8 +249,12 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
}
@Override
public void whisperReplyTarget(final @Nullable UUID uuid) {
this.whisperReplyTarget = uuid;
public void whisperReplyTarget(final @Nullable UUID whisperReplyTarget) {
this.whisperReplyTarget = whisperReplyTarget;
if (this.carbonChat.server().userManager() instanceof SaveOnChange userManager) {
userManager.saveWhisperReplyTarget(this.uuid(), whisperReplyTarget);
}
}
@Override
@@ -232,8 +263,12 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
}
@Override
public void lastWhisperTarget(final @Nullable UUID uuid) {
this.lastWhisperTarget = uuid;
public void lastWhisperTarget(final @Nullable UUID lastWhisperTarget) {
this.lastWhisperTarget = lastWhisperTarget;
if (this.carbonChat.server().userManager() instanceof SaveOnChange userManager) {
userManager.saveLastWhisperTarget(this.uuid(), lastWhisperTarget);
}
}
@Override
@@ -268,6 +303,10 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
@Override
public void selectedChannel(final @Nullable ChatChannel chatChannel) {
this.selectedChannel = chatChannel.key();
if (this.carbonChat.server().userManager() instanceof SaveOnChange userManager) {
userManager.saveSelectedChannel(this.uuid(), chatChannel.key());
}
}
@Override
@@ -0,0 +1,88 @@
/*
* CarbonChat
*
* Copyright (c) 2021 Josua Parks (Vicarious)
* 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 net.draycia.carbon.common.users;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.users.ComponentPlayerResult;
import net.draycia.carbon.api.users.UserManager;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import static net.kyori.adventure.text.Component.text;
@DefaultQualifier(NonNull.class)
public class ProxiedCachingUserManager implements UserManager<CarbonPlayerCommon> {
private final Map<UUID, CarbonPlayerCommon> userCache = new ConcurrentHashMap<>();
private final UserManager<CarbonPlayerCommon> proxiedUserManager;
private final CarbonChat carbonChat;
public ProxiedCachingUserManager(final UserManager<CarbonPlayerCommon> proxiedUserManager, final CarbonChat carbonChat) {
this.proxiedUserManager = proxiedUserManager;
this.carbonChat = carbonChat;
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> carbonPlayer(UUID uuid) {
return CompletableFuture.supplyAsync(() -> {
final @Nullable CarbonPlayerCommon cachedPlayer = this.userCache.get(uuid);
if (cachedPlayer != null) {
return new ComponentPlayerResult<>(cachedPlayer, Component.empty());
}
final ComponentPlayerResult<CarbonPlayerCommon> result = this.proxiedUserManager.carbonPlayer(uuid).join();
if (result.player() != null) {
this.userCache.put(uuid, result.player());
return new ComponentPlayerResult<>(result.player(), Component.empty());
}
final @Nullable String name = this.carbonChat.server().resolveName(uuid).join();
if (name != null) {
final CarbonPlayerCommon player = new CarbonPlayerCommon(name, uuid);
this.userCache.put(uuid, player);
return new ComponentPlayerResult<>(player, Component.empty());
}
return new ComponentPlayerResult<>(null, text("Name not found for uuid!"));
});
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> savePlayer(final CarbonPlayerCommon player) {
return this.proxiedUserManager.savePlayer(player);
}
@Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(final CarbonPlayerCommon player) {
return this.proxiedUserManager.saveAndInvalidatePlayer(player);
}
}
@@ -0,0 +1,50 @@
/*
* CarbonChat
*
* Copyright (c) 2021 Josua Parks (Vicarious)
* 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 net.draycia.carbon.common.users;
import java.util.UUID;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public interface SaveOnChange {
int saveDisplayName(final UUID id, final @Nullable Component component);
int saveMuted(final UUID id, final boolean muted);
int saveDeafened(final UUID id, final boolean deafened);
int saveSpying(final UUID id, final boolean spying);
int saveSelectedChannel(final UUID id, final @Nullable Key selectedChannel);
int saveLastWhisperTarget(final UUID id, final @Nullable UUID lastWhisperTarget);
int saveWhisperReplyTarget(final UUID id, final @Nullable UUID whisperReplyTarget);
int addIgnore(final UUID id, final UUID ignoredPlayer);
int removeIgnore(final UUID id, final UUID ignoredPlayer);
}
@@ -229,4 +229,9 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
return this.carbonPlayerCommon().speechPermitted(message);
}
@Override
public boolean vanished() {
return this.carbonPlayerCommon().vanished();
}
}
@@ -31,7 +31,7 @@ public final class DBType {
return this.basePath;
}
public static final DBType MYSQL = new DBType("queries/sql/");
public static final DBType MYSQL = new DBType("queries/mysql/");
//public static final DBType POSTGRESQL = new DBType("queries/postgresql/");
}
@@ -34,6 +34,13 @@ import net.draycia.carbon.api.users.ComponentPlayerResult;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.config.DatabaseSettings;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.draycia.carbon.common.users.SaveOnChange;
import net.draycia.carbon.common.users.db.jdbi.CarbonPlayerCommonRowMapper;
import net.draycia.carbon.common.users.db.jdbi.ComponentMapper;
import net.draycia.carbon.common.users.db.jdbi.KeyArgumentFactory;
import net.draycia.carbon.common.users.db.jdbi.KeyMapper;
import net.draycia.carbon.common.users.db.jdbi.UUIDArgumentFactory;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -42,13 +49,14 @@ import org.flywaydb.core.Flyway;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.statement.PreparedBatch;
import org.jdbi.v3.core.statement.Update;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.text;
// TODO: Dispatch updates using messaging system when users are modified
@DefaultQualifier(NonNull.class)
public final class MariaDBUserManager implements UserManager<CarbonPlayerCommon> {
public final class MariaDBUserManager implements UserManager<CarbonPlayerCommon>, SaveOnChange {
private final Jdbi jdbi;
@@ -74,7 +82,7 @@ public final class MariaDBUserManager implements UserManager<CarbonPlayerCommon>
Flyway.configure(CarbonChatProvider.carbonChat().getClass().getClassLoader())
.baselineVersion("0")
.baselineOnMigrate(true)
.locations("queries/migrations/sql")
.locations("queries/migrations/mysql")
.dataSource(dataSource)
.validateOnMigrate(true)
.load()
@@ -83,8 +91,11 @@ public final class MariaDBUserManager implements UserManager<CarbonPlayerCommon>
final Jdbi jdbi = Jdbi.create(dataSource)
.registerArrayType(UUID.class, "uuid")
.registerArgument(new UUIDArgumentFactory())
.registerArgument(new KeyArgumentFactory())
.registerColumnMapper(Component.class, new ComponentMapper())
.registerRowMapper(new CarbonPlayerCommonRowMapper());
.registerColumnMapper(Key.class, new KeyMapper())
.registerRowMapper(new CarbonPlayerCommonRowMapper())
.installPlugin(new SqlObjectPlugin());
return new MariaDBUserManager(jdbi);
}
@@ -175,4 +186,49 @@ public final class MariaDBUserManager implements UserManager<CarbonPlayerCommon>
});
}
@Override
public int saveDisplayName(final UUID id, final @Nullable Component displayName) {
return this.jdbi.withExtension(SQLSaveOnChange.class, changeSaver -> changeSaver.saveDisplayName(id, displayName));
}
@Override
public int saveMuted(UUID id, boolean muted) {
return this.jdbi.withExtension(SQLSaveOnChange.class, changeSaver -> changeSaver.saveMuted(id, muted));
}
@Override
public int saveDeafened(UUID id, boolean deafened) {
return this.jdbi.withExtension(SQLSaveOnChange.class, changeSaver -> changeSaver.saveDeafened(id, deafened));
}
@Override
public int saveSpying(UUID id, boolean spying) {
return this.jdbi.withExtension(SQLSaveOnChange.class, changeSaver -> changeSaver.saveSpying(id, spying));
}
@Override
public int saveSelectedChannel(UUID id, @Nullable Key selectedChannel) {
return this.jdbi.withExtension(SQLSaveOnChange.class, changeSaver -> changeSaver.saveSelectedChannel(id, selectedChannel));
}
@Override
public int saveLastWhisperTarget(UUID id, @Nullable UUID lastWhisperTarget) {
return this.jdbi.withExtension(SQLSaveOnChange.class, changeSaver -> changeSaver.saveLastWhisperTarget(id, lastWhisperTarget));
}
@Override
public int saveWhisperReplyTarget(UUID id, @Nullable UUID whisperReplyTarget) {
return this.jdbi.withExtension(SQLSaveOnChange.class, changeSaver -> changeSaver.saveWhisperReplyTarget(id, whisperReplyTarget));
}
@Override
public int addIgnore(UUID id, UUID ignoredPlayer) {
return this.jdbi.withExtension(SQLSaveOnChange.class, changeSaver -> changeSaver.addIgnore(id, ignoredPlayer));
}
@Override
public int removeIgnore(UUID id, UUID ignoredPlayer) {
return this.jdbi.withExtension(SQLSaveOnChange.class, changeSaver -> changeSaver.removeIgnore(id, ignoredPlayer));
}
}
@@ -0,0 +1,61 @@
/*
* CarbonChat
*
* Copyright (c) 2021 Josua Parks (Vicarious)
* 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 net.draycia.carbon.common.users.db;
import java.util.UUID;
import net.draycia.carbon.common.users.SaveOnChange;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
@DefaultQualifier(NonNull.class)
public interface SQLSaveOnChange extends SaveOnChange {
@SqlUpdate("UPDATE carbon_users SET (displayname = :displayname) WHERE id = UNHEX(REPLACE(:id, '-', ''))")
int saveDisplayName(final UUID id, final @Nullable Component component);
@SqlUpdate("UPDATE carbon_users SET (muted = :muted) WHERE id = UNHEX(REPLACE(:id, '-', ''))")
int saveMuted(final UUID id, final boolean muted);
@SqlUpdate("UPDATE carbon_users SET (deafened = :deafened) WHERE id = UNHEX(REPLACE(:id, '-', ''))")
int saveDeafened(final UUID id, final boolean deafened);
@SqlUpdate("UPDATE carbon_users SET (spying = :spying) WHERE id = UNHEX(REPLACE(:id, '-', ''))")
int saveSpying(final UUID id, final boolean spying);
@SqlUpdate("UPDATE carbon_users SET selectedchannel = :selectedChannel WHERE id = UNHEX(REPLACE(:id, '-', ''))")
int saveSelectedChannel(final UUID id, final @Nullable Key selectedChannel);
@SqlUpdate("UPDATE carbon_users SET (lastwhispertarget = :lastWhisperTarget) WHERE id = UNHEX(REPLACE(:id, '-', ''))")
int saveLastWhisperTarget(final UUID id, final @Nullable UUID lastWhisperTarget);
@SqlUpdate("UPDATE carbon_users SET (whisperreplytarget = :whisperReplyTarget) WHERE id = UNHEX(REPLACE(:id, '-', ''))")
int saveWhisperReplyTarget(final UUID id, final @Nullable UUID whisperReplyTarget);
@SqlUpdate("INSERT INTO carbon_ignores VALUES (id = UNHEX(REPLACE(:id, '-', '')), ignoredplayer = :ignoredPlayer)")
int addIgnore(final UUID id, final UUID ignoredPlayer);
@SqlUpdate("DELETE FROM carbon_ignores WHERE (id = UNHEX(REPLACE(:id, '-', '')), ignoredplayer = UNHEX(REPLACE(:ignoredPlayer, '-', '')))")
int removeIgnore(final UUID id, final UUID ignoredPlayer);
}
@@ -17,7 +17,7 @@
* 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 net.draycia.carbon.common.users.db;
package net.draycia.carbon.common.users.db.jdbi;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -26,6 +26,7 @@ import net.draycia.carbon.common.users.CarbonPlayerCommon;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.intellij.lang.annotations.Subst;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
@@ -35,17 +36,18 @@ public class CarbonPlayerCommonRowMapper implements RowMapper<CarbonPlayerCommon
public CarbonPlayerCommon map(final ResultSet rs, final StatementContext ctx) throws SQLException {
final String id = rs.getString("id");
final @Nullable String lastWhisperTarget = rs.getString("lastwhispertarget");
final @Nullable String whisperreplytarget = rs.getString("whisperreplytarget");
final @Nullable String whisperReplyTarget = rs.getString("whisperreplytarget");
final @Nullable @Subst("carbon:global") String selectedChannel = rs.getString("selectedchannel");
return new CarbonPlayerCommon(
rs.getBoolean("muted"),
rs.getBoolean("deafened"),
rs.getObject("selectedchannel", Key.class),
selectedChannel == null ? null : Key.key(selectedChannel),
rs.getString("username"),
UUID.fromString(id),
rs.getObject("displayname", Component.class),
lastWhisperTarget == null ? null : UUID.fromString(lastWhisperTarget),
whisperreplytarget == null ? null : UUID.fromString(whisperreplytarget),
whisperReplyTarget == null ? null : UUID.fromString(whisperReplyTarget),
rs.getBoolean("spying")
);
}
@@ -17,7 +17,7 @@
* 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 net.draycia.carbon.common.users.db;
package net.draycia.carbon.common.users.db.jdbi;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -0,0 +1,40 @@
/*
* CarbonChat
*
* Copyright (c) 2021 Josua Parks (Vicarious)
* 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 net.draycia.carbon.common.users.db.jdbi;
import java.sql.Types;
import java.util.UUID;
import net.kyori.adventure.key.Key;
import org.jdbi.v3.core.argument.AbstractArgumentFactory;
import org.jdbi.v3.core.argument.Argument;
import org.jdbi.v3.core.config.ConfigRegistry;
public final class KeyArgumentFactory extends AbstractArgumentFactory<Key> {
public KeyArgumentFactory() {
super(Types.VARCHAR);
}
@Override
public Argument build(final Key value, final ConfigRegistry config) {
return (position, statement, ctx) -> statement.setString(position, value.toString());
}
}
@@ -0,0 +1,42 @@
/*
* CarbonChat
*
* Copyright (c) 2021 Josua Parks (Vicarious)
* 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 net.draycia.carbon.common.users.db.jdbi;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;
public class KeyMapper implements ColumnMapper<Key> {
@Override
public Key map(final ResultSet r, final int columnNumber, final StatementContext ctx) throws SQLException {
return Key.key(r.getString(columnNumber));
}
@Override
public Key map(final ResultSet r, final String columnLabel, final StatementContext ctx) throws SQLException {
return Key.key(r.getString(columnLabel));
}
}
@@ -17,7 +17,7 @@
* 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 net.draycia.carbon.common.users.db;
package net.draycia.carbon.common.users.db.jdbi;
import java.sql.Types;
import java.util.UUID;