From 5a55c71491607b617cf3d9259e5bccb383fd7c5b Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Fri, 26 Apr 2024 16:43:59 -0700
Subject: [PATCH] deprecate our ItemRarity API

---
 .../Attributes-API-for-item-defaults.patch    |  2 +-
 patches/api/Expose-protocol-version.patch     |  4 +-
 .../api/Get-entity-default-attributes.patch   |  4 +-
 ...PI.patch => Improve-Item-Rarity-API.patch} | 70 ++++++++++++-------
 patches/api/ItemStack-repair-check-API.patch  | 13 ++--
 patches/server/Expose-protocol-version.patch  |  4 +-
 .../Get-entity-default-attributes.patch       |  4 +-
 patches/server/Item-Rarity-API.patch          | 63 -----------------
 .../server/ItemStack-repair-check-API.patch   | 12 ++--
 9 files changed, 67 insertions(+), 109 deletions(-)
 rename patches/api/{Item-Rarity-API.patch => Improve-Item-Rarity-API.patch} (58%)
 delete mode 100644 patches/server/Item-Rarity-API.patch

diff --git a/patches/api/Attributes-API-for-item-defaults.patch b/patches/api/Attributes-API-for-item-defaults.patch
index f84bddf771..db14ecbee2 100644
--- a/patches/api/Attributes-API-for-item-defaults.patch
+++ b/patches/api/Attributes-API-for-item-defaults.patch
@@ -11,7 +11,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +++ b/src/main/java/org/bukkit/Material.java
 @@ -0,0 +0,0 @@ public enum Material implements Keyed, Translatable, net.kyori.adventure.transla
      public io.papermc.paper.inventory.ItemRarity getItemRarity() {
-         return Bukkit.getUnsafe().getItemRarity(this);
+         return new org.bukkit.inventory.ItemStack(this).getRarity();
      }
 +
 +    /**
diff --git a/patches/api/Expose-protocol-version.patch b/patches/api/Expose-protocol-version.patch
index 023e6fae70..7761f67c41 100644
--- a/patches/api/Expose-protocol-version.patch
+++ b/patches/api/Expose-protocol-version.patch
@@ -9,9 +9,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/org/bukkit/UnsafeValues.java
 +++ b/src/main/java/org/bukkit/UnsafeValues.java
 @@ -0,0 +0,0 @@ public interface UnsafeValues {
-      * @return the itemstack rarity
+      * Just don't use it.
       */
-     public io.papermc.paper.inventory.ItemRarity getItemStackRarity(ItemStack itemStack);
+     @org.jetbrains.annotations.NotNull String getMainLevelName();
 +
 +    /**
 +     * Returns the server's protocol version.
diff --git a/patches/api/Get-entity-default-attributes.patch b/patches/api/Get-entity-default-attributes.patch
index 8c4f5849f5..ecb017b87b 100644
--- a/patches/api/Get-entity-default-attributes.patch
+++ b/patches/api/Get-entity-default-attributes.patch
@@ -9,9 +9,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/org/bukkit/UnsafeValues.java
 +++ b/src/main/java/org/bukkit/UnsafeValues.java
 @@ -0,0 +0,0 @@ public interface UnsafeValues {
-      * @return the server's protocol version
+      * @return true if valid repair, false if not
       */
-     int getProtocolVersion();
+     public boolean isValidRepairItemStack(@org.jetbrains.annotations.NotNull ItemStack itemToBeRepaired, @org.jetbrains.annotations.NotNull ItemStack repairMaterial);
 +
 +    /**
 +     * Checks if the entity represented by the namespaced key has default attributes.
diff --git a/patches/api/Item-Rarity-API.patch b/patches/api/Improve-Item-Rarity-API.patch
similarity index 58%
rename from patches/api/Item-Rarity-API.patch
rename to patches/api/Improve-Item-Rarity-API.patch
index 98d37aa6b4..c597412161 100644
--- a/patches/api/Item-Rarity-API.patch
+++ b/patches/api/Improve-Item-Rarity-API.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Jake Potrebic <jake.m.potrebic@gmail.com>
 Date: Fri, 12 Mar 2021 17:09:40 -0800
-Subject: [PATCH] Item Rarity API
+Subject: [PATCH] Improve Item Rarity API
 
 
 diff --git a/src/main/java/io/papermc/paper/inventory/ItemRarity.java b/src/main/java/io/papermc/paper/inventory/ItemRarity.java
@@ -16,6 +16,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +import net.kyori.adventure.text.format.TextColor;
 +import org.jetbrains.annotations.NotNull;
 +
++/**
++ * @deprecated use {@link org.bukkit.inventory.ItemRarity} with {@link org.bukkit.inventory.meta.ItemMeta#getRarity()}
++ */
++@Deprecated(forRemoval = true, since = "1.20.5")
 +public enum ItemRarity {
 +
 +    COMMON(NamedTextColor.WHITE),
@@ -52,40 +56,56 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +     * Use {@link #isItem()} before this.
 +     *
 +     * @return the item rarity
++     * @deprecated use {@link org.bukkit.inventory.meta.ItemMeta#hasRarity()} and {@link org.bukkit.inventory.meta.ItemMeta#getRarity()}
 +     */
 +    @NotNull
++    @Deprecated(forRemoval = true, since = "1.20.5")
 +    public io.papermc.paper.inventory.ItemRarity getItemRarity() {
-+        return Bukkit.getUnsafe().getItemRarity(this);
++        return new org.bukkit.inventory.ItemStack(this).getRarity();
 +    }
      // Paper end
  
      /**
-diff --git a/src/main/java/org/bukkit/UnsafeValues.java b/src/main/java/org/bukkit/UnsafeValues.java
+diff --git a/src/main/java/org/bukkit/inventory/ItemRarity.java b/src/main/java/org/bukkit/inventory/ItemRarity.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
---- a/src/main/java/org/bukkit/UnsafeValues.java
-+++ b/src/main/java/org/bukkit/UnsafeValues.java
-@@ -0,0 +0,0 @@ public interface UnsafeValues {
-      * Just don't use it.
+--- a/src/main/java/org/bukkit/inventory/ItemRarity.java
++++ b/src/main/java/org/bukkit/inventory/ItemRarity.java
+@@ -0,0 +0,0 @@ public enum ItemRarity {
+     /**
+      * White item name.
       */
-     @org.jetbrains.annotations.NotNull String getMainLevelName();
+-    COMMON,
++    COMMON(net.kyori.adventure.text.format.NamedTextColor.WHITE), // Paper
+     /**
+      * Yellow item name.
+      */
+-    UNCOMMON,
++    UNCOMMON(net.kyori.adventure.text.format.NamedTextColor.YELLOW), // Paper
+     /**
+      * Aqua item name.
+      */
+-    RARE,
++    RARE(net.kyori.adventure.text.format.NamedTextColor.AQUA), // Paper
+     /**
+      * Light purple item name.
+      */
+-    EPIC;
++    EPIC(net.kyori.adventure.text.format.NamedTextColor.LIGHT_PURPLE); // Paper
++    // Paper start - improve ItemRarity
++    private final net.kyori.adventure.text.format.NamedTextColor color;
++    ItemRarity(final net.kyori.adventure.text.format.NamedTextColor color) {
++        this.color = color;
++    }
 +
 +    /**
-+     * Gets the item rarity of a material. The material <b>MUST</b> be an item.
-+     * Use {@link Material#isItem()} before this.
++     * Gets the color formatting associated with this rarity.
 +     *
-+     * @param material the material to get the rarity of
-+     * @return the item rarity
++     * @return the color
 +     */
-+    public io.papermc.paper.inventory.ItemRarity getItemRarity(Material material);
-+
-+    /**
-+     * Gets the item rarity of the itemstack. The rarity can change based on enchantements.
-+     *
-+     * @param itemStack the itemstack to get the rarity of
-+     * @return the itemstack rarity
-+     */
-+    public io.papermc.paper.inventory.ItemRarity getItemStackRarity(ItemStack itemStack);
-     // Paper end
++    public net.kyori.adventure.text.format.@org.jetbrains.annotations.NotNull TextColor color() {
++        return this.color;
++    }
++    // Paper end
  }
 diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
@@ -97,13 +117,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      }
 +
 +    /**
-+     * Gets the item rarity of the itemstack. The rarity can change based on enchantements.
++     * Gets the item rarity of the itemstack. The rarity can change based on enchantments.
 +     *
 +     * @return the itemstack rarity
++     * @deprecated Use {@link ItemMeta#hasRarity()} and {@link ItemMeta#getRarity()}
 +     */
 +    @NotNull
++    @Deprecated(forRemoval = true, since = "1.20.5")
 +    public io.papermc.paper.inventory.ItemRarity getRarity() {
-+        return Bukkit.getUnsafe().getItemStackRarity(this);
++        return io.papermc.paper.inventory.ItemRarity.valueOf(this.getItemMeta().getRarity().name());
 +    }
      // Paper end
  }
diff --git a/patches/api/ItemStack-repair-check-API.patch b/patches/api/ItemStack-repair-check-API.patch
index 476c675975..15dec9d922 100644
--- a/patches/api/ItemStack-repair-check-API.patch
+++ b/patches/api/ItemStack-repair-check-API.patch
@@ -9,9 +9,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/org/bukkit/UnsafeValues.java
 +++ b/src/main/java/org/bukkit/UnsafeValues.java
 @@ -0,0 +0,0 @@ public interface UnsafeValues {
+      * @return the server's protocol version
       */
-     public io.papermc.paper.inventory.ItemRarity getItemStackRarity(ItemStack itemStack);
- 
+     int getProtocolVersion();
++
 +    /**
 +     * Checks if an itemstack can be repaired with another itemstack.
 +     * Returns false if either argument's type is not an item ({@link Material#isItem()}).
@@ -21,17 +22,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +     * @return true if valid repair, false if not
 +     */
 +    public boolean isValidRepairItemStack(@org.jetbrains.annotations.NotNull ItemStack itemToBeRepaired, @org.jetbrains.annotations.NotNull ItemStack repairMaterial);
-+
-     /**
-      * Returns the server's protocol version.
-      *
+     // Paper end
+ }
 diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/org/bukkit/inventory/ItemStack.java
 +++ b/src/main/java/org/bukkit/inventory/ItemStack.java
 @@ -0,0 +0,0 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat
      public io.papermc.paper.inventory.ItemRarity getRarity() {
-         return Bukkit.getUnsafe().getItemStackRarity(this);
+         return io.papermc.paper.inventory.ItemRarity.valueOf(this.getItemMeta().getRarity().name());
      }
 +
 +    /**
diff --git a/patches/server/Expose-protocol-version.patch b/patches/server/Expose-protocol-version.patch
index d60caf0797..30902c3a65 100644
--- a/patches/server/Expose-protocol-version.patch
+++ b/patches/server/Expose-protocol-version.patch
@@ -9,8 +9,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
 +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
 @@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
-     public io.papermc.paper.inventory.ItemRarity getItemStackRarity(org.bukkit.inventory.ItemStack itemStack) {
-         return io.papermc.paper.inventory.ItemRarity.values()[itemStack.getRarity().ordinal()];
+     public String getMainLevelName() {
+         return ((net.minecraft.server.dedicated.DedicatedServer) net.minecraft.server.MinecraftServer.getServer()).getProperties().levelName;
      }
 +
 +    @Override
diff --git a/patches/server/Get-entity-default-attributes.patch b/patches/server/Get-entity-default-attributes.patch
index c676628f05..0b081dfcf5 100644
--- a/patches/server/Get-entity-default-attributes.patch
+++ b/patches/server/Get-entity-default-attributes.patch
@@ -85,8 +85,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
 +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
 @@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
-     public int getProtocolVersion() {
-         return net.minecraft.SharedConstants.getCurrentVersion().getProtocolVersion();
+         }
+         return CraftMagicNumbers.getItem(itemToBeRepaired.getType()).isValidRepairItem(CraftItemStack.asNMSCopy(itemToBeRepaired), CraftItemStack.asNMSCopy(repairMaterial));
      }
 +
 +    @Override
diff --git a/patches/server/Item-Rarity-API.patch b/patches/server/Item-Rarity-API.patch
deleted file mode 100644
index 8da4053937..0000000000
--- a/patches/server/Item-Rarity-API.patch
+++ /dev/null
@@ -1,63 +0,0 @@
-From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
-From: Jake Potrebic <jake.m.potrebic@gmail.com>
-Date: Fri, 12 Mar 2021 17:09:42 -0800
-Subject: [PATCH] Item Rarity API
-
-== AT ==
-public net.minecraft.world.item.Item rarity
-
-diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
-index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
---- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
-+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
-@@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
-     public String getMainLevelName() {
-         return ((net.minecraft.server.dedicated.DedicatedServer) net.minecraft.server.MinecraftServer.getServer()).getProperties().levelName;
-     }
-+
-+    @Override
-+    public io.papermc.paper.inventory.ItemRarity getItemRarity(org.bukkit.Material material) {
-+        Item item = getItem(material);
-+        if (item == null) {
-+            throw new IllegalArgumentException(material + " is not an item, and rarity does not apply to blocks");
-+        }
-+        return io.papermc.paper.inventory.ItemRarity.values()[item.components().getOrDefault(net.minecraft.core.component.DataComponents.RARITY, net.minecraft.world.item.Rarity.COMMON).ordinal()];
-+    }
-+
-+    @Override
-+    public io.papermc.paper.inventory.ItemRarity getItemStackRarity(org.bukkit.inventory.ItemStack itemStack) {
-+        return io.papermc.paper.inventory.ItemRarity.values()[itemStack.getRarity().ordinal()];
-+    }
-     // Paper end
- 
-     /**
-diff --git a/src/test/java/io/papermc/paper/inventory/ItemRarityTest.java b/src/test/java/io/papermc/paper/inventory/ItemRarityTest.java
-new file mode 100644
-index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
---- /dev/null
-+++ b/src/test/java/io/papermc/paper/inventory/ItemRarityTest.java
-@@ -0,0 +0,0 @@
-+package io.papermc.paper.inventory;
-+
-+import io.papermc.paper.adventure.PaperAdventure;
-+import net.minecraft.world.item.Rarity;
-+import org.junit.jupiter.api.Test;
-+
-+import static org.junit.jupiter.api.Assertions.assertEquals;
-+
-+public class ItemRarityTest {
-+
-+    @Test
-+    public void testConvertFromNmsToBukkit() {
-+        for (Rarity nmsRarity : Rarity.values()) {
-+            assertEquals(ItemRarity.values()[nmsRarity.ordinal()].name(), nmsRarity.name(), "rarity names are mis-matched");
-+        }
-+    }
-+
-+    @Test
-+    public void testRarityFormatting() {
-+        for (Rarity nmsRarity : Rarity.values()) {
-+            assertEquals(nmsRarity.color(), PaperAdventure.asVanilla(ItemRarity.values()[nmsRarity.ordinal()].color), "rarity formatting is mis-matched");
-+        }
-+    }
-+}
diff --git a/patches/server/ItemStack-repair-check-API.patch b/patches/server/ItemStack-repair-check-API.patch
index a9785c3161..c0a9af534a 100644
--- a/patches/server/ItemStack-repair-check-API.patch
+++ b/patches/server/ItemStack-repair-check-API.patch
@@ -9,9 +9,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
 +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
 @@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
-         return io.papermc.paper.inventory.ItemRarity.values()[itemStack.getRarity().ordinal()];
+     public int getProtocolVersion() {
+         return net.minecraft.SharedConstants.getCurrentVersion().getProtocolVersion();
      }
- 
++
 +    @Override
 +    public boolean isValidRepairItemStack(org.bukkit.inventory.ItemStack itemToBeRepaired, org.bukkit.inventory.ItemStack repairMaterial) {
 +        if (!itemToBeRepaired.getType().isItem() || !repairMaterial.getType().isItem()) {
@@ -19,10 +20,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        }
 +        return CraftMagicNumbers.getItem(itemToBeRepaired.getType()).isValidRepairItem(CraftItemStack.asNMSCopy(itemToBeRepaired), CraftItemStack.asNMSCopy(repairMaterial));
 +    }
-+
-     @Override
-     public int getProtocolVersion() {
-         return net.minecraft.SharedConstants.getCurrentVersion().getProtocolVersion();
+     // Paper end
+ 
+     /**
 diff --git a/src/test/java/io/papermc/paper/util/ItemStackRepairCheckTest.java b/src/test/java/io/papermc/paper/util/ItemStackRepairCheckTest.java
 new file mode 100644
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000