From 01ffec6614db1a8fd334058ceb7578b0e9cb47dd Mon Sep 17 00:00:00 2001 From: Not Flamgop <76978462+Flopgop@users.noreply.github.com> Date: Fri, 12 May 2023 02:15:49 -0700 Subject: [PATCH] Add Cancel and Finish digging events (#1846) * Add Cancel and Finish digging events * Add a function for setting the block which was dug in PlayerFinishDiggingEvent.java --- .../player/PlayerCancelDiggingEvent.java | 46 ++++++++++++++ .../player/PlayerFinishDiggingEvent.java | 61 +++++++++++++++++++ .../listener/PlayerDiggingListener.java | 13 +++- 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/minestom/server/event/player/PlayerCancelDiggingEvent.java create mode 100644 src/main/java/net/minestom/server/event/player/PlayerFinishDiggingEvent.java diff --git a/src/main/java/net/minestom/server/event/player/PlayerCancelDiggingEvent.java b/src/main/java/net/minestom/server/event/player/PlayerCancelDiggingEvent.java new file mode 100644 index 000000000..0adf6a0f4 --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/PlayerCancelDiggingEvent.java @@ -0,0 +1,46 @@ +package net.minestom.server.event.player; + +import net.minestom.server.coordinate.Point; +import net.minestom.server.entity.Player; +import net.minestom.server.event.trait.BlockEvent; +import net.minestom.server.event.trait.PlayerInstanceEvent; +import net.minestom.server.instance.block.Block; +import org.jetbrains.annotations.NotNull; + +/** + * Called when a {@link Player} stops digging a block before it is broken + */ +public class PlayerCancelDiggingEvent implements PlayerInstanceEvent, BlockEvent { + private final Player player; + private final Block block; + private final Point blockPosition; + + public PlayerCancelDiggingEvent(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition) { + this.player = player; + this.block = block; + this.blockPosition = blockPosition; + } + + /** + * Gets the block which was being dug. + * + * @return the block + */ + @Override + public @NotNull Block getBlock() { + return block; + } + + /** + * Gets the block position. + * + * @return the block position + */ + public @NotNull Point getBlockPosition() { + return blockPosition; + } + @Override + public @NotNull Player getPlayer() { + return player; + } +} diff --git a/src/main/java/net/minestom/server/event/player/PlayerFinishDiggingEvent.java b/src/main/java/net/minestom/server/event/player/PlayerFinishDiggingEvent.java new file mode 100644 index 000000000..2ded2723a --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/PlayerFinishDiggingEvent.java @@ -0,0 +1,61 @@ +package net.minestom.server.event.player; + +import net.minestom.server.coordinate.Point; +import net.minestom.server.entity.Player; +import net.minestom.server.event.trait.BlockEvent; +import net.minestom.server.event.trait.PlayerInstanceEvent; +import net.minestom.server.instance.block.Block; +import org.jetbrains.annotations.NotNull; + +/** + * Called when a {@link Player} successfully finishes digging a block + */ +public class PlayerFinishDiggingEvent implements PlayerInstanceEvent, BlockEvent { + private final Player player; + private @NotNull Block block; + private final Point blockPosition; + + public PlayerFinishDiggingEvent(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition) { + this.player = player; + this.block = block; + this.blockPosition = blockPosition; + } + + /** + * Changes which block was dug + *
+ * This has somewhat odd behavior; + * If you set it from a previously solid block to a non-solid block + * then cancel the respective {@link PlayerBlockBreakEvent} + * it will allow the player to phase through the block and into the floor + * (only if the player is standing on top of the block) + * + * @param block the block to set the result to + */ + public void setBlock(@NotNull Block block) { + this.block = block; + } + + /** + * Gets the block which was dug. + * + * @return the block + */ + @Override + public @NotNull Block getBlock() { + return block; + } + + /** + * Gets the block position. + * + * @return the block position + */ + public @NotNull Point getBlockPosition() { + return blockPosition; + } + @Override + public @NotNull Player getPlayer() { + return player; + } +} diff --git a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java index 56e24b2c3..fbdfe21c2 100644 --- a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java @@ -7,6 +7,8 @@ import net.minestom.server.entity.Player; import net.minestom.server.entity.metadata.PlayerMeta; import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.item.ItemUpdateStateEvent; +import net.minestom.server.event.player.PlayerCancelDiggingEvent; +import net.minestom.server.event.player.PlayerFinishDiggingEvent; import net.minestom.server.event.player.PlayerStartDiggingEvent; import net.minestom.server.event.player.PlayerSwapItemEvent; import net.minestom.server.instance.Instance; @@ -33,7 +35,7 @@ public final class PlayerDiggingListener { diggingResult = startDigging(player, instance, blockPosition, packet.blockFace()); } else if (status == ClientPlayerDiggingPacket.Status.CANCELLED_DIGGING) { if (!instance.isChunkLoaded(blockPosition)) return; - diggingResult = cancelDigging(instance, blockPosition); + diggingResult = cancelDigging(player, instance, blockPosition); } else if (status == ClientPlayerDiggingPacket.Status.FINISHED_DIGGING) { if (!instance.isChunkLoaded(blockPosition)) return; diggingResult = finishDigging(player, instance, blockPosition, packet.blockFace()); @@ -77,8 +79,10 @@ public final class PlayerDiggingListener { return breakBlock(instance, player, blockPosition, block, blockFace); } - private static DiggingResult cancelDigging(Instance instance, Point blockPosition) { + private static DiggingResult cancelDigging(Player player, Instance instance, Point blockPosition) { final Block block = instance.getBlock(blockPosition); + PlayerCancelDiggingEvent playerCancelDiggingEvent = new PlayerCancelDiggingEvent(player, block, blockPosition); + EventDispatcher.call(playerCancelDiggingEvent); return new DiggingResult(block, true); } @@ -89,7 +93,10 @@ public final class PlayerDiggingListener { return new DiggingResult(block, false); } - return breakBlock(instance, player, blockPosition, block, blockFace); + PlayerFinishDiggingEvent playerFinishDiggingEvent = new PlayerFinishDiggingEvent(player, block, blockPosition); + EventDispatcher.call(playerFinishDiggingEvent); + + return breakBlock(instance, player, blockPosition, playerFinishDiggingEvent.getBlock(), blockFace); } private static boolean shouldPreventBreaking(@NotNull Player player, Block block) {