Search in sources :

Example 6 with DataManipulator

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

the class MixinItemStack method toContainer.

@Override
public DataContainer toContainer() {
    final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, getContentVersion()).set(DataQueries.ITEM_TYPE, this.itemstack$getType().getId()).set(DataQueries.ITEM_COUNT, this.itemstack$getQuantity()).set(DataQueries.ITEM_DAMAGE_VALUE, this.getItemDamage());
    if (hasTagCompound()) {
        // no tag? no data, simple as that.
        final NBTTagCompound compound = getTagCompound().copy();
        if (compound.hasKey(NbtDataUtil.SPONGE_DATA)) {
            final NBTTagCompound spongeCompound = compound.getCompoundTag(NbtDataUtil.SPONGE_DATA);
            if (spongeCompound.hasKey(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST)) {
                spongeCompound.removeTag(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST);
            }
        }
        // We must filter the custom data so it isn't stored twice
        NbtDataUtil.filterSpongeCustomData(compound);
        if (!compound.hasNoTags()) {
            final DataContainer unsafeNbt = NbtTranslator.getInstance().translateFrom(compound);
            container.set(DataQueries.UNSAFE_NBT, unsafeNbt);
        }
    }
    // We only need to include the custom data, not vanilla manipulators supported by sponge implementation
    final Collection<DataManipulator<?, ?>> manipulators = 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)

Example 7 with DataManipulator

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

the class ManipulatorTest method testSerialization.

@SuppressWarnings("unchecked")
@Test
public void testSerialization() {
    try {
        final Constructor<?> ctor = this.manipulatorClass.getConstructor();
        final DataManipulator<?, ?> manipulator = (DataManipulator<?, ?>) ctor.newInstance();
        final DataContainer container = manipulator.toContainer();
        if (this.builder != null) {
            final Optional<DataManipulator<?, ?>> optional = (Optional<DataManipulator<?, ?>>) this.builder.build(container);
            if (!optional.isPresent()) {
                throw new IllegalArgumentException("[Serialization]: A builder did not translate the data manipulator: " + this.dataName + "\n[Serialization]: Providing the DataContainer: " + container.toString());
            }
            final DataManipulator<?, ?> deserialized = this.builder.build(container).get();
            assertThat(manipulator.equals(deserialized), is(true));
        }
    } catch (NoSuchMethodException | InstantiationException | InvocationTargetException e) {
        throw new PEBKACException("Exceptions thrown trying to construct: " + this.dataName, e);
    } catch (Exception e) {
        throw new RuntimeException("There was an unknown exception trying to test " + this.dataName + ". Probably because the DataManipulator relies on an implementation class.", e);
    }
}
Also used : Optional(java.util.Optional) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PEBKACException(org.spongepowered.api.util.PEBKACException) DataContainer(org.spongepowered.api.data.DataContainer) PEBKACException(org.spongepowered.api.util.PEBKACException) Test(org.junit.Test)

Example 8 with DataManipulator

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

the class SpongeManipulatorRegistry method bake.

void bake() {
    checkState(this.tempRegistry != null);
    // ValueProcessors
    this.tempRegistry.valueProcessorMap.forEach((key, value) -> {
        ImmutableList.Builder<ValueProcessor<?, ?>> valueListBuilder = ImmutableList.builder();
        value.sort(ComparatorUtil.VALUE_PROCESSOR_COMPARATOR);
        valueListBuilder.addAll(value);
        final ValueProcessorDelegate<?, ?> delegate = new ValueProcessorDelegate(key, valueListBuilder.build());
        this.valueDelegates.put(key, delegate);
    });
    // DataProcessors
    this.tempRegistry.processorMap.forEach((key, value) -> {
        ImmutableList.Builder<DataProcessor<?, ?>> dataListBuilder = ImmutableList.builder();
        value.sort(ComparatorUtil.DATA_PROCESSOR_COMPARATOR);
        dataListBuilder.addAll(value);
        final DataProcessorDelegate<?, ?> delegate = new DataProcessorDelegate(dataListBuilder.build());
        this.dataProcessorDelegates.put(key, delegate);
    });
    SpongeDataManager manager = SpongeDataManager.getInstance();
    // DataManipulatorBuilders part 2 (Have to register them back for serialization stuff
    this.dataProcessorDelegates.forEach((key, value) -> {
        if (!Modifier.isInterface(key.getModifiers()) && !Modifier.isAbstract(key.getModifiers())) {
            DataFunction<DataContainer, DataManipulator, Optional<? extends DataManipulator<?, ?>>> function = ((DataProcessor) value)::fill;
            SpongeDataManipulatorBuilder builder = new SpongeDataManipulatorBuilder(value, key, function);
            manager.builderMap.put(key, checkNotNull(builder));
            manager.registerBuilder(key, builder);
        } else {
            final Class<? extends DataManipulator<?, ?>> clazz = this.interfaceToImplDataManipulatorClasses.get(key);
            DataFunction<DataContainer, DataManipulator, Optional<? extends DataManipulator<?, ?>>> function = ((DataProcessor) value)::fill;
            SpongeDataManipulatorBuilder builder = new SpongeDataManipulatorBuilder(value, clazz, function);
            manager.builderMap.put(key, checkNotNull(builder));
            manager.registerBuilder(key, builder);
        }
    });
    // Immutable DataProcessors
    this.tempRegistry.immutableProcessorMap.forEach((key, value) -> {
        ImmutableList.Builder<DataProcessor<?, ?>> dataListBuilder = ImmutableList.builder();
        value.sort(ComparatorUtil.DATA_PROCESSOR_COMPARATOR);
        dataListBuilder.addAll(value);
        final DataProcessorDelegate<?, ?> delegate = new DataProcessorDelegate(dataListBuilder.build());
        this.immutableDataProcessorDelegates.put(key, delegate);
    });
    // NBT processors
    ImmutableTable.Builder<Class<? extends DataManipulator<?, ?>>, NbtDataType, NbtDataProcessor<?, ?>> builder = ImmutableTable.builder();
    this.tempRegistry.nbtProcessorMap.forEach((key, value) -> {
        final HashMultimap<NbtDataType, NbtDataProcessor<?, ?>> processorMultimap = HashMultimap.create();
        for (NbtDataProcessor<?, ?> nbtDataProcessor : value) {
            processorMultimap.put(nbtDataProcessor.getTargetType(), nbtDataProcessor);
        }
        for (Map.Entry<NbtDataType, Collection<NbtDataProcessor<?, ?>>> nbtDataTypeCollectionEntry : processorMultimap.asMap().entrySet()) {
            ImmutableList.Builder<NbtDataProcessor<?, ?>> processorBuilder = ImmutableList.builder();
            processorBuilder.addAll(nbtDataTypeCollectionEntry.getValue());
            final NbtDataType dataType = nbtDataTypeCollectionEntry.getKey();
            builder.put(key, dataType, new SpongeNbtProcessorDelegate(processorBuilder.build(), dataType));
        }
    });
    this.nbtProcessorTable = builder.build();
    ImmutableSet.Builder<DataRegistration<?, ?>> registrationBuilder = ImmutableSet.builder();
    ImmutableMap.Builder<Class<? extends DataManipulator<?, ?>>, DataRegistration<?, ?>> manipulatorBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Class<? extends ImmutableDataManipulator<?, ?>>, DataRegistration<?, ?>> immutableBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<String, DataRegistration<?, ?>> idBuilder = ImmutableMap.builder();
    ImmutableMultimap.Builder<PluginContainer, DataRegistration<?, ?>> pluginBuilder = ImmutableMultimap.builder();
    this.tempRegistry.registrations.forEach(registration -> {
        registrationBuilder.add(registration);
        manipulatorBuilder.put(registration.getManipulatorClass(), registration);
        if (!registration.getImplementationClass().equals(registration.getManipulatorClass())) {
            manipulatorBuilder.put(registration.getImplementationClass(), registration);
        }
        immutableBuilder.put(registration.getImmutableManipulatorClass(), registration);
        if (!registration.getImmutableImplementationClass().equals(registration.getImmutableManipulatorClass())) {
            immutableBuilder.put(registration.getImmutableImplementationClass(), registration);
        }
        idBuilder.put(registration.getId(), registration);
        pluginBuilder.put(registration.getPluginContainer(), registration);
    });
    this.registrations = registrationBuilder.build();
    this.manipulatorRegistrationMap = manipulatorBuilder.build();
    this.immutableRegistrationMap = immutableBuilder.build();
    this.registrationMap = idBuilder.build();
    this.pluginBasedRegistrations = pluginBuilder.build();
    final SpongeConfig<CustomDataConfig> dataConfig = SpongeImpl.getDataConfig();
    dataConfig.reload();
    dataConfig.save();
    final CustomDataRegistrationCategory config = dataConfig.getConfig().getDataRegistrationConfig();
    config.populateRegistrations(this.registrations);
    // Save the list of registered id's, this way the config can be re-understood.
    dataConfig.save();
    // Finalizes the registration by setting the temporary object to null
    this.tempRegistry = null;
}
Also used : CustomDataRegistrationCategory(org.spongepowered.common.config.category.CustomDataRegistrationCategory) ImmutableList(com.google.common.collect.ImmutableList) NbtValueProcessor(org.spongepowered.common.data.nbt.value.NbtValueProcessor) CustomDataConfig(org.spongepowered.common.config.type.CustomDataConfig) NbtDataType(org.spongepowered.common.data.nbt.NbtDataType) DataProcessorDelegate(org.spongepowered.common.data.util.DataProcessorDelegate) DataContainer(org.spongepowered.api.data.DataContainer) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) SpongeDataManipulatorBuilder(org.spongepowered.common.data.builder.manipulator.SpongeDataManipulatorBuilder) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) ValueProcessorDelegate(org.spongepowered.common.data.util.ValueProcessorDelegate) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Optional(java.util.Optional) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) NbtDataProcessor(org.spongepowered.common.data.nbt.data.NbtDataProcessor) ImmutableTable(com.google.common.collect.ImmutableTable) ImmutableMap(com.google.common.collect.ImmutableMap) DataRegistration(org.spongepowered.api.data.DataRegistration) Collection(java.util.Collection) NbtDataProcessor(org.spongepowered.common.data.nbt.data.NbtDataProcessor) SpongeNbtProcessorDelegate(org.spongepowered.common.data.nbt.SpongeNbtProcessorDelegate) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 9 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator in project LanternServer by LanternPowered.

the class IDataHolder method remove.

// TODO: Support event? Would require special handling to restore the container
@Override
default DataTransactionResult remove(Class<? extends DataManipulator<?, ?>> containerClass) {
    checkNotNull(containerClass, "containerClass");
    // You cannot remove default data manipulators?
    final Optional optRegistration = DataManipulatorRegistry.get().getBy(containerClass);
    if (optRegistration.isPresent()) {
        return DataTransactionResult.failNoData();
    }
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<DataManipulator<?, ?>> containers = ((AdditionalContainerHolder<DataManipulator<?, ?>>) this).getAdditionalContainers();
        final Optional<DataManipulator<?, ?>> old = containers.remove(containerClass);
        if (old.isPresent()) {
            return DataTransactionResult.successRemove(old.get().getValues());
        }
    }
    return DataTransactionResult.failNoData();
}
Also used : Optional(java.util.Optional) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator)

Example 10 with DataManipulator

use of org.spongepowered.api.data.manipulator.DataManipulator in project LanternServer by LanternPowered.

the class IDataHolder method getContainers.

@Override
default Collection<DataManipulator<?, ?>> getContainers() {
    final ImmutableList.Builder<DataManipulator<?, ?>> builder = ImmutableList.builder();
    for (DataManipulatorRegistration registration : DataManipulatorRegistry.get().getAll()) {
        final DataManipulator manipulator = DataHelper.create(this, registration);
        if (manipulator != null) {
            builder.add(manipulator);
        }
    }
    // Try the additional manipulators if they are supported
    if (this instanceof AdditionalContainerHolder) {
        final AdditionalContainerCollection<DataManipulator<?, ?>> containers = ((AdditionalContainerHolder<DataManipulator<?, ?>>) this).getAdditionalContainers();
        containers.getAll().forEach(manipulator -> builder.add(manipulator.copy()));
    }
    return builder.build();
}
Also used : ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) ImmutableList(com.google.common.collect.ImmutableList) DataManipulatorRegistration(org.lanternpowered.server.data.manipulator.DataManipulatorRegistration)

Aggregations

DataManipulator (org.spongepowered.api.data.manipulator.DataManipulator)21 ImmutableDataManipulator (org.spongepowered.api.data.manipulator.ImmutableDataManipulator)13 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)8 DataView (org.spongepowered.api.data.DataView)7 ImmutableList (com.google.common.collect.ImmutableList)6 IMixinCustomDataHolder (org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)6 Optional (java.util.Optional)5 DataContainer (org.spongepowered.api.data.DataContainer)5 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)4 SerializedDataTransaction (org.spongepowered.common.data.persistence.SerializedDataTransaction)4 ArrayList (java.util.ArrayList)3 NBTTagList (net.minecraft.nbt.NBTTagList)3 DataManipulatorRegistration (org.lanternpowered.server.data.manipulator.DataManipulatorRegistration)3 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)3 World (org.spongepowered.api.world.World)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collection (java.util.Collection)2 List (java.util.List)2 IImmutableDataManipulator (org.lanternpowered.server.data.manipulator.immutable.IImmutableDataManipulator)2 DataRegistration (org.spongepowered.api.data.DataRegistration)2