From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Cryptite Date: Fri, 12 Aug 2022 09:54:06 -0500 Subject: [PATCH] Add BlockDestroyedByNeighborEvent diff --git a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java index 5a60f5dc202c44b06ca34e9a19d45cb715f74fd3..99c7fa99cf25a090859c276301752dcc94a446b4 100644 --- a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java +++ b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java @@ -410,6 +410,7 @@ public class ServerPlayerGameMode { org.bukkit.block.BlockState state = bblock.getState(); level.captureDrops = new ArrayList<>(); // CraftBukkit end + level.pendingPlayerBlockEvents.put(pos, new Level.PendingBlockEvent(pos, this.player)); // Paper block.playerWillDestroy(this.level, pos, iblockdata, this.player); boolean flag = this.level.removeBlock(pos, false); @@ -438,6 +439,7 @@ public class ServerPlayerGameMode { // CraftBukkit start java.util.List itemsToDrop = level.captureDrops; // Paper - store current list level.captureDrops = null; // Paper - Remove this earlier so that we can actually drop stuff + level.pendingPlayerBlockEvents.remove(pos); // Paper if (event.isDropItems()) { org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockDropItemEvent(bblock, state, this.player, itemsToDrop); // Paper - use stored ref } diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java index c18a0bc94d0210396046f4475e49a739088593f3..03f057134831bd119bb8dd820d4a0868b8f90b31 100644 --- a/src/main/java/net/minecraft/world/item/ItemStack.java +++ b/src/main/java/net/minecraft/world/item/ItemStack.java @@ -345,6 +345,7 @@ public final class ItemStack { } } Item item = this.getItem(); + if (entityhuman != null) world.pendingPlayerBlockEvents.put(blockposition, new Level.PendingBlockEvent(blockposition, entityhuman)); // Paper InteractionResult enuminteractionresult = item.useOn(itemactioncontext); CompoundTag newData = this.getTagClone(); int newCount = this.getCount(); diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java index f9117486a51a1456842b03b2d2f2ee72cf1bc297..5084cd803c27519115dadb298c46b98ad20059ef 100644 --- a/src/main/java/net/minecraft/world/level/Level.java +++ b/src/main/java/net/minecraft/world/level/Level.java @@ -182,6 +182,27 @@ public abstract class Level implements LevelAccessor, AutoCloseable { public final Map explosionDensityCache = new HashMap<>(); // Paper - Optimize explosions public java.util.ArrayDeque redstoneUpdateInfos; // Paper - Move from Map in BlockRedstoneTorch to here + // Paper start - Holder class used to track what Player is responsible the last block event + public static class PendingBlockEvent { + + public final BlockPos block; + public final Player player; + public @Nullable BlockPos sourceBlock; + + public PendingBlockEvent(BlockPos block, Player player) { + this(block, player, null); + } + + public PendingBlockEvent(BlockPos block, Player player, @Nullable BlockPos sourceBlock) { + this.block = block; + this.player = player; + this.sourceBlock = sourceBlock; + } + + } + public final Map pendingPlayerBlockEvents = new HashMap<>(); + // Paper end + // Paper start - fix and optimise world upgrading // copied from below public static ResourceKey getDimensionKey(DimensionType manager) { @@ -664,6 +685,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable { if (!this.preventPoiUpdated) { this.onBlockStateChange(blockposition, iblockdata1, iblockdata2); } + pendingPlayerBlockEvents.remove(blockposition); // Paper // CraftBukkit end } } @@ -685,6 +707,17 @@ public abstract class Level implements LevelAccessor, AutoCloseable { if (iblockdata.isAir()) { return false; } else { + // Paper start + PendingBlockEvent blockEvent = pendingPlayerBlockEvents.get(pos); + if (blockEvent != null && blockEvent.sourceBlock != null) { + io.papermc.paper.event.block.BlockDestroyedByNeighborEvent event = + new io.papermc.paper.event.block.BlockDestroyedByNeighborEvent(org.bukkit.craftbukkit.block.CraftBlock.at(this, pos), + (org.bukkit.entity.Player) blockEvent.player.getBukkitEntity(), + org.bukkit.craftbukkit.block.CraftBlock.at(this, blockEvent.sourceBlock)); + event.callEvent(); + } + // Paper end + FluidState fluid = this.getFluidState(pos); // Paper start - while the above setAir method is named same and looks very similar // they are NOT used with same intent and the above should not fire this event. The above method is more of a BlockSetToAirEvent, diff --git a/src/main/java/net/minecraft/world/level/block/DoublePlantBlock.java b/src/main/java/net/minecraft/world/level/block/DoublePlantBlock.java index fa97966d39f01301a8ba976c02dc697e0a74bfb1..3f0cbdb4294f3fc1b953d7baa7903d2e5471b337 100644 --- a/src/main/java/net/minecraft/world/level/block/DoublePlantBlock.java +++ b/src/main/java/net/minecraft/world/level/block/DoublePlantBlock.java @@ -103,6 +103,15 @@ public class DoublePlantBlock extends BushBlock { BlockPos blockposition1 = pos.below(); BlockState iblockdata1 = world.getBlockState(blockposition1); + Level.PendingBlockEvent blockEvent = world.pendingPlayerBlockEvents.remove(pos); + if (blockEvent != null) { + io.papermc.paper.event.block.BlockDestroyedByNeighborEvent event = + new io.papermc.paper.event.block.BlockDestroyedByNeighborEvent(org.bukkit.craftbukkit.block.CraftBlock.at(world, blockposition1), + (org.bukkit.entity.Player) blockEvent.player.getBukkitEntity(), + org.bukkit.craftbukkit.block.CraftBlock.at(world, pos)); + if (!event.callEvent()) return; + } + if (iblockdata1.is(state.getBlock()) && iblockdata1.getValue(DoublePlantBlock.HALF) == DoubleBlockHalf.LOWER) { BlockState iblockdata2 = iblockdata1.hasProperty(BlockStateProperties.WATERLOGGED) && (Boolean) iblockdata1.getValue(BlockStateProperties.WATERLOGGED) ? Blocks.WATER.defaultBlockState() : Blocks.AIR.defaultBlockState(); diff --git a/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java b/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java index fd74cc9c0dab84b176f7da3fbbbdbc8fd3a7e26d..94799f653f2888847ffe8733ae0eefe60143e6ca 100644 --- a/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java +++ b/src/main/java/net/minecraft/world/level/block/state/BlockBehaviour.java @@ -988,6 +988,16 @@ public abstract class BlockBehaviour { Direction enumdirection = aenumdirection[l]; blockposition_mutableblockposition.setWithOffset(pos, enumdirection); + // Paper start - Propagate the PendingBlockEvent from the current blockPos to the next block + // if it will break (!canSurvive) + if (this.getMaterial() == Material.AIR && !world.getBlockState(blockposition_mutableblockposition).canSurvive(world, blockposition_mutableblockposition)) { + Level.PendingBlockEvent blockEvent = ((Level) world).pendingPlayerBlockEvents.get(pos); + if (blockEvent != null) { + BlockPos blockPosCopy = blockposition_mutableblockposition.immutable(); + ((Level) world).pendingPlayerBlockEvents.put(blockPosCopy, new Level.PendingBlockEvent(blockPosCopy, blockEvent.player, pos)); + } + } + // Paper end world.neighborShapeChanged(enumdirection.getOpposite(), this.asState(), blockposition_mutableblockposition, pos, flags, maxUpdateDepth); }