Add ItemMeta customName methods (#11685)

This commit is contained in:
Nassim Jahnke 2024-12-07 20:05:14 +01:00
parent 41f4119eb0
commit 04df9cac25
4 changed files with 208 additions and 12 deletions

View File

@ -4716,31 +4716,73 @@ diff --git a/src/main/java/org/bukkit/inventory/meta/ItemMeta.java b/src/main/ja
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/inventory/meta/ItemMeta.java --- a/src/main/java/org/bukkit/inventory/meta/ItemMeta.java
+++ b/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 @@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
*/ */
boolean hasDisplayName(); public interface ItemMeta extends Cloneable, ConfigurationSerializable, PersistentDataHolder {
+ // Paper start + // 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. + * Gets the display name.
+ * + *
+ * <p>Plugins should check that {@link #hasDisplayName()} returns <code>true</code> before calling this method.</p> + * <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 + * @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. + * Sets the display name.
+ * + *
+ * @param displayName the display name to set + * @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 + // Paper end
+
/** /**
* Gets the display name that is set. * Gets the display name that is set.
* <p>
@@ -0,0 +0,0 @@ public interface ItemMeta extends Cloneable, ConfigurationSerializable, Persiste @@ -0,0 +0,0 @@ public interface ItemMeta extends Cloneable, ConfigurationSerializable, Persiste
* before calling this method. * before calling this method.
* *
@ -4859,6 +4901,79 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
void setLore(@Nullable List<String> lore); 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 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 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/inventory/meta/WritableBookMeta.java --- a/src/main/java/org/bukkit/inventory/meta/WritableBookMeta.java

View File

@ -5237,19 +5237,28 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ // Paper start + // Paper start
+ @Override + @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); + return displayName == null ? null : io.papermc.paper.adventure.PaperAdventure.asAdventure(displayName);
+ } + }
+ +
+ @Override + @Override
+ public void displayName(final net.kyori.adventure.text.Component displayName) { + public void customName(final net.kyori.adventure.text.Component customName) {
+ this.displayName = displayName == null ? null : io.papermc.paper.adventure.PaperAdventure.asVanilla(displayName); + this.displayName = customName == null ? null : io.papermc.paper.adventure.PaperAdventure.asVanilla(customName);
+ } + }
+ // Paper end + // Paper end
+ +
@Override @Override
public String getDisplayName() { public String getDisplayName() {
return CraftChatMessage.fromComponent(this.displayName); 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 { @@ -0,0 +0,0 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
return this.itemName != null; return this.itemName != null;
} }
@ -5288,6 +5297,78 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@Override @Override
public boolean hasRepairCost() { public boolean hasRepairCost() {
return this.repairCost > 0; 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 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 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java --- a/src/main/java/org/bukkit/craftbukkit/inventory/trim/CraftTrimMaterial.java

View File

@ -1653,7 +1653,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@@ -0,0 +0,0 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta { @@ -0,0 +0,0 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
@Override @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"); + Preconditions.checkArgument(customName == null || customName.length() <= 32767, "Custom name is longer than 32767 characters");
this.customName = customName; this.customName = customName;
} }

View File

@ -30,7 +30,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ } + }
+ // Paper end + // Paper end
@Override @Override
public boolean hasDisplayName() { public boolean hasCustomName() {
return this.displayName != null; return this.displayName != null;
@@ -0,0 +0,0 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { @@ -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)); return this.lore == null ? null : new ArrayList<String>(Lists.transform(this.lore, CraftChatMessage::fromComponent));