diff --git a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java index 8c2aa3558..e64c274c5 100644 --- a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java @@ -57,16 +57,12 @@ public final class PlayerDiggingListener { final Block block = instance.getBlock(blockPosition); final GameMode gameMode = player.getGameMode(); - if (gameMode == GameMode.SPECTATOR) { - // Spectators can't break blocks + // Prevent spectators and check players in adventure mode + if (shouldPreventBreaking(player, block)) { return new DiggingResult(block, false); - } else if (gameMode == GameMode.ADVENTURE) { - // Check if the item can break the block with the current item - final ItemStack itemInMainHand = player.getItemInMainHand(); - if (!itemInMainHand.meta().getCanDestroy().contains(block)) { - return new DiggingResult(block, false); - } - } else if (gameMode == GameMode.CREATIVE) { + } + + if (gameMode == GameMode.CREATIVE) { return breakBlock(instance, player, blockPosition, block); } @@ -89,10 +85,28 @@ public final class PlayerDiggingListener { private static DiggingResult finishDigging(Player player, Instance instance, Point blockPosition) { final Block block = instance.getBlock(blockPosition); - // TODO sanity check + + if (shouldPreventBreaking(player, block)) { + return new DiggingResult(block, false); + } + return breakBlock(instance, player, blockPosition, block); } + private static boolean shouldPreventBreaking(@NotNull Player player, Block block) { + if (player.getGameMode() == GameMode.SPECTATOR) { + // Spectators can't break blocks + return true; + } else if (player.getGameMode() == GameMode.ADVENTURE) { + // Check if the item can break the block with the current item + final ItemStack itemInMainHand = player.getItemInMainHand(); + if (!itemInMainHand.meta().getCanDestroy().contains(block)) { + return true; + } + } + return false; + } + private static void dropStack(Player player) { final ItemStack droppedItemStack = player.getInventory().getItemInMainHand(); dropItem(player, droppedItemStack, ItemStack.AIR);