9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-26 10:09:10 +00:00

Implement variable-sized user data; only save needed data

This commit is contained in:
William
2022-10-07 17:02:26 +01:00
parent b9e474d946
commit cbf5d9c24e
13 changed files with 424 additions and 188 deletions

View File

@@ -66,7 +66,7 @@ public class HuskSyncAPI extends BaseHuskSyncAPI {
return CompletableFuture.runAsync(() -> getUserData(user).thenAccept(userData ->
userData.ifPresent(data -> serializeItemStackArray(inventoryContents)
.thenAccept(serializedInventory -> {
data.getInventoryData().serializedItems = serializedInventory;
data.getInventory().orElse(ItemData.empty()).serializedItems = serializedInventory;
setUserData(user, data).join();
}))));
}
@@ -95,7 +95,7 @@ public class HuskSyncAPI extends BaseHuskSyncAPI {
return CompletableFuture.runAsync(() -> getUserData(user).thenAccept(userData ->
userData.ifPresent(data -> serializeItemStackArray(enderChestContents)
.thenAccept(serializedInventory -> {
data.getEnderChestData().serializedItems = serializedInventory;
data.getEnderChest().orElse(ItemData.empty()).serializedItems = serializedInventory;
setUserData(user, data).join();
}))));
}
@@ -106,12 +106,14 @@ public class HuskSyncAPI extends BaseHuskSyncAPI {
* @param user the {@link User} to get the {@link BukkitInventoryMap} for
* @return future returning the {@link BukkitInventoryMap} for the given {@link User} if they exist,
* otherwise an empty {@link Optional}
* @apiNote If the {@link UserData} does not contain an inventory (i.e. inventory synchronisation is disabled), the
* returned {@link BukkitInventoryMap} will be equivalent an empty inventory.
* @since 2.0
*/
public CompletableFuture<Optional<BukkitInventoryMap>> getPlayerInventory(@NotNull User user) {
return CompletableFuture.supplyAsync(() -> getUserData(user).join()
.map(userData -> deserializeInventory(userData
.getInventoryData().serializedItems).join()));
.map(userData -> deserializeInventory(userData.getInventory()
.orElse(ItemData.empty()).serializedItems).join()));
}
/**
@@ -120,12 +122,14 @@ public class HuskSyncAPI extends BaseHuskSyncAPI {
* @param user the {@link User} to get the Ender Chest contents of
* @return future returning the {@link ItemStack} array of Ender Chest items for the user if they exist,
* otherwise an empty {@link Optional}
* @apiNote If the {@link UserData} does not contain an Ender Chest (i.e. Ender Chest synchronisation is disabled),
* the returned {@link BukkitInventoryMap} will be equivalent to an empty inventory.
* @since 2.0
*/
public CompletableFuture<Optional<ItemStack[]>> getPlayerEnderChest(@NotNull User user) {
return CompletableFuture.supplyAsync(() -> getUserData(user).join()
.map(userData -> deserializeItemStackArray(userData
.getEnderChestData().serializedItems).join()));
.map(userData -> deserializeItemStackArray(userData.getEnderChest()
.orElse(ItemData.empty()).serializedItems).join()));
}
/**

View File

@@ -287,13 +287,16 @@ public class LegacyMigrator extends Migrator {
legacyLocationData == null ? 90f : legacyLocationData.yaw(),
legacyLocationData == null ? 180f : legacyLocationData.pitch());
return new UserData(new StatusData(health, maxHealth, healthScale, hunger, saturation,
saturationExhaustion, selectedSlot, totalExp, expLevel, expProgress, gameMode, isFlying),
new ItemData(serializedInventory), new ItemData(serializedEnderChest),
new PotionEffectData(serializedPotionEffects), convertedAdvancements,
convertedStatisticData, convertedLocationData,
new PersistentDataContainerData(new HashMap<>()),
minecraftVersion);
return UserData.builder(minecraftVersion)
.setStatus(new StatusData(health, maxHealth, healthScale, hunger, saturation,
saturationExhaustion, selectedSlot, totalExp, expLevel, expProgress, gameMode, isFlying))
.setInventory(new ItemData(serializedInventory))
.setEnderChest(new ItemData(serializedEnderChest))
.setPotionEffects(new PotionEffectData(serializedPotionEffects))
.setAdvancements(convertedAdvancements)
.setStatistics(convertedStatisticData)
.setLocation(convertedLocationData)
.build();
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@@ -280,18 +280,14 @@ public class MpdbMigrator extends Migrator {
}
// Create user data record
return new UserData(new StatusData(20, 20, 0, 20, 10,
1, 0, totalExp, expLevel, expProgress, "SURVIVAL",
false),
new ItemData(BukkitSerializer.serializeItemStackArray(inventory.getContents()).join()),
new ItemData(BukkitSerializer.serializeItemStackArray(converter
.getItemStackFromSerializedData(serializedEnderChest)).join()),
new PotionEffectData(""), new ArrayList<>(),
new StatisticsData(new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>()),
new LocationData("world", UUID.randomUUID(), "NORMAL", 0, 0, 0,
0f, 0f),
new PersistentDataContainerData(new HashMap<>()),
minecraftVersion);
return UserData.builder(minecraftVersion)
.setStatus(new StatusData(20, 20, 0, 20, 10,
1, 0, totalExp, expLevel, expProgress, "SURVIVAL",
false))
.setInventory(new ItemData(BukkitSerializer.serializeItemStackArray(inventory.getContents()).join()))
.setEnderChest(new ItemData(BukkitSerializer.serializeItemStackArray(converter
.getItemStackFromSerializedData(serializedEnderChest)).join()))
.build();
});
}
}