Add BlockHandler.PlayerPlacement#getHand

This commit is contained in:
TheMode 2021-08-30 15:52:07 +02:00
parent ae04ca5574
commit e99d8c6a6f
5 changed files with 17 additions and 14 deletions

View File

@ -139,8 +139,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable, Ta
} }
@ApiStatus.Internal @ApiStatus.Internal
public abstract boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition, public abstract boolean placeBlock(@NotNull BlockHandler.Placement placement);
@NotNull BlockFace blockFace, float cursorX, float cursorY, float cursorZ);
/** /**
* Does call {@link net.minestom.server.event.player.PlayerBlockBreakEvent} * Does call {@link net.minestom.server.event.player.PlayerBlockBreakEvent}

View File

@ -13,7 +13,6 @@ import net.minestom.server.event.instance.InstanceChunkUnloadEvent;
import net.minestom.server.event.player.PlayerBlockBreakEvent; import net.minestom.server.event.player.PlayerBlockBreakEvent;
import net.minestom.server.instance.batch.ChunkGenerationBatch; import net.minestom.server.instance.batch.ChunkGenerationBatch;
import net.minestom.server.instance.block.Block; 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.BlockHandler;
import net.minestom.server.instance.block.rule.BlockPlacementRule; import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.network.packet.server.play.BlockChangePacket; import net.minestom.server.network.packet.server.play.BlockChangePacket;
@ -154,12 +153,12 @@ public class InstanceContainer extends Instance {
} }
@Override @Override
public boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition, public boolean placeBlock(@NotNull BlockHandler.Placement placement) {
@NotNull BlockFace blockFace, float cursorX, float cursorY, float cursorZ) { final Point blockPosition = placement.getBlockPosition();
final Chunk chunk = getChunkAt(blockPosition); final Chunk chunk = getChunkAt(blockPosition);
if (!ChunkUtils.isLoaded(chunk)) return false; if (!ChunkUtils.isLoaded(chunk)) return false;
UNSAFE_setBlock(chunk, blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), block, UNSAFE_setBlock(chunk, blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(),
new BlockHandler.PlayerPlacement(block, this, blockPosition, player, blockFace, cursorX, cursorY, cursorZ), null); placement.getBlock(), placement, null);
return true; return true;
} }

View File

@ -3,7 +3,7 @@ package net.minestom.server.instance;
import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Point;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.instance.block.Block; 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 org.jetbrains.annotations.NotNull;
import java.util.Collection; import java.util.Collection;
@ -28,9 +28,8 @@ public class SharedInstance extends Instance {
} }
@Override @Override
public boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition, public boolean placeBlock(@NotNull BlockHandler.Placement placement) {
@NotNull BlockFace blockFace, float cursorX, float cursorY, float cursorZ) { return instanceContainer.placeBlock(placement);
return instanceContainer.placeBlock(player, block, blockPosition, blockFace, cursorX, cursorY, cursorZ);
} }
@Override @Override

View File

@ -114,14 +114,16 @@ public interface BlockHandler {
final class PlayerPlacement extends Placement { final class PlayerPlacement extends Placement {
private final Player player; private final Player player;
private final Player.Hand hand;
private final BlockFace blockFace; private final BlockFace blockFace;
private final float cursorX, cursorY, cursorZ; private final float cursorX, cursorY, cursorZ;
@ApiStatus.Internal @ApiStatus.Internal
public PlayerPlacement(Block block, Instance instance, Point blockPosition, 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); super(block, instance, blockPosition);
this.player = player; this.player = player;
this.hand = hand;
this.blockFace = blockFace; this.blockFace = blockFace;
this.cursorX = cursorX; this.cursorX = cursorX;
this.cursorY = cursorY; this.cursorY = cursorY;
@ -132,6 +134,10 @@ public interface BlockHandler {
return player; return player;
} }
public @NotNull Player.Hand getHand() {
return hand;
}
public @NotNull BlockFace getBlockFace() { public @NotNull BlockFace getBlockFace() {
return blockFace; return blockFace;
} }

View File

@ -157,8 +157,8 @@ public class BlockPlacementListener {
return; return;
} }
// Place the block // Place the block
instance.placeBlock(player, resultBlock, placementPosition, instance.placeBlock(new BlockHandler.PlayerPlacement(resultBlock, instance, blockPosition, player, hand, blockFace,
blockFace, packet.cursorPositionX, packet.cursorPositionY, packet.cursorPositionZ); packet.cursorPositionX, packet.cursorPositionY, packet.cursorPositionZ));
// Block consuming // Block consuming
if (playerBlockPlaceEvent.doesConsumeBlock()) { if (playerBlockPlaceEvent.doesConsumeBlock()) {
// Consume the block in the player's hand // Consume the block in the player's hand