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);
}
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
}
}
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
}
}
Aggregations