Search in sources :

Example 6 with DataTransactionResult

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

the class MixinDataHolder method offer.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public DataTransactionResult offer(DataManipulator<?, ?> valueContainer, MergeFunction function) {
    TimingsManager.DATA_GROUP_HANDLER.startTimingIfSync();
    SpongeTimings.dataOfferManipulator.startTimingIfSync();
    final Optional<DataProcessor> optional = DataUtil.getWildDataProcessor(valueContainer.getClass());
    if (optional.isPresent()) {
        final DataTransactionResult result = optional.get().set(this, valueContainer, checkNotNull(function));
        SpongeTimings.dataOfferManipulator.stopTimingIfSync();
        TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
        return result;
    } else if (this instanceof IMixinCustomDataHolder) {
        final DataTransactionResult result = ((IMixinCustomDataHolder) this).offerCustom(valueContainer, function);
        SpongeTimings.dataOfferManipulator.stopTimingIfSync();
        TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
        return result;
    }
    SpongeTimings.dataOfferManipulator.stopTimingIfSync();
    TimingsManager.DATA_GROUP_HANDLER.stopTimingIfSync();
    return DataTransactionResult.failResult(valueContainer.getValues());
}
Also used : DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) DataProcessor(org.spongepowered.common.data.DataProcessor) IMixinCustomDataHolder(org.spongepowered.common.interfaces.data.IMixinCustomDataHolder)

Example 7 with DataTransactionResult

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

the class SpongeItemStackBuilder method itemData.

@Override
public ItemStack.Builder itemData(final DataManipulator<?, ?> itemData) throws IllegalArgumentException {
    checkNotNull(itemData, "Must have a non-null item data!");
    checkNotNull(this.type, "Cannot set item data without having set a type first!");
    // Validation is required, we can't let devs set block data on a non-block item!
    DataTransactionResult result = validateData(this.type, itemData);
    if (result.getType() != DataTransactionResult.Type.SUCCESS) {
        throw new IllegalArgumentException("The item data is not compatible with the current item type!");
    }
    if (this.itemDataSet == null) {
        this.itemDataSet = new HashSet<>();
    }
    this.itemDataSet.add(itemData);
    return this;
}
Also used : DataTransactionResult(org.spongepowered.api.data.DataTransactionResult)

Example 8 with DataTransactionResult

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

the class SpongeItemStackSnapshot method with.

@Override
public <E> Optional<ItemStackSnapshot> with(Key<? extends BaseValue<E>> key, E value) {
    final ItemStack copy = this.privateStack.copy();
    final DataTransactionResult result = copy.offer(key, value);
    if (result.getType() != DataTransactionResult.Type.SUCCESS) {
        return Optional.empty();
    }
    return Optional.of(copy.createSnapshot());
}
Also used : DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 9 with DataTransactionResult

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

the class SpongeItemStackSnapshot method with.

@Override
public Optional<ItemStackSnapshot> with(ImmutableDataManipulator<?, ?> valueContainer) {
    final DataManipulator<?, ?> manipulator = valueContainer.asMutable();
    final ItemStack copyStack = this.privateStack.copy();
    final DataTransactionResult result = copyStack.offer(manipulator);
    if (result.getType() != DataTransactionResult.Type.FAILURE) {
        return Optional.of(copyStack.createSnapshot());
    }
    return Optional.empty();
}
Also used : DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 10 with DataTransactionResult

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

the class RecipeTest method onInit.

@Listener
public void onInit(GamePreInitializationEvent event) {
    final Ingredient s = Ingredient.of(STONE);
    final Ingredient b = Ingredient.of(BED, WOOL);
    final ItemStack item = ItemStack.of(BEDROCK, 1);
    final DataTransactionResult trans = item.offer(Keys.ITEM_ENCHANTMENTS, Collections.singletonList(Enchantment.of(EnchantmentTypes.UNBREAKING, 1)));
    if (trans.getType() != DataTransactionResult.Type.SUCCESS) {
        this.plugin.getLogger().error("Could not build recipe output!");
    }
    final ShapedCraftingRecipe recipe = CraftingRecipe.shapedBuilder().rows().row(s, s, s).row(s, b, s).row(s, s, s).result(item).build("bedrock", this.plugin);
    Sponge.getRegistry().getCraftingRecipeRegistry().register(recipe);
}
Also used : Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient) ShapedCraftingRecipe(org.spongepowered.api.item.recipe.crafting.ShapedCraftingRecipe) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Listener(org.spongepowered.api.event.Listener)

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