mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-30 13:08:19 +01:00
New event for when an item is used on a block
This commit is contained in:
parent
cae17a203e
commit
05acfdf59f
@ -196,6 +196,15 @@ public class PlayerInit {
|
|||||||
BelowNameScoreboard belowNameScoreboard = new BelowNameScoreboard();
|
BelowNameScoreboard belowNameScoreboard = new BelowNameScoreboard();
|
||||||
setBelowNameScoreboard(belowNameScoreboard);
|
setBelowNameScoreboard(belowNameScoreboard);
|
||||||
belowNameScoreboard.updateScore(this, 50);*/
|
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());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,9 @@ package net.minestom.server.event;
|
|||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event when an item is used without clicking a block
|
||||||
|
*/
|
||||||
public class PlayerUseItemEvent extends CancellableEvent {
|
public class PlayerUseItemEvent extends CancellableEvent {
|
||||||
|
|
||||||
private Player.Hand hand;
|
private Player.Hand hand;
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,7 @@ import net.minestom.server.entity.GameMode;
|
|||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.event.PlayerBlockInteractEvent;
|
import net.minestom.server.event.PlayerBlockInteractEvent;
|
||||||
import net.minestom.server.event.PlayerBlockPlaceEvent;
|
import net.minestom.server.event.PlayerBlockPlaceEvent;
|
||||||
|
import net.minestom.server.event.PlayerUseItemOnBlockEvent;
|
||||||
import net.minestom.server.instance.Chunk;
|
import net.minestom.server.instance.Chunk;
|
||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
@ -56,7 +57,7 @@ public class BlockPlacementListener {
|
|||||||
// Check if item at hand is a block
|
// Check if item at hand is a block
|
||||||
ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand();
|
ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand();
|
||||||
Material material = Material.fromId(usedItem.getMaterialId());
|
Material material = Material.fromId(usedItem.getMaterialId());
|
||||||
if (material != null && !material.isBlock()) {
|
if(material == Material.AIR) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,6 +123,8 @@ public class BlockPlacementListener {
|
|||||||
refreshChunk = true;
|
refreshChunk = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
PlayerUseItemOnBlockEvent event = new PlayerUseItemOnBlockEvent(hand, usedItem, blockPosition, blockFace.toDirection());
|
||||||
|
player.callEvent(PlayerUseItemOnBlockEvent.class, event);
|
||||||
refreshChunk = true;
|
refreshChunk = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.PacketReader;
|
||||||
import net.minestom.server.network.packet.client.ClientPlayPacket;
|
import net.minestom.server.network.packet.client.ClientPlayPacket;
|
||||||
import net.minestom.server.utils.BlockPosition;
|
import net.minestom.server.utils.BlockPosition;
|
||||||
|
import net.minestom.server.utils.Direction;
|
||||||
|
|
||||||
public class ClientPlayerDiggingPacket extends ClientPlayPacket {
|
public class ClientPlayerDiggingPacket extends ClientPlayPacket {
|
||||||
|
|
||||||
@ -28,12 +29,22 @@ public class ClientPlayerDiggingPacket extends ClientPlayPacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public enum BlockFace {
|
public enum BlockFace {
|
||||||
BOTTOM,
|
BOTTOM(Direction.DOWN),
|
||||||
TOP,
|
TOP(Direction.UP),
|
||||||
NORTH,
|
NORTH(Direction.NORTH),
|
||||||
SOUTH,
|
SOUTH(Direction.SOUTH),
|
||||||
WEST,
|
WEST(Direction.WEST),
|
||||||
EAST
|
EAST(Direction.EAST);
|
||||||
|
|
||||||
|
private final Direction direction;
|
||||||
|
|
||||||
|
BlockFace(Direction direction) {
|
||||||
|
this.direction = direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Direction toDirection() {
|
||||||
|
return direction;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user