New event for when an item is used on a block

This commit is contained in:
jglrxavpok 2020-05-03 22:44:38 +02:00
parent cae17a203e
commit 05acfdf59f
5 changed files with 73 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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

View File

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