From 63e77b531da93e74fe08851aa8f3892984fdec44 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/0285-More-Enchantment-API.patch | 43 +++++++++++++++++-- .../server/0598-More-Enchantment-API.patch | 14 +++++- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/patches/api/0285-More-Enchantment-API.patch b/patches/api/0285-More-Enchantment-API.patch index 0ccfe049b4..88be180baf 100644 --- a/patches/api/0285-More-Enchantment-API.patch +++ b/patches/api/0285-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 @@ -35,7 +36,7 @@ index 0000000000000000000000000000000000000000..e6a40c1fcea761bd66743b50e3da3d14 + } +} diff --git a/src/main/java/org/bukkit/enchantments/Enchantment.java b/src/main/java/org/bukkit/enchantments/Enchantment.java -index 1b6f42ff632b1f3f5034c825e99b72f389a890b4..4cfe8501368f601fdd4203570f4e6a8f6f9685e6 100644 +index 1b6f42ff632b1f3f5034c825e99b72f389a890b4..8183f26545998e076ad93b4e28f3a3ff693d1032 100644 --- a/src/main/java/org/bukkit/enchantments/Enchantment.java +++ b/src/main/java/org/bukkit/enchantments/Enchantment.java @@ -273,11 +273,7 @@ public abstract class Enchantment implements Keyed, net.kyori.adventure.translat @@ -50,7 +51,7 @@ index 1b6f42ff632b1f3f5034c825e99b72f389a890b4..4cfe8501368f601fdd4203570f4e6a8f public abstract boolean isCursed(); /** -@@ -311,6 +307,46 @@ public abstract class Enchantment implements Keyed, net.kyori.adventure.translat +@@ -311,6 +307,70 @@ public abstract class Enchantment implements Keyed, net.kyori.adventure.translat * @return the name of the enchantment with {@code level} applied */ public abstract net.kyori.adventure.text.@NotNull Component displayName(int level); @@ -71,6 +72,30 @@ index 1b6f42ff632b1f3f5034c825e99b72f389a890b4..4cfe8501368f601fdd4203570f4e6a8f + 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 @@ -98,10 +123,10 @@ index 1b6f42ff632b1f3f5034c825e99b72f389a890b4..4cfe8501368f601fdd4203570f4e6a8f @Override diff --git a/src/main/java/org/bukkit/enchantments/EnchantmentWrapper.java b/src/main/java/org/bukkit/enchantments/EnchantmentWrapper.java -index a39f9c078f42451bd122f3e3729d10ca299bee5f..5f42a9c0c43ced10b754170d7c83793a99fce81b 100644 +index a39f9c078f42451bd122f3e3729d10ca299bee5f..4a838d6eab5a17d7204dba96a882b6c5a22636fc 100644 --- a/src/main/java/org/bukkit/enchantments/EnchantmentWrapper.java +++ b/src/main/java/org/bukkit/enchantments/EnchantmentWrapper.java -@@ -74,5 +74,32 @@ public class EnchantmentWrapper extends Enchantment { +@@ -74,5 +74,42 @@ public class EnchantmentWrapper extends Enchantment { public @NotNull String translationKey() { return getEnchantment().translationKey(); } @@ -116,6 +141,16 @@ index a39f9c078f42451bd122f3e3729d10ca299bee5f..5f42a9c0c43ced10b754170d7c83793a + 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/0598-More-Enchantment-API.patch b/patches/server/0598-More-Enchantment-API.patch index fc96204583..8c55414347 100644 --- a/patches/server/0598-More-Enchantment-API.patch +++ b/patches/server/0598-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 5b09e7e08c61b10044ac5f481bf10bb79feda8a3..06a857ae432fc81737b53a6d2b8742c1e80e5f8a 100644 --- a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java @@ -19,7 +21,7 @@ index 5b09e7e08c61b10044ac5f481bf10bb79feda8a3..06a857ae432fc81737b53a6d2b8742c1 } @Override -@@ -199,6 +199,45 @@ public class CraftEnchantment extends Enchantment { +@@ -199,6 +199,55 @@ public class CraftEnchantment extends Enchantment { public String translationKey() { return this.target.getDescriptionId(); } @@ -35,6 +37,16 @@ index 5b09e7e08c61b10044ac5f481bf10bb79feda8a3..06a857ae432fc81737b53a6d2b8742c1 + } + + @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()); + }