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;
}
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();
}
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);
}
}
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();
}
Aggregations