Search in sources :

Example 1 with DataRegistration

use of org.spongepowered.api.data.DataRegistration 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 2 with DataRegistration

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

the class LanternItemStackSnapshot method without.

@SuppressWarnings("unchecked")
@Override
public Optional<ItemStackSnapshot> without(Class<? extends ImmutableDataManipulator<?, ?>> containerClass) {
    final LanternItemStack copy = this.itemStack.copy();
    final DataRegistration registration = Lantern.getGame().getDataManager().get(containerClass).orElseThrow(() -> new IllegalStateException("The container class " + containerClass.getName() + " isn't registered."));
    if (copy.removeFast(registration.getManipulatorClass())) {
        return Optional.of(new LanternItemStackSnapshot(copy));
    }
    return Optional.empty();
}
Also used : DataRegistration(org.spongepowered.api.data.DataRegistration)

Example 3 with DataRegistration

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

the class DataHelper method deserializeRawContainerData.

public static void deserializeRawContainerData(DataView dataView, IValueContainer valueContainer, DataQuery query) throws InvalidDataException {
    final List<DataView> dataViews = dataView.getViewList(query).orElse(null);
    if (dataViews == null) {
        return;
    }
    if (!(valueContainer instanceof AdditionalContainerHolder)) {
        getLogger().warn("{} is not a AdditionalContainerHolder, but data manipulators were found.", valueContainer);
        return;
    }
    final ValueCollection valueCollection = valueContainer.getValueCollection();
    final AdditionalContainerCollection<ValueContainer<?>> containers = ((AdditionalContainerHolder) valueContainer).getAdditionalContainers();
    final List<DataView> failedData = new ArrayList<>();
    final LanternDataManager dataManager = Lantern.getGame().getDataManager();
    for (DataView view : dataViews) {
        Optional<DataRegistration> optRegistration;
        String id;
        if (view.contains(DataQueries.MANIPULATOR_ID)) {
            id = view.getString(DataQueries.MANIPULATOR_ID).get();
            optRegistration = DataManipulatorRegistryModule.get().getById(id);
        } else if (view.contains(DATA_CLASS)) {
            id = view.getString(DATA_CLASS).get();
            optRegistration = dataManager.getLegacyRegistration(id);
        } else {
            getLogger().warn("Manipulator with missing id.");
            continue;
        }
        final Optional<DataView> manipulatorView = view.getView(DataQueries.MANIPULATOR_DATA);
        if (manipulatorView.isPresent()) {
            getLogger().warn("Missing manipulator data for id: {}", id);
        }
        if (optRegistration.isPresent()) {
            try {
                final Optional<DataManipulator> optManipulator = optRegistration.get().getDataManipulatorBuilder().build(manipulatorView.get());
                if (optManipulator.isPresent()) {
                    containers.offer(optManipulator.get());
                }
            } catch (InvalidDataException e) {
                getLogger().error("Could not deserialize " + id + "! Don't worry though, we'll try to deserialize the rest of the data.", e);
            }
        } else {
            getLogger().warn("Missing DataRegistration for ID: " + id + ". Don't worry, the data will be kept safe.");
            failedData.add(view);
        }
    }
    if (!failedData.isEmpty()) {
        // Should be safe to cast, at least if nobody touches this key
        Element<List<DataView>> holder = valueCollection.getElement(LanternKeys.FAILED_DATA_MANIPULATORS).orElse(null);
        if (holder == null) {
            holder = valueCollection.register(LanternKeys.FAILED_DATA_MANIPULATORS, null);
        }
        holder.set(failedData);
    }
}
Also used : IDataManipulator(org.lanternpowered.server.data.manipulator.mutable.IDataManipulator) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) ArrayList(java.util.ArrayList) DataView(org.spongepowered.api.data.DataView) DataRegistration(org.spongepowered.api.data.DataRegistration) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ValueContainer(org.spongepowered.api.data.value.ValueContainer)

Example 4 with DataRegistration

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

the class LanternFluidStackSnapshot method without.

@SuppressWarnings("unchecked")
@Override
public Optional<FluidStackSnapshot> without(Class<? extends ImmutableDataManipulator<?, ?>> containerClass) {
    final LanternFluidStack copy = this.fluidStack.copy();
    final DataRegistration registration = Lantern.getGame().getDataManager().get(containerClass).orElseThrow(() -> new IllegalStateException("The container class " + containerClass.getName() + " isn't registered."));
    if (copy.removeFast(registration.getManipulatorClass())) {
        return Optional.of(new LanternFluidStackSnapshot(copy));
    }
    return Optional.empty();
}
Also used : DataRegistration(org.spongepowered.api.data.DataRegistration)

Aggregations

DataRegistration (org.spongepowered.api.data.DataRegistration)4 ImmutableList (com.google.common.collect.ImmutableList)2 DataManipulator (org.spongepowered.api.data.manipulator.DataManipulator)2 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 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 IDataManipulator (org.lanternpowered.server.data.manipulator.mutable.IDataManipulator)1 DataContainer (org.spongepowered.api.data.DataContainer)1 DataView (org.spongepowered.api.data.DataView)1 ImmutableDataManipulator (org.spongepowered.api.data.manipulator.ImmutableDataManipulator)1 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)1 ValueContainer (org.spongepowered.api.data.value.ValueContainer)1 PluginContainer (org.spongepowered.api.plugin.PluginContainer)1