use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class MixinItemStack method readFromNbt.
@Override
public void readFromNbt(NBTTagCompound compound) {
if (compound.hasKey(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_LIST)) {
final NBTTagList list = compound.getTagList(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_COMPOUND);
if (!list.hasNoTags()) {
compound.removeTag(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST);
final List<DataView> views = Lists.newArrayList();
for (int i = 0; i < list.tagCount(); i++) {
final NBTTagCompound dataCompound = list.getCompoundTagAt(i);
views.add(NbtTranslator.getInstance().translateFrom(dataCompound));
}
final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(views);
final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
for (DataManipulator<?, ?> manipulator : manipulators) {
offerCustom(manipulator, MergeFunction.IGNORE_ALL);
}
if (!transaction.failedData.isEmpty()) {
addFailedData(transaction.failedData);
}
} else {
compound.removeTag(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST);
if (compound.hasNoTags()) {
getTagCompound().removeTag(NbtDataUtil.SPONGE_DATA);
return;
}
}
}
if (compound.hasKey(NbtDataUtil.FAILED_CUSTOM_DATA, NbtDataUtil.TAG_LIST)) {
final NBTTagList list = compound.getTagList(NbtDataUtil.FAILED_CUSTOM_DATA, NbtDataUtil.TAG_COMPOUND);
final ImmutableList.Builder<DataView> builder = ImmutableList.builder();
if (list.tagCount() != 0) {
for (int i = 0; i < list.tagCount(); i++) {
final NBTTagCompound internal = list.getCompoundTagAt(i);
builder.add(NbtTranslator.getInstance().translateFrom(internal));
}
}
// Re-attempt to deserialize custom data
final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(builder.build());
final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
for (DataManipulator<?, ?> manipulator : manipulators) {
offer(manipulator);
}
if (!transaction.failedData.isEmpty()) {
this.addFailedData(transaction.failedData);
}
}
if (compound.hasNoTags()) {
getTagCompound().removeTag(NbtDataUtil.SPONGE_DATA);
if (getTagCompound().hasNoTags()) {
setTagCompound(null);
}
}
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class NBTTranslationTest method testDotContainerKeys.
@Test
public void testDotContainerKeys() {
final DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(DataQuery.of("my.key.to.data"), 1);
NBTTagCompound compound = NbtTranslator.getInstance().translateData(container);
DataView translatedContainer = NbtTranslator.getInstance().translateFrom(compound);
assertEquals(container, translatedContainer);
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class NBTTranslationTest method testContainerToNBT.
@Test
public void testContainerToNBT() {
DataManager service = Mockito.mock(DataManager.class);
DataBuilder<FakeSerializable> builder = new FakeBuilder();
when(service.getBuilder(FakeSerializable.class)).thenReturn(Optional.of(builder));
DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
container.set(DataQuery.of("foo"), "bar");
FakeSerializable temp = new FakeSerializable("bar", 7, 10.0D, "nested");
container.set(DataQuery.of("myFake"), temp);
NBTTagCompound compound = NbtTranslator.getInstance().translateData(container);
DataView translatedContainer = NbtTranslator.getInstance().translateFrom(compound);
assertEquals(container, translatedContainer);
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class HomeDataImpl method from.
// Only required on mutable implementations
@Override
public Optional<HomeData> from(DataContainer container) {
if (!container.contains(MyHomes.DEFAULT_HOME, MyHomes.HOMES)) {
return Optional.empty();
}
// Loads the structure defined in toContainer
this.defaultHome = container.getSerializable(MyHomes.DEFAULT_HOME.getQuery(), Home.class).get();
// Loads the map of homes
this.homes = Maps.newHashMap();
DataView homes = container.getView(MyHomes.HOMES.getQuery()).get();
for (DataQuery homeQuery : homes.getKeys(false)) {
homes.getSerializable(homeQuery, Home.class).ifPresent(home -> this.homes.put(homeQuery.toString(), home));
}
return Optional.of(this);
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class SpongeBlockSnapshotBuilder method buildContent.
@Override
protected Optional<BlockSnapshot> buildContent(DataView container) throws InvalidDataException {
if (!container.contains(DataQueries.BLOCK_STATE, Queries.WORLD_ID, DataQueries.SNAPSHOT_WORLD_POSITION)) {
return Optional.empty();
}
checkDataExists(container, DataQueries.BLOCK_STATE);
checkDataExists(container, Queries.WORLD_ID);
final SpongeBlockSnapshotBuilder builder = new SpongeBlockSnapshotBuilder();
final UUID worldUuid = UUID.fromString(container.getString(Queries.WORLD_ID).get());
final Vector3i coordinate = DataUtil.getPosition3i(container);
Optional<String> creatorUuid = container.getString(Queries.CREATOR_ID);
Optional<String> notifierUuid = container.getString(Queries.NOTIFIER_ID);
// We now reconstruct the custom data and all extra data.
final BlockState blockState = container.getSerializable(DataQueries.BLOCK_STATE, BlockState.class).get();
BlockState extendedState = null;
if (container.contains(DataQueries.BLOCK_EXTENDED_STATE)) {
extendedState = container.getSerializable(DataQueries.BLOCK_EXTENDED_STATE, BlockState.class).get();
} else {
extendedState = blockState;
}
builder.blockState(blockState).extendedState(extendedState).position(coordinate).worldId(worldUuid);
if (creatorUuid.isPresent()) {
builder.creator(UUID.fromString(creatorUuid.get()));
}
if (notifierUuid.isPresent()) {
builder.notifier(UUID.fromString(notifierUuid.get()));
}
Optional<DataView> unsafeCompound = container.getView(DataQueries.UNSAFE_NBT);
final NBTTagCompound compound = unsafeCompound.isPresent() ? NbtTranslator.getInstance().translateData(unsafeCompound.get()) : null;
if (compound != null) {
builder.unsafeNbt(compound);
}
if (container.contains(DataQueries.SNAPSHOT_TILE_DATA)) {
final List<DataView> dataViews = container.getViewList(DataQueries.SNAPSHOT_TILE_DATA).get();
DataUtil.deserializeImmutableManipulatorList(dataViews).stream().forEach(builder::add);
}
return Optional.of(new SpongeBlockSnapshot(builder));
}
Aggregations