Search in sources :

Example 1 with SimpleValueContainer

use of org.lanternpowered.server.data.io.store.SimpleValueContainer in project LanternServer by LanternPowered.

the class UserStore method deserializeValues.

@Override
public void deserializeValues(T player, SimpleValueContainer valueContainer, DataView dataView) {
    // Try to convert old bukkit values first
    dataView.getLong(BUKKIT_FIRST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.FIRST_DATE_PLAYED, Instant.ofEpochMilli(v)));
    dataView.getLong(BUKKIT_LAST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.LAST_DATE_PLAYED, Instant.ofEpochMilli(v)));
    // Deserialize sponge data
    dataView.getView(DataQueries.EXTENDED_SPONGE_DATA).ifPresent(view -> {
        view.getLong(FIRST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.FIRST_DATE_PLAYED, Instant.ofEpochMilli(v)));
        view.getLong(LAST_DATE_PLAYED).ifPresent(v -> valueContainer.set(Keys.LAST_DATE_PLAYED, Instant.ofEpochMilli(v)));
    });
    dataView.getView(ABILITIES).ifPresent(view -> {
        view.getInt(FLYING).ifPresent(v -> valueContainer.set(Keys.IS_FLYING, v > 0));
        view.getDouble(FLYING_SPEED).ifPresent(v -> valueContainer.set(Keys.FLYING_SPEED, v));
        view.getInt(CAN_FLY).ifPresent(v -> valueContainer.set(Keys.CAN_FLY, v > 0));
    });
    final Map<UUID, RespawnLocation> respawnLocations = new HashMap<>();
    // Overworld respawn location is saved in the root container
    final Optional<Double> optSpawnX = dataView.getDouble(RESPAWN_LOCATIONS_X);
    final Optional<Double> optSpawnY = dataView.getDouble(RESPAWN_LOCATIONS_Y);
    final Optional<Double> optSpawnZ = dataView.getDouble(RESPAWN_LOCATIONS_Z);
    if (optSpawnX.isPresent() && optSpawnY.isPresent() && optSpawnZ.isPresent()) {
        UUID uniqueId = Lantern.getWorldManager().getWorldProperties(0).get().getUniqueId();
        respawnLocations.put(uniqueId, deserializeRespawnLocation(dataView, uniqueId, optSpawnX.get(), optSpawnY.get(), optSpawnZ.get()));
    }
    dataView.getViewList(RESPAWN_LOCATIONS).ifPresent(v -> v.forEach(view -> {
        int dimensionId = view.getInt(RESPAWN_LOCATIONS_DIMENSION).get();
        Lantern.getWorldManager().getWorldProperties(dimensionId).ifPresent(props -> {
            UUID uniqueId = props.getUniqueId();
            double x = view.getDouble(RESPAWN_LOCATIONS_X).get();
            double y = view.getDouble(RESPAWN_LOCATIONS_Y).get();
            double z = view.getDouble(RESPAWN_LOCATIONS_Z).get();
            respawnLocations.put(uniqueId, deserializeRespawnLocation(view, uniqueId, x, y, z));
        });
    }));
    valueContainer.set(Keys.RESPAWN_LOCATIONS, respawnLocations);
    dataView.getInt(SCORE).ifPresent(v -> valueContainer.set(LanternKeys.SCORE, v));
    final GameMode gameMode = dataView.getInt(GAME_MODE).flatMap(v -> GameModeRegistryModule.get().getByInternalId(v)).orElse(GameModes.NOT_SET);
    valueContainer.set(Keys.GAME_MODE, gameMode);
    player.getInventory().getHotbar().setSelectedSlotIndex(dataView.getInt(SELECTED_ITEM_SLOT).orElse(0));
    // Deserialize the player inventory
    dataView.getViewList(INVENTORY).ifPresent(views -> deserializePlayerInventory(player.getInventory(), views));
    // Deserialize the ender chest inventory
    dataView.getViewList(ENDER_CHEST_INVENTORY).ifPresent(views -> deserializeEnderChest(player.getEnderChestInventory(), views));
    dataView.getView(RECIPE_BOOK).ifPresent(view -> {
        view.getInt(RECIPE_BOOK_FILTER_ACTIVE).ifPresent(v -> valueContainer.set(LanternKeys.RECIPE_BOOK_FILTER_ACTIVE, v > 0));
        view.getInt(RECIPE_BOOK_GUI_OPEN).ifPresent(v -> valueContainer.set(LanternKeys.RECIPE_BOOK_GUI_OPEN, v > 0));
    });
    dataView.getString(OPEN_ADVANCEMENT_TREE).ifPresent(id -> valueContainer.set(LanternKeys.OPEN_ADVANCEMENT_TREE, AdvancementTreeRegistryModule.get().getById(id)));
    super.deserializeValues(player, valueContainer, dataView);
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) Vector3d(com.flowpowered.math.vector.Vector3d) HashMap(java.util.HashMap) DataHelper.getOrCreateView(org.lanternpowered.server.data.DataHelper.getOrCreateView) ItemStackStore(org.lanternpowered.server.data.io.store.item.ItemStackStore) DataQuery(org.spongepowered.api.data.DataQuery) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) DataQueries(org.lanternpowered.server.data.DataQueries) AbstractUser(org.lanternpowered.server.entity.living.player.AbstractUser) ArrayList(java.util.ArrayList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Queries(org.spongepowered.api.data.Queries) LanternPlayerEquipmentInventory(org.lanternpowered.server.inventory.vanilla.LanternPlayerEquipmentInventory) Map(java.util.Map) LanternWorld(org.lanternpowered.server.world.LanternWorld) GameModeRegistryModule(org.lanternpowered.server.game.registry.type.entity.player.GameModeRegistryModule) ObjectSerializer(org.lanternpowered.server.data.io.store.ObjectSerializer) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) DataContainer(org.spongepowered.api.data.DataContainer) LanternMainPlayerInventory(org.lanternpowered.server.inventory.vanilla.LanternMainPlayerInventory) Slot(org.spongepowered.api.item.inventory.Slot) RespawnLocation(org.spongepowered.api.util.RespawnLocation) UUID(java.util.UUID) Instant(java.time.Instant) AbstractUserInventory(org.lanternpowered.server.inventory.vanilla.AbstractUserInventory) LanternWorldProperties(org.lanternpowered.server.world.LanternWorldProperties) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) List(java.util.List) SimpleValueContainer(org.lanternpowered.server.data.io.store.SimpleValueContainer) DataView(org.spongepowered.api.data.DataView) Lantern(org.lanternpowered.server.game.Lantern) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) AdvancementTreeRegistryModule(org.lanternpowered.server.game.registry.type.advancement.AdvancementTreeRegistryModule) Optional(java.util.Optional) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) RespawnLocation(org.spongepowered.api.util.RespawnLocation) HashMap(java.util.HashMap) UUID(java.util.UUID)

Example 2 with SimpleValueContainer

use of org.lanternpowered.server.data.io.store.SimpleValueContainer in project LanternServer by LanternPowered.

the class CompositeValueContainerStore method deserialize.

@SuppressWarnings("unchecked")
@Override
public void deserialize(T object, DataView dataView) {
    if (object instanceof IValueContainer) {
        final IValueContainer<S> valueContainer = (IValueContainer<S>) object;
        final SimpleValueContainer simpleValueContainer = new SimpleValueContainer(new HashMap<>());
        deserializeValues(object, simpleValueContainer, dataView);
        final Optional<DataView> optDataView = dataView.getView(VALUES);
        if (optDataView.isPresent()) {
            final DataTypeSerializerContext context = Lantern.getGame().getDataManager().getTypeSerializerContext();
            final DataView valuesView = optDataView.get();
            for (Map.Entry<DataQuery, Object> entry : valuesView.getValues(false).entrySet()) {
                final Key key = KeyRegistryModule.get().getById(entry.getKey().toString()).orElse(null);
                if (key == null) {
                    Lantern.getLogger().warn("Unable to deserialize the data value with key: {} because it doesn't exist.", entry.getKey().toString());
                } else {
                    final TypeToken<?> typeToken = key.getElementToken();
                    final DataTypeSerializer dataTypeSerializer = Lantern.getGame().getDataManager().getTypeSerializer(typeToken).orElse(null);
                    if (dataTypeSerializer == null) {
                        Lantern.getLogger().warn("Unable to deserialize the data key value: {}, " + "no supported deserializer exists.", key.getId());
                    } else {
                        if (simpleValueContainer.get(key).isPresent()) {
                            Lantern.getLogger().warn("Duplicate usage of the key: {}", key.getId());
                        } else {
                            simpleValueContainer.set(key, dataTypeSerializer.deserialize(typeToken, context, entry.getValue()));
                        }
                    }
                }
            }
        }
        for (Map.Entry<Key<?>, Object> entry : simpleValueContainer.getValues().entrySet()) {
            final Element element = (Element) valueContainer.getValueCollection().getElement((Key) entry.getKey()).orElse(null);
            if (element != null) {
                element.set(entry.getValue());
            } else {
                Lantern.getLogger().warn("Attempted to offer a unsupported value with key \"{}\" to the object {}", entry.getKey().toString(), object.toString());
            }
        }
        dataView.getView(DataQueries.SPONGE_DATA).ifPresent(view -> DataHelper.deserializeRawContainerData(dataView, valueContainer, DataQueries.CUSTOM_MANIPULATORS));
    } else {
    // Not sure what to do
    }
}
Also used : IValueContainer(org.lanternpowered.server.data.IValueContainer) DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) SimpleValueContainer(org.lanternpowered.server.data.io.store.SimpleValueContainer) DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) HashMap(java.util.HashMap) Map(java.util.Map) Key(org.spongepowered.api.data.key.Key)

Example 3 with SimpleValueContainer

use of org.lanternpowered.server.data.io.store.SimpleValueContainer in project LanternServer by LanternPowered.

the class CompositeValueContainerStore method serialize.

@SuppressWarnings("unchecked")
@Override
public void serialize(T object, DataView dataView) {
    if (object instanceof IValueContainer) {
        final IValueContainer<S> valueContainer = (IValueContainer<S>) object;
        final SimpleValueContainer simpleValueContainer = new SimpleValueContainer(new HashMap<>());
        for (KeyRegistration<?, ?> registration : valueContainer.getValueCollection().getAll()) {
            if (!(registration instanceof Element)) {
                continue;
            }
            final Key key = registration.getKey();
            final Object element = ((Element) registration).get();
            if (element != null) {
                simpleValueContainer.set(key, element);
            }
        }
        // Serialize the values, all written values will be removed from
        // the simple value container
        serializeValues(object, simpleValueContainer, dataView);
        // Write the rest to the Values tag
        final Map<Key<?>, Object> values = simpleValueContainer.getValues();
        if (!values.isEmpty()) {
            final DataView valuesView = dataView.createView(VALUES);
            final DataTypeSerializerContext context = Lantern.getGame().getDataManager().getTypeSerializerContext();
            for (Map.Entry<Key<?>, Object> entry : values.entrySet()) {
                final TypeToken<?> typeToken = entry.getKey().getElementToken();
                final DataTypeSerializer dataTypeSerializer = Lantern.getGame().getDataManager().getTypeSerializer(typeToken).orElse(null);
                if (dataTypeSerializer == null) {
                    Lantern.getLogger().warn("Unable to serialize the data key value: " + entry.getKey());
                } else {
                    valuesView.set(DataQuery.of(entry.getKey().getId()), dataTypeSerializer.serialize(typeToken, context, entry.getValue()));
                }
            }
            if (valuesView.isEmpty()) {
                dataView.remove(VALUES);
            }
        }
        DataHelper.serializeRawContainerData(getOrCreateView(dataView, DataQueries.SPONGE_DATA), valueContainer, DataQueries.CUSTOM_MANIPULATORS);
    } else {
    // Not sure what to do
    }
}
Also used : IValueContainer(org.lanternpowered.server.data.IValueContainer) DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) SimpleValueContainer(org.lanternpowered.server.data.io.store.SimpleValueContainer) DataView(org.spongepowered.api.data.DataView) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) HashMap(java.util.HashMap) Map(java.util.Map) Key(org.spongepowered.api.data.key.Key)

Aggregations

HashMap (java.util.HashMap)3 Map (java.util.Map)3 SimpleValueContainer (org.lanternpowered.server.data.io.store.SimpleValueContainer)3 DataView (org.spongepowered.api.data.DataView)3 IValueContainer (org.lanternpowered.server.data.IValueContainer)2 Element (org.lanternpowered.server.data.element.Element)2 DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)2 DataTypeSerializerContext (org.lanternpowered.server.data.persistence.DataTypeSerializerContext)2 DataQuery (org.spongepowered.api.data.DataQuery)2 Key (org.spongepowered.api.data.key.Key)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 DataHelper.getOrCreateView (org.lanternpowered.server.data.DataHelper.getOrCreateView)1 DataQueries (org.lanternpowered.server.data.DataQueries)1 ObjectSerializer (org.lanternpowered.server.data.io.store.ObjectSerializer)1 ItemStackStore (org.lanternpowered.server.data.io.store.item.ItemStackStore)1