Add Enchantment cost API (#9856)

Cost is a property of individual enchantments, and is used by vanilla in combination with environmental aspects like tool enchantability and bookshelf count to determine the final cost of an enchantment as shown in an enchanting table.

Having access to the base cost of an enchantment using these vanilla methods can allow plugin developers to determine the "value" of an enchantment, and use it in custom logic where needed. I came across this recently when trying to assign an economic value to enchantments during tool repairing, and noticed these values don't seem to be obtainable under the current API.
This commit is contained in:
Luis 2023-11-11 21:09:48 +01:00
parent 95ec1d2acb
commit 316e921c18
2 changed files with 47 additions and 0 deletions

View file

@ -3,6 +3,7 @@ From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Thu, 6 May 2021 19:58:03 -0700
Subject: [PATCH] More Enchantment API
Co-authored-by: Luis <luisc99@icloud.com>
diff --git a/src/main/java/io/papermc/paper/enchantments/EnchantmentRarity.java b/src/main/java/io/papermc/paper/enchantments/EnchantmentRarity.java
new file mode 100644
@ -71,6 +72,30 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ public abstract boolean isDiscoverable();
+
+ /**
+ * Gets the minimum modified cost of this enchantment at a specific level.
+ * <p>
+ * Note this is not the number of experience levels needed, and does not directly translate to the levels shown in an enchanting table.
+ * This value is used in combination with factors such as tool enchantability to determine a final cost.
+ * See <a href="https://minecraft.wiki/w/Enchanting/Levels">https://minecraft.wiki/w/Enchanting/Levels</a> for more information.
+ * </p>
+ * @param level The level of the enchantment
+ * @return The modified cost of this enchantment
+ */
+ public abstract int getMinModifiedCost(int level);
+
+ /**
+ * Gets the maximum modified cost of this enchantment at a specific level.
+ * <p>
+ * Note this is not the number of experience levels needed, and does not directly translate to the levels shown in an enchanting table.
+ * This value is used in combination with factors such as tool enchantability to determine a final cost.
+ * See <a href="https://minecraft.wiki/w/Enchanting/Levels">https://minecraft.wiki/w/Enchanting/Levels</a> for more information.
+ * </p>
+ * @param level The level of the enchantment
+ * @return The modified cost of this enchantment
+ */
+ public abstract int getMaxModifiedCost(int level);
+
+ /**
+ * Gets the rarity of this enchantment.
+ *
+ * @return the rarity
@ -116,6 +141,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ return getEnchantment().isDiscoverable();
+ }
+
+ @Override
+ public int getMinModifiedCost(int level) {
+ return getEnchantment().getMinModifiedCost(level);
+ }
+
+ @Override
+ public int getMaxModifiedCost(int level) {
+ return getEnchantment().getMaxModifiedCost(level);
+ }
+
+ @NotNull
+ @Override
+ public io.papermc.paper.enchantments.EnchantmentRarity getRarity() {

View file

@ -6,6 +6,8 @@ Subject: [PATCH] More Enchantment API
== AT ==
public net.minecraft.world.item.enchantment.Enchantment slots
Co-authored-by: Luis <luisc99@icloud.com>
diff --git a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
@ -35,6 +37,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ @Override
+ public int getMinModifiedCost(int level) {
+ return target.getMinCost(level);
+ }
+
+ @Override
+ public int getMaxModifiedCost(int level) {
+ return target.getMaxCost(level);
+ }
+
+ @Override
+ public io.papermc.paper.enchantments.EnchantmentRarity getRarity() {
+ return fromNMSRarity(target.getRarity());
+ }