Add Inventory#modifyItem and Inventory#replaceItem

This commit is contained in:
NichtStudioCode 2023-09-04 18:54:24 +02:00
parent 131e44bc75
commit 2b56eed030

@ -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.
* <p>