From a2c7a9b490bd7851c0eca05d8a21718a95594f65 Mon Sep 17 00:00:00 2001 From: Bjarne Koll Date: Sun, 16 Jun 2024 00:19:10 +0200 Subject: [PATCH] Deprecate BlockData#getDestroySpeed for removal The method sadly is not usable in 1.21 without a player as all of an enchantments attribtue modifiers rely on a base value supplied by a player. The method could only offer a rough estimate based on some default values, however a better method for this should be added down the line rather than trying to force such logic into the existing one. --- patches/api/Add-Destroy-Speed-API.patch | 6 ++++++ patches/api/Block-Ticking-API.patch | 2 +- patches/server/Add-Destroy-Speed-API.patch | 17 ++++++++++------- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/patches/api/Add-Destroy-Speed-API.patch b/patches/api/Add-Destroy-Speed-API.patch index 5bf36ae3a6..8e33c0979e 100644 --- a/patches/api/Add-Destroy-Speed-API.patch +++ b/patches/api/Add-Destroy-Speed-API.patch @@ -58,7 +58,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + * + * @param itemStack {@link ItemStack} used to mine this Block + * @return the speed that this Block will be mined by the given {@link ItemStack} ++ * @deprecated the destroy speed of a block was never purely tied to an item stack. Since 1.21 enchantments ++ * also use complex effects that require a consuming player to compute their effects, including mining efficiency. + */ ++ @Deprecated(forRemoval = true, since = "1.21") + default float getDestroySpeed(final @NotNull ItemStack itemStack) { + return this.getDestroySpeed(itemStack, false); + } @@ -71,7 +74,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + * @param itemStack {@link ItemStack} used to mine this Block + * @param considerEnchants true to look at enchants on the itemstack + * @return the speed that this Block will be mined by the given {@link ItemStack} ++ * @deprecated the destroy speed of a block was never purely tied to an item stack. Since 1.21 enchantments ++ * also use complex effects that require a consuming player to compute their effects, including mining efficiency. + */ ++ @Deprecated(forRemoval = true, since = "1.21") + float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnchants); + // Paper end - destroy speed API } diff --git a/patches/api/Block-Ticking-API.patch b/patches/api/Block-Ticking-API.patch index 7b9db3daba..ada40d87d3 100644 --- a/patches/api/Block-Ticking-API.patch +++ b/patches/api/Block-Ticking-API.patch @@ -55,7 +55,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 --- a/src/main/java/org/bukkit/block/data/BlockData.java +++ b/src/main/java/org/bukkit/block/data/BlockData.java @@ -0,0 +0,0 @@ public interface BlockData extends Cloneable { - */ + @Deprecated(forRemoval = true, since = "1.21") float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnchants); // Paper end - destroy speed API + diff --git a/patches/server/Add-Destroy-Speed-API.patch b/patches/server/Add-Destroy-Speed-API.patch index 38bd4b365f..656e804b6c 100644 --- a/patches/server/Add-Destroy-Speed-API.patch +++ b/patches/server/Add-Destroy-Speed-API.patch @@ -20,15 +20,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.unwrap(itemStack); + float speed = nmsItemStack.getDestroySpeed(this.state); + if (speed > 1.0F && considerEnchants) { -+ final org.apache.commons.lang3.mutable.MutableFloat mutableFloat = new org.apache.commons.lang3.mutable.MutableFloat(0); -+ net.minecraft.world.item.enchantment.EnchantmentHelper.forEachModifier( -+ nmsItemStack, net.minecraft.world.entity.EquipmentSlotGroup.MAINHAND, -+ (attributeHolder, attributeModifier) -> { -+ if (!attributeHolder.is(net.minecraft.world.entity.ai.attributes.Attributes.MINING_EFFICIENCY)) return; ++ final net.minecraft.core.Holder efficiencyHolder = net.minecraft.server.MinecraftServer ++ .getServer() ++ .registryAccess() ++ .registryOrThrow(net.minecraft.core.registries.Registries.ENCHANTMENT) ++ .getHolderOrThrow(net.minecraft.world.item.enchantment.Enchantments.EFFICIENCY); + -+ // TODO do we just attempt to calculate the full "modifier" to the mining efficiency? -+ } ++ final int enchantLevel = net.minecraft.world.item.enchantment.EnchantmentHelper.getItemEnchantmentLevel( ++ efficiencyHolder, nmsItemStack + ); ++ if (enchantLevel > 0) { ++ speed += enchantLevel * enchantLevel + 1; ++ } + } + return speed; + }