From e99d8c6a6f66dfdd44ee85e65bf1def11392f8fd Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 30 Aug 2021 15:52:07 +0200 Subject: [PATCH] Add BlockHandler.PlayerPlacement#getHand --- src/main/java/net/minestom/server/instance/Instance.java | 3 +-- .../net/minestom/server/instance/InstanceContainer.java | 9 ++++----- .../net/minestom/server/instance/SharedInstance.java | 7 +++---- .../net/minestom/server/instance/block/BlockHandler.java | 8 +++++++- .../minestom/server/listener/BlockPlacementListener.java | 4 ++-- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index 46991b268..4dc1b8623 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -139,8 +139,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta } @ApiStatus.Internal - public abstract boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition, - @NotNull BlockFace blockFace, float cursorX, float cursorY, float cursorZ); + public abstract boolean placeBlock(@NotNull BlockHandler.Placement placement); /** * Does call {@link net.minestom.server.event.player.PlayerBlockBreakEvent} diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index b94cf0012..3a320cf6d 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -13,7 +13,6 @@ import net.minestom.server.event.instance.InstanceChunkUnloadEvent; import net.minestom.server.event.player.PlayerBlockBreakEvent; import net.minestom.server.instance.batch.ChunkGenerationBatch; import net.minestom.server.instance.block.Block; -import net.minestom.server.instance.block.BlockFace; import net.minestom.server.instance.block.BlockHandler; import net.minestom.server.instance.block.rule.BlockPlacementRule; import net.minestom.server.network.packet.server.play.BlockChangePacket; @@ -154,12 +153,12 @@ public class InstanceContainer extends Instance { } @Override - public boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition, - @NotNull BlockFace blockFace, float cursorX, float cursorY, float cursorZ) { + public boolean placeBlock(@NotNull BlockHandler.Placement placement) { + final Point blockPosition = placement.getBlockPosition(); final Chunk chunk = getChunkAt(blockPosition); if (!ChunkUtils.isLoaded(chunk)) return false; - UNSAFE_setBlock(chunk, blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), block, - new BlockHandler.PlayerPlacement(block, this, blockPosition, player, blockFace, cursorX, cursorY, cursorZ), null); + UNSAFE_setBlock(chunk, blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), + placement.getBlock(), placement, null); return true; } diff --git a/src/main/java/net/minestom/server/instance/SharedInstance.java b/src/main/java/net/minestom/server/instance/SharedInstance.java index df2b80391..a4ae6eaaf 100644 --- a/src/main/java/net/minestom/server/instance/SharedInstance.java +++ b/src/main/java/net/minestom/server/instance/SharedInstance.java @@ -3,7 +3,7 @@ package net.minestom.server.instance; import net.minestom.server.coordinate.Point; import net.minestom.server.entity.Player; import net.minestom.server.instance.block.Block; -import net.minestom.server.instance.block.BlockFace; +import net.minestom.server.instance.block.BlockHandler; import org.jetbrains.annotations.NotNull; import java.util.Collection; @@ -28,9 +28,8 @@ public class SharedInstance extends Instance { } @Override - public boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition, - @NotNull BlockFace blockFace, float cursorX, float cursorY, float cursorZ) { - return instanceContainer.placeBlock(player, block, blockPosition, blockFace, cursorX, cursorY, cursorZ); + public boolean placeBlock(@NotNull BlockHandler.Placement placement) { + return instanceContainer.placeBlock(placement); } @Override diff --git a/src/main/java/net/minestom/server/instance/block/BlockHandler.java b/src/main/java/net/minestom/server/instance/block/BlockHandler.java index b07c5f8f4..acd794dc1 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockHandler.java +++ b/src/main/java/net/minestom/server/instance/block/BlockHandler.java @@ -114,14 +114,16 @@ public interface BlockHandler { final class PlayerPlacement extends Placement { private final Player player; + private final Player.Hand hand; private final BlockFace blockFace; private final float cursorX, cursorY, cursorZ; @ApiStatus.Internal public PlayerPlacement(Block block, Instance instance, Point blockPosition, - Player player, BlockFace blockFace, float cursorX, float cursorY, float cursorZ) { + Player player, Player.Hand hand, BlockFace blockFace, float cursorX, float cursorY, float cursorZ) { super(block, instance, blockPosition); this.player = player; + this.hand = hand; this.blockFace = blockFace; this.cursorX = cursorX; this.cursorY = cursorY; @@ -132,6 +134,10 @@ public interface BlockHandler { return player; } + public @NotNull Player.Hand getHand() { + return hand; + } + public @NotNull BlockFace getBlockFace() { return blockFace; } diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index 92ca45854..662575af6 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -157,8 +157,8 @@ public class BlockPlacementListener { return; } // Place the block - instance.placeBlock(player, resultBlock, placementPosition, - blockFace, packet.cursorPositionX, packet.cursorPositionY, packet.cursorPositionZ); + instance.placeBlock(new BlockHandler.PlayerPlacement(resultBlock, instance, blockPosition, player, hand, blockFace, + packet.cursorPositionX, packet.cursorPositionY, packet.cursorPositionZ)); // Block consuming if (playerBlockPlaceEvent.doesConsumeBlock()) { // Consume the block in the player's hand