gradle sucks etc

This commit is contained in:
kento2 2026-06-27 16:46:30 +02:00
parent 72b4edf48b
commit b0d4a7d061
25 changed files with 463 additions and 123 deletions

View file

@ -6,7 +6,6 @@ plugins {
}
group = "de.kentoj.scrow"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
@ -16,9 +15,6 @@ dependencies {
implementation(project(":core-bukkit-api"))
implementation(kotlin("stdlib-jdk8"))
//implementation("org.spigotmc:spigot-api:1.21.11-R0.1-SNAPSHOT")
//implementation("de.kentoj.scrow:kencommandapi-bukkit:0.8")
}
tasks {
@ -31,6 +27,14 @@ tasks {
}
}
tasks.processResources {
filesMatching("plugin.yml") {
expand(mapOf(
"version" to project.version.toString()
))
}
}
tasks.build {
dependsOn("shadowJar")
}
}

View file

@ -1,27 +1,23 @@
package de.kentoj.scrow.bukkit;
import com.mongodb.ConnectionString;
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.economy.InMemoryEconomyService;
import de.kentoj.scrow.bukkit.economy.MongoEconomyService;
import de.kentoj.scrow.bukkit.economy.PostgresEconomyService;
import de.kentoj.scrow.bukkit.friends.friendship.InMemoryFriendshipService;
import de.kentoj.scrow.bukkit.friends.friendship.MongoFriendshipService;
import de.kentoj.scrow.bukkit.friends.friendship.PostgresFriendshipService;
import de.kentoj.scrow.bukkit.friends.request.InMemoryFriendRequestService;
import de.kentoj.scrow.bukkit.friends.request.MongoFriendRequestService;
import de.kentoj.scrow.bukkit.friends.request.PostgresFriendRequestService;
import de.kentoj.scrowlib.connection.DatabaseConnectionFactoryImpl;
import de.kentoj.scrowlib.servermanager.ServerManagerImpl;
import io.nats.client.Nats;
import io.nats.client.Options;
import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.bson.UuidRepresentation;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.io.IOException;
@ -29,8 +25,6 @@ import java.io.IOException;
@NoArgsConstructor(access = AccessLevel.NONE)
public class ScrowAPISurface {
private static @Nullable MongoClient mongoClient;
public static void initScrowAPI(Plugin plugin, boolean enableDatabase) {
ScrowAPI.setMinecraftScheduler(Schedulers.fromExecutor(cmd -> {
Bukkit.getScheduler().runTask(plugin, cmd);
@ -48,39 +42,25 @@ public class ScrowAPISurface {
}
if (enableDatabase) {
var pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build();
var codecRegistry = CodecRegistries.fromRegistries(
MongoClientSettings.getDefaultCodecRegistry(),
CodecRegistries.fromProviders(pojoCodecProvider)
);
String mongoHost = System.getenv("HOST_MONGO");
var settings = MongoClientSettings.builder()
.codecRegistry(codecRegistry)
.uuidRepresentation(UuidRepresentation.STANDARD)
.applyConnectionString(new ConnectionString(mongoHost))
.build();
mongoClient = MongoClients.create(settings);
ScrowAPI.setDatabase(mongoClient.getDatabase("scrow"));
ConnectionFactory factory = ConnectionFactories.get(System.getenv("HOST_POSTGRES"));
ScrowAPI.setDbConFactory(new DatabaseConnectionFactoryImpl(factory));
}
ScrowAPI.setServerManager(new ServerManagerImpl(ScrowAPI.getNats()));
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.setEconomyService(ScrowAPI.getDbConFactory() != null ?
new PostgresEconomyService(ScrowAPI.getDbConFactory()) : new InMemoryEconomyService());
ScrowAPI.setFriendRequestService(ScrowAPI.getDbConFactory() != null ?
new PostgresFriendRequestService(ScrowAPI.getDbConFactory()) : new InMemoryFriendRequestService());
ScrowAPI.setFriendshipService(ScrowAPI.getDbConFactory() != null ?
new PostgresFriendshipService(ScrowAPI.getDbConFactory()) : new InMemoryFriendshipService());
ScrowAPI.setCommandApi(new BukkitKenCommandApi());
}
public static void destroyScrowAPI() {
if (mongoClient != null) {
mongoClient.close();
try {
ScrowAPI.getDbConFactory().close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View file

@ -0,0 +1,86 @@
package de.kentoj.scrow.bukkit.economy;
import com.google.common.base.Preconditions;
import de.kentoj.scrow.bukkit.EconomyService;
import de.kentoj.scrowlib.connection.DatabaseConnectionFactory;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;
import java.util.UUID;
// TODO log operations to separate table
public class PostgresEconomyService implements EconomyService {
private final DatabaseConnectionFactory conFactory;
public PostgresEconomyService(DatabaseConnectionFactory conFactory) {
this.conFactory = conFactory;
conFactory.create().sql("""
CREATE TABLE IF NOT EXISTS economy (
playerId UUID NOT NULL,
balance INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY(playerId)
)
""").then().block();
}
@Override
public Mono<@NotNull Integer> getCoins(UUID playerId) {
return conFactory.create()
.sql("""
SELECT balance
FROM economy
WHERE playerId = :playerId
""")
.bind("playerId", playerId)
.map(row -> row.get("balance", Integer.class))
.one();
}
@Override
public Mono<@NotNull Void> setCoins(UUID playerId, int amount) {
Preconditions.checkArgument(amount >= 0, "amount can not be negative");
return conFactory.create().sql("""
INSERT INTO economy (playerId, balance)
VALUES (:playerId, :amount)
ON CONFLICT (playerId)
DO UPDATE
SET balance = :amount
WHERE economy.playerId = :playerId
""")
.bind("playerId", playerId)
.bind("amount", amount)
.then();
}
@Override
public Mono<@NotNull Void> depositCoins(UUID playerId, int amount) {
Preconditions.checkArgument(amount >= 0, "amount can not be negative");
return conFactory.create().sql("""
INSERT INTO economy (playerId, balance)
VALUES (:playerId, :amount)
ON CONFLICT (playerId)
DO UPDATE
SET balance = economy.balance + EXCLUDED.balance
WHERE economy.playerId = :playerId
""")
.bind("playerId", playerId)
.bind("amount", amount)
.then();
}
@Override
public Mono<@NotNull Boolean> tryWithdrawCoins(UUID playerId, int amount) {
Preconditions.checkArgument(amount >= 0, "amount can not be negative");
return conFactory.create().sql("""
UPDATE economy
SET balance = balance - :amount
WHERE playerId = :playerId
""")
.bind("playerId", playerId)
.bind("amount", amount)
.fetch()
.rowsUpdated()
.map(updated -> updated > 0);
}
}

View file

@ -19,11 +19,10 @@ public class FriendAddLiteral implements CommandExecutor<CommandSender, BukkitCo
public FriendAddLiteral() {
var cmds = ScrowAPI.getCommandApi();
this.playerArg = cmds.createArgument("player", OfflinePlayerArgumentType.getInstance());
this.rootNode = cmds.createNode("add");
this.rootNode.addArgument(playerArg);
this.rootNode.setExecutor(this);
playerArg = cmds.createArgument("player", OfflinePlayerArgumentType.getInstance());
rootNode = cmds.createNode("add");
rootNode.addArgument(playerArg);
rootNode.setExecutor(this);
}
@Override

View file

@ -0,0 +1,96 @@
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 de.kentoj.scrowlib.connection.DatabaseConnectionFactory;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Instant;
import java.util.UUID;
public class PostgresFriendshipService implements FriendshipService {
private final DatabaseConnectionFactory dbConFactory;
public PostgresFriendshipService(DatabaseConnectionFactory dbConFactory) {
this.dbConFactory = dbConFactory;
this.dbConFactory.create().sql("""
CREATE TABLE IF NOT EXISTS friendships (
first_uuid UUID NOT NULL,
second_uuid UUID NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (first_uuid, second_uuid),
CONSTRAINT friendship_no_self_friendship
CHECK (first_uuid <> second_uuid)
)
""").then().block();
}
@Override
public Mono<@NotNull Friendship> getFriendship(UUID uuid1, UUID uuid2) {
var uuids = new OrderedUUIDPair(uuid1, uuid2);
return dbConFactory.create().sql("""
SELECT first_uuid, second_uuid, created_at
FROM friendships
WHERE first_uuid = :first AND second_uuid = :second
""")
.bind("first", uuids.getFirst())
.bind("second", uuids.getSecond())
.map(row -> (Friendship) new FriendshipImpl(
row.get("first_uuid", UUID.class),
row.get("second_uuid", UUID.class),
row.get("created_at", Instant.class)
))
.one();
}
@Override
public Flux<@NotNull Friendship> getFriendships(UUID playerId) {
return dbConFactory.create().sql("""
SELECT first_uuid, second_uuid, created_at
FROM friendships
WHERE first_uuid = :playerId OR second_uuid = :playerId
""")
.bind("playerId", playerId)
.map((row, meta) -> (Friendship) new FriendshipImpl(
row.get("first_uuid", UUID.class),
row.get("second_uuid", UUID.class),
row.get("created_at", Instant.class)
))
.all();
}
@Override
public Mono<@NotNull Boolean> saveFriendship(Friendship friendship) {
return dbConFactory.create().sql("""
INSERT INTO friendships (first_uuid, second_uuid, created_at)
VALUES (:first, :second, :createdAt)
ON CONFLICT (first_uuid, second_uuid) DO NOTHING
""")
.bind("first", friendship.getUuids().getFirst())
.bind("second", friendship.getUuids().getSecond())
.bind("createdAt", friendship.getCreatedAt())
.fetch()
.rowsUpdated()
.map(rows -> rows > 0);
}
@Override
public Mono<@NotNull Boolean> deleteFriendship(UUID uuid1, UUID uuid2) {
var uuids = new OrderedUUIDPair(uuid1, uuid2);
return dbConFactory.create().sql("""
DELETE FROM friendships
WHERE first_uuid = :first AND second_uuid = :second
""")
.bind("first", uuids.getFirst())
.bind("second", uuids.getSecond())
.fetch()
.rowsUpdated()
.map(rows -> rows > 0);
}
}

View file

@ -0,0 +1,104 @@
package de.kentoj.scrow.bukkit.friends.request;
import de.kentoj.scrow.bukkit.friends.FriendRequest;
import de.kentoj.scrow.bukkit.friends.FriendRequestService;
import de.kentoj.scrowlib.connection.DatabaseConnectionFactory;
import io.r2dbc.spi.Readable;
import org.jetbrains.annotations.NotNull;
import org.springframework.r2dbc.core.DatabaseClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Instant;
import java.util.UUID;
public class PostgresFriendRequestService implements FriendRequestService {
private final DatabaseConnectionFactory conFactory;
public PostgresFriendRequestService(DatabaseConnectionFactory conFactory) {
this.conFactory = conFactory;
conFactory.create().sql("""
CREATE TABLE IF NOT EXISTS friend-requests (
from_uuid UUID NOT NULL,
to_uuid UUID NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (first_uuid, second_uuid),
CONSTRAINT friendship_no_self_friendship
CHECK (first_uuid <> second_uuid)
)
""").then().block();
}
@Override
public Mono<@NotNull FriendRequest> getFriendRequest(UUID from, UUID to) {
return conFactory.create().sql("""
SELECT *
FROM friend-requests
WHERE from_uuid = :from AND to_uuid = :to
""")
.bind("from", from)
.bind("to", to)
.map(this::parseFriendRequest)
.one();
}
@Override
public Mono<@NotNull Boolean> saveFriendRequest(FriendRequest friendRequest) {
return conFactory.create().sql("""
INSERT INTO friend-requests (from_uuid, to_uuid, created_at)
VALUES (:from, :to, :created_at)
""")
.bind("from", friendRequest.getFrom())
.bind("to", friendRequest.getTo())
.fetch()
.rowsUpdated()
.map(cnt -> cnt > 0);
}
@Override
public Flux<@NotNull FriendRequest> getFriendRequestsTo(UUID playerId) {
return conFactory.create().sql("""
SELECT *
FROM friend-requests
WHERE to_uuid = :to
""")
.bind("to", playerId)
.map(this::parseFriendRequest)
.all();
}
@Override
public Flux<@NotNull FriendRequest> getFriendRequestsFrom(UUID playerId) {
return conFactory.create().sql("""
SELECT *
FROM friend-requests
WHERE from_uuid = :from
""")
.bind("from", playerId)
.map(this::parseFriendRequest)
.all();
}
@Override
public Mono<@NotNull Boolean> deleteFriendRequest(UUID from, UUID to) {
return conFactory.create().sql("""
DELETE
FROM friend-requests
WHERE from_uuid = :from AND to_uuid = :to
""")
.bind("from", from)
.bind("to", to)
.fetch()
.rowsUpdated()
.map(deleted -> deleted > 0);
}
private FriendRequest parseFriendRequest(Readable row) {
return new FriendRequestImpl(
row.get("from_uuid", UUID.class),
row.get("to_uuid", UUID.class),
row.get("created_at", Instant.class)
);
}
}

View file

@ -1,5 +1,5 @@
name: "CoreBukkit"
version: "1.0"
version: "${version}"
main: "de.kentoj.scrow.bukkit.CoreImplPlugin"
api-version: "1.21"
author: "Kento2 <kento@placeq.com>"
author: "Kento2"