From ad4e00a6ab717d6fe9790b4e66ebf775c9872348 Mon Sep 17 00:00:00 2001 From: kento2 Date: Thu, 16 Apr 2026 21:42:17 +0200 Subject: [PATCH] . --- core-bukkit-api/build.gradle.kts | 4 +- .../java/de/kentoj/scrow/bukkit/ScrowAPI.java | 10 +-- .../de/kentoj/scrow/bukkit/email/Email.java | 18 ---- .../kentoj/scrow/bukkit/email/EmailDraft.java | 28 ------ .../scrow/bukkit/email/EmailService.java | 25 ------ .../scrow/bukkit/friends/FriendsService.java | 2 +- .../scrow/bukkit/friends/Friendship.java | 11 +-- .../scrow/bukkit/friends/OrderedUUIDPair.java | 28 ++++++ core-bukkit-impl/build.gradle.kts | 1 - .../kentoj/scrow/bukkit/CoreImplPlugin.java | 6 +- .../kentoj/scrow/bukkit/ScrowAPISurface.java | 3 - .../scrow/bukkit/economy/CoinsCommand.java | 24 ++--- .../bukkit/friends/command/FriendCommand.java | 20 ++--- .../friends/command/FriendListLiteral.java | 53 +++++------ .../friends/friendship/BsonFriendship.java | 36 -------- .../friends/friendship/FriendshipImpl.java | 20 +++++ .../friendship/FriendshipRepository.java | 6 +- .../friends/friendship/FriendshipUtils.java | 22 ----- .../friendship/MongoFriendshipRepository.java | 89 ++++++++++--------- 19 files changed, 153 insertions(+), 253 deletions(-) delete mode 100644 core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/Email.java delete mode 100644 core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/EmailDraft.java delete mode 100644 core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/EmailService.java create mode 100644 core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/OrderedUUIDPair.java delete mode 100644 core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/BsonFriendship.java create mode 100644 core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipImpl.java delete mode 100644 core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipUtils.java diff --git a/core-bukkit-api/build.gradle.kts b/core-bukkit-api/build.gradle.kts index a604487..211bc70 100644 --- a/core-bukkit-api/build.gradle.kts +++ b/core-bukkit-api/build.gradle.kts @@ -36,7 +36,9 @@ dependencies { api("org.spigotmc:spigot-api:1.21.11-R0.1-SNAPSHOT") api("net.kyori:adventure-platform-bukkit:4.4.1") - api("de.kentoj:kencommandapi-core:1.2.0") + api("de.kentoj:kencommandapi-core:2.0.1") + implementation("de.kentoj:kencommandapi-bukkit:2.0.1") + api("de.kentoj:kencommandapi-bukkit:2.0.1") } publishing { diff --git a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/ScrowAPI.java b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/ScrowAPI.java index ffd8a59..9d96b04 100644 --- a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/ScrowAPI.java +++ b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/ScrowAPI.java @@ -1,15 +1,11 @@ package de.kentoj.scrow.bukkit; import com.mongodb.reactivestreams.client.MongoDatabase; -import de.kentoj.kencommandapi.api.KenCommandApi; -import de.kentoj.kencommandapi.api.processing.CommandContext; -import de.kentoj.kencommandapi.api.structure.node.CommandNode; +import de.kentoj.kencommandapi.BukkitKenCommandApi; import de.kentoj.scrow.bukkit.friends.FriendsService; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.Setter; -import org.bukkit.command.CommandSender; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import reactor.core.scheduler.Scheduler; @@ -20,7 +16,7 @@ public class ScrowAPI { @Getter private static @Nullable MongoDatabase database; @Getter - private static KenCommandApi> commandManager; + private static BukkitKenCommandApi commandManager; @Getter private static Scheduler minecraftScheduler; @Getter @@ -39,7 +35,7 @@ public class ScrowAPI { } @ApiStatus.Internal - public static void setCommandManager(KenCommandApi> commandManager) { + public static void setCommandManager(BukkitKenCommandApi commandManager) { ScrowAPI.commandManager = commandManager; } diff --git a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/Email.java b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/Email.java deleted file mode 100644 index 1b5ac62..0000000 --- a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/Email.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.kentoj.scrow.bukkit.email; - -import org.jetbrains.annotations.Nullable; - -import java.time.Instant; -import java.util.UUID; - -public interface Email extends EmailDraft { - - Long getId(); - - /** - * @return UUID of player the mail is sent to or null if console - */ - @Nullable UUID getFrom(); - - @Nullable Instant getSentAt(); -} diff --git a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/EmailDraft.java b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/EmailDraft.java deleted file mode 100644 index cfcf406..0000000 --- a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/EmailDraft.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.kentoj.scrow.bukkit.email; - -import org.jetbrains.annotations.Nullable; - -import java.util.UUID; - -public interface EmailDraft { - - /** - * @return UUID of player sending the mail or null if console - */ - @Nullable UUID getFrom(); - - /** - * @return non-empty string specifying the subject - */ - String getSubject(); - - /** - * @return body of the message - */ - String getBody(); - - /** - * @return id of the mail this mail is replying to or null if not replying to any email - */ - @Nullable Long getReplyTo(); -} diff --git a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/EmailService.java b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/EmailService.java deleted file mode 100644 index 7778116..0000000 --- a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/email/EmailService.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.kentoj.scrow.bukkit.email; - -import org.jetbrains.annotations.NotNull; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; - -import java.util.Set; -import java.util.UUID; - -/** - * like email but for minecraft - */ -public interface EmailService { - - Mono<@NotNull Email> sendMail(UUID to, EmailDraft emailDraft); - Flux<@NotNull Email> sendMailToMany(Set to, EmailDraft emailDraft); - - Flux<@NotNull Email> fetchMails(UUID to); - - /** - * @param to uuid of player whose inbox to operate on - * @param id id of the email - */ - Mono<@NotNull Void> deleteMail(UUID to, long id); -} diff --git a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/FriendsService.java b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/FriendsService.java index ce13313..060dc50 100644 --- a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/FriendsService.java +++ b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/FriendsService.java @@ -15,7 +15,7 @@ public interface FriendsService { * @param other id of the player the initiator wants to end the friendship with * @throws FriendRequestUsageException */ - Mono<@NotNull Boolean> removeFriend(UUID initiator, UUID other); + Mono<@NotNull Boolean> removeFriend(Friendship friendship); /** * @param playerId diff --git a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/Friendship.java b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/Friendship.java index be4cf13..3eb61f0 100644 --- a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/Friendship.java +++ b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/Friendship.java @@ -1,16 +1,11 @@ package de.kentoj.scrow.bukkit.friends; -import de.kentoj.scrow.bukkit.util.pair.Tuple2; - import java.time.Instant; import java.util.UUID; public interface Friendship { - Tuple2 getPlayerIds(); - Instant getCreatedAt(); - default UUID getOther(UUID self) { - final var ids = getPlayerIds(); - return ids.getFirst() != self ? ids.getFirst() : ids.getSecond(); - } + OrderedUUIDPair getUuids(); + + Instant getCreatedAt(); } diff --git a/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/OrderedUUIDPair.java b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/OrderedUUIDPair.java new file mode 100644 index 0000000..e472f73 --- /dev/null +++ b/core-bukkit-api/src/main/java/de/kentoj/scrow/bukkit/friends/OrderedUUIDPair.java @@ -0,0 +1,28 @@ +package de.kentoj.scrow.bukkit.friends; + +import com.google.common.base.Preconditions; +import lombok.Getter; + +import java.util.UUID; + +@Getter +public class OrderedUUIDPair { + private final UUID first; + private final UUID second; + + public OrderedUUIDPair(UUID uuid1, UUID uuid2) { + Preconditions.checkArgument(uuid1 != uuid2); + if (uuid1.compareTo(uuid2) < 0) { + this.first = uuid1; + this.second = uuid2; + } else { + this.first = uuid2; + this.second = uuid1; + } + } + + public UUID getOther(UUID self) { + final var ids = getFirst(); + return getFirst().equals(self) ? getSecond() : getFirst(); + } +} diff --git a/core-bukkit-impl/build.gradle.kts b/core-bukkit-impl/build.gradle.kts index fa19958..9673d07 100644 --- a/core-bukkit-impl/build.gradle.kts +++ b/core-bukkit-impl/build.gradle.kts @@ -22,7 +22,6 @@ dependencies { implementation(project(":core-bukkit-api")) implementation("org.spigotmc:spigot-api:1.21.11-R0.1-SNAPSHOT") - implementation("de.kentoj:kencommandapi-bukkit:1.2.0") } tasks { diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/CoreImplPlugin.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/CoreImplPlugin.java index c6a3d1b..78b295d 100644 --- a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/CoreImplPlugin.java +++ b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/CoreImplPlugin.java @@ -1,6 +1,5 @@ package de.kentoj.scrow.bukkit; -import de.kentoj.kencommandapi.BukkitKenCommandApi; import de.kentoj.scrow.bukkit.economy.CoinsCommand; import de.kentoj.scrow.bukkit.friends.command.FriendCommand; import org.bukkit.plugin.java.JavaPlugin; @@ -15,9 +14,8 @@ public class CoreImplPlugin extends JavaPlugin { ScrowAPISurface.initScrowAPI(this, false); { - BukkitKenCommandApi kenCommandApi = new BukkitKenCommandApi(); - ScrowAPI.getCommandManager().register(new CoinsCommand().create(), this); - ScrowAPI.getCommandManager().register(new FriendCommand().create(), this); + ScrowAPI.getCommandManager().register(new CoinsCommand().getRootNode()); + ScrowAPI.getCommandManager().register(new FriendCommand().getRootNode()); } { diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/ScrowAPISurface.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/ScrowAPISurface.java index 943616b..c4c3144 100644 --- a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/ScrowAPISurface.java +++ b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/ScrowAPISurface.java @@ -5,11 +5,8 @@ import com.mongodb.MongoClientSettings; import com.mongodb.reactivestreams.client.MongoClient; import com.mongodb.reactivestreams.client.MongoClients; import de.kentoj.kencommandapi.BukkitKenCommandApi; -import de.kentoj.scrow.bukkit.cmds2.CommandFactoryImpl; -import de.kentoj.scrow.bukkit.cmds2.bukkit.BukkitCommandManager; import de.kentoj.scrow.bukkit.economy.InMemoryEconomyService; import de.kentoj.scrow.bukkit.economy.MongoEconomyService; -import de.kentoj.scrow.bukkit.command.Commands; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.bson.UuidRepresentation; diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/economy/CoinsCommand.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/economy/CoinsCommand.java index e253a5e..d0f477c 100644 --- a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/economy/CoinsCommand.java +++ b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/economy/CoinsCommand.java @@ -1,44 +1,48 @@ package de.kentoj.scrow.bukkit.economy; +import de.kentoj.kencommandapi.BukkitCommandContext; import de.kentoj.kencommandapi.api.processing.CommandContext; import de.kentoj.kencommandapi.api.structure.argument.CommandArgument; import de.kentoj.kencommandapi.api.structure.argument.types.IntegerArgumentType; +import de.kentoj.kencommandapi.api.structure.node.CommandNode; import de.kentoj.kencommandapi.type.OfflinePlayerArgumentType; import de.kentoj.scrow.bukkit.ScrowAPI; +import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import reactor.core.scheduler.Schedulers; import java.util.logging.Level; public class CoinsCommand { - private final CommandArgument, OfflinePlayer> playerArg; - private final CommandArgument, Integer> amountArg; + @Getter + private final CommandNode rootNode; + private final CommandArgument playerArg; + private final CommandArgument amountArg; public CoinsCommand() { - var rootCommand = ScrowAPI.getCommandManager().createNode("coins"); + this.rootNode = ScrowAPI.getCommandManager().createNode("coins"); this.playerArg = ScrowAPI.getCommandManager().createArgument("player", OfflinePlayerArgumentType.getInstance()); - this.amountArg = ScrowAPI.getCommandManager().createArgument("amount", IntegerArgumentType.getInstance()); + this.amountArg = ScrowAPI.getCommandManager().createArgument("amount", new IntegerArgumentType<>()); { var setLiteral = ScrowAPI.getCommandManager().createNode("set"); - rootCommand.addLiteral(setLiteral); + rootNode.addLiteral(setLiteral); setLiteral.addArgument(playerArg); + setLiteral.addArgument(amountArg); setLiteral.setExecutor(this::setCoins); } { var getLiteral = ScrowAPI.getCommandManager().createNode("get"); - rootCommand.addLiteral(getLiteral); + rootNode.addLiteral(getLiteral); getLiteral.addArgument(playerArg); - getLiteral.addArgument(amountArg); getLiteral.setExecutor(this::getCoins); } } - private void getCoins(CommandContext ctx) { + private void getCoins(BukkitCommandContext ctx) { var player = ctx.getArg(playerArg); ScrowAPI.getEconomyService() @@ -57,7 +61,7 @@ public class CoinsCommand { }); } - public void setCoins(CommandContext ctx) { + private void setCoins(BukkitCommandContext ctx) { var player = ctx.getArg(playerArg); var amount = ctx.getArg(amountArg); diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/command/FriendCommand.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/command/FriendCommand.java index a28eacc..c4252e7 100644 --- a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/command/FriendCommand.java +++ b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/command/FriendCommand.java @@ -1,18 +1,18 @@ package de.kentoj.scrow.bukkit.friends.command; -import de.kentoj.scrow.bukkit.command.Commands; -import de.kentoj.scrow.bukkit.command.literal.RootLiteral; +import de.kentoj.kencommandapi.BukkitCommandContext; +import de.kentoj.kencommandapi.api.structure.node.CommandNode; +import de.kentoj.scrow.bukkit.ScrowAPI; +import lombok.Getter; +import org.bukkit.command.CommandSender; public class FriendCommand { + @Getter + private final CommandNode rootNode; + public FriendCommand() { - rootLiteral = Commands.root("friend"); - rootLiteral.addAliases("f", "friends"); - - rootLiteral.addLiteral(new FriendListLiteral().create()); - } - - public RootLiteral create() { - return rootLiteral; + this.rootNode = ScrowAPI.getCommandManager().createNode("friends", "f", "friend"); + rootNode.addLiteral(new FriendListLiteral().getNode()); } } diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/command/FriendListLiteral.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/command/FriendListLiteral.java index 246eceb..0bfaa57 100644 --- a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/command/FriendListLiteral.java +++ b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/command/FriendListLiteral.java @@ -1,48 +1,37 @@ package de.kentoj.scrow.bukkit.friends.command; +import de.kentoj.kencommandapi.BukkitCommandContext; +import de.kentoj.kencommandapi.api.structure.node.CommandNode; import de.kentoj.scrow.bukkit.ScrowAPI; -import de.kentoj.scrow.bukkit.command.Commands; -import de.kentoj.scrow.bukkit.command.execution.CommandContext; -import de.kentoj.scrow.bukkit.command.literal.Literal; +import lombok.Getter; import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; +import org.bukkit.command.CommandSender; import reactor.core.scheduler.Schedulers; -import java.util.UUID; - public class FriendListLiteral { - private final Literal literal; + @Getter + private final CommandNode node; public FriendListLiteral() { - literal = Commands.literal("list"); - literal.setExecutor(this::listFriends); + this.node = ScrowAPI.getCommandManager().createNode("list"); + node.setExecutor(this::friendList); } - public Literal create() { - return literal; - } - - private void listFriends(CommandContext ctx) { - ScrowAPI.getFriendsService().getFriendships(ctx.getSenderPlayer().getUniqueId()) - .subscribeOn(Schedulers.boundedElastic()) - .collectList() + private void friendList(BukkitCommandContext ctx) { + var ownUid = ctx.getPlayerSender().getUniqueId(); + ctx.getPlayerSender().sendMessage("friends:"); + ScrowAPI.getFriendsService().getFriendships(ownUid) .publishOn(ScrowAPI.getMinecraftScheduler()) - .subscribe(friendships -> { - if (friendships.isEmpty()) { - ctx.getSender().sendMessage("You have no friends."); - } else { - ctx.getSender().sendMessage("You have " + friendships.size() + " friends:"); - friendships.forEach(friendship -> { - final UUID friendId = friendship.getOther(ctx.getSenderPlayer().getUniqueId()); - // TODO get name even if never online on the network - final OfflinePlayer friend = Bukkit.getOfflinePlayer(friendId); - ctx.getSender().sendMessage("- " + friend.getName()); - }); - } - }, error -> { - ctx.getSender().sendMessage("Error fetching friends"); - // TODO improve handling + .subscribeOn(Schedulers.boundedElastic()) + .map(friendship -> { + var friendPlayerUid = friendship.getOther(ownUid); + var friendPlayerName = Bukkit.getOfflinePlayer(friendPlayerUid).getName(); + return "- " + friendPlayerName; + }).doOnNext(line -> { + ctx.getPlayerSender().sendMessage(line); + }).doOnComplete(() -> { + ctx.getPlayerSender().sendMessage("EOF"); }); } } diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/BsonFriendship.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/BsonFriendship.java deleted file mode 100644 index 1dbaf7b..0000000 --- a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/BsonFriendship.java +++ /dev/null @@ -1,36 +0,0 @@ -package de.kentoj.scrow.bukkit.friends.friendship; - -import com.google.common.base.Preconditions; -import de.kentoj.scrow.bukkit.friends.Friendship; -import de.kentoj.scrow.bukkit.util.pair.Tuple2; -import lombok.Value; -import org.bson.codecs.pojo.annotations.BsonCreator; -import org.bson.codecs.pojo.annotations.BsonProperty; -import org.bson.types.ObjectId; - -import java.time.Instant; -import java.util.UUID; - - -@Value -public class BsonFriendship implements Friendship { - ObjectId id; - Tuple2 playerIds; - Instant createdAt; - - @BsonCreator - public BsonFriendship( - @BsonProperty("_id") ObjectId id, - @BsonProperty("playerIds") Tuple2 playerIds, - @BsonProperty("createdAt") Instant createdAt - ) { - Preconditions.checkArgument(playerIds.getFirst() != playerIds.getSecond()); - this.id = id; - this.playerIds = FriendshipUtils.toOrderedTuple(playerIds); - this.createdAt = createdAt; - } - - public BsonFriendship(Tuple2 playerIds, Instant createdAt) { - this(ObjectId.get(), playerIds, createdAt); - } -} diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipImpl.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipImpl.java new file mode 100644 index 0000000..09d17c4 --- /dev/null +++ b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipImpl.java @@ -0,0 +1,20 @@ +package de.kentoj.scrow.bukkit.friends.friendship; + +import de.kentoj.scrow.bukkit.friends.Friendship; +import de.kentoj.scrow.bukkit.friends.OrderedUUIDPair; +import lombok.RequiredArgsConstructor; +import lombok.Value; + +import java.time.Instant; +import java.util.UUID; + +@Value +@RequiredArgsConstructor +public class FriendshipImpl implements Friendship { + OrderedUUIDPair uuids; + Instant createdAt; + + public FriendshipImpl(UUID uuid1, UUID uuid2, Instant createdAt) { + this(new OrderedUUIDPair(uuid1, uuid2), createdAt); + } +} diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipRepository.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipRepository.java index 516a0e7..4ba662a 100644 --- a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipRepository.java +++ b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipRepository.java @@ -13,15 +13,13 @@ public interface FriendshipRepository { Flux<@NotNull Friendship> getFriendships(UUID playerId); - Mono<@NotNull Friendship> getFriendship(Tuple2 playerIds); - /** * @return True if friendship saved, false if friendship already exists */ - Mono<@NotNull Boolean> saveFriendship(Tuple2 playerIds, Instant createdAt); + Mono<@NotNull Boolean> saveFriendship(Friendship playerIds); /** * @return True if friendship deleted, false if none found */ - Mono<@NotNull Boolean> deleteFriendship(Tuple2 playerIds); + Mono<@NotNull Boolean> deleteFriendship(UUID uuid1, UUID uuid2); } diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipUtils.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipUtils.java deleted file mode 100644 index 1efa7c1..0000000 --- a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/FriendshipUtils.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.kentoj.scrow.bukkit.friends.friendship; - -import de.kentoj.scrow.bukkit.util.pair.Tuple2; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -import java.util.UUID; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -class FriendshipUtils { - - /** - * ensures constant order - * @return an ordered tuple - */ - public static Tuple2 toOrderedTuple(Tuple2 playerIds) { - if (playerIds.getFirst().compareTo(playerIds.getSecond()) < 0) - return new Tuple2<>(playerIds.getFirst(), playerIds.getSecond()); - else - return new Tuple2<>(playerIds.getSecond(), playerIds.getFirst()); - } -} diff --git a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/MongoFriendshipRepository.java b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/MongoFriendshipRepository.java index 075d87b..b371f83 100644 --- a/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/MongoFriendshipRepository.java +++ b/core-bukkit-impl/src/main/java/de/kentoj/scrow/bukkit/friends/friendship/MongoFriendshipRepository.java @@ -8,12 +8,13 @@ import com.mongodb.client.model.Indexes; import com.mongodb.reactivestreams.client.MongoCollection; import com.mongodb.reactivestreams.client.MongoDatabase; import de.kentoj.scrow.bukkit.friends.Friendship; -import de.kentoj.scrow.bukkit.util.pair.Tuple2; +import de.kentoj.scrow.bukkit.friends.OrderedUUIDPair; +import org.bson.Document; import org.jetbrains.annotations.NotNull; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.time.Instant; +import java.sql.Date; import java.util.UUID; public class MongoFriendshipRepository implements FriendshipRepository { @@ -28,8 +29,8 @@ public class MongoFriendshipRepository implements FriendshipRepository { database.createCollection(COLLECTION_NAME); collection().createIndex( Indexes.compoundIndex( - Indexes.ascending("playerIds.first"), - Indexes.ascending("playerIds.second") + Indexes.ascending("first"), + Indexes.ascending("second") ), new IndexOptions().unique(true) ); @@ -39,48 +40,50 @@ public class MongoFriendshipRepository implements FriendshipRepository { public Flux<@NotNull Friendship> getFriendships(UUID playerId) { return Flux.from(collection() .find(Filters.or( - Filters.eq("playerIds.first", playerId), - Filters.eq("playerIds.second", playerId) - ))); - } - - @Override - public Mono<@NotNull Friendship> getFriendship(Tuple2 playerIds) { - playerIds = FriendshipUtils.toOrderedTuple(playerIds); - return Mono.from(collection() - .find(Filters.and( - Filters.eq("playerIds.first", playerIds.getFirst()), - Filters.eq("playerIds.second", playerIds.getSecond()) - ))); - } - - @Override - public Mono<@NotNull Boolean> saveFriendship(Tuple2 playerIds, Instant createdAt) { - var friendship = new BsonFriendship(playerIds, createdAt); - return Mono.from(collection() - .insertOne(friendship) - ) - .map(__ -> true) - .onErrorResume(e -> { - if (!(e instanceof MongoWriteException)) return Mono.error(e); - if (((MongoWriteException) e).getError().getCategory() != ErrorCategory.DUPLICATE_KEY) - return Mono.error(e); - return Mono.just(false); - }); - } - - @Override - public Mono<@NotNull Boolean> deleteFriendship(Tuple2 playerIds) { - playerIds = FriendshipUtils.toOrderedTuple(playerIds); - return Mono.from(collection() - .deleteOne(Filters.and( - Filters.eq("playerIds.first", playerIds.getFirst()), - Filters.eq("playerIds.second", playerIds.getSecond()) + Filters.eq("first", playerId), + Filters.eq("second", playerId) ))) + .map(this::fromDocument); + } + + @Override + public Mono<@NotNull Boolean> saveFriendship(Friendship friendship) { + return Mono.from(collection() + .insertOne(toDocument(friendship))) + .thenReturn(true) + .onErrorResume(MongoWriteException.class, e -> + e.getError().getCategory() == ErrorCategory.DUPLICATE_KEY + ? Mono.just(Boolean.FALSE) + : Mono.error(e) + ); + } + + @Override + public Mono<@NotNull Boolean> deleteFriendship(UUID uuid1, UUID uuid2) { + var uuids = new OrderedUUIDPair(uuid1, uuid2); + return Mono.from(collection() + .deleteOne(Filters.and( + Filters.eq("first", uuids.getFirst()), + Filters.eq("second", uuids.getSecond()) + ))) .map(res -> res.getDeletedCount() > 0); } - private MongoCollection collection() { - return database.getCollection(COLLECTION_NAME, BsonFriendship.class); + private MongoCollection collection() { + return database.getCollection(COLLECTION_NAME); + } + + private @NotNull Friendship fromDocument(Document doc) { + var first = UUID.fromString(doc.getString("first")); + var second = UUID.fromString(doc.getString("second")); + var createdAt = doc.getDate("createdAt").toInstant(); + return new FriendshipImpl(first, second, createdAt); + } + + private @NotNull Document toDocument(Friendship friendship) { + return new Document() + .append("first", friendship.getUuids().getFirst().toString()) + .append("second", friendship.getUuids().getSecond().toString()) + .append("createdAt", Date.from(friendship.getCreatedAt())); } }