From 8897cea0b05d9b5f110ed5f153a00daee32875c4 Mon Sep 17 00:00:00 2001 From: Jason <11360596+jpenilla@users.noreply.github.com> Date: Thu, 17 Mar 2022 01:51:43 -0700 Subject: [PATCH] Add enchantWithLevels API (#7615) --- .../api/0375-Add-enchantWithLevels-API.patch | 62 +++++++++++++++++++ ...0882-Implement-enchantWithLevels-API.patch | 32 ++++++++++ 2 files changed, 94 insertions(+) create mode 100644 patches/api/0375-Add-enchantWithLevels-API.patch create mode 100644 patches/server/0882-Implement-enchantWithLevels-API.patch diff --git a/patches/api/0375-Add-enchantWithLevels-API.patch b/patches/api/0375-Add-enchantWithLevels-API.patch new file mode 100644 index 0000000000..45beef6a1a --- /dev/null +++ b/patches/api/0375-Add-enchantWithLevels-API.patch @@ -0,0 +1,62 @@ +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 6a4c4161adabb8e131761c2af4bdf1f26b52434d..3705ac93c9555a0288127bda917e0483056ae1af 100644 +--- a/src/main/java/org/bukkit/inventory/ItemFactory.java ++++ b/src/main/java/org/bukkit/inventory/ItemFactory.java +@@ -144,6 +144,22 @@ public interface ItemFactory { + Material updateMaterial(@NotNull final ItemMeta meta, @NotNull final Material material) 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 7dfc84c77d8e185bb0513d9f9c603ce1b501a5e0..56897cfb96b53e43fec343568e514ee636ddd5c5 100644 +--- a/src/main/java/org/bukkit/inventory/ItemStack.java ++++ b/src/main/java/org/bukkit/inventory/ItemStack.java +@@ -645,6 +645,24 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, net.kyor + } + + // 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) { diff --git a/patches/server/0882-Implement-enchantWithLevels-API.patch b/patches/server/0882-Implement-enchantWithLevels-API.patch new file mode 100644 index 0000000000..cad975c396 --- /dev/null +++ b/patches/server/0882-Implement-enchantWithLevels-API.patch @@ -0,0 +1,32 @@ +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:21 -0700 +Subject: [PATCH] Implement enchantWithLevels API + + +diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java +index 446dfea89b4fbab55d1379315247172b6cc4730c..c4962cae329ba11d4d0dafafc6216bde5d2b0f90 100644 +--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java ++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java +@@ -346,6 +346,21 @@ public final class CraftItemFactory implements ItemFactory { + } + + // Paper start ++ @Override ++ public ItemStack enchantWithLevels(ItemStack itemStack, int levels, boolean allowTreasure, java.util.Random random) { ++ Validate.notNull(itemStack, "Argument 'itemStack' must not be null"); ++ Validate.isTrue(itemStack.getType() != Material.AIR, "Argument 'itemStack' must not be of type AIR"); ++ Validate.isTrue(itemStack.getAmount() > 0, "Argument 'itemStack' amount must be greater than 0"); ++ Validate.isTrue(levels > 0 && levels <= 30, "Argument 'levels' must be in range [1, 30] (attempted " + levels + ")"); ++ Validate.notNull(random, "Argument 'random' must not be null"); ++ final net.minecraft.world.item.ItemStack internalStack = CraftItemStack.asNMSCopy(itemStack); ++ if (internalStack.tag != null) { ++ internalStack.tag.remove(net.minecraft.world.item.ItemStack.TAG_ENCH); ++ } ++ final net.minecraft.world.item.ItemStack enchanted = net.minecraft.world.item.enchantment.EnchantmentHelper.enchantItem(random, internalStack, levels, allowTreasure); ++ return CraftItemStack.asCraftMirror(enchanted); ++ } ++ + @Override + public net.kyori.adventure.text.event.HoverEvent asHoverEvent(final ItemStack item, final java.util.function.UnaryOperator op) { + final net.minecraft.nbt.CompoundTag tag = CraftItemStack.asNMSCopy(item).getTag();