From 04df9cac2512dac939d712baa92a3bafc0c34465 Mon Sep 17 00:00:00 2001
From: Nassim Jahnke <nassim@njahnke.dev>
Date: Sat, 7 Dec 2024 20:05:14 +0100
Subject: [PATCH] Add ItemMeta customName methods (#11685)

---
 patches/api/Adventure.patch                   | 129 +++++++++++++++++-
 patches/server/Adventure.patch                |  87 +++++++++++-
 patches/server/General-ItemMeta-fixes.patch   |   2 +-
 .../Support-components-in-ItemMeta.patch      |   2 +-
 4 files changed, 208 insertions(+), 12 deletions(-)

diff --git a/patches/api/Adventure.patch b/patches/api/Adventure.patch
index 5c4a58981f..60ac07f447 100644
--- a/patches/api/Adventure.patch
+++ b/patches/api/Adventure.patch
@@ -4716,31 +4716,73 @@ diff --git a/src/main/java/org/bukkit/inventory/meta/ItemMeta.java b/src/main/ja
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/org/bukkit/inventory/meta/ItemMeta.java
 +++ b/src/main/java/org/bukkit/inventory/meta/ItemMeta.java
-@@ -0,0 +0,0 @@ public interface ItemMeta extends Cloneable, ConfigurationSerializable, Persiste
-      */
-     boolean hasDisplayName();
+@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
+  */
+ public interface ItemMeta extends Cloneable, ConfigurationSerializable, PersistentDataHolder {
  
 +    // Paper start
 +    /**
++     * Checks for existence of a custom name.
++     *
++     * @return true if this has a custom name
++     */
++    boolean hasCustomName();
++
++    /**
++     * Gets the custom name.
++     *
++     * <p>Plugins should check that {@link #hasCustomName()} returns {@code true} before calling this method.</p>
++     *
++     * @return the custom name
++     */
++    net.kyori.adventure.text.@Nullable Component customName();
++
++    /**
++     * Sets the custom name.
++     *
++     * @param customName the custom name to set
++     */
++    void customName(final net.kyori.adventure.text.@Nullable Component customName);
++
+     /**
+      * Checks for existence of a display name.
+      *
++     * @apiNote This method is obsolete, use {@link #hasCustomName()} instead.
+      * @return true if this has a display name
+      */
+-    boolean hasDisplayName();
++    @ApiStatus.Obsolete(since = "1.21.4")
++    default boolean hasDisplayName() {
++        return this.hasCustomName();
++    }
++
++    /**
 +     * Gets the display name.
 +     *
 +     * <p>Plugins should check that {@link #hasDisplayName()} returns <code>true</code> before calling this method.</p>
 +     *
++     * @apiNote This method is obsolete, use {@link #customName()} instead.
 +     * @return the display name
 +     */
-+    net.kyori.adventure.text.@Nullable Component displayName();
++    @ApiStatus.Obsolete(since = "1.21.4")
++    default net.kyori.adventure.text.@Nullable Component displayName() {
++        return this.customName();
++    }
 +
 +    /**
 +     * Sets the display name.
 +     *
 +     * @param displayName the display name to set
++     * @apiNote This method is obsolete, use {@link #customName(Component)} instead.
 +     */
-+    void displayName(final net.kyori.adventure.text.@Nullable Component displayName);
++    @ApiStatus.Obsolete(since = "1.21.4")
++    default void displayName(final net.kyori.adventure.text.@Nullable Component displayName) {
++        this.customName(displayName);
++    }
 +    // Paper end
-+
+ 
      /**
       * Gets the display name that is set.
-      * <p>
 @@ -0,0 +0,0 @@ public interface ItemMeta extends Cloneable, ConfigurationSerializable, Persiste
       * before calling this method.
       *
@@ -4859,6 +4901,79 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      void setLore(@Nullable List<String> lore);
  
      /**
+diff --git a/src/main/java/org/bukkit/inventory/meta/PotionMeta.java b/src/main/java/org/bukkit/inventory/meta/PotionMeta.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/org/bukkit/inventory/meta/PotionMeta.java
++++ b/src/main/java/org/bukkit/inventory/meta/PotionMeta.java
+@@ -0,0 +0,0 @@ public interface PotionMeta extends ItemMeta {
+     /**
+      * Checks for existence of a custom potion name translation suffix.
+      *
++     * @deprecated conflicting name, use {@link #hasCustomPotionName()}
+      * @return true if this has a custom potion name
+      */
+-    boolean hasCustomName();
++    @Deprecated(forRemoval = true, since = "1.21.4")
++    default boolean hasCustomName() {
++        return this.hasCustomPotionName();
++    }
+ 
+     /**
+      * Gets the potion name translation suffix that is set.
+      * <p>
+-     * Plugins should check that hasCustomName() returns <code>true</code>
++     * Plugins should check that {@link #hasCustomPotionName()} returns {@code true}
+      * before calling this method.
+      *
++     * @deprecated conflicting name, use {@link #getCustomPotionName()}
+      * @return the potion name that is set
+      */
++    @Deprecated(forRemoval = true, since = "1.21.4")
+     @Nullable
+-    String getCustomName();
++    default String getCustomName() {
++        return this.getCustomPotionName();
++    }
+ 
+     /**
+      * Sets the potion name translation suffix.
+      *
++     * @deprecated conflicting name, use {@link #setCustomPotionName(String)}
+      * @param name the name to set
+      */
+-    void setCustomName(@Nullable String name);
++    @Deprecated(forRemoval = true, since = "1.21.4")
++    default void setCustomName(@Nullable String name) {
++        this.setCustomPotionName(name);
++    }
++
++    /**
++     * Checks for existence of a custom potion name translation suffix.
++     *
++     * @return true if this has a custom potion name
++     */
++    boolean hasCustomPotionName();
++
++    /**
++     * Gets the potion name translation suffix that is set.
++     * <p>
++     * Plugins should check that {@link #hasCustomPotionName()} returns {@code true}
++     * before calling this method.
++     *
++     * @return the potion name that is set
++     */
++    @Nullable
++    String getCustomPotionName();
++
++    /**
++     * Sets the potion name translation suffix.
++     *
++     * @param name the name to set
++     */
++    void setCustomPotionName(@Nullable String name);
+ 
+     @Override
+     PotionMeta clone();
 diff --git a/src/main/java/org/bukkit/inventory/meta/WritableBookMeta.java b/src/main/java/org/bukkit/inventory/meta/WritableBookMeta.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/org/bukkit/inventory/meta/WritableBookMeta.java
diff --git a/patches/server/Adventure.patch b/patches/server/Adventure.patch
index f580a63d74..f20459e42c 100644
--- a/patches/server/Adventure.patch
+++ b/patches/server/Adventure.patch
@@ -5237,19 +5237,28 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
  
 +    // Paper start
 +    @Override
-+    public net.kyori.adventure.text.Component displayName() {
++    public net.kyori.adventure.text.Component customName() {
 +        return displayName == null ? null : io.papermc.paper.adventure.PaperAdventure.asAdventure(displayName);
 +    }
 +
 +    @Override
-+    public void displayName(final net.kyori.adventure.text.Component displayName) {
-+        this.displayName = displayName == null ? null : io.papermc.paper.adventure.PaperAdventure.asVanilla(displayName);
++    public void customName(final net.kyori.adventure.text.Component customName) {
++        this.displayName = customName == null ? null : io.papermc.paper.adventure.PaperAdventure.asVanilla(customName);
 +    }
 +    // Paper end
 +
      @Override
      public String getDisplayName() {
          return CraftChatMessage.fromComponent(this.displayName);
+@@ -0,0 +0,0 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+     }
+ 
+     @Override
+-    public boolean hasDisplayName() {
++    public boolean hasCustomName() {
+         return this.displayName != null;
+     }
+ 
 @@ -0,0 +0,0 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
          return this.itemName != null;
      }
@@ -5288,6 +5297,78 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      @Override
      public boolean hasRepairCost() {
          return this.repairCost > 0;
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
+@@ -0,0 +0,0 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
+ 
+         String name = SerializableMeta.getString(map, CraftMetaPotion.CUSTOM_NAME.BUKKIT, true);
+         if (name != null) {
+-            this.setCustomName(name);
++            this.setCustomPotionName(name);
+         }
+ 
+         Iterable<?> rawEffectList = SerializableMeta.getObject(Iterable.class, map, CraftMetaPotion.POTION_EFFECTS.BUKKIT, true);
+@@ -0,0 +0,0 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
+     }
+ 
+     boolean isPotionEmpty() {
+-        return (this.type == null) && !(this.hasCustomEffects() || this.hasColor() || this.hasCustomName());
++        return (this.type == null) && !(this.hasCustomEffects() || this.hasColor() || this.hasCustomPotionName());
+     }
+ 
+     @Override
+@@ -0,0 +0,0 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
+     }
+ 
+     @Override
+-    public boolean hasCustomName() {
++    public boolean hasCustomPotionName() {
+         return this.customName != null;
+     }
+ 
+     @Override
+-    public String getCustomName() {
++    public String getCustomPotionName() {
+         return this.customName;
+     }
+ 
+     @Override
+-    public void setCustomName(String customName) {
++    public void setCustomPotionName(String customName) {
+         this.customName = customName;
+     }
+ 
+@@ -0,0 +0,0 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
+         if (this.hasColor()) {
+             hash = 73 * hash + this.color.hashCode();
+         }
+-        if (this.hasCustomName()) {
++        if (this.hasCustomPotionName()) {
+             hash = 73 * hash + this.customName.hashCode();
+         }
+         if (this.hasCustomEffects()) {
+@@ -0,0 +0,0 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
+             return Objects.equals(this.type, that.type)
+                     && (this.hasCustomEffects() ? that.hasCustomEffects() && this.customEffects.equals(that.customEffects) : !that.hasCustomEffects())
+                     && (this.hasColor() ? that.hasColor() && this.color.equals(that.color) : !that.hasColor())
+-                    && (this.hasCustomName() ? that.hasCustomName() && this.customName.equals(that.customName) : !that.hasCustomName());
++                    && (this.hasCustomPotionName() ? that.hasCustomPotionName() && this.customName.equals(that.customName) : !that.hasCustomPotionName());
+         }
+         return true;
+     }
+@@ -0,0 +0,0 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
+             builder.put(CraftMetaPotion.POTION_COLOR.BUKKIT, this.getColor());
+         }
+ 
+-        if (this.hasCustomName()) {
+-            builder.put(CraftMetaPotion.CUSTOM_NAME.BUKKIT, this.getCustomName());
++        if (this.hasCustomPotionName()) {
++            builder.put(CraftMetaPotion.CUSTOM_NAME.BUKKIT, this.getCustomPotionName());
+         }
+ 
+         if (this.hasCustomEffects()) {
 diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java b/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java
diff --git a/patches/server/General-ItemMeta-fixes.patch b/patches/server/General-ItemMeta-fixes.patch
index 6b12704dec..db87e0c7ad 100644
--- a/patches/server/General-ItemMeta-fixes.patch
+++ b/patches/server/General-ItemMeta-fixes.patch
@@ -1653,7 +1653,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 @@ -0,0 +0,0 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
  
      @Override
-     public void setCustomName(String customName) {
+     public void setCustomPotionName(String customName) {
 +        Preconditions.checkArgument(customName == null || customName.length() <= 32767, "Custom name is longer than 32767 characters");
          this.customName = customName;
      }
diff --git a/patches/server/Support-components-in-ItemMeta.patch b/patches/server/Support-components-in-ItemMeta.patch
index 55d923667b..179299fddc 100644
--- a/patches/server/Support-components-in-ItemMeta.patch
+++ b/patches/server/Support-components-in-ItemMeta.patch
@@ -30,7 +30,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    }
 +    // Paper end
      @Override
-     public boolean hasDisplayName() {
+     public boolean hasCustomName() {
          return this.displayName != null;
 @@ -0,0 +0,0 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
          return this.lore == null ? null : new ArrayList<String>(Lists.transform(this.lore, CraftChatMessage::fromComponent));