use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class SpongeItemStackBuilder method buildContent.
@Override
protected Optional<ItemStack> buildContent(DataView container) throws InvalidDataException {
checkNotNull(container);
if (!container.contains(DataQueries.ITEM_TYPE) || !container.contains(DataQueries.ITEM_COUNT) || !container.contains(DataQueries.ITEM_DAMAGE_VALUE)) {
return Optional.empty();
}
final String itemTypeId = getData(container, DataQueries.ITEM_TYPE, String.class);
final int count = getData(container, DataQueries.ITEM_COUNT, Integer.class);
final ItemType itemType = SpongeImpl.getRegistry().getType(ItemType.class, itemTypeId).get();
final int damage = getData(container, DataQueries.ITEM_DAMAGE_VALUE, Integer.class);
final net.minecraft.item.ItemStack itemStack = new net.minecraft.item.ItemStack((Item) itemType, count, damage);
if (container.contains(DataQueries.UNSAFE_NBT)) {
final NBTTagCompound compound = NbtTranslator.getInstance().translateData(container.getView(DataQueries.UNSAFE_NBT).get());
itemStack.setTagCompound(compound);
}
if (container.contains(DataQueries.DATA_MANIPULATORS)) {
final List<DataView> views = container.getViewList(DataQueries.DATA_MANIPULATORS).get();
final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(views);
final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
for (DataManipulator<?, ?> manipulator : manipulators) {
((IMixinCustomDataHolder) itemStack).offerCustom(manipulator, MergeFunction.IGNORE_ALL);
}
if (!transaction.failedData.isEmpty()) {
((IMixinCustomDataHolder) itemStack).addFailedData(transaction.failedData);
}
}
return Optional.of((ItemStack) itemStack);
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class DataUtil method getSerializedManipulatorList.
private static <T extends DataSerializable> List<DataView> getSerializedManipulatorList(Iterable<T> manipulators, Function<T, DataRegistration> func) {
checkNotNull(manipulators);
final ImmutableList.Builder<DataView> builder = ImmutableList.builder();
for (T manipulator : manipulators) {
final DataContainer container = DataContainer.createNew();
container.set(Queries.CONTENT_VERSION, DataVersions.Data.CURRENT_CUSTOM_DATA);
container.set(DataQueries.DATA_ID, func.apply(manipulator).getId()).set(DataQueries.INTERNAL_DATA, manipulator.toContainer());
builder.add(container);
}
return builder.build();
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class DataUtil method deserializeManipulatorList.
public static SerializedDataTransaction deserializeManipulatorList(List<DataView> containers) {
checkNotNull(containers);
final SerializedDataTransaction.Builder builder = SerializedDataTransaction.builder();
for (DataView view : containers) {
final DataView updated = updateDataViewForDataManipulator(view);
findDataId(builder, updated).ifPresent(dataId -> tryDeserializeManipulator(builder, updated, dataId));
}
return builder.build();
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class DataUtil method getPosition3d.
public static Vector3d getPosition3d(DataView view, DataQuery query) {
checkDataExists(view, query);
final DataView internal = view.getView(query).get();
final double x = internal.getDouble(Queries.POSITION_X).get();
final double y = internal.getDouble(Queries.POSITION_Y).get();
final double z = internal.getDouble(Queries.POSITION_Z).get();
return new Vector3d(x, y, z);
}
use of org.spongepowered.api.data.DataView in project SpongeCommon by SpongePowered.
the class MixinTileEntityLockable method toContainer.
@Override
public DataContainer toContainer() {
DataContainer container = super.toContainer();
if (this.code != null) {
container.set(DataQueries.BLOCK_ENTITY_LOCK_CODE, this.code.getLock());
}
List<DataView> items = Lists.newArrayList();
for (int i = 0; i < getSizeInventory(); i++) {
ItemStack stack = getStackInSlot(i);
if (!stack.isEmpty()) {
// todo make a helper object for this
DataContainer stackView = DataContainer.createNew().set(Queries.CONTENT_VERSION, 1).set(DataQueries.BLOCK_ENTITY_SLOT, i).set(DataQueries.BLOCK_ENTITY_SLOT_ITEM, ((org.spongepowered.api.item.inventory.ItemStack) stack).toContainer());
items.add(stackView);
}
}
container.set(DataQueries.BLOCK_ENTITY_ITEM_CONTENTS, items);
return container;
}
Aggregations