Fix/improve destroy speed API (#9645)

further improvements could be a method that takes in an entity
to account for effects
This commit is contained in:
Jake Potrebic 2023-08-25 10:57:11 -07:00
parent 82aefb5454
commit 2a49ab43f6
6 changed files with 89 additions and 39 deletions

View File

@ -10,32 +10,68 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/block/Block.java
+++ b/src/main/java/org/bukkit/block/Block.java
@@ -0,0 +0,0 @@ public interface Block extends Metadatable, Translatable, net.kyori.adventure.tr
@NotNull
@Deprecated(forRemoval = true)
String getTranslationKey();
// Paper end
+
+ // Paper start - destroy speed API
+ /**
+ * Gets the speed at which this block will be destroyed by a given {@link ItemStack}
+ *
+ * <p>Default value is 1.0</p>
+ * <p>
+ * Default value is 1.0
+ *
+ * @param itemStack {@link ItemStack} used to mine this Block
+ * @return the speed that this Block will be mined by the given {@link ItemStack}
+ */
+ @NotNull
+ public default float getDestroySpeed(@NotNull ItemStack itemStack) {
+ return getDestroySpeed(itemStack, false);
+ default float getDestroySpeed(final @NotNull ItemStack itemStack) {
+ return this.getBlockData().getDestroySpeed(itemStack);
+ }
+
+ /**
+ * Gets the speed at which this blook will be destroyed by a given {@link org.bukkit.inventory.ItemStack}
+ * Gets the speed at which this block will be destroyed by a given {@link ItemStack}
+ * <p>
+ * Default value is 1.0
+ * @param itemStack {@link org.bukkit.inventory.ItemStack} used to mine this Block
+ *
+ * @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 org.bukkit.inventory.ItemStack}
+ * @return the speed that this Block will be mined by the given {@link ItemStack}
+ */
+ @NotNull
+ float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnchants);
// Paper end
+ default float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnchants) {
+ return this.getBlockData().getDestroySpeed(itemStack, considerEnchants);
+ }
+ // Paper end - destroy speed API
}
diff --git a/src/main/java/org/bukkit/block/data/BlockData.java b/src/main/java/org/bukkit/block/data/BlockData.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- 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 {
@NotNull
@ApiStatus.Experimental
BlockState createBlockState();
+
+ // Paper start - destroy speed API
+ /**
+ * Gets the speed at which this block will be destroyed by a given {@link ItemStack}
+ * <p>
+ * Default value is 1.0
+ *
+ * @param itemStack {@link ItemStack} used to mine this Block
+ * @return the speed that this Block will be mined by the given {@link ItemStack}
+ */
+ default float getDestroySpeed(final @NotNull ItemStack itemStack) {
+ return this.getDestroySpeed(itemStack, false);
+ }
+
+ /**
+ * Gets the speed at which this block will be destroyed by a given {@link ItemStack}
+ * <p>
+ * Default value is 1.0
+ *
+ * @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}
+ */
+ float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnchants);
+ // Paper end - destroy speed API
}

View File

@ -35,9 +35,9 @@ 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 {
@NotNull
@ApiStatus.Experimental
BlockState createBlockState();
*/
float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnchants);
// Paper end - destroy speed API
+
+ // Paper start - Tick API
+ /**
@ -47,5 +47,5 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * @return is ticked randomly
+ */
+ boolean isRandomlyTicked();
+ // Paper end
+ // Paper end - Tick API
}

View File

@ -9,8 +9,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
@@ -0,0 +0,0 @@ public class CraftBlock implements Block {
}
return speed;
public String translationKey() {
return this.getNMS().getBlock().getDescriptionId();
}
+
+ public boolean isValidTool(ItemStack itemStack) {

View File

@ -5,27 +5,20 @@ Subject: [PATCH] Add Destroy Speed API
Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
diff --git a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
@@ -0,0 +0,0 @@ public class CraftBlock implements Block {
public String translationKey() {
return this.getNMS().getBlock().getDescriptionId();
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
@@ -0,0 +0,0 @@ public class CraftBlockData implements BlockData {
public BlockState createBlockState() {
return CraftBlockStates.getBlockState(this.state, null);
}
+
+ // Paper start - destroy speed API
+ @Override
+ public float getDestroySpeed(ItemStack itemStack, boolean considerEnchants) {
+ net.minecraft.world.item.ItemStack nmsItemStack;
+ if (itemStack instanceof CraftItemStack) {
+ nmsItemStack = ((CraftItemStack) itemStack).handle;
+ if (nmsItemStack == null) {
+ nmsItemStack = net.minecraft.world.item.ItemStack.EMPTY;
+ }
+ } else {
+ nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
+ }
+ float speed = nmsItemStack.getDestroySpeed(this.getNMS().getBlock().defaultBlockState());
+ public float getDestroySpeed(final ItemStack itemStack, final boolean considerEnchants) {
+ net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.unwrap(itemStack);
+ float speed = nmsItemStack.getDestroySpeed(this.state);
+ if (speed > 1.0F && considerEnchants) {
+ int enchantLevel = net.minecraft.world.item.enchantment.EnchantmentHelper.getItemEnchantmentLevel(net.minecraft.world.item.enchantment.Enchantments.BLOCK_EFFICIENCY, nmsItemStack);
+ if (enchantLevel > 0) {
@ -34,5 +27,5 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+ return speed;
+ }
// Paper end
+ // Paper end - destroy speed API
}

View File

@ -35,14 +35,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
@@ -0,0 +0,0 @@ public class CraftBlockData implements BlockData {
public BlockState createBlockState() {
return CraftBlockStates.getBlockState(this.state, null);
return speed;
}
// Paper end - destroy speed API
+
+ // Paper start - Block tick API
+ @Override
+ public boolean isRandomlyTicked() {
+ return this.state.isRandomlyTicking();
+ }
+ // Paper end
+ // Paper end - Block tick API
}

View File

@ -7423,6 +7423,27 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+ // Paper end
}
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
@@ -0,0 +0,0 @@ import org.bukkit.material.MaterialData;
@DelegateDeserialization(ItemStack.class)
public final class CraftItemStack extends ItemStack {
+ // Paper start - MC Utils
+ public static net.minecraft.world.item.ItemStack unwrap(ItemStack bukkit) {
+ if (bukkit instanceof CraftItemStack craftItemStack) {
+ return craftItemStack.handle != null ? craftItemStack.handle : net.minecraft.world.item.ItemStack.EMPTY;
+ } else {
+ return asNMSCopy(bukkit);
+ }
+ }
+ // Paper end - MC Utils
+
public static net.minecraft.world.item.ItemStack asNMSCopy(ItemStack original) {
if (original instanceof CraftItemStack) {
CraftItemStack stack = (CraftItemStack) original;
diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java