Search in sources :

Example 1 with AbstractUser

use of org.lanternpowered.server.entity.living.player.AbstractUser in project LanternServer by LanternPowered.

the class UserIO method load.

public static void load(Path dataFolder, AbstractUser player) throws IOException {
    final String fileName = player.getUniqueId().toString() + ".dat";
    // Search for the player data and load it
    Path dataFile = dataFolder.resolve(PLAYER_DATA_FOLDER).resolve(fileName);
    if (Files.exists(dataFile)) {
        final DataContainer dataContainer = NbtStreamUtils.read(Files.newInputStream(dataFile), true);
        // Load sponge data if present and attach it to the main data
        dataFile = dataFolder.resolve(SPONGE_PLAYER_DATA_FOLDER).resolve(fileName);
        if (Files.exists(dataFile)) {
            final DataContainer spongeDataContainer = NbtStreamUtils.read(Files.newInputStream(dataFile), true);
            dataContainer.set(DataQueries.EXTENDED_SPONGE_DATA, spongeDataContainer);
        }
        final ObjectStore<AbstractUser> objectStore = ObjectStoreRegistry.get().get(AbstractUser.class).get();
        objectStore.deserialize(player, dataContainer);
    }
    final Path statisticsFile = dataFolder.resolve(STATISTICS_FOLDER).resolve(player.getUniqueId().toString() + ".json");
    if (Files.exists(statisticsFile)) {
        player.getStatisticMap().load(statisticsFile);
    }
}
Also used : Path(java.nio.file.Path) DataContainer(org.spongepowered.api.data.DataContainer) AbstractUser(org.lanternpowered.server.entity.living.player.AbstractUser)

Example 2 with AbstractUser

use of org.lanternpowered.server.entity.living.player.AbstractUser in project LanternServer by LanternPowered.

the class UserIO method save.

public static void save(Path dataFolder, AbstractUser player) throws IOException {
    final String fileName = player.getUniqueId().toString() + ".dat";
    final DataContainer dataContainer = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    final ObjectStore<AbstractUser> objectStore = ObjectStoreRegistry.get().get(AbstractUser.class).get();
    objectStore.serialize(player, dataContainer);
    final Optional<DataView> optSpongeData = dataContainer.getView(DataQueries.EXTENDED_SPONGE_DATA);
    dataContainer.remove(DataQueries.EXTENDED_SPONGE_DATA);
    Path dataFolder0 = dataFolder.resolve(PLAYER_DATA_FOLDER);
    if (!Files.exists(dataFolder0)) {
        Files.createDirectories(dataFolder0);
    }
    Path dataFile = dataFolder0.resolve(fileName);
    NbtStreamUtils.write(dataContainer, Files.newOutputStream(dataFile), true);
    dataFolder0 = dataFolder.resolve(SPONGE_PLAYER_DATA_FOLDER);
    if (!Files.exists(dataFolder0)) {
        Files.createDirectories(dataFolder0);
    }
    dataFile = dataFolder0.resolve(fileName);
    if (optSpongeData.isPresent()) {
        final DataView spongeData = optSpongeData.get();
        spongeData.set(NAME, player.getName());
        NbtStreamUtils.write(spongeData, Files.newOutputStream(dataFile), true);
    } else {
        Files.deleteIfExists(dataFile);
    }
    final Path statisticsFile = dataFolder.resolve(STATISTICS_FOLDER).resolve(player.getUniqueId().toString() + ".json");
    player.getStatisticMap().save(statisticsFile);
}
Also used : Path(java.nio.file.Path) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) AbstractUser(org.lanternpowered.server.entity.living.player.AbstractUser)

Aggregations

Path (java.nio.file.Path)2 AbstractUser (org.lanternpowered.server.entity.living.player.AbstractUser)2 DataContainer (org.spongepowered.api.data.DataContainer)2 DataView (org.spongepowered.api.data.DataView)1