Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ItemUtils#changeItemMeta, #setItem #7217

Open
wants to merge 11 commits into
base: dev/feature
Choose a base branch
from

Conversation

TheAbsolutionism
Copy link
Contributor

@TheAbsolutionism TheAbsolutionism commented Nov 15, 2024

Description

This PR is to help with shorthand ItemStack changes.
Example: (Before)

for (Object object : getExpr().getArray(event)) {
    ItemStack item = ItemUtils.asItemStack(object);
    if (item == null)
        continue;
    ItemMeta meta = item.getItemMeta();
    consumer.accept(meta);
    item.setItemMeta(meta);
    if (object instanceof Slot slot) {
        slot.setItem(item);
    } else if (object instaceof ItemType itemType) {
        itemType.setItemMeta(meta);
    } else if (object instanceof ItemStack itemStack) {
        itemStack.setItemMeta(meta);
    }
}

(After)

for (Object object : getExpr().getArray(event)) {
    ItemStack item = ItemUtils.asItemStack(object);
    if (item == null)
        continue;
    ItemUtils.setItem(object, ItemUtils.changeItemMeta(item, consumer));
}

Obviously this will more than likely be up for debate but here's hoping.


Target Minecraft Versions: any
Requirements: none
Related Issues: none

@sovdeeth sovdeeth added the feature Pull request adding a new feature. label Nov 16, 2024
* @param <T>
* @return the updated item
*/
public static <T extends ItemMeta> T changeItemMeta(@NotNull ItemStack itemStack, @NotNull Consumer<T> metaChanger) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paper has these editMeta methods which might be useful call here if available: https://jd.papermc.io/paper/1.21.4/org/bukkit/inventory/ItemStack.html#editMeta(java.util.function.Consumer)

Though, I'm not sure they are doing anything different internally from what is being done here, so it might be worth looking into that first.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I remember they did the exact same thing as he does here, when he was working on it I mentioned in a discord call about it.
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep, if it's the same then no need to worry about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Pull request adding a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants