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

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