Skip to content

Commit 782ce95

Browse files
authored
Allow forcing break effects when using breakNaturally (#12734)
1 parent 0dad7f1 commit 782ce95

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

paper-api/src/main/java/org/bukkit/block/Block.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,18 @@ default boolean breakNaturally(@NotNull ItemStack tool, boolean triggerEffect) {
591591
*/
592592
boolean breakNaturally(@NotNull ItemStack tool, boolean triggerEffect, boolean dropExperience);
593593

594+
/**
595+
* Breaks the block and spawns item drops as if a player had broken it
596+
* with a specific tool
597+
*
598+
* @param tool The tool or item in hand used for digging
599+
* @param triggerEffect Play the block break particle effect and sound
600+
* @param dropExperience drop exp if the block normally does so
601+
* @param forceEffect Forces the break effect to be triggered even if the tool is not the correct tool for the block
602+
* @return true if the block was destroyed
603+
*/
604+
boolean breakNaturally(@NotNull ItemStack tool, boolean triggerEffect, boolean dropExperience, boolean forceEffect);
605+
594606
/**
595607
* Causes the block to be ticked, this is different from {@link Block#randomTick()},
596608
* in that it is usually scheduled to occur, for example

paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@ public boolean breakNaturally(boolean triggerEffect, boolean dropExperience) {
499499

500500
@Override
501501
public boolean breakNaturally(ItemStack item, boolean triggerEffect, boolean dropExperience) {
502+
return this.breakNaturally(item, triggerEffect, dropExperience, false);
503+
}
504+
505+
@Override
506+
public boolean breakNaturally(ItemStack item, boolean triggerEffect, boolean dropExperience, boolean forceEffect) {
502507
// Paper end
503508
// Order matters here, need to drop before setting to air so skulls can get their data
504509
net.minecraft.world.level.block.state.BlockState state = this.getNMS();
@@ -510,18 +515,19 @@ public boolean breakNaturally(ItemStack item, boolean triggerEffect, boolean dro
510515
if (block != Blocks.AIR && (item == null || !state.requiresCorrectToolForDrops() || nmsItem.isCorrectToolForDrops(state))) {
511516
net.minecraft.world.level.block.Block.dropResources(state, this.world.getMinecraftWorld(), this.position, this.world.getBlockEntity(this.position), null, nmsItem, false); // Paper - Properly handle xp dropping
512517
// Paper start - improve Block#breakNaturally
513-
if (triggerEffect) {
514-
if (state.getBlock() instanceof net.minecraft.world.level.block.BaseFireBlock) {
515-
this.world.levelEvent(net.minecraft.world.level.block.LevelEvent.SOUND_EXTINGUISH_FIRE, this.position, 0);
516-
} else {
517-
this.world.levelEvent(net.minecraft.world.level.block.LevelEvent.PARTICLES_DESTROY_BLOCK, this.position, net.minecraft.world.level.block.Block.getId(state));
518-
}
519-
}
520518
if (dropExperience) block.popExperience(this.world.getMinecraftWorld(), this.position, block.getExpDrop(state, this.world.getMinecraftWorld(), this.position, nmsItem, true));
521519
// Paper end
522520
result = true;
523521
}
524522

523+
if ((result && triggerEffect) || (forceEffect && block != Blocks.AIR)) {
524+
if (state.getBlock() instanceof net.minecraft.world.level.block.BaseFireBlock) {
525+
this.world.levelEvent(net.minecraft.world.level.block.LevelEvent.SOUND_EXTINGUISH_FIRE, this.position, 0);
526+
} else {
527+
this.world.levelEvent(net.minecraft.world.level.block.LevelEvent.PARTICLES_DESTROY_BLOCK, this.position, net.minecraft.world.level.block.Block.getId(state));
528+
}
529+
}
530+
525531
// SPIGOT-6778: Directly call setBlock instead of setBlockState, so that the block entity is not removed and custom remove logic is run.
526532
// Paper start - improve breakNaturally
527533
boolean destroyed = this.world.removeBlock(this.position, false);

0 commit comments

Comments
 (0)