Search in sources :

Example 1 with DataTransactionResult

use of org.spongepowered.api.data.DataTransactionResult in project SpongeCommon by SpongePowered.

the class MinecartBlockDataProcessor method remove.

@Override
public DataTransactionResult remove(DataHolder dataHolder) {
    if (dataHolder instanceof EntityMinecart) {
        EntityMinecart cart = (EntityMinecart) dataHolder;
        DataTransactionResult.Builder builder = DataTransactionResult.builder().result(DataTransactionResult.Type.SUCCESS);
        if (cart.hasDisplayTile()) {
            ImmutableValue<BlockState> block = new ImmutableSpongeValue<>(Keys.REPRESENTED_BLOCK, (BlockState) cart.getDisplayTile());
            ImmutableValue<Integer> offset = new ImmutableSpongeValue<>(Keys.OFFSET, cart.getDisplayTileOffset());
            cart.setHasDisplayTile(false);
            builder.replace(block).replace(offset);
        }
        return builder.build();
    }
    return DataTransactionResult.failNoData();
}
Also used : BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) EntityMinecart(net.minecraft.entity.item.EntityMinecart) ImmutableSpongeValue(org.spongepowered.common.data.value.immutable.ImmutableSpongeValue)

Example 2 with DataTransactionResult

use of org.spongepowered.api.data.DataTransactionResult 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()));
}
Also used : SpongeImpl(org.spongepowered.common.SpongeImpl) Value(org.spongepowered.api.data.value.mutable.Value) ImmutableDataManipulator(org.spongepowered.api.data.manipulator.ImmutableDataManipulator) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) PropertyStore(org.spongepowered.api.data.property.PropertyStore) Key(org.spongepowered.api.data.key.Key) ArrayList(java.util.ArrayList) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) ImmutableList(com.google.common.collect.ImmutableList) Mixin(org.spongepowered.asm.mixin.Mixin) MergeFunction(org.spongepowered.api.data.merge.MergeFunction) Location(org.spongepowered.api.world.Location) ImmutableSet(com.google.common.collect.ImmutableSet) DirectionFacingProvider(org.spongepowered.common.registry.provider.DirectionFacingProvider) Collection(java.util.Collection) BaseValue(org.spongepowered.api.data.value.BaseValue) EnumFacing(net.minecraft.util.EnumFacing) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) DataHolder(org.spongepowered.api.data.DataHolder) BlockState(org.spongepowered.api.block.BlockState) Direction(org.spongepowered.api.util.Direction) List(java.util.List) DataView(org.spongepowered.api.data.DataView) World(org.spongepowered.api.world.World) Property(org.spongepowered.api.data.Property) DataManipulator(org.spongepowered.api.data.manipulator.DataManipulator) Optional(java.util.Optional) Collections(java.util.Collections) ImmutableValue(org.spongepowered.api.data.value.immutable.ImmutableValue) BlockState(org.spongepowered.api.block.BlockState) ArrayList(java.util.ArrayList)

Example 3 with DataTransactionResult

use of org.spongepowered.api.data.DataTransactionResult in project SpongeCommon by SpongePowered.

the class MixinDataHolder method remove.

@Override
public DataTransactionResult remove(Key<?> key) {
    TimingsManager.DATA_GROUP_HANDLER.startTimingIfSync();
    SpongeTimings.dataRemoveKey.startTimingIfSync();
    final Optional<ValueProcessor<?, ?>> optional = DataUtil.getWildValueProcessor(checkNotNull(key));
    if (optional.isPresent()) {
        final DataTransactionResult result = optional.get().removeFrom(this);
        SpongeTimings.dataRemoveKey.stopTimingIfSync();
        TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
        return result;
    } else if (this instanceof IMixinCustomDataHolder) {
        final DataTransactionResult result = ((IMixinCustomDataHolder) this).removeCustom(key);
        SpongeTimings.dataRemoveKey.stopTimingIfSync();
        TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
        return result;
    }
    SpongeTimings.dataRemoveKey.stopTimingIfSync();
    TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
    return DataTransactionResult.failNoData();
}
Also used : ValueProcessor(org.spongepowered.common.data.ValueProcessor) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

Example 4 with DataTransactionResult

use of org.spongepowered.api.data.DataTransactionResult in project SpongeCommon by SpongePowered.

the class MixinDataHolder method remove.

@Override
public DataTransactionResult remove(Class<? extends DataManipulator<?, ?>> containerClass) {
    TimingsManager.DATA_GROUP_HANDLER.startTimingIfSync();
    SpongeTimings.dataRemoveManipulator.startTimingIfSync();
    final Optional<DataProcessor<?, ?>> optional = DataUtil.getWildProcessor(containerClass);
    if (optional.isPresent()) {
        final DataTransactionResult result = optional.get().remove(this);
        SpongeTimings.dataRemoveManipulator.stopTimingIfSync();
        TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
        return result;
    } else if (this instanceof IMixinCustomDataHolder) {
        final DataTransactionResult result = ((IMixinCustomDataHolder) this).removeCustom(containerClass);
        SpongeTimings.dataRemoveManipulator.stopTimingIfSync();
        TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
        return result;
    }
    SpongeTimings.dataOfferMultiManipulators.stopTimingIfSync();
    TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
    return DataTransactionResult.failNoData();
}
Also used : DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) DataProcessor(org.spongepowered.common.data.DataProcessor) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

Example 5 with DataTransactionResult

use of org.spongepowered.api.data.DataTransactionResult in project SpongeCommon by SpongePowered.

the class MixinDataHolder method offer.

@Override
public DataTransactionResult offer(Iterable<DataManipulator<?, ?>> valueContainers) {
    TimingsManager.DATA_GROUP_HANDLER.startTimingIfSync();
    SpongeTimings.dataOfferMultiManipulators.startTimingIfSync();
    DataTransactionResult.Builder builder = DataTransactionResult.builder();
    for (DataManipulator<?, ?> manipulator : valueContainers) {
        final DataTransactionResult result = offer(manipulator);
        if (!result.getRejectedData().isEmpty()) {
            builder.reject(result.getRejectedData());
        }
        if (!result.getReplacedData().isEmpty()) {
            builder.replace(result.getReplacedData());
        }
        if (!result.getSuccessfulData().isEmpty()) {
            builder.success(result.getSuccessfulData());
        }
        final DataTransactionResult.Type type = result.getType();
        builder.result(type);
        switch(type) {
            case UNDEFINED:
            case ERROR:
            case CANCELLED:
                SpongeTimings.dataOfferMultiManipulators.stopTimingIfSync();
                TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
                return builder.build();
            default:
                break;
        }
    }
    SpongeTimings.dataOfferMultiManipulators.stopTimingIfSync();
    TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
    return builder.build();
}
Also used : DataTransactionResult(org.spongepowered.api.data.DataTransactionResult)

Aggregations

DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)29 ItemStack (org.spongepowered.api.item.inventory.ItemStack)6 DataHolder (org.spongepowered.api.data.DataHolder)4 ImmutableValue (org.spongepowered.api.data.value.immutable.ImmutableValue)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BlockState (org.spongepowered.api.block.BlockState)3 BaseValue (org.spongepowered.api.data.value.BaseValue)3 Player (org.spongepowered.api.entity.living.player.Player)3 IMixinCustomDataHolder (org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 Set (java.util.Set)2 EnumFacing (net.minecraft.util.EnumFacing)2 Sponge (org.spongepowered.api.Sponge)2 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)2 DataView (org.spongepowered.api.data.DataView)2