diff --git a/README.md b/README.md index b84724f2..6a9427c7 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ To fetch PlayerData from a UUID as you need it, create an instance of the HuskSy HuskSyncAPI huskSyncApi = HuskSyncAPI.getInstance(); try { CompletableFuture playerDataCompletableFuture = huskSyncApi.getPlayerData(playerUUID); - // thenAccept blocks the main thread until HuskSync has grabbed the data, so you may wish to run this asynchronously. + // thenAccept blocks the thread until HuskSync has grabbed the data, so you may wish to run this asynchronously (e.g. Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {});. playerDataCompletableFuture.thenAccept(playerData -> { // You now have a PlayerData object which you can get serialized data from and deserialize with the DataSerializer static methods }); diff --git a/api/src/main/java/me/william278/husksync/bukkit/api/HuskSyncAPI.java b/api/src/main/java/me/william278/husksync/bukkit/api/HuskSyncAPI.java index c3dc8ade..7459b631 100644 --- a/api/src/main/java/me/william278/husksync/bukkit/api/HuskSyncAPI.java +++ b/api/src/main/java/me/william278/husksync/bukkit/api/HuskSyncAPI.java @@ -44,17 +44,19 @@ public class HuskSyncAPI { * @throws IOException If an exception occurs with serializing during processing of the request */ public CompletableFuture getPlayerData(UUID playerUUID) throws IOException { + // Create the request to be completed final UUID requestUUID = UUID.randomUUID(); - CompletableFuture playerDataCompletableFuture = new CompletableFuture<>(); - playerDataCompletableFuture.whenComplete((playerData, throwable) -> apiRequests.remove(requestUUID)); + apiRequests.put(requestUUID, new CompletableFuture<>()); + + // Remove the request from the map on completion + apiRequests.get(requestUUID).whenComplete((playerData, throwable) -> 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(); - apiRequests.put(requestUUID, playerDataCompletableFuture); - return playerDataCompletableFuture; + return apiRequests.get(requestUUID); } /** @@ -64,6 +66,7 @@ public class HuskSyncAPI { * @throws IOException If an exception occurs with serializing during processing of the update */ public void updatePlayerData(PlayerData playerData) throws IOException { + // Serialize and send the updated player data final String serializedPlayerData = RedisMessage.serialize(playerData); new RedisMessage(RedisMessage.MessageType.PLAYER_DATA_UPDATE, new RedisMessage.MessageTarget(Settings.ServerType.PROXY, null, Settings.cluster), diff --git a/api/src/main/java/me/william278/husksync/bukkit/data/DataSerializer.java b/api/src/main/java/me/william278/husksync/bukkit/data/DataSerializer.java index b3880b48..edd5d219 100644 --- a/api/src/main/java/me/william278/husksync/bukkit/data/DataSerializer.java +++ b/api/src/main/java/me/william278/husksync/bukkit/data/DataSerializer.java @@ -56,7 +56,7 @@ public class DataSerializer { } /** - * Returns an array of ItemStacks from serialized inventory data + * Returns an array of ItemStacks from serialized inventory data. Note: empty slots will be represented by {@code null} * * @param inventoryData The serialized {@link ItemStack[]} array * @return The inventory contents as an array of {@link ItemStack}s