This commit is contained in:
kento2 2026-04-16 22:17:48 +02:00
parent ad4e00a6ab
commit 3433e1d4df
9 changed files with 43 additions and 35 deletions

View file

@ -2,6 +2,7 @@ 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 org.bukkit.plugin.java.JavaPlugin;
import reactor.core.publisher.Mono;
@ -11,11 +12,11 @@ public class CoreImplPlugin extends JavaPlugin {
@Override
public void onEnable() {
ScrowAPISurface.initScrowAPI(this, false);
ScrowAPISurface.initScrowAPI(this, true);
{
ScrowAPI.getCommandManager().register(new CoinsCommand().getRootNode());
ScrowAPI.getCommandManager().register(new FriendCommand().getRootNode());
ScrowAPI.getCommandManager().register(new FriendCommand(new MongoFriendshipDAO(ScrowAPI.getDatabase())).getRootNode());
}
{
@ -33,6 +34,10 @@ public class CoreImplPlugin extends JavaPlugin {
.doOnNext(a -> System.out.println("coins: " + a))
.block();
}
}
@Override
public void onDisable() {
ScrowAPISurface.destroyScrowAPI();
}
}

View file

@ -20,7 +20,7 @@ import reactor.core.scheduler.Schedulers;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ScrowAPISurface {
private static @Nullable MongoClient databaseClient = null;
private static @Nullable MongoClient mongoClient;
public static void initScrowAPI(Plugin plugin, boolean enableDatabase) {
ScrowAPI.setMinecraftScheduler(Schedulers.fromExecutor(cmd -> {
@ -38,7 +38,9 @@ public class ScrowAPISurface {
.uuidRepresentation(UuidRepresentation.STANDARD)
.applyConnectionString(new ConnectionString("mongodb://localhost:27017/scrow"))
.build();
databaseClient = MongoClients.create(settings);
mongoClient = MongoClients.create(settings);
ScrowAPI.setDatabase(mongoClient.getDatabase("scrow"));
}
ScrowAPI.setEconomyService(
@ -52,8 +54,8 @@ public class ScrowAPISurface {
}
public static void destroyScrowAPI() {
if (databaseClient != null) {
databaseClient.close();
if (mongoClient != null) {
mongoClient.close();
}
}
}

View file

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

View file

@ -3,6 +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.friendship.FriendshipDAO;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -12,8 +13,10 @@ public class FriendListLiteral {
@Getter
private final CommandNode<CommandSender, BukkitCommandContext> node;
private final FriendshipDAO friendshipDAO;
public FriendListLiteral() {
public FriendListLiteral(FriendshipDAO friendshipDAO) {
this.friendshipDAO = friendshipDAO;
this.node = ScrowAPI.getCommandManager().createNode("list");
node.setExecutor(this::friendList);
}
@ -21,11 +24,11 @@ public class FriendListLiteral {
private void friendList(BukkitCommandContext ctx) {
var ownUid = ctx.getPlayerSender().getUniqueId();
ctx.getPlayerSender().sendMessage("friends:");
ScrowAPI.getFriendsService().getFriendships(ownUid)
friendshipDAO.getFriendships(ownUid)
.publishOn(ScrowAPI.getMinecraftScheduler())
.subscribeOn(Schedulers.boundedElastic())
.map(friendship -> {
var friendPlayerUid = friendship.getOther(ownUid);
var friendPlayerUid = friendship.getUuids().getOther(ownUid);
var friendPlayerName = Bukkit.getOfflinePlayer(friendPlayerUid).getName();
return "- " + friendPlayerName;
}).doOnNext(line -> {

View file

@ -1,15 +1,13 @@
package de.kentoj.scrow.bukkit.friends.friendship;
import de.kentoj.scrow.bukkit.friends.Friendship;
import de.kentoj.scrow.bukkit.util.pair.Tuple2;
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 FriendshipRepository {
public interface FriendshipDAO {
Flux<@NotNull Friendship> getFriendships(UUID playerId);

View file

@ -17,32 +17,32 @@ import reactor.core.publisher.Mono;
import java.sql.Date;
import java.util.UUID;
public class MongoFriendshipRepository implements FriendshipRepository {
public class MongoFriendshipDAO implements FriendshipDAO {
private static final String COLLECTION_NAME = "friendships";
private final MongoDatabase database;
public MongoFriendshipRepository(MongoDatabase database) {
public MongoFriendshipDAO(MongoDatabase database) {
this.database = database;
database.createCollection(COLLECTION_NAME);
collection().createIndex(
Mono.from(database.createCollection(COLLECTION_NAME)).block();
Mono.from(collection().createIndex(
Indexes.compoundIndex(
Indexes.ascending("first"),
Indexes.ascending("second")
),
new IndexOptions().unique(true)
);
)).block();
}
@Override
public Flux<@NotNull Friendship> getFriendships(UUID playerId) {
return Flux.from(collection()
.find(Filters.or(
Filters.eq("first", playerId),
Filters.eq("second", playerId)
)))
.find(Filters.or(
Filters.eq("first", playerId),
Filters.eq("second", playerId)
)))
.map(this::fromDocument);
}