use of org.spongepowered.common.interfaces.data.IMixinCustomDataHolder in project SpongeCommon by SpongePowered.
the class MixinDataHolder method offer.
@Override
public <E> DataTransactionResult offer(Key<? extends BaseValue<E>> key, E value) {
TimingsManager.DATA_GROUP_HANDLER.startTimingIfSync();
SpongeTimings.dataOfferKey.startTimingIfSync();
final Optional<ValueProcessor<E, ? extends BaseValue<E>>> optional = DataUtil.getBaseValueProcessor(key);
if (optional.isPresent()) {
final DataTransactionResult result = optional.get().offerToStore(this, value);
SpongeTimings.dataOfferKey.stopTimingIfSync();
TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
return result;
} else if (this instanceof IMixinCustomDataHolder) {
final DataTransactionResult result = ((IMixinCustomDataHolder) this).offerCustom(key, value);
SpongeTimings.dataOfferKey.stopTimingIfSync();
TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
return result;
}
SpongeTimings.dataOfferKey.stopTimingIfSync();
TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
return DataTransactionResult.failNoData();
}
use of org.spongepowered.common.interfaces.data.IMixinCustomDataHolder 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.common.interfaces.data.IMixinCustomDataHolder in project SpongeCommon by SpongePowered.
the class SpongeItemStackSnapshot method merge.
@Override
public ItemStackSnapshot merge(ItemStackSnapshot that, MergeFunction function) {
final ItemStack thisCopy = this.privateStack.copy();
final ItemStack thatCopy = that.createStack();
for (DataManipulator<?, ?> manipulator : ((IMixinCustomDataHolder) thatCopy).getCustomManipulators()) {
thisCopy.offer(manipulator, function);
}
return thisCopy.createSnapshot();
}
use of org.spongepowered.common.interfaces.data.IMixinCustomDataHolder in project SpongeCommon by SpongePowered.
the class SpongeBlockSnapshotBuilder method from.
@Override
public SpongeBlockSnapshotBuilder from(Location<World> location) {
this.blockState = location.getBlock();
this.worldUuid = location.getExtent().getUniqueId();
this.coords = location.getBlockPosition();
if (this.blockState.getType() instanceof ITileEntityProvider) {
if (location.hasTileEntity()) {
this.compound = new NBTTagCompound();
org.spongepowered.api.block.tileentity.TileEntity te = location.getTileEntity().get();
((TileEntity) te).writeToNBT(this.compound);
this.manipulators = ((IMixinCustomDataHolder) te).getCustomManipulators().stream().map(DataManipulator::asImmutable).collect(Collectors.toList());
}
}
return this;
}
use of org.spongepowered.common.interfaces.data.IMixinCustomDataHolder in project SpongeCommon by SpongePowered.
the class CustomDataNbtUtil method readCustomData.
@SuppressWarnings("unchecked")
public static void readCustomData(NBTTagCompound compound, DataHolder dataHolder) {
if (dataHolder instanceof IMixinCustomDataHolder) {
if (compound.hasKey(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_LIST)) {
final NBTTagList list = compound.getTagList(NbtDataUtil.CUSTOM_MANIPULATOR_TAG_LIST, NbtDataUtil.TAG_COMPOUND);
final ImmutableList.Builder<DataView> builder = ImmutableList.builder();
if (list != null && list.tagCount() != 0) {
for (int i = 0; i < list.tagCount(); i++) {
final NBTTagCompound internal = list.getCompoundTagAt(i);
builder.add(NbtTranslator.getInstance().translateFrom(internal));
}
}
try {
final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(builder.build());
final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
for (DataManipulator<?, ?> manipulator : manipulators) {
dataHolder.offer(manipulator);
}
if (!transaction.failedData.isEmpty()) {
((IMixinCustomDataHolder) dataHolder).addFailedData(transaction.failedData);
}
} catch (InvalidDataException e) {
SpongeImpl.getLogger().error("Could not translate custom plugin data! ", e);
}
}
if (compound.hasKey(NbtDataUtil.FAILED_CUSTOM_DATA, NbtDataUtil.TAG_LIST)) {
final NBTTagList list = compound.getTagList(NbtDataUtil.FAILED_CUSTOM_DATA, NbtDataUtil.TAG_COMPOUND);
final ImmutableList.Builder<DataView> builder = ImmutableList.builder();
if (list != null && list.tagCount() != 0) {
for (int i = 0; i < list.tagCount(); i++) {
final NBTTagCompound internal = list.getCompoundTagAt(i);
builder.add(NbtTranslator.getInstance().translateFrom(internal));
}
}
// We want to attempt to refresh the failed data if it does succeed in getting read.
compound.removeTag(NbtDataUtil.FAILED_CUSTOM_DATA);
// Re-attempt to deserialize custom data
final SerializedDataTransaction transaction = DataUtil.deserializeManipulatorList(builder.build());
final List<DataManipulator<?, ?>> manipulators = transaction.deserializedManipulators;
List<Class<? extends DataManipulator<?, ?>>> classesLoaded = new ArrayList<>();
for (DataManipulator<?, ?> manipulator : manipulators) {
if (!classesLoaded.contains(manipulator.getClass())) {
classesLoaded.add((Class<? extends DataManipulator<?, ?>>) manipulator.getClass());
// ignore the failed data for removal.
if (!((IMixinCustomDataHolder) dataHolder).getCustom(manipulator.getClass()).isPresent()) {
dataHolder.offer(manipulator);
}
}
}
if (!transaction.failedData.isEmpty()) {
((IMixinCustomDataHolder) dataHolder).addFailedData(transaction.failedData);
}
}
}
}
Aggregations