2022-03-17 09:51:43 +01:00
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
2023-11-11 21:25:45 +01:00
Deprecate upstream's newer and poorly implemented similar
API.
2022-03-17 09:51:43 +01:00
diff --git a/src/main/java/org/bukkit/inventory/ItemFactory.java b/src/main/java/org/bukkit/inventory/ItemFactory.java
2024-10-25 13:52:04 +02:00
index 579a9037b656bef9fb65c6da03611e981492074a..e1986aea72bb1f1ba2ea76f3ba53f274b6aac899 100644
2022-03-17 09:51:43 +01:00
--- a/src/main/java/org/bukkit/inventory/ItemFactory.java
+++ b/src/main/java/org/bukkit/inventory/ItemFactory.java
2024-06-13 16:45:27 +02:00
@@ -170,8 +170,11 @@ public interface ItemFactory {
2023-11-11 21:25:45 +01:00
* @param level the level to use, which is the level in the enchantment table
* @param allowTreasures allows treasure enchants, e.g. mending, if true.
2024-01-14 10:46:04 +01:00
* @return a new ItemStack containing the result of the Enchantment
2023-11-11 21:25:45 +01:00
+ * @deprecated use {@link #enchantWithLevels(ItemStack, int, boolean, java.util.Random)}. This method's implementation is poorly
+ * designed and was originally broken.
*/
@NotNull
2024-06-04 21:40:18 +02:00
+ @Deprecated(since = "1.19.3") // Paper
2023-11-11 21:25:45 +01:00
ItemStack enchantItem(@NotNull final Entity entity, @NotNull final ItemStack item, final int level, final boolean allowTreasures);
/**
2024-06-13 16:45:27 +02:00
@@ -184,8 +187,11 @@ public interface ItemFactory {
2024-01-14 10:46:04 +01:00
* @param level the level to use, which is the level in the enchantment table
2023-11-11 21:25:45 +01:00
* @param allowTreasures allow the treasure enchants, e.g. mending, if true.
2024-01-14 10:46:04 +01:00
* @return a new ItemStack containing the result of the Enchantment
2023-11-11 21:25:45 +01:00
+ * @deprecated use {@link #enchantWithLevels(ItemStack, int, boolean, java.util.Random)}. This method's implementation is poorly
+ * designed and was originally broken.
*/
@NotNull
2024-06-04 21:40:18 +02:00
+ @Deprecated(since = "1.19.3") // Paper
2023-11-11 21:25:45 +01:00
ItemStack enchantItem(@NotNull final World world, @NotNull final ItemStack item, final int level, final boolean allowTreasures);
/**
2024-06-13 16:45:27 +02:00
@@ -197,8 +203,11 @@ public interface ItemFactory {
2024-01-14 10:46:04 +01:00
* @param level the level to use, which is the level in the enchantment table
2023-11-11 21:25:45 +01:00
* @param allowTreasures allow treasure enchantments, e.g. mending, if true.
2024-01-14 10:46:04 +01:00
* @return a new ItemStack containing the result of the Enchantment
2023-11-11 21:25:45 +01:00
+ * @deprecated use {@link #enchantWithLevels(ItemStack, int, boolean, java.util.Random)}. This method's implementation is poorly
+ * designed and was originally broken.
*/
@NotNull
2024-06-04 21:40:18 +02:00
+ @Deprecated(since = "1.19.3") // Paper
2023-11-11 21:25:45 +01:00
ItemStack enchantItem(@NotNull final ItemStack item, final int level, final boolean allowTreasures);
2022-03-17 09:51:43 +01:00
2023-08-06 02:21:59 +02:00
// Paper start - Adventure
2024-06-13 16:45:27 +02:00
@@ -311,4 +320,22 @@ public interface ItemFactory {
2023-11-11 21:25:45 +01:00
@Deprecated
net.md_5.bungee.api.chat.hover.content.Content hoverContentOf(@NotNull org.bukkit.entity.Entity entity, @NotNull net.md_5.bungee.api.chat.BaseComponent[] customName);
// Paper end - bungee hover events
+
+ // Paper start - enchantWithLevels API
2022-03-17 09:51:43 +01:00
+ /**
+ * Randomly enchants a copy of the provided {@link ItemStack} using the given experience levels.
+ *
+ * <p>If the provided ItemStack is already enchanted, the existing enchants will be removed before enchanting.</p>
+ *
+ * <p>Levels must be in range {@code [1, 30]}.</p>
+ *
+ * @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);
2023-11-11 21:25:45 +01:00
+ // Paper end - enchantWithLevels API
}
2022-03-17 09:51:43 +01:00
diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java
2024-10-22 18:23:19 +02:00
index 1d1731776af5f59cd9e6bd07cb3b9fab5073ef66..9b2a62dc3da6718a3e8b39b4fb8bee3781e800cb 100644
2022-03-17 09:51:43 +01:00
--- a/src/main/java/org/bukkit/inventory/ItemStack.java
+++ b/src/main/java/org/bukkit/inventory/ItemStack.java
2024-06-14 14:11:52 +02:00
@@ -678,6 +678,24 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat
2022-03-17 09:51:43 +01:00
}
// Paper start
+ /**
+ * Randomly enchants a copy of this {@link ItemStack} using the given experience levels.
+ *
+ * <p>If this ItemStack is already enchanted, the existing enchants will be removed before enchanting.</p>
+ *
+ * <p>Levels must be in range {@code [1, 30]}.</p>
+ *
+ * @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<net.kyori.adventure.text.event.HoverEvent.ShowItem> asHoverEvent(final @NotNull java.util.function.UnaryOperator<net.kyori.adventure.text.event.HoverEvent.ShowItem> op) {