mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-29 12:27:59 +01:00
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:
parent
82aefb5454
commit
2a49ab43f6
@ -10,32 +10,68 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
|||||||
--- a/src/main/java/org/bukkit/block/Block.java
|
--- a/src/main/java/org/bukkit/block/Block.java
|
||||||
+++ b/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
|
@@ -0,0 +0,0 @@ public interface Block extends Metadatable, Translatable, net.kyori.adventure.tr
|
||||||
@NotNull
|
|
||||||
@Deprecated(forRemoval = true)
|
@Deprecated(forRemoval = true)
|
||||||
String getTranslationKey();
|
String getTranslationKey();
|
||||||
|
// Paper end
|
||||||
+
|
+
|
||||||
|
+ // Paper start - destroy speed API
|
||||||
+ /**
|
+ /**
|
||||||
+ * Gets the speed at which this block will be destroyed by a given {@link ItemStack}
|
+ * Gets the speed at which this block will be destroyed by a given {@link ItemStack}
|
||||||
+ *
|
+ * <p>
|
||||||
+ * <p>Default value is 1.0</p>
|
+ * Default value is 1.0
|
||||||
+ *
|
+ *
|
||||||
+ * @param itemStack {@link ItemStack} used to mine this Block
|
+ * @param itemStack {@link ItemStack} used to mine this Block
|
||||||
+ * @return the speed that this Block will be mined by the given {@link ItemStack}
|
+ * @return the speed that this Block will be mined by the given {@link ItemStack}
|
||||||
+ */
|
+ */
|
||||||
+ @NotNull
|
+ default float getDestroySpeed(final @NotNull ItemStack itemStack) {
|
||||||
+ public default float getDestroySpeed(@NotNull ItemStack itemStack) {
|
+ return this.getBlockData().getDestroySpeed(itemStack);
|
||||||
+ return getDestroySpeed(itemStack, false);
|
|
||||||
+ }
|
+ }
|
||||||
+
|
+
|
||||||
+ /**
|
+ /**
|
||||||
+ * 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>
|
+ * <p>
|
||||||
+ * Default value is 1.0
|
+ * 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
|
+ * @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
|
+ default float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnchants) {
|
||||||
+ float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnchants);
|
+ return this.getBlockData().getDestroySpeed(itemStack, considerEnchants);
|
||||||
// Paper end
|
+ }
|
||||||
|
+ // 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
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
|||||||
--- a/src/main/java/org/bukkit/block/data/BlockData.java
|
--- a/src/main/java/org/bukkit/block/data/BlockData.java
|
||||||
+++ b/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 {
|
@@ -0,0 +0,0 @@ public interface BlockData extends Cloneable {
|
||||||
@NotNull
|
*/
|
||||||
@ApiStatus.Experimental
|
float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnchants);
|
||||||
BlockState createBlockState();
|
// Paper end - destroy speed API
|
||||||
+
|
+
|
||||||
+ // Paper start - Tick API
|
+ // Paper start - Tick API
|
||||||
+ /**
|
+ /**
|
||||||
@ -47,5 +47,5 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
|||||||
+ * @return is ticked randomly
|
+ * @return is ticked randomly
|
||||||
+ */
|
+ */
|
||||||
+ boolean isRandomlyTicked();
|
+ boolean isRandomlyTicked();
|
||||||
+ // Paper end
|
+ // Paper end - Tick API
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
|||||||
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
|
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
|
||||||
+++ b/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 {
|
@@ -0,0 +0,0 @@ public class CraftBlock implements Block {
|
||||||
}
|
public String translationKey() {
|
||||||
return speed;
|
return this.getNMS().getBlock().getDescriptionId();
|
||||||
}
|
}
|
||||||
+
|
+
|
||||||
+ public boolean isValidTool(ItemStack itemStack) {
|
+ public boolean isValidTool(ItemStack itemStack) {
|
||||||
|
@ -5,27 +5,20 @@ Subject: [PATCH] Add Destroy Speed API
|
|||||||
|
|
||||||
Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
|
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
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||||
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
|
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
|
||||||
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
|
+++ b/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
|
||||||
@@ -0,0 +0,0 @@ public class CraftBlock implements Block {
|
@@ -0,0 +0,0 @@ public class CraftBlockData implements BlockData {
|
||||||
public String translationKey() {
|
public BlockState createBlockState() {
|
||||||
return this.getNMS().getBlock().getDescriptionId();
|
return CraftBlockStates.getBlockState(this.state, null);
|
||||||
}
|
}
|
||||||
+
|
+
|
||||||
|
+ // Paper start - destroy speed API
|
||||||
+ @Override
|
+ @Override
|
||||||
+ public float getDestroySpeed(ItemStack itemStack, boolean considerEnchants) {
|
+ public float getDestroySpeed(final ItemStack itemStack, final boolean considerEnchants) {
|
||||||
+ net.minecraft.world.item.ItemStack nmsItemStack;
|
+ net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.unwrap(itemStack);
|
||||||
+ if (itemStack instanceof CraftItemStack) {
|
+ float speed = nmsItemStack.getDestroySpeed(this.state);
|
||||||
+ 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());
|
|
||||||
+ if (speed > 1.0F && considerEnchants) {
|
+ if (speed > 1.0F && considerEnchants) {
|
||||||
+ int enchantLevel = net.minecraft.world.item.enchantment.EnchantmentHelper.getItemEnchantmentLevel(net.minecraft.world.item.enchantment.Enchantments.BLOCK_EFFICIENCY, nmsItemStack);
|
+ int enchantLevel = net.minecraft.world.item.enchantment.EnchantmentHelper.getItemEnchantmentLevel(net.minecraft.world.item.enchantment.Enchantments.BLOCK_EFFICIENCY, nmsItemStack);
|
||||||
+ if (enchantLevel > 0) {
|
+ if (enchantLevel > 0) {
|
||||||
@ -34,5 +27,5 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
|||||||
+ }
|
+ }
|
||||||
+ return speed;
|
+ return speed;
|
||||||
+ }
|
+ }
|
||||||
// Paper end
|
+ // Paper end - destroy speed API
|
||||||
}
|
}
|
||||||
|
@ -35,14 +35,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
|||||||
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
|
--- a/src/main/java/org/bukkit/craftbukkit/block/data/CraftBlockData.java
|
||||||
+++ b/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 {
|
@@ -0,0 +0,0 @@ public class CraftBlockData implements BlockData {
|
||||||
public BlockState createBlockState() {
|
return speed;
|
||||||
return CraftBlockStates.getBlockState(this.state, null);
|
|
||||||
}
|
}
|
||||||
|
// Paper end - destroy speed API
|
||||||
+
|
+
|
||||||
+ // Paper start - Block tick API
|
+ // Paper start - Block tick API
|
||||||
+ @Override
|
+ @Override
|
||||||
+ public boolean isRandomlyTicked() {
|
+ public boolean isRandomlyTicked() {
|
||||||
+ return this.state.isRandomlyTicking();
|
+ return this.state.isRandomlyTicking();
|
||||||
+ }
|
+ }
|
||||||
+ // Paper end
|
+ // Paper end - Block tick API
|
||||||
}
|
}
|
||||||
|
@ -7423,6 +7423,27 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
|||||||
+ }
|
+ }
|
||||||
+ // Paper end
|
+ // 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
|
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
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||||
--- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java
|
--- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftScheduler.java
|
||||||
|
Loading…
Reference in New Issue
Block a user