From 2b56eed030067fde4a5233fd83394ef4328715bb Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Mon, 4 Sep 2023 18:54:24 +0200 Subject: [PATCH] Add Inventory#modifyItem and Inventory#replaceItem --- .../xenondevs/invui/inventory/Inventory.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/invui-core/src/main/java/xyz/xenondevs/invui/inventory/Inventory.java b/invui-core/src/main/java/xyz/xenondevs/invui/inventory/Inventory.java index 447cafb..9c1cfaf 100644 --- a/invui-core/src/main/java/xyz/xenondevs/invui/inventory/Inventory.java +++ b/invui-core/src/main/java/xyz/xenondevs/invui/inventory/Inventory.java @@ -566,6 +566,37 @@ public abstract class Inventory { return forceSetItem(updateReason, slot, itemStack); } + /** + * Changes the {@link ItemStack} on a specific slot based on the current {@link ItemStack} on that slot, + * using a modifier consumer. + * + * @param updateReason The reason used in the {@link ItemPreUpdateEvent} and {@link ItemPostUpdateEvent}. + * @param slot The slot. + * @param modifier The modifier consumer. Accepts the current {@link ItemStack} and modifies it. + * @return If the action was successful. + */ + public boolean modifyItem(@Nullable UpdateReason updateReason, int slot, @NotNull Consumer<@Nullable ItemStack> modifier) { + ItemStack itemStack = getItem(slot); + modifier.accept(itemStack); + return setItem(updateReason, slot, itemStack); + } + + /** + * Replaces the {@link ItemStack} on a specific slot based on the current {@link ItemStack} on that slot, + * using a replace function. + * + * @param updateReason The reason used in the {@link ItemPreUpdateEvent} and {@link ItemPostUpdateEvent}. + * @param slot The slot. + * @param function The replace function. The argument is the current {@link ItemStack}, + * the return value is the new {@link ItemStack}. + * @return If the action was successful. + */ + public boolean replaceItem(@Nullable UpdateReason updateReason, int slot, @NotNull Function<@Nullable ItemStack, @Nullable ItemStack> function) { + ItemStack currentStack = getItem(slot); + ItemStack newStack = function.apply(currentStack); + return setItem(updateReason, slot, newStack); + } + /** * Adds an {@link ItemStack} on a specific slot and returns the amount of items that did not fit on that slot. *