currently refactoring a lot
This commit is contained in:
parent
7fe7409a15
commit
a32805a623
46 changed files with 571 additions and 642 deletions
|
|
@ -4,7 +4,7 @@ plugins {
|
|||
id("de.kentoj.scrow.scrow-repository")
|
||||
id("de.kentoj.scrow.base-dependencies")
|
||||
id("de.kentoj.scrow.java-library")
|
||||
id("de.kentoj.scrow.reactor")
|
||||
id("de.kentoj.scrow.database")
|
||||
}
|
||||
|
||||
group = "de.kentoj.scrow"
|
||||
|
|
@ -24,9 +24,9 @@ dependencies {
|
|||
api("io.nats:jnats:2.25.2")
|
||||
|
||||
// http://forge.kentoj.de/scrow/-/packages/maven/de.kentoj.scrow:kencommandapi-core
|
||||
api("de.kentoj.scrow:kencommandapi-core:0.21")
|
||||
api("de.kentoj.scrow:kencommandapi-core:0.22")
|
||||
// http://forge.kentoj.de/scrow/-/packages/maven/de.kentoj.scrow:kencommandapi-bukkit
|
||||
api("de.kentoj.scrow:kencommandapi-bukkit:0.21")
|
||||
api("de.kentoj.scrow:kencommandapi-bukkit:0.22")
|
||||
}
|
||||
|
||||
publishing {
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
package de.kentoj.scrow.bukkit;
|
||||
|
||||
import com.leakyabstractions.result.api.Result;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface PlayerManager {
|
||||
|
||||
Result<Void, String> sendPlayer(UUID uuid, String instanceHandle);
|
||||
}
|
||||
|
|
@ -3,24 +3,29 @@ package de.kentoj.scrow.bukkit;
|
|||
import de.kentoj.kencommandapi.CommandAPI;
|
||||
import de.kentoj.scrow.bukkit.friends.FriendRequestService;
|
||||
import de.kentoj.scrow.bukkit.friends.FriendshipService;
|
||||
import de.kentoj.scrow.bukkit.misc.EconomyService;
|
||||
import de.kentoj.scrow.bukkit.misc.PlayerManager;
|
||||
import de.kentoj.scrow.bukkit.region.RegionManager;
|
||||
import de.kentoj.scrowlib.connection.DatabaseConnectionFactory;
|
||||
import de.kentoj.scrowlib.servermanager.InstanceManager;
|
||||
import io.nats.client.Connection;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import reactor.core.scheduler.Scheduler;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class ScrowAPI {
|
||||
|
||||
/**
|
||||
* Might be null in testing environments
|
||||
*/
|
||||
@Getter
|
||||
private static DatabaseConnectionFactory dbConFactory;
|
||||
@Getter
|
||||
private static Scheduler minecraftScheduler;
|
||||
private static @Nullable Jdbi jdbi;
|
||||
@ApiStatus.Internal
|
||||
public static Plugin plugin;
|
||||
@Getter
|
||||
private static EconomyService economyService;
|
||||
@Getter
|
||||
|
|
@ -44,8 +49,8 @@ public class ScrowAPI {
|
|||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static void setMinecraftScheduler(Scheduler minecraftScheduler) {
|
||||
ScrowAPI.minecraftScheduler = minecraftScheduler;
|
||||
public static void setCorePlugin(Plugin plugin) {
|
||||
ScrowAPI.plugin = plugin;
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
|
|
@ -69,8 +74,8 @@ public class ScrowAPI {
|
|||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static void setDbConFactory(@Nullable DatabaseConnectionFactory dbConFactory) {
|
||||
ScrowAPI.dbConFactory = dbConFactory;
|
||||
public static void setJdbi(@Nullable Jdbi jdbi) {
|
||||
ScrowAPI.jdbi = jdbi;
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
|
|
|
|||
|
|
@ -1,26 +1,23 @@
|
|||
package de.kentoj.scrow.bukkit.friends;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface FriendRequestService {
|
||||
|
||||
Flux<@NotNull FriendRequest> getFriendRequestsTo(UUID playerId);
|
||||
List<FriendRequest> getFriendRequestsTo(UUID playerId);
|
||||
|
||||
Flux<@NotNull FriendRequest> getFriendRequestsFrom(UUID playerId);
|
||||
List<FriendRequest> getFriendRequestsFrom(UUID playerId);
|
||||
|
||||
Mono<@NotNull FriendRequest> getFriendRequest(UUID from, UUID to);
|
||||
FriendRequest getFriendRequest(UUID from, UUID to);
|
||||
|
||||
/**
|
||||
* @return True if request saved, false if already existing request found.
|
||||
*/
|
||||
Mono<@NotNull Boolean> saveFriendRequest(FriendRequest friendRequest);
|
||||
boolean saveFriendRequest(FriendRequest friendRequest);
|
||||
|
||||
/**
|
||||
* @return True if request deleted, false if none found.
|
||||
*/
|
||||
Mono<@NotNull Boolean> deleteFriendRequest(UUID from, UUID to);
|
||||
boolean deleteFriendRequest(UUID from, UUID to);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,21 @@
|
|||
package de.kentoj.scrow.bukkit.friends;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface FriendshipService {
|
||||
|
||||
Mono<@NotNull Friendship> getFriendship(UUID uuid1, UUID uuid2);
|
||||
Friendship getFriendship(UUID uuid1, UUID uuid2);
|
||||
|
||||
Flux<@NotNull Friendship> getFriendships(UUID playerId);
|
||||
List<Friendship> getFriendships(UUID playerId);
|
||||
|
||||
/*
|
||||
* @return True if friendship saved, false if friendship already exists
|
||||
*/
|
||||
Mono<@NotNull Boolean> saveFriendship(Friendship playerIds);
|
||||
boolean saveFriendship(Friendship playerIds);
|
||||
|
||||
/**
|
||||
* @return True if friendship deleted, false if none found
|
||||
*/
|
||||
Mono<@NotNull Boolean> deleteFriendship(UUID uuid1, UUID uuid2);
|
||||
boolean deleteFriendship(UUID uuid1, UUID uuid2);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
package de.kentoj.scrow.bukkit;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import reactor.core.publisher.Mono;
|
||||
package de.kentoj.scrow.bukkit.misc;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
|
|
@ -10,24 +7,24 @@ public interface EconomyService {
|
|||
/**
|
||||
* @throws IllegalArgumentException if amount is less than 0
|
||||
*/
|
||||
Mono<@NotNull Integer> getCoins(UUID playerId);
|
||||
int getCoins(UUID playerId);
|
||||
|
||||
/**
|
||||
* @param playerId uuid of player
|
||||
* @throws IllegalArgumentException if amount is less than 0
|
||||
*/
|
||||
Mono<@NotNull Void> depositCoins(UUID playerId, int amount);
|
||||
void depositCoins(UUID playerId, int amount);
|
||||
|
||||
/**
|
||||
* @param playerId uuid of player
|
||||
* @return true on success, false on insufficient funds
|
||||
* @throws IllegalArgumentException if amount is less than 0
|
||||
*/
|
||||
Mono<@NotNull Boolean> tryWithdrawCoins(UUID playerId, int amount);
|
||||
boolean tryWithdrawCoins(UUID playerId, int amount);
|
||||
|
||||
/**
|
||||
* @param playerId uuid of player
|
||||
* @throws IllegalArgumentException if amount is less than 0
|
||||
*/
|
||||
Mono<@NotNull Void> setCoins(UUID playerId, int amount);
|
||||
void setCoins(UUID playerId, int amount);
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package de.kentoj.scrow.bukkit.misc;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface PlayerManager {
|
||||
|
||||
void sendPlayer(UUID uuid, String instanceHandle);
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package de.kentoj.scrow.bukkit.scheduler;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public interface Task<T> {
|
||||
|
||||
CompletableFuture<T> asFuture();
|
||||
|
||||
default Task<?> runSync(Runnable task) {
|
||||
return mapSync(__ -> {
|
||||
task.run();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
default <S> Task<S> supplySync(Supplier<S> supplier) {
|
||||
return mapSync(__ -> supplier.get());
|
||||
}
|
||||
|
||||
default Task<?> thenSync(Consumer<T> consumer) {
|
||||
return mapSync(previous -> {
|
||||
consumer.accept(previous);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
<S> Task<S> mapSync(Function<T, S> mapper);
|
||||
|
||||
default Task<?> runAsync(Runnable task) {
|
||||
return mapAsync(__ -> {
|
||||
task.run();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
default <S> Task<S> supplyAsync(Supplier<S> supplier) {
|
||||
return mapAsync(__ -> supplier.get());
|
||||
}
|
||||
|
||||
default Task<?> thenAsync(Consumer<T> consumer) {
|
||||
return mapAsync(previous -> {
|
||||
consumer.accept(previous);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
<S> Task<S> mapAsync(Function<T, S> mapper);
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package de.kentoj.scrow.bukkit.scheduler;
|
||||
|
||||
import de.kentoj.scrow.bukkit.ScrowAPI;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* A Task runs entirely async, when sync is used, a subtask is run on the main thread and the async thread
|
||||
* blocks until the subtask returns.
|
||||
*
|
||||
* @param <T> type of computed result
|
||||
*/
|
||||
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
|
||||
public class TaskImpl<T> implements Task<T> {
|
||||
|
||||
private static final ExecutorService EXECUTOR = Executors.newVirtualThreadPerTaskExecutor();
|
||||
|
||||
private final Supplier<T> compute;
|
||||
|
||||
@Override
|
||||
public CompletableFuture<T> asFuture() {
|
||||
return CompletableFuture.supplyAsync(compute, EXECUTOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the last result on the main thread
|
||||
*/
|
||||
public <S> Task<S> mapSync(Function<T, S> mapper) {
|
||||
Supplier<S> supplier = () -> {
|
||||
try {
|
||||
return Bukkit.getScheduler().callSyncMethod(ScrowAPI.plugin, () -> mapper.apply(compute.get())).get();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
} catch (ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
};
|
||||
return new TaskImpl<>(supplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the last result async
|
||||
*/
|
||||
public <S> Task<S> mapAsync(Function<T, S> mapper) {
|
||||
return new TaskImpl<>(() -> mapper.apply(compute.get()));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package de.kentoj.scrow.bukkit.scheduler;
|
||||
|
||||
import de.kentoj.scrow.bukkit.ScrowAPI;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.NONE)
|
||||
public class Tasks {
|
||||
public static <T> Task<T> supplyAsync(Supplier<T> supplier) {
|
||||
return new TaskImpl<>(supplier);
|
||||
}
|
||||
|
||||
public static Task<?> runAsync(Runnable task) {
|
||||
return supplyAsync(() -> {
|
||||
task.run();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
public static <T> Task<T> supplySync(Supplier<T> supplier) {
|
||||
return new TaskImpl<>(() -> {
|
||||
try {
|
||||
return Bukkit.getScheduler().callSyncMethod(ScrowAPI.plugin, supplier::get).get();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException(e);
|
||||
} catch (ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static Task<?> runSync(Runnable task) {
|
||||
return supplySync(() -> {
|
||||
task.run();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue