diff --git a/src/main/java/fr/themode/demo/blocks/StoneBlock.java b/src/main/java/fr/themode/demo/blocks/StoneBlock.java index 821507e65..b5a2d95ef 100644 --- a/src/main/java/fr/themode/demo/blocks/StoneBlock.java +++ b/src/main/java/fr/themode/demo/blocks/StoneBlock.java @@ -25,8 +25,8 @@ public class StoneBlock extends CustomBlock { } @Override - public void onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) { - + public boolean onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) { + return false; } @Override diff --git a/src/main/java/fr/themode/demo/blocks/UpdatableBlockDemo.java b/src/main/java/fr/themode/demo/blocks/UpdatableBlockDemo.java index 7b25074cb..1cfe92945 100644 --- a/src/main/java/fr/themode/demo/blocks/UpdatableBlockDemo.java +++ b/src/main/java/fr/themode/demo/blocks/UpdatableBlockDemo.java @@ -33,8 +33,8 @@ public class UpdatableBlockDemo extends CustomBlock { } @Override - public void onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) { - + public boolean onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data) { + return false; } @Override diff --git a/src/main/java/net/minestom/server/event/PlayerBlockInteractEvent.java b/src/main/java/net/minestom/server/event/PlayerBlockInteractEvent.java index 71d7f3f55..276a97cc6 100644 --- a/src/main/java/net/minestom/server/event/PlayerBlockInteractEvent.java +++ b/src/main/java/net/minestom/server/event/PlayerBlockInteractEvent.java @@ -8,11 +8,25 @@ public class PlayerBlockInteractEvent extends CancellableEvent { private BlockPosition blockPosition; private Player.Hand hand; + /** + * Does this interaction block the normal item use? + * True for containers which open an inventory instead of letting blocks be placed + */ + private boolean blocksItemUse; + public PlayerBlockInteractEvent(BlockPosition blockPosition, Player.Hand hand) { this.blockPosition = blockPosition; this.hand = hand; } + public boolean isBlockingItemUse() { + return blocksItemUse; + } + + public void setBlockingItemUse(boolean blocks) { + this.blocksItemUse = blocks; + } + public BlockPosition getBlockPosition() { return blockPosition; } diff --git a/src/main/java/net/minestom/server/instance/block/CustomBlock.java b/src/main/java/net/minestom/server/instance/block/CustomBlock.java index 55f4a0add..c5ad3b7dd 100644 --- a/src/main/java/net/minestom/server/instance/block/CustomBlock.java +++ b/src/main/java/net/minestom/server/instance/block/CustomBlock.java @@ -33,7 +33,16 @@ public abstract class CustomBlock { public abstract void onDestroy(Instance instance, BlockPosition blockPosition, Data data); - public abstract void onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data); + /** + * Handles interactions with this block. Can also block normal item use (containers should block when opening the + * menu, this prevents the player from placing a block when opening it for instance) + * @param player the player interacting + * @param hand the hand used to interact + * @param blockPosition the position of this block + * @param data the data at this position + * @return true if this block blocks normal item use, false otherwise + */ + public abstract boolean onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data); public abstract UpdateOption getUpdateOption(); diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index 703c3482b..5d781367e 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -42,10 +42,17 @@ public class BlockPlacementListener { CustomBlock customBlock = instance.getCustomBlock(blockPosition); if (customBlock != null) { Data data = instance.getBlockData(blockPosition); - customBlock.onInteract(player, hand, blockPosition, data); + boolean blocksItem = customBlock.onInteract(player, hand, blockPosition, data); + if(blocksItem) { + playerBlockInteractEvent.setBlockingItemUse(true); + } } }); + if(playerBlockInteractEvent.isBlockingItemUse()) { + return; + } + // Check if item at hand is a block ItemStack usedItem = hand == Player.Hand.MAIN ? playerInventory.getItemInMainHand() : playerInventory.getItemInOffHand(); Material material = Material.fromId(usedItem.getMaterialId());