From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Fri, 12 Mar 2021 17:09:40 -0800 Subject: [PATCH] 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 new file mode 100644 index 0000000000000000000000000000000000000000..74ef8395cc040ce488c2acaa416db20272cc2734 --- /dev/null +++ b/src/main/java/io/papermc/paper/inventory/ItemRarity.java @@ -0,0 +1,28 @@ +package io.papermc.paper.inventory; + +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextColor; +import org.jetbrains.annotations.NotNull; + +public enum ItemRarity { + + COMMON(NamedTextColor.WHITE), + UNCOMMON(NamedTextColor.YELLOW), + RARE(NamedTextColor.AQUA), + EPIC(NamedTextColor.LIGHT_PURPLE); + + TextColor color; + + ItemRarity(TextColor color) { + this.color = color; + } + + /** + * Gets the color formatting associated with the rarity. + * @return + */ + @NotNull + public TextColor getColor() { + return color; + } +} diff --git a/src/main/java/org/bukkit/Material.java b/src/main/java/org/bukkit/Material.java index ffcb8e24a553f0cd81ec8c48511f28d284dbbd42..d6c3c6b320e4996dfbc185d594d94efbedf453c9 100644 --- a/src/main/java/org/bukkit/Material.java +++ b/src/main/java/org/bukkit/Material.java @@ -4464,6 +4464,20 @@ public enum Material implements Keyed, Translatable, net.kyori.adventure.transla return Bukkit.getUnsafe().getBlockTranslationKey(this); } } + + /** + * Returns the item rarity for the item. The Material MUST be an Item not a block. + * Use {@link #isItem()} before this. + * + * @return the item rarity + * @deprecated use {@link ItemType#getItemRarity()} + */ + @Deprecated + @NotNull + public io.papermc.paper.inventory.ItemRarity getItemRarity() { + Preconditions.checkArgument(this.isItem(), this + " must be an item"); + return this.asItemType().getItemRarity(); + } // Paper end /** diff --git a/src/main/java/org/bukkit/UnsafeValues.java b/src/main/java/org/bukkit/UnsafeValues.java index 413085e6793a17762685198a5416cc3dfcc88995..4fbb9153c34ef95170e62e4e519ce4ea2e685558 100644 --- a/src/main/java/org/bukkit/UnsafeValues.java +++ b/src/main/java/org/bukkit/UnsafeValues.java @@ -151,5 +151,13 @@ public interface UnsafeValues { * Just don't use it. */ @org.jetbrains.annotations.NotNull String getMainLevelName(); + + /** + * 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 } diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java index 89fe32de713df3a5f2a25d72cbfe316e5b6c3f1b..d4bc360cf7f0b0fbed07dc10b5778605fde7c219 100644 --- a/src/main/java/org/bukkit/inventory/ItemStack.java +++ b/src/main/java/org/bukkit/inventory/ItemStack.java @@ -926,5 +926,15 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat public @NotNull String translationKey() { return Bukkit.getUnsafe().getTranslationKey(this); } + + /** + * Gets the item rarity of the itemstack. The rarity can change based on enchantements. + * + * @return the itemstack rarity + */ + @NotNull + public io.papermc.paper.inventory.ItemRarity getRarity() { + return Bukkit.getUnsafe().getItemStackRarity(this); + } // Paper end } diff --git a/src/main/java/org/bukkit/inventory/ItemType.java b/src/main/java/org/bukkit/inventory/ItemType.java index 834c6b0ba732a8f5c2bdb3163a74efb3aa687ba9..27a1d255b8fcbd6487e4d4eb1abf784b43eaf90e 100644 --- a/src/main/java/org/bukkit/inventory/ItemType.java +++ b/src/main/java/org/bukkit/inventory/ItemType.java @@ -1371,4 +1371,14 @@ public interface ItemType extends Keyed, Translatable, net.kyori.adventure.trans */ @Nullable CreativeCategory getCreativeCategory(); + + // Paper start - Item Rarity API + /** + * Returns the item rarity for the item. + * + * @return the item rarity + */ + @NotNull + io.papermc.paper.inventory.ItemRarity getItemRarity(); + // Paper end }