Search in sources :

Example 1 with DataContainer

use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.

the class SchematicTranslator method translate.

@Override
public DataContainer translate(Schematic schematic) throws InvalidDataException {
    DataContainer data = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    addTo(schematic, data);
    return data;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Example 2 with DataContainer

use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.

the class CustomDataNbtUtil method apply.

public static DataTransactionResult apply(DataView view, DataManipulator<?, ?> manipulator) {
    if (!view.contains(DataQueries.Compatibility.Forge.ROOT)) {
        view.set(DataQueries.Compatibility.Forge.ROOT, DataContainer.createNew());
    }
    final DataView forgeCompound = view.getView(DataQueries.Compatibility.Forge.ROOT).orElseThrow(DataUtil.dataNotFound());
    if (!forgeCompound.contains(DataQueries.General.SPONGE_ROOT)) {
        forgeCompound.set(DataQueries.General.SPONGE_ROOT, DataContainer.createNew());
    }
    final DataView spongeTag = forgeCompound.getView(DataQueries.General.SPONGE_ROOT).orElseThrow(DataUtil.dataNotFound());
    boolean isReplacing;
    // Validate that the custom manipulator isn't already existing in the compound
    final List<DataView> customData;
    if (spongeTag.contains(DataQueries.General.CUSTOM_MANIPULATOR_LIST)) {
        customData = spongeTag.getViewList(DataQueries.General.CUSTOM_MANIPULATOR_LIST).orElseThrow(DataUtil.dataNotFound());
        for (DataView dataView : customData) {
            final String dataId = dataView.getString(DataQueries.DATA_ID).orElseThrow(DataUtil.dataNotFound());
            if (DataUtil.getRegistrationFor(manipulator).getId().equals(dataId)) {
                final DataView existingData = dataView.getView(DataQueries.INTERNAL_DATA).orElseThrow(DataUtil.dataNotFound());
                DataManipulator<?, ?> existing = deserialize(dataId, existingData);
                isReplacing = existing != null;
                final DataContainer container = manipulator.toContainer();
                dataView.set(DataQueries.INTERNAL_DATA, container);
                if (isReplacing) {
                    return DataTransactionResult.successReplaceResult(manipulator.getValues(), existing.getValues());
                }
                return DataTransactionResult.successReplaceResult(manipulator.getValues(), ImmutableList.of());
            }
        }
    } else {
        customData = new ArrayList<>();
    }
    final DataContainer container = DataContainer.createNew();
    container.set(DataQueries.DATA_ID, DataUtil.getRegistrationFor(manipulator).getId());
    container.set(DataQueries.INTERNAL_DATA, manipulator.toContainer());
    customData.add(container);
    spongeTag.set(DataQueries.General.CUSTOM_MANIPULATOR_LIST, customData);
    return DataTransactionResult.builder().result(DataTransactionResult.Type.SUCCESS).success(manipulator.getValues()).build();
}
Also used : DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer)

Example 3 with DataContainer

use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.

the class CustomDataNbtUtil method apply.

public static DataTransactionResult apply(NBTTagCompound compound, DataManipulator<?, ?> manipulator) {
    if (!compound.hasKey(NbtDataUtil.FORGE_DATA, NbtDataUtil.TAG_COMPOUND)) {
        compound.setTag(NbtDataUtil.FORGE_DATA, new NBTTagCompound());
    }
    final NBTTagCompound forgeCompound = compound.getCompoundTag(NbtDataUtil.FORGE_DATA);
    if (!forgeCompound.hasKey(NbtDataUtil.SPONGE_DATA, NbtDataUtil.TAG_COMPOUND)) {
        forgeCompound.setTag(NbtDataUtil.SPONGE_DATA, new NBTTagCompound());
    }
    final NBTTagCompound spongeTag = forgeCompound.getCompoundTag(NbtDataUtil.SPONGE_DATA);
    boolean isReplacing;
    // Validate that the custom manipulator isn't already existing in the compound
    final NBTTagList list;
    if (spongeTag.hasKey(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_LIST)) {
        list = spongeTag.getTagList(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_COMPOUND);
        for (int i = 0; i < list.tagCount(); i++) {
            final NBTTagCompound dataCompound = list.getCompoundTagAt(i);
            final String clazzName = dataCompound.getString(NbtDataUtil.CUSTOM_DATA_CLASS);
            if (manipulator.getClass().getName().equals(clazzName)) {
                final NBTTagCompound current = dataCompound.getCompoundTag(NbtDataUtil.CUSTOM_DATA);
                final DataContainer currentView = NbtTranslator.getInstance().translate(current);
                DataManipulator<?, ?> existing = deserialize(clazzName, currentView);
                isReplacing = existing != null;
                final DataContainer container = manipulator.toContainer();
                final NBTTagCompound newCompound = NbtTranslator.getInstance().translateData(container);
                dataCompound.setTag(NbtDataUtil.CUSTOM_DATA_CLASS, newCompound);
                if (isReplacing) {
                    return DataTransactionResult.successReplaceResult(manipulator.getValues(), existing.getValues());
                }
                return DataTransactionResult.successReplaceResult(manipulator.getValues(), ImmutableList.of());
            }
        }
    } else {
        list = new NBTTagList();
        spongeTag.setTag(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, list);
    }
    // We are now adding to the list, not replacing
    final NBTTagCompound newCompound = new NBTTagCompound();
    newCompound.setString(NbtDataUtil.CUSTOM_DATA_CLASS, manipulator.getClass().getName());
    final DataContainer dataContainer = manipulator.toContainer();
    final NBTTagCompound dataCompound = NbtTranslator.getInstance().translateData(dataContainer);
    newCompound.setTag(NbtDataUtil.CUSTOM_DATA, dataCompound);
    list.appendTag(newCompound);
    return DataTransactionResult.builder().result(DataTransactionResult.Type.SUCCESS).success(manipulator.getValues()).build();
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) DataContainer(org.spongepowered.api.data.DataContainer) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 4 with DataContainer

use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.

the class MixinTileEntity method toContainer.

@Override
public DataContainer toContainer() {
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, getContentVersion()).set(Queries.WORLD_ID, ((World) this.world).getUniqueId().toString()).set(Queries.POSITION_X, this.getPos().getX()).set(Queries.POSITION_Y, this.getPos().getY()).set(Queries.POSITION_Z, this.getPos().getZ()).set(DataQueries.BLOCK_ENTITY_TILE_TYPE, this.tileType.getId());
    final NBTTagCompound compound = new NBTTagCompound();
    this.writeToNBT(compound);
    // We must filter the custom data so it isn't stored twice
    NbtDataUtil.filterSpongeCustomData(compound);
    container.set(DataQueries.UNSAFE_NBT, NbtTranslator.getInstance().translateFrom(compound));
    final Collection<DataManipulator<?, ?>> manipulators = ((IMixinCustomDataHolder) this).getCustomManipulators();
    if (!manipulators.isEmpty()) {
        container.set(DataQueries.DATA_MANIPULATORS, DataUtil.getSerializedManipulatorList(manipulators));
    }
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) World(org.spongepowered.api.world.World) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

Example 5 with DataContainer

use of org.spongepowered.api.data.DataContainer in project SpongeCommon by SpongePowered.

the class MixinTileEntityBeacon method toContainer.

@Override
public DataContainer toContainer() {
    DataContainer container = super.toContainer();
    container.set(of("effect1"), getField(1));
    container.set(of("effect2"), getField(2));
    return container;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer)

Aggregations

DataContainer (org.spongepowered.api.data.DataContainer)104 DataQuery (org.spongepowered.api.data.DataQuery)26 DataView (org.spongepowered.api.data.DataView)26 Test (org.junit.Test)25 List (java.util.List)14 ArrayList (java.util.ArrayList)12 Map (java.util.Map)12 IOException (java.io.IOException)9 Path (java.nio.file.Path)9 Nullable (javax.annotation.Nullable)8 ImmutableList (com.google.common.collect.ImmutableList)7 Collection (java.util.Collection)7 Optional (java.util.Optional)7 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)7 Sponge (org.spongepowered.api.Sponge)7 Vector3i (com.flowpowered.math.vector.Vector3i)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 Set (java.util.Set)6 UUID (java.util.UUID)6 Collectors (java.util.stream.Collectors)6