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