This commit is contained in:
kento2 2026-04-20 15:13:15 +02:00
parent c234c600fe
commit 8eb15179db
16 changed files with 152 additions and 98 deletions

View file

@ -2,29 +2,21 @@ package de.kentoj.scrow.bukkit;
import de.kentoj.scrow.bukkit.economy.CoinsCommand;
import de.kentoj.scrow.bukkit.friends.command.FriendCommand;
import de.kentoj.scrow.bukkit.friends.friendship.MongoFriendshipDAO;
import de.kentoj.scrow.bukkit.friends.request.FriendRequestImpl;
import de.kentoj.scrow.bukkit.friends.request.MongoFriendRequestDAO;
import org.bukkit.plugin.java.JavaPlugin;
import reactor.core.publisher.Mono;
import java.time.Instant;
import java.util.UUID;
public class CoreImplPlugin extends JavaPlugin {
public static boolean ENABLE_DATABASE = true;
public static boolean ENABLE_DATABASE = false;
@Override
public void onEnable() {
ScrowAPISurface.initScrowAPI(this, ENABLE_DATABASE);
{
if (ENABLE_DATABASE) {
var friendshipDAO = new MongoFriendshipDAO(ScrowAPI.getDatabase());
var friendRequestsDAO = new MongoFriendRequestDAO(ScrowAPI.getDatabase());
ScrowAPI.getCommandManager().register(new FriendCommand(friendshipDAO, friendRequestsDAO).getRootNode());
}
ScrowAPI.getCommandManager().register(new FriendCommand().getRootNode());
ScrowAPI.getCommandManager().register(new CoinsCommand().getRootNode());
}

View file

@ -7,6 +7,10 @@ import com.mongodb.reactivestreams.client.MongoClients;
import de.kentoj.kencommandapi.BukkitKenCommandApi;
import de.kentoj.scrow.bukkit.economy.InMemoryEconomyService;
import de.kentoj.scrow.bukkit.economy.MongoEconomyService;
import de.kentoj.scrow.bukkit.friends.friendship.InMemoryFriendshipService;
import de.kentoj.scrow.bukkit.friends.friendship.MongoFriendshipService;
import de.kentoj.scrow.bukkit.friends.request.InMemoryFriendRequestService;
import de.kentoj.scrow.bukkit.friends.request.MongoFriendRequestService;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.bson.UuidRepresentation;
@ -43,14 +47,16 @@ public class ScrowAPISurface {
ScrowAPI.setDatabase(mongoClient.getDatabase("scrow"));
}
ScrowAPI.setEconomyService(
ScrowAPI.getDatabase() != null ?
new MongoEconomyService(ScrowAPI.getDatabase()) : new InMemoryEconomyService()
);
ScrowAPI.setEconomyService(ScrowAPI.getDatabase() != null ?
new MongoEconomyService(ScrowAPI.getDatabase()) : new InMemoryEconomyService());
ScrowAPI.setFriendRequestService(ScrowAPI.getDatabase() != null ?
new MongoFriendRequestService(ScrowAPI.getDatabase()) : new InMemoryFriendRequestService());
ScrowAPI.setFriendshipService(ScrowAPI.getDatabase() != null ?
new MongoFriendshipService(ScrowAPI.getDatabase()) : new InMemoryFriendshipService());
ScrowAPI.setCommandManager(new BukkitKenCommandApi());
// TODO friends service
}
public static void destroyScrowAPI() {

View file

@ -5,8 +5,6 @@ import de.kentoj.kencommandapi.api.structure.node.CommandNode;
import de.kentoj.kencommandapi.type.OfflinePlayerArgumentType;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.friends.handler.FriendAddHandler;
import de.kentoj.scrow.bukkit.friends.friendship.FriendshipDAO;
import de.kentoj.scrow.bukkit.friends.request.FriendRequestDAO;
import lombok.Getter;
import org.bukkit.command.CommandSender;
@ -15,14 +13,14 @@ public class FriendAddLiteral {
@Getter
private final CommandNode<CommandSender, BukkitCommandContext> node;
public FriendAddLiteral(FriendshipDAO friendshipDAO, FriendRequestDAO friendRequestDAO) {
public FriendAddLiteral() {
var cmdManager = ScrowAPI.getCommandManager();
var playerArg = cmdManager.createArgument("player", OfflinePlayerArgumentType.getInstance());
this.node = cmdManager.createNode("add");
this.node.addArgument(playerArg);
this.node.setExecutor(ctx -> {
var handler = new FriendAddHandler(ctx.getPlayerSender(), ctx.getArg(playerArg), friendshipDAO, friendRequestDAO);
var handler = new FriendAddHandler(ctx.getPlayerSender(), ctx.getArg(playerArg));
handler.handle();
});
}

View file

@ -3,8 +3,6 @@ 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.friends.friendship.FriendshipDAO;
import de.kentoj.scrow.bukkit.friends.request.FriendRequestDAO;
import lombok.Getter;
import org.bukkit.command.CommandSender;
@ -13,11 +11,11 @@ public class FriendCommand {
@Getter
private final CommandNode<CommandSender, BukkitCommandContext> rootNode;
public FriendCommand(FriendshipDAO friendshipDAO, FriendRequestDAO friendRequestDAO) {
public FriendCommand() {
this.rootNode = ScrowAPI.getCommandManager().createNode("friends", "f", "friend");
rootNode.addLiteral(new FriendListLiteral(friendshipDAO).getNode());
rootNode.addLiteral(new FriendAddLiteral(friendshipDAO, friendRequestDAO).getNode());
rootNode.addLiteral(new FriendRemoveLiteral(friendshipDAO).getNode());
rootNode.addLiteral(new FriendRequestsLiteral(friendRequestDAO).getNode());
rootNode.addLiteral(new FriendListLiteral().getNode());
rootNode.addLiteral(new FriendAddLiteral().getNode());
rootNode.addLiteral(new FriendRemoveLiteral().getNode());
rootNode.addLiteral(new FriendRequestsLiteral().getNode());
}
}

View file

@ -3,23 +3,19 @@ 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.friends.friendship.FriendshipDAO;
import de.kentoj.scrow.bukkit.friends.FriendshipService;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import reactor.core.scheduler.Schedulers;
import java.util.Comparator;
public class FriendListLiteral {
@Getter
private final CommandNode<CommandSender, BukkitCommandContext> node;
private final FriendshipDAO friendshipDAO;
public FriendListLiteral(FriendshipDAO friendshipDAO) {
this.friendshipDAO = friendshipDAO;
public FriendListLiteral() {
this.node = ScrowAPI.getCommandManager().createNode("list");
node.setExecutor(this::friendList);
}
@ -27,7 +23,7 @@ public class FriendListLiteral {
private void friendList(BukkitCommandContext ctx) {
var ownUid = ctx.getPlayerSender().getUniqueId();
ctx.getPlayerSender().sendMessage("friends:");
friendshipDAO.getFriendships(ownUid)
ScrowAPI.getFriendshipService().getFriendships(ownUid)
.map(friendship -> Bukkit.getOfflinePlayer(friendship.getUuids().getOther(ownUid)))
.sort(this::compareByOnlineStatus)
.map(friendPlayer -> {

View file

@ -5,9 +5,9 @@ import de.kentoj.kencommandapi.api.structure.argument.CommandArgument;
import de.kentoj.kencommandapi.api.structure.node.CommandNode;
import de.kentoj.kencommandapi.type.OfflinePlayerArgumentType;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.friends.friendship.FriendshipDAO;
import lombok.Getter;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import reactor.core.publisher.Mono;
@ -15,10 +15,8 @@ public class FriendRemoveLiteral {
@Getter
private final CommandNode<CommandSender, BukkitCommandContext> node;
private final CommandArgument<CommandSender, BukkitCommandContext, OfflinePlayer> playerArg;
private final FriendshipDAO friendshipDAO;
public FriendRemoveLiteral(FriendshipDAO friendshipDAO) {
this.friendshipDAO = friendshipDAO;
public FriendRemoveLiteral() {
this.node = ScrowAPI.getCommandManager().createNode("remove");
this.playerArg = ScrowAPI.getCommandManager().createArgument("player", OfflinePlayerArgumentType.getInstance());
this.node.addArgument(playerArg);
@ -28,14 +26,10 @@ public class FriendRemoveLiteral {
private void removeFriend(BukkitCommandContext ctx) {
var friend = ctx.getArg(playerArg);
friendshipDAO.deleteFriendship(friend.getUniqueId(), ctx.getPlayerSender().getUniqueId())
.map(wasDeleted -> {
ctx.getSender().sendMessage(
wasDeleted
? "[friend] You are no longer friends with " + friend.getName()
: "[friend] Failed. You are not friends with " + friend.getName()
);
return Mono.empty();
ScrowAPI.getFriendshipService().deleteFriendship(friend.getUniqueId(), ctx.getPlayerSender().getUniqueId())
.doOnNext(wasDeleted -> {
if (wasDeleted) throw new CommandException("You were not friends with " + friend.getName() + " in the first place");
ctx.getSender().sendMessage("You are no longer friends with " + friend.getName());
})
.subscribe();
}

View file

@ -3,7 +3,7 @@ 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.friends.request.FriendRequestDAO;
import de.kentoj.scrow.bukkit.friends.FriendRequestService;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -13,10 +13,8 @@ public class FriendRequestsLiteral {
@Getter
private final CommandNode<CommandSender, BukkitCommandContext> node;
private final FriendRequestDAO friendRequestDAO;
public FriendRequestsLiteral(FriendRequestDAO friendRequestDAO) {
this.friendRequestDAO = friendRequestDAO;
public FriendRequestsLiteral() {
this.node = ScrowAPI.getCommandManager().createNode("requests", "list-requests");
node.setExecutor(this::friendRequestsList);
}
@ -24,7 +22,7 @@ public class FriendRequestsLiteral {
private void friendRequestsList(BukkitCommandContext ctx) {
var ownUid = ctx.getPlayerSender().getUniqueId();
ctx.getPlayerSender().sendMessage("incoming friends requests:");
friendRequestDAO.getFriendRequestsTo(ownUid)
ScrowAPI.getFriendRequestService().getFriendRequestsTo(ownUid)
.map(friendRequest -> {
var fromUid = friendRequest.getFrom();
var fromName = Bukkit.getOfflinePlayer(fromUid).getName();

View file

@ -1,26 +0,0 @@
package de.kentoj.scrow.bukkit.friends.friendship;
import de.kentoj.scrow.bukkit.friends.Friendship;
import de.kentoj.scrow.bukkit.friends.OrderedUUIDPair;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.UUID;
public interface FriendshipDAO {
Mono<@NotNull Friendship> getFriendship(UUID uuid1, UUID uuid2);
Flux<@NotNull Friendship> getFriendships(UUID playerId);
/*
* @return True if friendship saved, false if friendship already exists
*/
Mono<@NotNull Boolean> saveFriendship(Friendship playerIds);
/**
* @return True if friendship deleted, false if none found
*/
Mono<@NotNull Boolean> deleteFriendship(UUID uuid1, UUID uuid2);
}

View file

@ -0,0 +1,39 @@
package de.kentoj.scrow.bukkit.friends.friendship;
import de.kentoj.scrow.bukkit.friends.Friendship;
import de.kentoj.scrow.bukkit.friends.FriendshipService;
import de.kentoj.scrow.bukkit.friends.OrderedUUIDPair;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class InMemoryFriendshipService implements FriendshipService {
private final Map<OrderedUUIDPair, Friendship> friendships = new HashMap<>();
@Override
public Mono<@NotNull Friendship> getFriendship(UUID uuid1, UUID uuid2) {
return Mono.just(friendships.get(new OrderedUUIDPair(uuid1, uuid2)));
}
@Override
public Flux<@NotNull Friendship> getFriendships(UUID playerId) {
return Flux.fromStream(friendships.keySet().stream()
.filter(pair -> pair.getFirst().equals(playerId) || pair.getSecond().equals(playerId))
.map(friendships::get));
}
@Override
public Mono<@NotNull Boolean> saveFriendship(Friendship friendship) {
return Mono.just(friendships.putIfAbsent(friendship.getUuids(), friendship) == null);
}
@Override
public Mono<@NotNull Boolean> deleteFriendship(UUID uuid1, UUID uuid2) {
return Mono.just(friendships.remove(new OrderedUUIDPair(uuid1, uuid2)) != null);
}
}

View file

@ -8,6 +8,7 @@ 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.friends.FriendshipService;
import de.kentoj.scrow.bukkit.friends.OrderedUUIDPair;
import org.bson.Document;
import org.jetbrains.annotations.NotNull;
@ -17,13 +18,13 @@ import reactor.core.publisher.Mono;
import java.sql.Date;
import java.util.UUID;
public class MongoFriendshipDAO implements FriendshipDAO {
public class MongoFriendshipService implements FriendshipService {
private static final String COLLECTION_NAME = "friendships";
private final MongoDatabase database;
public MongoFriendshipDAO(MongoDatabase database) {
public MongoFriendshipService(MongoDatabase database) {
this.database = database;
Mono.when(

View file

@ -2,9 +2,7 @@ package de.kentoj.scrow.bukkit.friends.handler;
import de.kentoj.kencommandapi.api.processing.CommandException;
import de.kentoj.scrow.bukkit.ScrowAPI;
import de.kentoj.scrow.bukkit.friends.friendship.FriendshipDAO;
import de.kentoj.scrow.bukkit.friends.friendship.FriendshipImpl;
import de.kentoj.scrow.bukkit.friends.request.FriendRequestDAO;
import de.kentoj.scrow.bukkit.friends.request.FriendRequestImpl;
import lombok.RequiredArgsConstructor;
import org.bukkit.OfflinePlayer;
@ -20,8 +18,6 @@ public class FriendAddHandler {
private final Player self;
private final OfflinePlayer other;
private final FriendshipDAO friendshipDAO;
private final FriendRequestDAO friendRequestDAO;
public void handle() {
findAction()
@ -29,6 +25,7 @@ public class FriendAddHandler {
.doOnError(CommandException.class, e -> {
ScrowAPI.getCommandManager().sendErrorMessage(self, e);
})
.onErrorComplete(CommandException.class)
.subscribeOn(Schedulers.boundedElastic())
.subscribe();
}
@ -37,14 +34,15 @@ public class FriendAddHandler {
return switch (action) {
case ERR_CAN_NOT_ADD_SELF -> throw new CommandException("You can not add yourself");
case ERR_ALREADY_FRIENDS -> throw new CommandException("You are already friends with " + other.getName());
case ERR_ALREADY_REQUESTED -> throw new CommandException("You already requested " + other.getName() + " to be friends with you");
case ERR_ALREADY_REQUESTED ->
throw new CommandException("You already requested " + other.getName() + " to be friends with you");
case SEND_REQUEST -> sendRequest();
case ACCEPT_REQUEST -> acceptRequest();
};
}
private Mono<?> sendRequest() {
return friendRequestDAO.saveFriendRequest(new FriendRequestImpl(self.getUniqueId(), other.getUniqueId(), Instant.now()))
return ScrowAPI.getFriendRequestService().saveFriendRequest(new FriendRequestImpl(self.getUniqueId(), other.getUniqueId(), Instant.now()))
.then(sendMessage(self.getPlayer(), "Successfully sent " + other.getName() + " a friend request."))
.then(Mono.fromRunnable(() -> {
var otherPlayer = other.getPlayer();
@ -56,9 +54,9 @@ public class FriendAddHandler {
private Mono<?> acceptRequest() {
return Mono.when(
friendRequestDAO.deleteFriendRequest(self.getUniqueId(), other.getUniqueId()),
friendRequestDAO.deleteFriendRequest(other.getUniqueId(), self.getUniqueId()),
friendshipDAO.saveFriendship(new FriendshipImpl(self.getUniqueId(), other.getUniqueId(), Instant.now()))
ScrowAPI.getFriendRequestService().deleteFriendRequest(self.getUniqueId(), other.getUniqueId()),
ScrowAPI.getFriendRequestService().deleteFriendRequest(other.getUniqueId(), self.getUniqueId()),
ScrowAPI.getFriendshipService().saveFriendship(new FriendshipImpl(self.getUniqueId(), other.getUniqueId(), Instant.now()))
)
.then(sendMessage(self, "You are now friends with " + other.getName()))
.then(Mono.fromRunnable(() -> {
@ -71,13 +69,13 @@ public class FriendAddHandler {
private Mono<@NotNull AddAction> findAction() {
if (self.getUniqueId() == other.getUniqueId()) return Mono.just(AddAction.ERR_CAN_NOT_ADD_SELF);
return friendshipDAO.getFriendship(self.getUniqueId(), other.getUniqueId())
return ScrowAPI.getFriendshipService().getFriendship(self.getUniqueId(), other.getUniqueId())
.map(f -> AddAction.ERR_ALREADY_FRIENDS)
.switchIfEmpty(
friendRequestDAO.getFriendRequest(self.getUniqueId(), other.getUniqueId())
ScrowAPI.getFriendRequestService().getFriendRequest(self.getUniqueId(), other.getUniqueId())
.map(r -> AddAction.ERR_ALREADY_REQUESTED)
.switchIfEmpty(
friendRequestDAO.getFriendRequest(other.getUniqueId(), self.getUniqueId())
ScrowAPI.getFriendRequestService().getFriendRequest(other.getUniqueId(), self.getUniqueId())
.map(r -> AddAction.ACCEPT_REQUEST)
.switchIfEmpty(Mono.just(AddAction.SEND_REQUEST))
)

View file

@ -1,28 +0,0 @@
package de.kentoj.scrow.bukkit.friends.request;
import de.kentoj.scrow.bukkit.friends.FriendRequest;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Instant;
import java.util.UUID;
public interface FriendRequestDAO {
Flux<@NotNull FriendRequest> getFriendRequestsTo(UUID playerId);
Flux<@NotNull FriendRequest> getFriendRequestsFrom(UUID playerId);
Mono<@NotNull FriendRequest> getFriendRequest(UUID from, UUID to);
/**
* @return True if request saved, false if already existing request found.
*/
Mono<@NotNull Boolean> saveFriendRequest(FriendRequest friendRequest);
/**
* @return True if request deleted, false if none found.
*/
Mono<@NotNull Boolean> deleteFriendRequest(UUID from, UUID to);
}

View file

@ -0,0 +1,48 @@
package de.kentoj.scrow.bukkit.friends.request;
import de.kentoj.scrow.bukkit.friends.FriendRequest;
import de.kentoj.scrow.bukkit.friends.FriendRequestService;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
public class InMemoryFriendRequestService implements FriendRequestService {
private final Set<FriendRequest> requests = new HashSet<>();
@Override
public Flux<@NotNull FriendRequest> getFriendRequestsTo(UUID playerId) {
return Flux.fromStream(requests.stream()
.filter(req -> req.getTo().equals(playerId)));
}
@Override
public Flux<@NotNull FriendRequest> getFriendRequestsFrom(UUID playerId) {
return Flux.fromStream(requests.stream()
.filter(req -> req.getFrom().equals(playerId)));
}
@Override
public Mono<@NotNull FriendRequest> getFriendRequest(UUID from, UUID to) {
return Mono.justOrEmpty(requests.stream()
.filter(req -> req.getFrom().equals(from) && req.getTo().equals(to))
.findFirst()
.orElse(null));
}
@Override
public Mono<@NotNull Boolean> saveFriendRequest(FriendRequest friendRequest) {
return getFriendRequest(friendRequest.getFrom(), friendRequest.getTo())
.map(__ -> false)
.switchIfEmpty(Mono.fromRunnable(() -> requests.add(friendRequest)));
}
@Override
public Mono<@NotNull Boolean> deleteFriendRequest(UUID from, UUID to) {
return Mono.just(requests.removeIf(req -> req.getFrom().equals(from) && req.getTo().equals(to)));
}
}

View file

@ -8,6 +8,7 @@ import com.mongodb.client.model.Indexes;
import com.mongodb.reactivestreams.client.MongoCollection;
import com.mongodb.reactivestreams.client.MongoDatabase;
import de.kentoj.scrow.bukkit.friends.FriendRequest;
import de.kentoj.scrow.bukkit.friends.FriendRequestService;
import org.bson.Document;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
@ -16,13 +17,13 @@ import reactor.core.publisher.Mono;
import java.util.Date;
import java.util.UUID;
public class MongoFriendRequestDAO implements FriendRequestDAO {
public class MongoFriendRequestService implements FriendRequestService {
private static final String COLLECTION_NAME = "friend-requests";
private final MongoDatabase database;
public MongoFriendRequestDAO(MongoDatabase database) {
public MongoFriendRequestService(MongoDatabase database) {
this.database = database;
Mono.when(