use of org.lanternpowered.server.data.persistence.DataTypeSerializer in project LanternServer by LanternPowered.
the class DataHelper method serializeRawRegisteredKeyData.
public static void serializeRawRegisteredKeyData(DataView dataView, IValueContainer valueContainer, Set<Key> ignoredKeys) {
DataView view = null;
final ValueCollection valueCollection = valueContainer.getValueCollection();
final LanternDataManager dataManager = Lantern.getGame().getDataManager();
final DataTypeSerializerContext context = dataManager.getTypeSerializerContext();
for (KeyRegistration<?, ?> registration : valueCollection.getAll()) {
final Key<?> key = registration.getKey();
if (!(registration instanceof Element) || ignoredKeys.contains(key) || key == LanternKeys.FAILED_DATA_MANIPULATORS || key == LanternKeys.FAILED_DATA_VALUES) {
continue;
}
final Element element = (Element) registration;
final TypeToken<?> typeToken = key.getElementToken();
final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
final Object object = element.get();
if (object == null) {
continue;
}
final Object value = typeSerializer.serialize(typeToken, context, object);
if (view == null) {
view = dataView.createView(DataQueries.DATA_VALUES);
}
view.set(key.getQuery(), value);
}
Element<DataView> holder = valueCollection.getElement(LanternKeys.FAILED_DATA_VALUES).orElse(null);
if (holder != null) {
if (view == null) {
view = dataView.createView(DataQueries.DATA_VALUES);
}
for (Map.Entry<DataQuery, Object> entry : holder.get().getValues(false).entrySet()) {
if (!view.contains(entry.getKey())) {
view.set(entry.getKey(), entry.getValue());
}
}
}
}
use of org.lanternpowered.server.data.persistence.DataTypeSerializer in project LanternServer by LanternPowered.
the class DataHelper method deserializeRawRegisteredKeyData.
public static void deserializeRawRegisteredKeyData(DataView dataView, IValueContainer valueContainer) throws InvalidDataException {
dataView = dataView.getView(DataQueries.DATA_VALUES).orElse(null);
if (dataView == null) {
return;
}
final ValueCollection valueCollection = valueContainer.getValueCollection();
final LanternDataManager dataManager = Lantern.getGame().getDataManager();
final DataTypeSerializerContext context = dataManager.getTypeSerializerContext();
for (KeyRegistration<?, ?> registration : valueCollection.getAll()) {
final Key<?> key = registration.getKey();
if (!(registration instanceof Element) || key == LanternKeys.FAILED_DATA_MANIPULATORS || key == LanternKeys.FAILED_DATA_VALUES) {
continue;
}
final Optional<Object> data = dataView.get(key.getQuery());
if (!data.isPresent()) {
continue;
}
dataView.remove(key.getQuery());
final TypeToken<?> typeToken = key.getElementToken();
final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
((Element) registration).set(typeSerializer.deserialize(typeToken, context, data.get()));
}
if (valueContainer instanceof CompositeValueStore) {
final CompositeValueStore store = (CompositeValueStore) valueContainer;
for (Map.Entry<DataQuery, Object> entry : dataView.getValues(false).entrySet()) {
final Key<?> key = KeyRegistryModule.get().getByQuery(entry.getKey()).orElse(null);
if (key == null) {
continue;
}
final TypeToken<?> typeToken = key.getElementToken();
final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
store.offer(key, typeSerializer.deserialize(typeToken, context, entry.getValue()));
dataView.remove(entry.getKey());
}
}
if (!dataView.isEmpty()) {
// Should be safe to cast, at least if nobody touches this key
Element<DataView> holder = valueCollection.getElement(LanternKeys.FAILED_DATA_VALUES).orElse(null);
if (holder == null) {
holder = valueCollection.register(LanternKeys.FAILED_DATA_VALUES, null);
}
holder.set(dataView);
}
}
use of org.lanternpowered.server.data.persistence.DataTypeSerializer in project LanternServer by LanternPowered.
the class DataHelper method toContainer.
public static DataContainer toContainer(IValueContainer valueContainer) {
final DataContainer dataContainer = DataContainer.createNew();
final ValueCollection valueCollection = valueContainer.getValueCollection();
final LanternDataManager dataManager = Lantern.getGame().getDataManager();
for (KeyRegistration<?, ?> registration : valueCollection.getAll()) {
if (!(registration instanceof Element)) {
continue;
}
final Key<?> key = registration.getKey();
final DataQuery dataQuery = key.getQuery();
final TypeToken<?> typeToken = key.getElementToken();
final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
final DataTypeSerializerContext context = dataManager.getTypeSerializerContext();
// The value's shouldn't be null inside a data manipulator,
// since it doesn't support removal of values
dataContainer.set(dataQuery, typeSerializer.serialize(typeToken, context, checkNotNull(((Element) registration).get(), "element")));
}
return dataContainer;
}
use of org.lanternpowered.server.data.persistence.DataTypeSerializer in project LanternServer by LanternPowered.
the class CommandSetData method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
final ThreadLocal<Key<?>> currentKey = new ThreadLocal<>();
specBuilder.arguments(GenericArguments.playerOrSource(Text.of("player")), new PatternMatchingCommandElement(Text.of("key")) {
@Override
protected Iterable<String> getChoices(CommandSource source) {
return Sponge.getGame().getRegistry().getAllOf(Key.class).stream().map(Key::getId).collect(Collectors.toList());
}
@Override
protected Object getValue(String choice) throws IllegalArgumentException {
final Optional<Key> ret = Sponge.getGame().getRegistry().getType(Key.class, choice);
if (!ret.isPresent()) {
throw new IllegalArgumentException("Invalid input " + choice + " was found");
}
currentKey.set(ret.get());
return ret.get();
}
}, new CommandElement(Text.of("data")) {
@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
args.next();
final String content = args.getRaw().substring(args.getRawPosition()).trim();
while (args.hasNext()) {
args.next();
}
final Object data;
try {
// Don't be too strict
data = JsonDataFormat.read(content, true).orElse(null);
} catch (IOException e) {
throw args.createError(t("Invalid json data: %s\nError: %s", content, e.getMessage()));
}
final Key key = currentKey.get();
final TypeToken<?> typeToken = key.getElementToken();
if (content.isEmpty()) {
return null;
}
final DataTypeSerializer dataTypeSerializer = Lantern.getGame().getDataManager().getTypeSerializer(typeToken).orElse(null);
if (dataTypeSerializer == null) {
throw args.createError(Text.of("Unable to deserialize the data key value: {}, " + "no supported deserializer exists.", key.getId()));
} else {
final DataTypeSerializerContext context = Lantern.getGame().getDataManager().getTypeSerializerContext();
try {
// Put it in a holder object, the command element separates iterable objects
return new ValueHolder(dataTypeSerializer.deserialize(typeToken, context, data));
} catch (InvalidDataException e) {
throw args.createError(t("Invalid data: %s", e.getMessage()));
}
}
}
@Override
public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
return new ArrayList<>();
}
}).executor((src, args) -> {
final Player target = args.<Player>getOne("player").get();
final Key key = args.<Key>getOne("key").get();
final Object data = args.<ValueHolder>getOne("data").get().data;
target.offer(key, data);
src.sendMessage(t("Successfully offered the data for the key %s to the player %s", key.getId(), target.getName()));
return CommandResult.success();
});
}
Aggregations