From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Wed, 16 Mar 2022 20:35:13 -0700 Subject: [PATCH] Add enchantWithLevels API diff --git a/src/main/java/org/bukkit/inventory/ItemFactory.java b/src/main/java/org/bukkit/inventory/ItemFactory.java index 2b95876d06022ee897279294be341bcfe01ded5d..95054b8573bb96eebcf9be8d26078f82efa91c33 100644 --- a/src/main/java/org/bukkit/inventory/ItemFactory.java +++ b/src/main/java/org/bukkit/inventory/ItemFactory.java @@ -160,6 +160,22 @@ public interface ItemFactory { ItemType updateItemType(@NotNull final ItemMeta meta, @NotNull final ItemType itemType) throws IllegalArgumentException; // Paper start + /** + * Randomly enchants a copy of the provided {@link ItemStack} using the given experience levels. + * + *

If the provided ItemStack is already enchanted, the existing enchants will be removed before enchanting.

+ * + *

Levels must be in range {@code [1, 30]}.

+ * + * @param itemStack ItemStack to enchant + * @param levels levels to use for enchanting + * @param allowTreasure whether to allow enchantments where {@link org.bukkit.enchantments.Enchantment#isTreasure()} returns true + * @param random {@link java.util.Random} instance to use for enchanting + * @return enchanted copy of the provided ItemStack + * @throws IllegalArgumentException on bad arguments + */ + @NotNull ItemStack enchantWithLevels(@NotNull ItemStack itemStack, @org.jetbrains.annotations.Range(from = 1, to = 30) int levels, boolean allowTreasure, @NotNull java.util.Random random); + /** * Creates a hover event for the given item. * diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java index 3a5d5968f1c3b75292c06606733c9f449d6b550f..a139c9e3fdd61c3e228284ae56c39162f0cc045d 100644 --- a/src/main/java/org/bukkit/inventory/ItemStack.java +++ b/src/main/java/org/bukkit/inventory/ItemStack.java @@ -705,6 +705,24 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat } // Paper start + /** + * Randomly enchants a copy of this {@link ItemStack} using the given experience levels. + * + *

If this ItemStack is already enchanted, the existing enchants will be removed before enchanting.

+ * + *

Levels must be in range {@code [1, 30]}.

+ * + * @param levels levels to use for enchanting + * @param allowTreasure whether to allow enchantments where {@link org.bukkit.enchantments.Enchantment#isTreasure()} returns true + * @param random {@link java.util.Random} instance to use for enchanting + * @return enchanted copy of the provided ItemStack + * @throws IllegalArgumentException on bad arguments + */ + @NotNull + public ItemStack enchantWithLevels(final @org.jetbrains.annotations.Range(from = 1, to = 30) int levels, final boolean allowTreasure, final @NotNull java.util.Random random) { + return Bukkit.getServer().getItemFactory().enchantWithLevels(this, levels, allowTreasure, random); + } + @NotNull @Override public net.kyori.adventure.text.event.HoverEvent asHoverEvent(final @NotNull java.util.function.UnaryOperator op) {