From 316e921c1894ba05c1da20b776cfc267cb63140d Mon Sep 17 00:00:00 2001 From: Luis Date: Sat, 11 Nov 2023 21:09:48 +0100 Subject: [PATCH] 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. --- patches/api/More-Enchantment-API.patch | 35 +++++++++++++++++++++++ patches/server/More-Enchantment-API.patch | 12 ++++++++ 2 files changed, 47 insertions(+) diff --git a/patches/api/More-Enchantment-API.patch b/patches/api/More-Enchantment-API.patch index 1c654591a7..44a9849f61 100644 --- a/patches/api/More-Enchantment-API.patch +++ b/patches/api/More-Enchantment-API.patch @@ -3,6 +3,7 @@ From: Jake Potrebic Date: Thu, 6 May 2021 19:58:03 -0700 Subject: [PATCH] More Enchantment API +Co-authored-by: Luis 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. ++ *

++ * 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 https://minecraft.wiki/w/Enchanting/Levels for more information. ++ *

++ * @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. ++ *

++ * 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 https://minecraft.wiki/w/Enchanting/Levels for more information. ++ *

++ * @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() { diff --git a/patches/server/More-Enchantment-API.patch b/patches/server/More-Enchantment-API.patch index d44d49def3..b1e5e3fce3 100644 --- a/patches/server/More-Enchantment-API.patch +++ b/patches/server/More-Enchantment-API.patch @@ -6,6 +6,8 @@ Subject: [PATCH] More Enchantment API == AT == public net.minecraft.world.item.enchantment.Enchantment slots +Co-authored-by: Luis + 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()); + }