use of org.spongepowered.api.data.DataQuery 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;
}
Aggregations