13
0
geforkt von Mirrors/Paper

deprecate our ItemRarity API

Dieser Commit ist enthalten in:
Jake Potrebic 2024-04-26 16:43:59 -07:00
Ursprung d93d74a7d5
Commit 5a55c71491
9 geänderte Dateien mit 67 neuen und 109 gelöschten Zeilen

Datei anzeigen

@ -11,7 +11,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+++ b/src/main/java/org/bukkit/Material.java +++ b/src/main/java/org/bukkit/Material.java
@@ -0,0 +0,0 @@ public enum Material implements Keyed, Translatable, net.kyori.adventure.transla @@ -0,0 +0,0 @@ public enum Material implements Keyed, Translatable, net.kyori.adventure.transla
public io.papermc.paper.inventory.ItemRarity getItemRarity() { public io.papermc.paper.inventory.ItemRarity getItemRarity() {
return Bukkit.getUnsafe().getItemRarity(this); return new org.bukkit.inventory.ItemStack(this).getRarity();
} }
+ +
+ /** + /**

Datei anzeigen

@ -9,9 +9,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/UnsafeValues.java --- a/src/main/java/org/bukkit/UnsafeValues.java
+++ b/src/main/java/org/bukkit/UnsafeValues.java +++ b/src/main/java/org/bukkit/UnsafeValues.java
@@ -0,0 +0,0 @@ public interface UnsafeValues { @@ -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. + * Returns the server's protocol version.

Datei anzeigen

@ -9,9 +9,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/UnsafeValues.java --- a/src/main/java/org/bukkit/UnsafeValues.java
+++ b/src/main/java/org/bukkit/UnsafeValues.java +++ b/src/main/java/org/bukkit/UnsafeValues.java
@@ -0,0 +0,0 @@ public interface UnsafeValues { @@ -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. + * Checks if the entity represented by the namespaced key has default attributes.

Datei anzeigen

@ -1,7 +1,7 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com> From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Fri, 12 Mar 2021 17:09:40 -0800 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 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 net.kyori.adventure.text.format.TextColor;
+import org.jetbrains.annotations.NotNull; +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 { +public enum ItemRarity {
+ +
+ COMMON(NamedTextColor.WHITE), + COMMON(NamedTextColor.WHITE),
@ -52,40 +56,56 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * Use {@link #isItem()} before this. + * Use {@link #isItem()} before this.
+ * + *
+ * @return the item rarity + * @return the item rarity
+ * @deprecated use {@link org.bukkit.inventory.meta.ItemMeta#hasRarity()} and {@link org.bukkit.inventory.meta.ItemMeta#getRarity()}
+ */ + */
+ @NotNull + @NotNull
+ @Deprecated(forRemoval = true, since = "1.20.5")
+ public io.papermc.paper.inventory.ItemRarity getItemRarity() { + public io.papermc.paper.inventory.ItemRarity getItemRarity() {
+ return Bukkit.getUnsafe().getItemRarity(this); + return new org.bukkit.inventory.ItemStack(this).getRarity();
+ } + }
// Paper end // 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 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/UnsafeValues.java --- a/src/main/java/org/bukkit/inventory/ItemRarity.java
+++ b/src/main/java/org/bukkit/UnsafeValues.java +++ b/src/main/java/org/bukkit/inventory/ItemRarity.java
@@ -0,0 +0,0 @@ public interface UnsafeValues { @@ -0,0 +0,0 @@ public enum ItemRarity {
* Just don't use it. /**
* 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. + * Gets the color formatting associated with this rarity.
+ * Use {@link Material#isItem()} before this.
+ * + *
+ * @param material the material to get the rarity of + * @return the color
+ * @return the item rarity
+ */ + */
+ public io.papermc.paper.inventory.ItemRarity getItemRarity(Material material); + public net.kyori.adventure.text.format.@org.jetbrains.annotations.NotNull TextColor color() {
+ + return this.color;
+ /** + }
+ * Gets the item rarity of the itemstack. The rarity can change based on enchantements. + // Paper end
+ *
+ * @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 diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 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 + * @return the itemstack rarity
+ * @deprecated Use {@link ItemMeta#hasRarity()} and {@link ItemMeta#getRarity()}
+ */ + */
+ @NotNull + @NotNull
+ @Deprecated(forRemoval = true, since = "1.20.5")
+ public io.papermc.paper.inventory.ItemRarity getRarity() { + 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 // Paper end
} }

Datei anzeigen

@ -9,9 +9,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/UnsafeValues.java --- a/src/main/java/org/bukkit/UnsafeValues.java
+++ b/src/main/java/org/bukkit/UnsafeValues.java +++ b/src/main/java/org/bukkit/UnsafeValues.java
@@ -0,0 +0,0 @@ public interface UnsafeValues { @@ -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. + * Checks if an itemstack can be repaired with another itemstack.
+ * Returns false if either argument's type is not an item ({@link Material#isItem()}). + * 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 + * @return true if valid repair, false if not
+ */ + */
+ public boolean isValidRepairItemStack(@org.jetbrains.annotations.NotNull ItemStack itemToBeRepaired, @org.jetbrains.annotations.NotNull ItemStack repairMaterial); + public boolean isValidRepairItemStack(@org.jetbrains.annotations.NotNull ItemStack itemToBeRepaired, @org.jetbrains.annotations.NotNull ItemStack repairMaterial);
+ // Paper end
/** }
* Returns the server's protocol version.
*
diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/inventory/ItemStack.java --- a/src/main/java/org/bukkit/inventory/ItemStack.java
+++ b/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 @@ -0,0 +0,0 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat
public io.papermc.paper.inventory.ItemRarity getRarity() { public io.papermc.paper.inventory.ItemRarity getRarity() {
return Bukkit.getUnsafe().getItemStackRarity(this); return io.papermc.paper.inventory.ItemRarity.valueOf(this.getItemMeta().getRarity().name());
} }
+ +
+ /** + /**

Datei anzeigen

@ -9,8 +9,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/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 { @@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
public io.papermc.paper.inventory.ItemRarity getItemStackRarity(org.bukkit.inventory.ItemStack itemStack) { public String getMainLevelName() {
return io.papermc.paper.inventory.ItemRarity.values()[itemStack.getRarity().ordinal()]; return ((net.minecraft.server.dedicated.DedicatedServer) net.minecraft.server.MinecraftServer.getServer()).getProperties().levelName;
} }
+ +
+ @Override + @Override

Datei anzeigen

@ -85,8 +85,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/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 { @@ -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 + @Override

Datei anzeigen

@ -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");
+ }
+ }
+}

Datei anzeigen

@ -9,9 +9,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/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 { @@ -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 + @Override
+ public boolean isValidRepairItemStack(org.bukkit.inventory.ItemStack itemToBeRepaired, org.bukkit.inventory.ItemStack repairMaterial) { + public boolean isValidRepairItemStack(org.bukkit.inventory.ItemStack itemToBeRepaired, org.bukkit.inventory.ItemStack repairMaterial) {
+ if (!itemToBeRepaired.getType().isItem() || !repairMaterial.getType().isItem()) { + 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)); + return CraftMagicNumbers.getItem(itemToBeRepaired.getType()).isValidRepairItem(CraftItemStack.asNMSCopy(itemToBeRepaired), CraftItemStack.asNMSCopy(repairMaterial));
+ } + }
+ // Paper end
@Override
public int getProtocolVersion() { /**
return net.minecraft.SharedConstants.getCurrentVersion().getProtocolVersion();
diff --git a/src/test/java/io/papermc/paper/util/ItemStackRepairCheckTest.java b/src/test/java/io/papermc/paper/util/ItemStackRepairCheckTest.java 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 new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000