This commit is contained in:
kento2 2026-04-18 12:57:34 +02:00
parent d4370de78c
commit c234c600fe
6 changed files with 30 additions and 40 deletions

View file

@ -1,5 +1,6 @@
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;
@ -25,15 +26,18 @@ public class FriendAddHandler {
public void handle() {
findAction()
.flatMap(this::executeAction)
.doOnError(CommandException.class, e -> {
ScrowAPI.getCommandManager().sendErrorMessage(self, e);
})
.subscribeOn(Schedulers.boundedElastic())
.subscribe();
}
private Mono<?> executeAction(AddAction action) {
return switch (action) {
case ERR_ALREADY_FRIENDS -> sendMessage(self, "[friends] You are already friends with " + other.getName());
case ERR_ALREADY_REQUESTED ->
sendMessage(self, "[friends] You already requested " + other.getName() + " to be friends with you");
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 SEND_REQUEST -> sendRequest();
case ACCEPT_REQUEST -> acceptRequest();
};
@ -41,11 +45,11 @@ public class FriendAddHandler {
private Mono<?> sendRequest() {
return friendRequestDAO.saveFriendRequest(new FriendRequestImpl(self.getUniqueId(), other.getUniqueId(), Instant.now()))
.then(sendMessage(self.getPlayer(), "[friends] Successfully sent " + other.getName() + " a friend request."))
.then(sendMessage(self.getPlayer(), "Successfully sent " + other.getName() + " a friend request."))
.then(Mono.fromRunnable(() -> {
var otherPlayer = other.getPlayer();
if (otherPlayer != null)
sendMessage(otherPlayer, "[friends] " + self.getName() + " wants to be friends with you. Use '/f add " + self.getName() + "' to accept.").subscribe();
sendMessage(otherPlayer, self.getName() + " wants to be friends with you. Use '/f add " + self.getName() + "' to accept.").subscribe();
}))
.doOnError(this::handleError);
}
@ -56,16 +60,17 @@ public class FriendAddHandler {
friendRequestDAO.deleteFriendRequest(other.getUniqueId(), self.getUniqueId()),
friendshipDAO.saveFriendship(new FriendshipImpl(self.getUniqueId(), other.getUniqueId(), Instant.now()))
)
.then(sendMessage(self, "[friends] You are now friends with " + other.getName()))
.then(sendMessage(self, "You are now friends with " + other.getName()))
.then(Mono.fromRunnable(() -> {
var otherPlayer = other.getPlayer();
if (otherPlayer != null)
sendMessage(otherPlayer, "[friends] You are now friends with " + self.getName()).subscribe();
sendMessage(otherPlayer, "You are now friends with " + self.getName()).subscribe();
}))
.doOnError(this::handleError);
}
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())
.map(f -> AddAction.ERR_ALREADY_FRIENDS)
.switchIfEmpty(
@ -91,6 +96,7 @@ public class FriendAddHandler {
private enum AddAction {
ERR_ALREADY_FRIENDS,
ERR_ALREADY_REQUESTED,
ERR_CAN_NOT_ADD_SELF,
SEND_REQUEST,
ACCEPT_REQUEST
}