/** * If this result of {@link #isSuccessful()} returns {@code true}, * the provided {@link Consumer} is called provided a list of all * "successful" data as retrieved from {@link #getSuccessfulData()}. * * @param consumer The consumer to call */ public void ifSuccessful(Consumer<List<ImmutableValue<?>>> consumer) { if (isSuccessful()) { consumer.accept(this.success); } }
/** * Used to call a {@link Supplier} for an {@link Exception} of type * {@code E} such that if this transaction's {@link #isSuccessful()} * returns {@code false}, the supplier's exception is thrown. * * @param supplier The supplier of the exception to throw * @param <E> The type of exception * @throws E The exception to throw if this transaction is not successful */ public <E extends Exception> void ifNotSuccessful(Supplier<E> supplier) throws E { if (!isSuccessful()) { throw supplier.get(); } }
/** * Offers the given {@link ValueContainer} such that all of the available * {@link BaseValue}s from the given {@link ValueContainer} are offered * to this {@link CompositeValueStore}. Intentionally, however, this differs * from {@link #offer(ValueContainer)} as it will intentionally throw an * exception if the result was a failure. * * @param valueContainer The value to set * @return The transaction result * @throws IllegalArgumentException If the result is a failure likely due to * incompatibility */ default DataTransactionResult tryOffer(H valueContainer) { final DataTransactionResult result = offer(valueContainer, MergeFunction.IGNORE_ALL); if (!result.isSuccessful()) { throw new IllegalArgumentException("Failed offer transaction!"); } return result; }
/** * Offers the given {@link ValueContainer} such that all of the available * {@link BaseValue}s from the given {@link ValueContainer} are offered * to this {@link CompositeValueStore}. Any overlaps of data are merged via * the {@link MergeFunction}. Intentionally, however, this differs * from {@link #offer(ValueContainer)} as it will intentionally throw an * exception if the result was a failure. * * @param valueContainer The value to set * @param function The merge function * @return The transaction result * @throws IllegalArgumentException If the result is a failure likely due to * incompatibility */ default DataTransactionResult tryOffer(H valueContainer, MergeFunction function) throws IllegalArgumentException { final DataTransactionResult result = offer(valueContainer, function); if (!result.isSuccessful()) { throw new IllegalArgumentException("Failed offer transaction!"); } return result; }
/** * Offers the given {@code value} as defined by the provided {@link Key} * such that a {@link DataTransactionResult} is returned for any * successful {@link BaseValue}s from this {@link CompositeValueStore}. * Intentionally, however, this differs from {@link #offer(Key, Object)} * as it will intentionally throw an exception if the result was a failure. * * @param key The key to the value to set * @param value The value to set * @param <E> The type of value * @return The transaction result * @throws IllegalArgumentException If the result is a failure likely due to * incompatibility */ default <E> DataTransactionResult tryOffer(Key<? extends BaseValue<E>> key, E value) throws IllegalArgumentException { final DataTransactionResult result = offer(key, value); if (!result.isSuccessful()) { throw new IllegalArgumentException("Failed offer transaction!"); } return result; }
private static <E> BiConsumer<ItemStack.Builder, Random> setValue(Key<? extends BaseValue<E>> key, Function<Random, E> element) { return (builder, random) -> { final ItemStack itemStack = builder.build(); final DataTransactionResult result = itemStack.offer(key, element.apply(random)); if (result.isSuccessful()) { builder.from(itemStack); } }; }
.collect(Collectors.toList()); final DataTransactionResult result = itemStack.offer(key, suppliedElements); if (result.isSuccessful()) { builder.from(itemStack);
/** * Offers the given {@code value} as defined by the provided {@link Key} * such that a {@link DataTransactionResult} is returned for any * successful {@link BaseValue}s from this {@link CompositeValueStore}. * Intentionally, however, this differs from {@link #offer(Key, Object)} * as it will intentionally throw an exception if the result was a failure. * * @param value The value to set * @param <E> The type of value * @return The transaction result * @throws IllegalArgumentException If the result is a failure likely due to * incompatibility */ default <E> DataTransactionResult tryOffer(BaseValue<E> value) throws IllegalArgumentException { final DataTransactionResult result = offer(value.getKey(), value.get()); if (!result.isSuccessful()) { throw new IllegalArgumentException("Failed offer transaction!"); } return result; }
/** * Creates a new {@link BiConsumer} that sets a single provided * value with the provided {@link Key}. Only a single value is chosen * to provide to the itemstack builder. * * <p>Note that custom data is not supported through this method, use * {@link #data(Collection)} or any variant thereof for applying custom data.</p> * * @param key The key to use * @param values The pool of possible values * @param <E> The type of value * @return The new biconsumer to apply to an itemstack builder */ public static <E> BiConsumer<ItemStack.Builder, Random> keyValues(Key<? extends BaseValue<E>> key, Iterable<E> values) { checkNotNull(values, "Iterable cannot be null!"); checkNotNull(key, "Key cannot be null!"); WeightedTable<E> tableEntries = new WeightedTable<>(1); for (E e : values) { tableEntries.add(checkNotNull(e, "Value cannot be null!"), 1); } return (builder, random) -> { final ItemStack itemStack = builder.build(); final DataTransactionResult dataTransactionResult = itemStack.offer(key, tableEntries.get(random).get(0)); if (dataTransactionResult.isSuccessful()) { builder.from(itemStack); } }; }
/** * Creates a new {@link BiConsumer} that applies the provided {@link Value} * to the generated {@link ItemStack}. * * <p>Note that custom data is not supported through this method, use * {@link #data(Collection)} or any variant thereof for applying custom data.</p> * * @param value The value to use * @param <E> The type of element * @param <V> The type of value * @return The new biconsumer to apply to an itemstack builder */ public static <E, V extends BaseValue<E>> BiConsumer<ItemStack.Builder, Random> value(V value) { return (builder, random) -> { final ItemStack itemStack = builder.build(); final DataTransactionResult dataTransactionResult = itemStack.offer(value); if (dataTransactionResult.isSuccessful()) { builder.from(itemStack); } }; }
/** * Creates a new {@link BiConsumer} that sets the provided {@link Key}'ed * object where the value is possibly ignored or not supported. No checks * on whether the key or object is supported until called upon. * * <p>Note that custom data is not supported through this method, use * {@link #data(Collection)} or any variant thereof for applying custom data.</p> * * @param key The key to use * @param value The value to use * @param <E> The type of value * @return The new biconsumer to apply to an itemstack builder */ public static <E> BiConsumer<ItemStack.Builder, Random> keyValue(Key<? extends BaseValue<E>> key, E value) { return (builder, random) -> { final ItemStack itemStack = builder.build(); final DataTransactionResult dataTransactionResult = itemStack.offer(key, value); if (dataTransactionResult.isSuccessful()) { builder.from(itemStack); } }; }
/** * Creates a new {@link BiConsumer} that applies a random selection of the * provided {@link BaseValue}s. * * <p>Note that custom data is not supported through this method, use * {@link #data(Collection)} or any variant thereof for applying custom data.</p> * * @param values The iterable collection of values to choose from * @param <E> The type of element * @param <V> The type of value * @return The new biconsumer to apply to an itemstack builder */ public static <E, V extends BaseValue<E>> BiConsumer<ItemStack.Builder, Random> values(Iterable<V> values) { WeightedTable<V> tableEntries = new WeightedTable<>(1); for (V value : values) { tableEntries.add(checkNotNull(value, "Value cannot be null!"), 1); } return ((builder, random) -> { final V value = tableEntries.get(random).get(0); final ItemStack itemStack = builder.build(); final DataTransactionResult result = itemStack.offer(value); if (result.isSuccessful()) { builder.from(itemStack); } }); }
/** * Offers the given {@link ValueContainer} such that all of the available * {@link BaseValue}s from the given {@link ValueContainer} are offered * to this {@link CompositeValueStore}. Any overlaps of data are merged via * the {@link MergeFunction}. Intentionally, however, this differs * from {@link #offer(ValueContainer)} as it will intentionally throw an * exception if the result was a failure. * * @param valueContainer The value to set * @param function The merge function * @return The transaction result * @throws IllegalArgumentException If the result is a failure likely due to * incompatibility */ default DataTransactionResult tryOffer(H valueContainer, MergeFunction function) throws IllegalArgumentException { final DataTransactionResult result = offer(valueContainer, function); if (!result.isSuccessful()) { throw new IllegalArgumentException("Failed offer transaction!"); } return result; }
/** * Offers the given {@link ValueContainer} such that all of the available * {@link BaseValue}s from the given {@link ValueContainer} are offered * to this {@link CompositeValueStore}. Intentionally, however, this differs * from {@link #offer(ValueContainer)} as it will intentionally throw an * exception if the result was a failure. * * @param valueContainer The value to set * @return The transaction result * @throws IllegalArgumentException If the result is a failure likely due to * incompatibility */ default DataTransactionResult tryOffer(H valueContainer) { final DataTransactionResult result = offer(valueContainer, MergeFunction.IGNORE_ALL); if (!result.isSuccessful()) { throw new IllegalArgumentException("Failed offer transaction!"); } return result; }
/** * Offers the given {@code value} as defined by the provided {@link Key} * such that a {@link DataTransactionResult} is returned for any * successful {@link BaseValue}s from this {@link CompositeValueStore}. * Intentionally, however, this differs from {@link #offer(Key, Object)} * as it will intentionally throw an exception if the result was a failure. * * @param key The key to the value to set * @param value The value to set * @param <E> The type of value * @return The transaction result * @throws IllegalArgumentException If the result is a failure likely due to * incompatibility */ default <E> DataTransactionResult tryOffer(Key<? extends BaseValue<E>> key, E value) throws IllegalArgumentException { final DataTransactionResult result = offer(key, value); if (!result.isSuccessful()) { throw new IllegalArgumentException("Failed offer transaction!"); } return result; }
private static <E> BiConsumer<ItemStack.Builder, Random> setValue(Key<? extends BaseValue<E>> key, Function<Random, E> element) { return (builder, random) -> { final ItemStack itemStack = builder.build(); final DataTransactionResult result = itemStack.offer(key, element.apply(random)); if (result.isSuccessful()) { builder.from(itemStack); } }; }
/** * Offers the given {@code value} as defined by the provided {@link Key} * such that a {@link DataTransactionResult} is returned for any * successful {@link BaseValue}s from this {@link CompositeValueStore}. * Intentionally, however, this differs from {@link #offer(Key, Object)} * as it will intentionally throw an exception if the result was a failure. * * @param value The value to set * @param <E> The type of value * @return The transaction result * @throws IllegalArgumentException If the result is a failure likely due to * incompatibility */ default <E> DataTransactionResult tryOffer(BaseValue<E> value) throws IllegalArgumentException { final DataTransactionResult result = offer(value.getKey(), value.get()); if (!result.isSuccessful()) { throw new IllegalArgumentException("Failed offer transaction!"); } return result; }
/** * Creates a new {@link BiConsumer} that sets the provided {@link Key}'ed * object where the value is possibly ignored or not supported. No checks * on whether the key or object is supported until called upon. * * <p>Note that custom data is not supported through this method, use * {@link #data(Collection)} or any variant thereof for applying custom data.</p> * * @param key The key to use * @param value The value to use * @param <E> The type of value * @return The new biconsumer to apply to an itemstack builder */ public static <E> BiConsumer<ItemStack.Builder, Random> keyValue(Key<? extends BaseValue<E>> key, E value) { return (builder, random) -> { final ItemStack itemStack = builder.build(); final DataTransactionResult dataTransactionResult = itemStack.offer(key, value); if (dataTransactionResult.isSuccessful()) { builder.from(itemStack); } }; }
/** * Creates a new {@link BiConsumer} that applies the provided {@link Value} * to the generated {@link ItemStack}. * * <p>Note that custom data is not supported through this method, use * {@link #data(Collection)} or any variant thereof for applying custom data.</p> * * @param value The value to use * @param <E> The type of element * @param <V> The type of value * @return The new biconsumer to apply to an itemstack builder */ public static <E, V extends BaseValue<E>> BiConsumer<ItemStack.Builder, Random> value(V value) { return (builder, random) -> { final ItemStack itemStack = builder.build(); final DataTransactionResult dataTransactionResult = itemStack.offer(value); if (dataTransactionResult.isSuccessful()) { builder.from(itemStack); } }; }
@Listener public void onInventoryChange(ChangeInventoryEvent.Held event, @First Player player) { getPainting(player).ifPresent(painting -> { if (event.getTransactions().size() != 2) { return; } int oldSlot = event.getTransactions().get(0).getSlot().getProperty(SlotIndex.class, "slotindex").get().getValue(); int newSlot = event.getTransactions().get(1).getSlot().getProperty(SlotIndex.class, "slotindex").get().getValue(); if (oldSlot < 2 && newSlot > 6) { oldSlot += 9; } else if (oldSlot > 6 && newSlot < 2) { oldSlot -= 9; } int offset = newSlot - oldSlot; List<Art> artsCollection = new ArrayList<>(Sponge.getRegistry().getAllOf(Art.class)); int current = artsCollection.indexOf(painting.art().get()); current += offset; current %= artsCollection.size(); while (!painting.offer(Keys.ART, artsCollection.get(current)).isSuccessful()) { current += offset; current %= artsCollection.size(); } }); }