use of org.spongepowered.api.data.persistence.DataContainer 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.DataContainer in project SpongeCommon by SpongePowered.
the class MemoryDataView method copy.
@Override
public DataContainer copy() {
final DataContainer container = new MemoryDataContainer(this.safety);
this.keys(false).forEach(query -> this.get(query).ifPresent(obj -> container.set(query, obj)));
return container;
}
use of org.spongepowered.api.data.persistence.DataContainer 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.DataContainer in project SpongeCommon by SpongePowered.
the class SpongeItemStackSnapshot method toContainer.
@Override
public DataContainer toContainer() {
final ResourceKey resourceKey = Sponge.game().registry(RegistryTypes.ITEM_TYPE).valueKey(this.itemType);
final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Constants.ItemStack.TYPE, resourceKey).set(Constants.ItemStack.COUNT, this.quantity).set(Constants.ItemStack.DAMAGE_VALUE, this.damageValue);
if (!this.manipulators.isEmpty()) {
// TODO container.set(Constants.Sponge.DATA_MANIPULATORS, DataUtil.getSerializedImmutableManipulatorList(this.manipulators));
}
if (this.compound != null) {
container.set(Constants.Sponge.UNSAFE_NBT, NBTTranslator.INSTANCE.translateFrom(this.compound));
}
return container;
}
use of org.spongepowered.api.data.persistence.DataContainer in project SpongeCommon by SpongePowered.
the class ItemStackMixin_API method toContainer.
@Override
public DataContainer toContainer() {
final ResourceKey key = (ResourceKey) (Object) Registry.ITEM.getKey((Item) this.itemStack$type());
final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Constants.ItemStack.TYPE, key).set(Constants.ItemStack.COUNT, this.itemStack$quantity()).set(Constants.ItemStack.DAMAGE_VALUE, this.shadow$getDamageValue());
if (this.shadow$hasTag()) {
// no tag? no data, simple as that.
final CompoundTag compound = this.shadow$getTag().copy();
if (compound.contains(Constants.Sponge.Data.V2.SPONGE_DATA)) {
final CompoundTag spongeCompound = compound.getCompound(Constants.Sponge.Data.V2.SPONGE_DATA);
if (spongeCompound.contains(Constants.Sponge.Data.V2.CUSTOM_MANIPULATOR_TAG_LIST)) {
spongeCompound.remove(Constants.Sponge.Data.V2.CUSTOM_MANIPULATOR_TAG_LIST);
}
}
// We must filter the custom data so it isn't stored twice
Constants.NBT.filterSpongeCustomData(compound);
if (!compound.isEmpty()) {
final DataContainer unsafeNbt = NBTTranslator.INSTANCE.translateFrom(compound);
container.set(Constants.Sponge.UNSAFE_NBT, unsafeNbt);
}
}
try {
PlatformHooks.INSTANCE.getItemHooks().writeItemStackCapabilitiesToDataView(container, (net.minecraft.world.item.ItemStack) (Object) this);
} catch (final Exception e) {
e.printStackTrace();
}
return container;
}
Aggregations