Search in sources :

Example 1 with NbtDataProcessor

use of org.spongepowered.common.data.nbt.data.NbtDataProcessor 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)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableTable (com.google.common.collect.ImmutableTable)1 Collection (java.util.Collection)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 DataContainer (org.spongepowered.api.data.DataContainer)1 DataRegistration (org.spongepowered.api.data.DataRegistration)1 DataManipulator (org.spongepowered.api.data.manipulator.DataManipulator)1 ImmutableDataManipulator (org.spongepowered.api.data.manipulator.ImmutableDataManipulator)1 PluginContainer (org.spongepowered.api.plugin.PluginContainer)1 CustomDataRegistrationCategory (org.spongepowered.common.config.category.CustomDataRegistrationCategory)1 CustomDataConfig (org.spongepowered.common.config.type.CustomDataConfig)1 SpongeDataManipulatorBuilder (org.spongepowered.common.data.builder.manipulator.SpongeDataManipulatorBuilder)1 NbtDataType (org.spongepowered.common.data.nbt.NbtDataType)1 SpongeNbtProcessorDelegate (org.spongepowered.common.data.nbt.SpongeNbtProcessorDelegate)1 NbtDataProcessor (org.spongepowered.common.data.nbt.data.NbtDataProcessor)1