Add BlockBreakBlockEvent

This commit is contained in:
Jake Potrebic 2021-01-03 17:58:11 -08:00
parent a6362921cc
commit b96b8f7723
4 changed files with 82 additions and 11 deletions

View file

@ -1,6 +1,33 @@
--- a/net/minecraft/world/level/block/Block.java
+++ b/net/minecraft/world/level/block/Block.java
@@ -340,7 +340,13 @@
@@ -292,8 +292,26 @@
});
state.spawnAfterBreak((ServerLevel) world, pos, ItemStack.EMPTY, true);
}
+
+ }
+ // Paper start - Add BlockBreakBlockEvent
+ public static boolean dropResources(BlockState state, LevelAccessor levelAccessor, BlockPos pos, @Nullable BlockEntity blockEntity, BlockPos source) {
+ if (levelAccessor instanceof ServerLevel serverLevel) {
+ List<org.bukkit.inventory.ItemStack> items = new java.util.ArrayList<>();
+ for (ItemStack drop : Block.getDrops(state, serverLevel, pos, blockEntity)) {
+ items.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(drop));
+ }
+ io.papermc.paper.event.block.BlockBreakBlockEvent event = new io.papermc.paper.event.block.BlockBreakBlockEvent(org.bukkit.craftbukkit.block.CraftBlock.at(levelAccessor, pos), org.bukkit.craftbukkit.block.CraftBlock.at(levelAccessor, source), items);
+ event.callEvent();
+ for (org.bukkit.inventory.ItemStack drop : event.getDrops()) {
+ popResource(serverLevel, pos, org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(drop));
+ }
+ state.spawnAfterBreak(serverLevel, pos, ItemStack.EMPTY, true);
+ }
+ return true;
}
+ // Paper end - Add BlockBreakBlockEvent
public static void dropResources(BlockState state, Level world, BlockPos pos, @Nullable BlockEntity blockEntity, @Nullable Entity entity, ItemStack tool) {
if (world instanceof ServerLevel) {
@@ -340,7 +358,13 @@
ItemEntity entityitem = (ItemEntity) itemEntitySupplier.get();
entityitem.setDefaultPickUpDelay();
@ -15,7 +42,7 @@
return;
}
}
@@ -348,8 +354,13 @@
@@ -348,8 +372,13 @@
}
public void popExperience(ServerLevel world, BlockPos pos, int size) {
@ -30,7 +57,7 @@
}
}
@@ -369,7 +380,7 @@
@@ -369,7 +398,7 @@
public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) {
player.awardStat(Stats.BLOCK_MINED.get(this));
@ -39,7 +66,7 @@
Block.dropResources(state, world, pos, blockEntity, player, tool);
}
@@ -490,15 +501,35 @@
@@ -490,15 +519,35 @@
return this.builtInRegistryHolder;
}

View file

@ -14,16 +14,17 @@
public class PistonBaseBlock extends DirectionalBlock {
@@ -155,6 +162,18 @@
@@ -154,7 +161,19 @@
}
}
}
+
+ // CraftBukkit start
+ if (!this.isSticky) {
+ org.bukkit.block.Block block = world.getWorld().getBlockAt(pos.getX(), pos.getY(), pos.getZ());
+ BlockPistonRetractEvent event = new BlockPistonRetractEvent(block, ImmutableList.<org.bukkit.block.Block>of(), CraftBlock.notchToBlockFace(enumdirection));
+ world.getCraftServer().getPluginManager().callEvent(event);
+
+ if (event.isCancelled()) {
+ return;
+ }
@ -83,6 +84,15 @@
BlockPos blockposition3;
int j;
BlockState iblockdata1;
@@ -345,7 +406,7 @@
iblockdata1 = world.getBlockState(blockposition3);
BlockEntity tileentity = iblockdata1.hasBlockEntity() ? world.getBlockEntity(blockposition3) : null;
- dropResources(iblockdata1, world, blockposition3, tileentity);
+ dropResources(iblockdata1, world, blockposition3, tileentity, pos); // Paper - Add BlockBreakBlockEvent
world.setBlock(blockposition3, Blocks.AIR.defaultBlockState(), 18);
world.gameEvent((Holder) GameEvent.BLOCK_DESTROY, blockposition3, GameEvent.Context.of(iblockdata1));
if (!iblockdata1.is(BlockTags.FIRE)) {
@@ -358,13 +419,25 @@
BlockState iblockdata2;

View file

@ -62,7 +62,24 @@
FluidState fluid = iblockdata1.getFluidState();
if (fluid.getType().isSame(this) && FlowingFluid.canPassThroughWall(enumdirection, world, pos, state, blockposition_mutableblockposition1, iblockdata1)) {
@@ -306,7 +335,8 @@
@@ -287,7 +316,7 @@
ifluidcontainer.placeLiquid(world, pos, state, fluidState);
} else {
if (!state.isAir()) {
- this.beforeDestroyingBlock(world, pos, state);
+ this.beforeDestroyingBlock(world, pos, state, pos.relative(direction.getOpposite())); // Paper - Add BlockBreakBlockEvent
}
world.setBlock(pos, fluidState.createLegacyBlock(), 3);
@@ -295,6 +324,7 @@
}
+ protected void beforeDestroyingBlock(LevelAccessor world, BlockPos pos, BlockState state, BlockPos source) { beforeDestroyingBlock(world, pos, state); } // Paper - Add BlockBreakBlockEvent
protected abstract void beforeDestroyingBlock(LevelAccessor world, BlockPos pos, BlockState state);
protected int getSlopeDistance(LevelReader world, BlockPos pos, int i, Direction direction, BlockState state, FlowingFluid.SpreadContext spreadCache) {
@@ -306,7 +336,8 @@
if (enumdirection1 != direction) {
BlockPos blockposition1 = pos.relative(enumdirection1);
@ -72,7 +89,7 @@
FluidState fluid = iblockdata1.getFluidState();
if (this.canPassThrough(world, this.getFlowing(), pos, state, enumdirection1, blockposition1, iblockdata1, fluid)) {
@@ -372,7 +402,8 @@
@@ -372,7 +403,8 @@
while (iterator.hasNext()) {
Direction enumdirection = (Direction) iterator.next();
BlockPos blockposition1 = pos.relative(enumdirection);
@ -82,7 +99,7 @@
FluidState fluid = iblockdata1.getFluidState();
if (this.canMaybePassThrough(world, pos, state, enumdirection, blockposition1, iblockdata1, fluid)) {
@@ -444,10 +475,24 @@
@@ -444,10 +476,24 @@
if (fluid1.isEmpty()) {
fluidState = fluid1;
blockState = Blocks.AIR.defaultBlockState();
@ -107,7 +124,7 @@
world.setBlock(pos, blockState, 3);
world.scheduleTick(pos, fluid1.getType(), i);
}
@@ -524,12 +569,27 @@
@@ -524,12 +570,27 @@
public BlockState getBlockState(BlockPos pos) {
return this.getBlockState(pos, this.getCacheKey(pos));
}

View file

@ -0,0 +1,17 @@
--- a/net/minecraft/world/level/material/WaterFluid.java
+++ b/net/minecraft/world/level/material/WaterFluid.java
@@ -81,7 +81,14 @@
return world.getGameRules().getBoolean(GameRules.RULE_WATER_SOURCE_CONVERSION);
}
+ // Paper start - Add BlockBreakBlockEvent
@Override
+ protected void beforeDestroyingBlock(LevelAccessor world, BlockPos pos, BlockState state, BlockPos source) {
+ BlockEntity tileentity = state.hasBlockEntity() ? world.getBlockEntity(pos) : null;
+ Block.dropResources(state, world, pos, tileentity, source);
+ }
+ // Paper end - Add BlockBreakBlockEvent
+ @Override
protected void beforeDestroyingBlock(LevelAccessor world, BlockPos pos, BlockState state) {
BlockEntity blockEntity = state.hasBlockEntity() ? world.getBlockEntity(pos) : null;
Block.dropResources(state, world, pos, blockEntity);