From f3414224fec38d2f5c4900a69f090cd6cf6bdece Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Thu, 13 Aug 2020 19:00:19 +0200 Subject: [PATCH] Added ItemStack callbacks for interactions --- .../net/minestom/server/item/ItemStack.java | 36 +++++++++++++++++++ .../server/listener/AnimationListener.java | 3 ++ .../listener/BlockPlacementListener.java | 10 ++++-- .../server/listener/UseItemListener.java | 1 + 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minestom/server/item/ItemStack.java b/src/main/java/net/minestom/server/item/ItemStack.java index 9cddf7904..ffb17503d 100644 --- a/src/main/java/net/minestom/server/item/ItemStack.java +++ b/src/main/java/net/minestom/server/item/ItemStack.java @@ -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; + } } diff --git a/src/main/java/net/minestom/server/listener/AnimationListener.java b/src/main/java/net/minestom/server/listener/AnimationListener.java index 66cd01a8b..571f2586a 100644 --- a/src/main/java/net/minestom/server/listener/AnimationListener.java +++ b/src/main/java/net/minestom/server/listener/AnimationListener.java @@ -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) { diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index 82e11a3df..2903aae46 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -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; } diff --git a/src/main/java/net/minestom/server/listener/UseItemListener.java b/src/main/java/net/minestom/server/listener/UseItemListener.java index 571470e76..bee605a16 100644 --- a/src/main/java/net/minestom/server/listener/UseItemListener.java +++ b/src/main/java/net/minestom/server/listener/UseItemListener.java @@ -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);