Added ItemStack callbacks for interactions

This commit is contained in:
Felix Cravic 2020-08-13 19:00:19 +02:00
parent 55b36624cc
commit f3414224fe
4 changed files with 48 additions and 2 deletions

View File

@ -3,10 +3,13 @@ package net.minestom.server.item;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.data.Data;
import net.minestom.server.data.DataContainer;
import net.minestom.server.entity.Player;
import net.minestom.server.item.attribute.ItemAttribute;
import net.minestom.server.item.metadata.*;
import net.minestom.server.item.rule.VanillaStackingRule;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Direction;
import net.minestom.server.utils.NBTUtils;
import net.minestom.server.utils.validate.Check;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -613,4 +616,37 @@ public class ItemStack implements DataContainer {
}
return compound;
}
// Callback events
/**
* Called when the player right clicks with this item
*
* @param player
* @param hand
*/
public void onRightClick(Player player, Player.Hand hand) {
}
/**
* Called when the player left clicks with this item
*
* @param player
* @param hand
*/
public void onLeftClick(Player player, Player.Hand hand) {
}
/**
* Called when the player right clicks with this item on a block
*
* @param player
* @param hand
* @param position
* @param blockFace
* @return true if it prevents normal item use (placing blocks for instance)
*/
public boolean onUseOnBlock(Player player, Player.Hand hand, BlockPosition position, Direction blockFace) {
return false;
}
}

View File

@ -2,12 +2,15 @@ package net.minestom.server.listener;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerHandAnimationEvent;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.client.play.ClientAnimationPacket;
public class AnimationListener {
public static void animationListener(ClientAnimationPacket packet, Player player) {
final Player.Hand hand = packet.hand;
final ItemStack itemStack = player.getItemInHand(hand);
itemStack.onLeftClick(player, hand);
PlayerHandAnimationEvent handAnimationEvent = new PlayerHandAnimationEvent(player, hand);
player.callCancellableEvent(PlayerHandAnimationEvent.class, handAnimationEvent, () -> {
switch (hand) {

View File

@ -20,6 +20,7 @@ import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.network.packet.client.play.ClientPlayerBlockPlacementPacket;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Direction;
import net.minestom.server.utils.chunk.ChunkUtils;
import java.util.Set;
@ -31,13 +32,19 @@ public class BlockPlacementListener {
final Player.Hand hand = packet.hand;
final BlockFace blockFace = packet.blockFace;
final BlockPosition blockPosition = packet.blockPosition;
final Direction direction = blockFace.toDirection();
final Instance instance = player.getInstance();
if (instance == null)
return;
final ItemStack usedItem = player.getItemInHand(hand);
// Interact at block
final boolean cancel = usedItem.onUseOnBlock(player, hand, blockPosition, direction);
PlayerBlockInteractEvent playerBlockInteractEvent = new PlayerBlockInteractEvent(player, blockPosition, hand, blockFace);
playerBlockInteractEvent.setCancelled(cancel);
playerBlockInteractEvent.setBlockingItemUse(cancel);
player.callCancellableEvent(PlayerBlockInteractEvent.class, playerBlockInteractEvent, () -> {
final CustomBlock customBlock = instance.getCustomBlock(blockPosition);
if (customBlock != null) {
@ -54,7 +61,6 @@ public class BlockPlacementListener {
}
// Check if item at hand is a block
final ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand();
final Material material = usedItem.getMaterial();
if (material == Material.AIR) {
return;
@ -123,7 +129,7 @@ public class BlockPlacementListener {
}
} else {
// Player didn't try to place a block but interacted with one
PlayerUseItemOnBlockEvent event = new PlayerUseItemOnBlockEvent(player, hand, usedItem, blockPosition, blockFace.toDirection());
PlayerUseItemOnBlockEvent event = new PlayerUseItemOnBlockEvent(player, hand, usedItem, blockPosition, direction);
player.callEvent(PlayerUseItemOnBlockEvent.class, event);
refreshChunk = true;
}

View File

@ -16,6 +16,7 @@ public class UseItemListener {
final PlayerInventory inventory = player.getInventory();
final Player.Hand hand = packet.hand;
final ItemStack itemStack = hand == Player.Hand.MAIN ? inventory.getItemInMainHand() : inventory.getItemInOffHand();
itemStack.onRightClick(player, hand);
PlayerUseItemEvent useItemEvent = new PlayerUseItemEvent(player, hand, itemStack);
player.callEvent(PlayerUseItemEvent.class, useItemEvent);