use of org.spongepowered.api.data.persistence.DataView in project SpongeCommon by SpongePowered.
the class MemoryDataView method setCollection.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void setCollection(final String key, final Collection<?> value) {
final ImmutableList.Builder<Object> builder = ImmutableList.builder();
@Nullable DataManager manager;
try {
manager = Sponge.dataManager();
} catch (final Exception e) {
manager = null;
}
for (final Object object : value) {
if (object instanceof DataSerializable) {
builder.add(((DataSerializable) object).toContainer());
} else if (object instanceof DataView) {
if (this.safety == org.spongepowered.api.data.persistence.DataView.SafetyMode.ALL_DATA_CLONED || this.safety == org.spongepowered.api.data.persistence.DataView.SafetyMode.CLONED_ON_SET) {
final MemoryDataView view = new MemoryDataContainer(this.safety);
final DataView internalView = (DataView) object;
for (final Map.Entry<DataQuery, Object> entry : internalView.values(false).entrySet()) {
view.set(entry.getKey(), entry.getValue());
}
builder.add(view);
} else {
builder.add(object);
}
} else if (object instanceof ResourceKey) {
builder.add(object.toString());
} else if (object instanceof Map) {
builder.add(this.ensureSerialization((Map) object));
} else if (object instanceof Collection) {
builder.add(this.ensureSerialization((Collection) object));
} else {
if (manager != null) {
final Optional<? extends DataTranslator<?>> translatorOptional = manager.translator(object.getClass());
if (translatorOptional.isPresent()) {
final DataTranslator translator = translatorOptional.get();
final DataContainer container = translator.translate(object);
checkArgument(!container.equals(this), "Cannot insert self-referencing Objects!");
builder.add(container);
} else {
builder.add(object);
}
} else {
builder.add(object);
}
}
}
this.map.put(key, builder.build());
}
use of org.spongepowered.api.data.persistence.DataView in project SpongeCommon by SpongePowered.
the class MemoryDataView method createView.
@Override
public DataView createView(final DataQuery path) {
Objects.requireNonNull(path, "path");
final List<String> queryParts = path.parts();
final int sz = queryParts.size();
checkArgument(sz != 0, "The size of the query must be at least 1");
final String key = queryParts.get(0);
final DataQuery keyQuery = DataQuery.of(key);
if (sz == 1) {
final DataView result = new MemoryDataView(this, keyQuery, this.safety);
this.map.put(key, result);
return result;
}
final DataQuery subQuery = path.popFirst();
DataView subView = (DataView) this.map.get(key);
if (subView == null) {
subView = new MemoryDataView(this.parent, keyQuery, this.safety);
this.map.put(key, subView);
}
return subView.createView(subQuery);
}
use of org.spongepowered.api.data.persistence.DataView in project SpongeCommon by SpongePowered.
the class DataUtil method serializeSpongeData.
public static <T extends SpongeDataHolderBridge & DataCompoundHolder> boolean serializeSpongeData(final T dataHolder) {
CompoundTag compound = dataHolder.data$getCompound();
if (compound == null) {
compound = new CompoundTag();
dataHolder.data$setCompound(compound);
}
// Remove all previous SpongeData
compound.remove(Constants.Sponge.Data.V3.SPONGE_DATA_ROOT.asString("."));
final DataContainer allData = NBTTranslator.INSTANCE.translate(compound);
// Clear old custom data root
final DataView customDataRoot = allData.createView(Constants.Sponge.Data.V3.SPONGE_DATA_ROOT);
// Add back failed data
dataHolder.bridge$getFailedData().forEach(customDataRoot::set);
final DataManipulator.Mutable manipulator = dataHolder.bridge$getManipulator();
final Type dataHolderType = dataHolder.getClass();
manipulator.getKeys().stream().map(key -> SpongeDataManager.getDatastoreRegistry().getDataStore(key, dataHolderType)).forEach(dataStore -> dataStore.serialize(manipulator, allData));
// If data is still present after cleanup merge it back into nbt
if (DataUtil.cleanupEmptySpongeData(allData)) {
compound.merge(NBTTranslator.INSTANCE.translate(allData));
}
if (compound.isEmpty()) {
dataHolder.data$setCompound(null);
return false;
}
return true;
}
use of org.spongepowered.api.data.persistence.DataView in project SpongeCommon by SpongePowered.
the class MemoryDataView method createView.
@Override
public DataView createView(final DataQuery path, final Map<?, ?> map) {
Objects.requireNonNull(path, "path");
final DataView section = this.createView(path);
for (final Map.Entry<?, ?> entry : map.entrySet()) {
if (entry.getValue() instanceof Map) {
section.createView(DataQuery.of('.', entry.getKey().toString()), (Map<?, ?>) entry.getValue());
} else {
section.set(DataQuery.of('.', entry.getKey().toString()), entry.getValue());
}
}
return section;
}
use of org.spongepowered.api.data.persistence.DataView in project SpongeCommon by SpongePowered.
the class MemoryDataView method get.
@Override
public Optional<Object> get(final DataQuery path) {
Objects.requireNonNull(path, "path");
final List<String> queryParts = path.parts();
final int sz = queryParts.size();
if (sz == 0) {
return Optional.of(this);
}
final String key = queryParts.get(0);
if (sz == 1) {
final Object object = this.map.get(key);
if (object == null) {
return Optional.empty();
}
if (this.safety == org.spongepowered.api.data.persistence.DataView.SafetyMode.ALL_DATA_CLONED) {
if (object.getClass().isArray()) {
if (object instanceof byte[]) {
return Optional.of(ArrayUtils.clone((byte[]) object));
} else if (object instanceof short[]) {
return Optional.of(ArrayUtils.clone((short[]) object));
} else if (object instanceof int[]) {
return Optional.of(ArrayUtils.clone((int[]) object));
} else if (object instanceof long[]) {
return Optional.of(ArrayUtils.clone((long[]) object));
} else if (object instanceof float[]) {
return Optional.of(ArrayUtils.clone((float[]) object));
} else if (object instanceof double[]) {
return Optional.of(ArrayUtils.clone((double[]) object));
} else if (object instanceof boolean[]) {
return Optional.of(ArrayUtils.clone((boolean[]) object));
} else {
return Optional.of(ArrayUtils.clone((Object[]) object));
}
}
}
return Optional.of(object);
}
final Optional<DataView> subViewOptional = this.getUnsafeView(key);
if (!subViewOptional.isPresent()) {
return Optional.empty();
}
final DataView subView = subViewOptional.get();
return subView.get(path.popFirst());
}
Aggregations