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
This commit is contained in:
Not Flamgop 2023-05-12 02:15:49 -07:00 committed by GitHub
parent 2c3e38b178
commit 01ffec6614
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 117 additions and 3 deletions

View File

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

View File

@ -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
* <p>
* 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;
}
}

View File

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