From 7c5bb7126a4d8e23c5c2505b67627027599160a5 Mon Sep 17 00:00:00 2001 From: themode Date: Thu, 7 Jan 2021 16:12:56 +0100 Subject: [PATCH] Added PlayerBlockPlaceEvent#setBlockData --- .../event/player/PlayerBlockPlaceEvent.java | 25 +++++++++++++++++++ .../listener/BlockPlacementListener.java | 8 +++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/event/player/PlayerBlockPlaceEvent.java b/src/main/java/net/minestom/server/event/player/PlayerBlockPlaceEvent.java index 4d266aa51..2651b92d5 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerBlockPlaceEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerBlockPlaceEvent.java @@ -1,6 +1,7 @@ package net.minestom.server.event.player; import net.minestom.server.MinecraftServer; +import net.minestom.server.data.Data; import net.minestom.server.entity.Player; import net.minestom.server.event.CancellableEvent; import net.minestom.server.event.PlayerEvent; @@ -8,7 +9,9 @@ import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.BlockManager; import net.minestom.server.instance.block.CustomBlock; import net.minestom.server.utils.BlockPosition; +import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Called when a player tries placing a block. @@ -19,6 +22,7 @@ public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEve private short blockStateId; private short customBlockId; + private Data blockData; private final BlockPosition blockPosition; private final Player.Hand hand; @@ -52,6 +56,7 @@ public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEve */ public void setCustomBlock(short customBlockId) { final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); + Check.notNull(customBlock, "The custom block with the id '" + customBlockId + "' does not exist"); setCustomBlock(customBlock); } @@ -62,6 +67,7 @@ public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEve */ public void setCustomBlock(@NotNull String customBlockId) { final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); + Check.notNull(customBlock, "The custom block with the identifier '" + customBlockId + "' does not exist"); setCustomBlock(customBlock); } @@ -104,6 +110,25 @@ public class PlayerBlockPlaceEvent extends PlayerEvent implements CancellableEve this.blockStateId = blockStateId; } + /** + * Gets the data that the (not placed yet) block should have + * + * @return the block data, null if not any + */ + @Nullable + public Data getBlockData() { + return blockData; + } + + /** + * Sets the data of the block to place. + * + * @param blockData the block data, null if not any + */ + public void setBlockData(@Nullable Data blockData) { + this.blockData = blockData; + } + /** * Gets the block position. * diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index 97c4aef40..b14347514 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -133,11 +133,9 @@ public class BlockPlacementListener { // Place the block final short customBlockId = playerBlockPlaceEvent.getCustomBlockId(); - if (customBlockId != 0) { - instance.setSeparateBlocks(blockPosition, blockStateId, customBlockId); - } else { - instance.setBlockStateId(blockPosition, blockStateId); - } + final Data blockData = playerBlockPlaceEvent.getBlockData(); // Possibly null + instance.setSeparateBlocks(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), + blockStateId, customBlockId, blockData); // Block consuming if (playerBlockPlaceEvent.doesConsumeBlock()) {