mirror of
https://github.com/WiIIiam278/HuskSync.git
synced 2026-01-04 15:31:37 +00:00
Refactor API
This commit is contained in:
@@ -1,49 +1,14 @@
|
||||
//file:noinspection GroovyAssignabilityCheck
|
||||
plugins {
|
||||
id 'java-library'
|
||||
id 'maven-publish'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation project(path: ':common')
|
||||
compileOnly project(path: ':common')
|
||||
implementation project(path: ':bukkit')
|
||||
|
||||
compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
|
||||
compileOnly 'org.jetbrains:annotations:23.0.0'
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
|
||||
}
|
||||
|
||||
afterEvaluate {
|
||||
publishing {
|
||||
publications {
|
||||
maven(MavenPublication) {
|
||||
groupId = "${rootProject.group}.${rootProject.name.toLowerCase()}"
|
||||
artifactId = project.name
|
||||
|
||||
from components.java
|
||||
artifact javadocsJar
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
mavenLocal()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task javadocs(type: Javadoc) {
|
||||
options.encoding = 'UTF-8'
|
||||
options.addStringOption('Xdoclint:none', '-quiet')
|
||||
source = project(':common').sourceSets.main.allJava
|
||||
source += project(':api').sourceSets.main.allJava
|
||||
classpath = files(project(':common').sourceSets.main.compileClasspath)
|
||||
classpath += files(project(':api').sourceSets.main.compileClasspath)
|
||||
destinationDir = file("${buildDir}/docs/javadoc")
|
||||
}
|
||||
|
||||
task javadocsJar(type: Jar, dependsOn: javadocs) {
|
||||
archiveClassifier.set 'javadoc'
|
||||
from javadocs.destinationDir
|
||||
shadowJar {
|
||||
}
|
||||
@@ -2,16 +2,17 @@ package net.william278.husksync.bukkit.api;
|
||||
|
||||
import net.william278.husksync.PlayerData;
|
||||
import net.william278.husksync.Settings;
|
||||
import net.william278.husksync.bukkit.listener.BukkitRedisListener;
|
||||
import net.william278.husksync.redis.RedisMessage;
|
||||
import net.william278.husksync.bukkit.data.DataSerializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* API method class for HuskSync. To access methods, use the {@link #getInstance()} entrypoint.
|
||||
* HuskSync's API. To access methods, use the {@link #getInstance()} entrypoint.
|
||||
*
|
||||
* @author William
|
||||
*/
|
||||
public class HuskSyncAPI {
|
||||
|
||||
@@ -21,7 +22,7 @@ public class HuskSyncAPI {
|
||||
private static HuskSyncAPI instance;
|
||||
|
||||
/**
|
||||
* API entry point. Returns an instance of the {@link HuskSyncAPI}
|
||||
* The API entry point. Returns an instance of the {@link HuskSyncAPI}
|
||||
*
|
||||
* @return instance of the {@link HuskSyncAPI}
|
||||
*/
|
||||
@@ -33,35 +34,38 @@ public class HuskSyncAPI {
|
||||
}
|
||||
|
||||
/**
|
||||
* <b>(Internal only)</b> Map of API requests that are processed by the bukkit plugin that implements the API.
|
||||
*/
|
||||
public static HashMap<UUID, CompletableFuture<PlayerData>> apiRequests = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Returns a {@link CompletableFuture} that will fetch the {@link PlayerData} for a user given their {@link UUID}, which contains synchronised data that can then be deserialized into ItemStacks and other usable values using the {@link DataSerializer} class. If no data could be returned, such as if an invalid UUID is specified, the CompletableFuture will be cancelled. Note that this only returns the last cached data of the user; not necessarily the current state of their inventory if they are online.
|
||||
* Returns a {@link CompletableFuture} that will fetch the {@link PlayerData} for a user given their {@link UUID},
|
||||
* which contains serialized synchronised data.
|
||||
* <p>
|
||||
* This can then be deserialized into ItemStacks and other usable values using the {@code DataSerializer} class.
|
||||
* <p>
|
||||
* If no data could be returned, such as if an invalid UUID is specified, the CompletableFuture will be cancelled.
|
||||
*
|
||||
* @param playerUUID The {@link UUID} of the player to get data for
|
||||
* @return a {@link CompletableFuture} with the user's {@link PlayerData} accessible on completion
|
||||
* @throws IOException If an exception occurs with serializing during processing of the request
|
||||
* @apiNote This only returns the latest saved and cached data of the user. This is <b>not</b> necessarily the current state of their inventory if they are online.
|
||||
*/
|
||||
public CompletableFuture<PlayerData> getPlayerData(UUID playerUUID) throws IOException {
|
||||
// Create the request to be completed
|
||||
final UUID requestUUID = UUID.randomUUID();
|
||||
apiRequests.put(requestUUID, new CompletableFuture<>());
|
||||
BukkitRedisListener.apiRequests.put(requestUUID, new CompletableFuture<>());
|
||||
|
||||
// Remove the request from the map on completion
|
||||
apiRequests.get(requestUUID).whenComplete((playerData, throwable) -> apiRequests.remove(requestUUID));
|
||||
BukkitRedisListener.apiRequests.get(requestUUID).whenComplete((playerData, throwable) -> BukkitRedisListener.apiRequests.remove(requestUUID));
|
||||
|
||||
// Request the data via the proxy
|
||||
new RedisMessage(RedisMessage.MessageType.API_DATA_REQUEST,
|
||||
new RedisMessage.MessageTarget(Settings.ServerType.PROXY, null, Settings.cluster),
|
||||
playerUUID.toString(), requestUUID.toString()).send();
|
||||
|
||||
return apiRequests.get(requestUUID);
|
||||
return BukkitRedisListener.apiRequests.get(requestUUID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a player's {@link PlayerData} to the central cache and database. If the player is online on the Proxy network, they will be updated and overwritten with this data.
|
||||
* Updates a player's {@link PlayerData} to the proxy cache and database.
|
||||
* <p>
|
||||
* If the player is online on the Proxy network, they will be updated and overwritten with this data.
|
||||
*
|
||||
* @param playerData The {@link PlayerData} (which contains the {@link UUID}) of the player data to update to the central cache and database
|
||||
* @throws IOException If an exception occurs with serializing during processing of the update
|
||||
@@ -71,7 +75,7 @@ public class HuskSyncAPI {
|
||||
final String serializedPlayerData = RedisMessage.serialize(playerData);
|
||||
new RedisMessage(RedisMessage.MessageType.PLAYER_DATA_UPDATE,
|
||||
new RedisMessage.MessageTarget(Settings.ServerType.PROXY, null, Settings.cluster),
|
||||
serializedPlayerData).send();
|
||||
serializedPlayerData, Boolean.toString(true)).send();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,6 +19,7 @@ allprojects {
|
||||
|
||||
compileJava.options.encoding = 'UTF-8'
|
||||
javadoc.options.encoding = 'UTF-8'
|
||||
javadoc.options.addStringOption('Xdoclint:none', '-quiet')
|
||||
|
||||
compileJava.options.release.set 16
|
||||
|
||||
@@ -50,7 +51,7 @@ subprojects {
|
||||
version rootProject.version
|
||||
archivesBaseName = "${rootProject.name}-${project.name.capitalize()}"
|
||||
|
||||
if (['bukkit', 'bungeecord', 'velocity', 'api', 'plugin'].contains(project.name)) {
|
||||
if (['bukkit', 'api', 'bungeecord', 'velocity', 'plugin'].contains(project.name)) {
|
||||
shadowJar {
|
||||
destinationDirectory.set(file("$rootDir/target"))
|
||||
archiveClassifier.set('')
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
dependencies {
|
||||
implementation project(':api')
|
||||
implementation project(path: ':common')
|
||||
|
||||
implementation 'org.bstats:bstats-bukkit:3.0.0'
|
||||
|
||||
@@ -58,7 +58,7 @@ public class DataViewer {
|
||||
// Send a redis message with the updated data after the viewing
|
||||
new RedisMessage(RedisMessage.MessageType.PLAYER_DATA_UPDATE,
|
||||
new RedisMessage.MessageTarget(Settings.ServerType.PROXY, null, Settings.cluster),
|
||||
RedisMessage.serialize(playerData))
|
||||
RedisMessage.serialize(playerData), Boolean.toString(true))
|
||||
.send();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.william278.husksync.bukkit.api.events;
|
||||
package net.william278.husksync.bukkit.events;
|
||||
|
||||
import net.william278.husksync.PlayerData;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.william278.husksync.bukkit.api.events;
|
||||
package net.william278.husksync.bukkit.events;
|
||||
|
||||
import net.william278.husksync.PlayerData;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -4,7 +4,6 @@ import de.themoep.minedown.MineDown;
|
||||
import net.william278.husksync.HuskSyncBukkit;
|
||||
import net.william278.husksync.PlayerData;
|
||||
import net.william278.husksync.Settings;
|
||||
import net.william278.husksync.bukkit.api.HuskSyncAPI;
|
||||
import net.william278.husksync.bukkit.config.ConfigLoader;
|
||||
import net.william278.husksync.bukkit.data.DataViewer;
|
||||
import net.william278.husksync.bukkit.migrator.MPDBDeserializer;
|
||||
@@ -17,13 +16,17 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class BukkitRedisListener extends RedisListener {
|
||||
|
||||
private static final HuskSyncBukkit plugin = HuskSyncBukkit.getInstance();
|
||||
|
||||
public static HashMap<UUID, CompletableFuture<PlayerData>> apiRequests = new HashMap<>();
|
||||
|
||||
// Initialize the listener on the bukkit server
|
||||
public BukkitRedisListener() {
|
||||
super();
|
||||
@@ -111,10 +114,10 @@ public class BukkitRedisListener extends RedisListener {
|
||||
}
|
||||
case API_DATA_RETURN -> {
|
||||
final UUID requestUUID = UUID.fromString(message.getMessageDataElements()[0]);
|
||||
if (HuskSyncAPI.apiRequests.containsKey(requestUUID)) {
|
||||
if (apiRequests.containsKey(requestUUID)) {
|
||||
try {
|
||||
final PlayerData data = (PlayerData) RedisMessage.deserialize(message.getMessageDataElements()[1]);
|
||||
HuskSyncAPI.apiRequests.get(requestUUID).complete(data);
|
||||
apiRequests.get(requestUUID).complete(data);
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
log(Level.SEVERE, "Failed to serialize returned API-requested player data");
|
||||
}
|
||||
@@ -124,8 +127,8 @@ public class BukkitRedisListener extends RedisListener {
|
||||
case API_DATA_CANCEL -> {
|
||||
final UUID requestUUID = UUID.fromString(message.getMessageDataElements()[0]);
|
||||
// Cancel requests if no data could be found on the proxy
|
||||
if (HuskSyncAPI.apiRequests.containsKey(requestUUID)) {
|
||||
HuskSyncAPI.apiRequests.get(requestUUID).cancel(true);
|
||||
if (apiRequests.containsKey(requestUUID)) {
|
||||
apiRequests.get(requestUUID).cancel(true);
|
||||
}
|
||||
}
|
||||
case RELOAD_CONFIG -> {
|
||||
|
||||
@@ -3,8 +3,8 @@ package net.william278.husksync.bukkit.util;
|
||||
import net.william278.husksync.HuskSyncBukkit;
|
||||
import net.william278.husksync.PlayerData;
|
||||
import net.william278.husksync.Settings;
|
||||
import net.william278.husksync.bukkit.api.events.SyncCompleteEvent;
|
||||
import net.william278.husksync.bukkit.api.events.SyncEvent;
|
||||
import net.william278.husksync.bukkit.events.SyncCompleteEvent;
|
||||
import net.william278.husksync.bukkit.events.SyncEvent;
|
||||
import net.william278.husksync.bukkit.data.DataSerializer;
|
||||
import net.william278.husksync.bukkit.util.nms.AdvancementUtils;
|
||||
import net.william278.husksync.redis.RedisMessage;
|
||||
|
||||
@@ -13,10 +13,8 @@ shadowJar {
|
||||
relocate 'de.themoep', 'net.william278.husksync.libraries'
|
||||
relocate 'net.byteflux', 'net.william278.husksync.libraries'
|
||||
relocate 'org.bstats', 'net.william278.husksync.libraries.bstats'
|
||||
|
||||
relocate 'redis.clients', 'net.william278.husksync.libraries'
|
||||
relocate 'org.apache', 'net.william278.husksync.libraries'
|
||||
|
||||
relocate 'com.zaxxer', 'net.william278.husksync.libraries'
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly 'com.zaxxer:HikariCP:5.0.1'
|
||||
}
|
||||
|
||||
java {
|
||||
withJavadocJar()
|
||||
}
|
||||
|
||||
shadowJar {
|
||||
relocate 'com.zaxxer', 'net.william278.husksync.libraries'
|
||||
}
|
||||
@@ -5,6 +5,7 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
implementation project(path: ':bukkit', configuration: 'shadow')
|
||||
implementation project(path: ':api', configuration: 'shadow')
|
||||
implementation project(path: ':bungeecord', configuration: 'shadow')
|
||||
implementation project(path: ':velocity', configuration: 'shadow')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user