Check for adventure mode when finishing to break a block (#948) (#949)

This commit is contained in:
Weiiswurst 2022-04-19 16:52:56 +02:00 committed by GitHub
parent f0fa81b712
commit 21e6ed4918
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);