diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index 1f2038528..bc14f37e4 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -196,6 +196,15 @@ public class PlayerInit { BelowNameScoreboard belowNameScoreboard = new BelowNameScoreboard(); setBelowNameScoreboard(belowNameScoreboard); belowNameScoreboard.updateScore(this, 50);*/ + + player.addEventCallback(PlayerUseItemEvent.class, useEvent -> { + player.sendMessage("Using item in air: "+useEvent.getItemStack().getMaterial()); + }); + + player.addEventCallback(PlayerUseItemOnBlockEvent.class, useEvent -> { + player.sendMessage("Main item: "+player.getInventory().getItemInMainHand().getMaterial()); + player.sendMessage("Using item on block: "+useEvent.getItemStack().getMaterial()+" at "+useEvent.getPosition()+" on face "+useEvent.getBlockFace()); + }); }); }); } diff --git a/src/main/java/net/minestom/server/event/PlayerUseItemEvent.java b/src/main/java/net/minestom/server/event/PlayerUseItemEvent.java index bdd73fae9..4e1f3b7e1 100644 --- a/src/main/java/net/minestom/server/event/PlayerUseItemEvent.java +++ b/src/main/java/net/minestom/server/event/PlayerUseItemEvent.java @@ -3,6 +3,9 @@ package net.minestom.server.event; import net.minestom.server.entity.Player; import net.minestom.server.item.ItemStack; +/** + * Event when an item is used without clicking a block + */ public class PlayerUseItemEvent extends CancellableEvent { private Player.Hand hand; diff --git a/src/main/java/net/minestom/server/event/PlayerUseItemOnBlockEvent.java b/src/main/java/net/minestom/server/event/PlayerUseItemOnBlockEvent.java new file mode 100644 index 000000000..865b28841 --- /dev/null +++ b/src/main/java/net/minestom/server/event/PlayerUseItemOnBlockEvent.java @@ -0,0 +1,40 @@ +package net.minestom.server.event; + +import net.minestom.server.entity.Player; +import net.minestom.server.item.ItemStack; +import net.minestom.server.utils.BlockPosition; +import net.minestom.server.utils.Direction; + +/** + * Used when a player is clicking a block with an item (but is not a block in item form) + */ +public class PlayerUseItemOnBlockEvent extends Event { + + private Player.Hand hand; + private ItemStack itemStack; + private final BlockPosition position; + private final Direction blockFace; + + public PlayerUseItemOnBlockEvent(Player.Hand hand, ItemStack itemStack, BlockPosition position, Direction blockFace) { + this.hand = hand; + this.itemStack = itemStack; + this.position = position; + this.blockFace = blockFace; + } + + public BlockPosition getPosition() { + return position; + } + + public Direction getBlockFace() { + return blockFace; + } + + public Player.Hand getHand() { + return hand; + } + + public ItemStack getItemStack() { + return itemStack; + } +} diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index e06bafb82..ca22b898a 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -7,6 +7,7 @@ import net.minestom.server.entity.GameMode; import net.minestom.server.entity.Player; import net.minestom.server.event.PlayerBlockInteractEvent; import net.minestom.server.event.PlayerBlockPlaceEvent; +import net.minestom.server.event.PlayerUseItemOnBlockEvent; import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; import net.minestom.server.instance.block.Block; @@ -56,7 +57,7 @@ public class BlockPlacementListener { // Check if item at hand is a block ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand(); Material material = Material.fromId(usedItem.getMaterialId()); - if (material != null && !material.isBlock()) { + if(material == Material.AIR) { return; } @@ -122,6 +123,8 @@ public class BlockPlacementListener { refreshChunk = true; } } else { + PlayerUseItemOnBlockEvent event = new PlayerUseItemOnBlockEvent(hand, usedItem, blockPosition, blockFace.toDirection()); + player.callEvent(PlayerUseItemOnBlockEvent.class, event); refreshChunk = true; } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java index 6d14c685e..460576810 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java @@ -3,6 +3,7 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.network.packet.PacketReader; import net.minestom.server.network.packet.client.ClientPlayPacket; import net.minestom.server.utils.BlockPosition; +import net.minestom.server.utils.Direction; public class ClientPlayerDiggingPacket extends ClientPlayPacket { @@ -28,12 +29,22 @@ public class ClientPlayerDiggingPacket extends ClientPlayPacket { } public enum BlockFace { - BOTTOM, - TOP, - NORTH, - SOUTH, - WEST, - EAST + BOTTOM(Direction.DOWN), + TOP(Direction.UP), + NORTH(Direction.NORTH), + SOUTH(Direction.SOUTH), + WEST(Direction.WEST), + EAST(Direction.EAST); + + private final Direction direction; + + BlockFace(Direction direction) { + this.direction = direction; + } + + public Direction toDirection() { + return direction; + } } }