use of org.spongepowered.api.data.manipulator.ImmutableDataManipulator in project SpongeCommon by SpongePowered.
the class DataUtil method deserializeImmutableManipulatorList.
public static ImmutableList<ImmutableDataManipulator<?, ?>> deserializeImmutableManipulatorList(List<DataView> containers) {
checkNotNull(containers);
final ImmutableList.Builder<ImmutableDataManipulator<?, ?>> builder = ImmutableList.builder();
for (DataView view : containers) {
view = updateDataViewForDataManipulator(view);
final String dataId = view.getString(DataQueries.DATA_ID).orElseThrow(DataUtil.dataNotFound());
final DataView manipulatorView = view.getView(DataQueries.INTERNAL_DATA).orElseThrow(DataUtil.dataNotFound());
try {
deserializeManipulator(dataId, manipulatorView).map(DataManipulator::asImmutable).ifPresent(builder::add);
} catch (Exception e) {
new InvalidDataException("Could not translate " + dataId + "!", e).printStackTrace();
}
}
return builder.build();
}
use of org.spongepowered.api.data.manipulator.ImmutableDataManipulator in project SpongeCommon by SpongePowered.
the class MixinWorld_Data method offer.
@Override
public DataTransactionResult offer(int x, int y, int z, DataManipulator<?, ?> manipulator, MergeFunction function) {
final BlockState blockState = getBlock(x, y, z).withExtendedProperties(new Location<>(this, x, y, z));
final ImmutableDataManipulator<?, ?> immutableDataManipulator = manipulator.asImmutable();
if (blockState.supports((Class) immutableDataManipulator.getClass())) {
final List<ImmutableValue<?>> old = new ArrayList<>(blockState.getValues());
final BlockState newState = blockState.with(immutableDataManipulator).get();
old.removeAll(newState.getValues());
setBlock(x, y, z, newState);
return DataTransactionResult.successReplaceResult(old, manipulator.getValues());
}
return getTileEntity(x, y, z).map(tileEntity -> tileEntity.offer(manipulator, function)).orElseGet(() -> DataTransactionResult.failResult(manipulator.getValues()));
}
use of org.spongepowered.api.data.manipulator.ImmutableDataManipulator 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.ImmutableDataManipulator in project SpongeCommon by SpongePowered.
the class SpongeItemStackSnapshotBuilder method buildContent.
@Override
protected Optional<ItemStackSnapshot> buildContent(DataView container) throws InvalidDataException {
if (container.contains(DataQueries.ITEM_TYPE, DataQueries.ITEM_COUNT)) {
final String itemString = getData(container, DataQueries.ITEM_TYPE, String.class);
final ItemType itemType = SpongeImpl.getRegistry().getType(ItemType.class, itemString).get();
final int count = getData(container, DataQueries.ITEM_COUNT, Integer.class);
final int damage = container.getInt(DataQueries.ITEM_DAMAGE_VALUE).orElse(0);
final ImmutableList<ImmutableDataManipulator<?, ?>> manipulators;
if (container.contains(DataQueries.DATA_MANIPULATORS)) {
manipulators = DataUtil.deserializeImmutableManipulatorList(container.getViewList(DataQueries.DATA_MANIPULATORS).get());
} else {
manipulators = ImmutableList.of();
}
@Nullable final NBTTagCompound compound;
if (container.contains(DataQueries.UNSAFE_NBT)) {
compound = NbtTranslator.getInstance().translateData(container.getView(DataQueries.UNSAFE_NBT).get());
} else {
compound = null;
}
return Optional.of(new SpongeItemStackSnapshot(itemType, count, damage, manipulators, compound));
}
return Optional.empty();
}
use of org.spongepowered.api.data.manipulator.ImmutableDataManipulator in project LanternServer by LanternPowered.
the class IImmutableDataHolderBase method getContainers.
@Override
default List<ImmutableDataManipulator<?, ?>> getContainers() {
final ImmutableList.Builder<ImmutableDataManipulator<?, ?>> builder = ImmutableList.builder();
for (DataManipulatorRegistration registration : DataManipulatorRegistry.get().getAll()) {
final ImmutableDataManipulator manipulator = (ImmutableDataManipulator) ImmutableContainerCache.get(this, registration, registration.getImmutableManipulatorClass()).orElse(null);
if (manipulator != null) {
builder.add(manipulator);
}
}
if (this instanceof AdditionalContainerHolder) {
final AdditionalContainerCollection<ImmutableDataManipulator<?, ?>> manipulators = ((AdditionalContainerHolder<ImmutableDataManipulator<?, ?>>) this).getAdditionalContainers();
manipulators.getAll().forEach(builder::add);
}
return builder.build();
}
Aggregations