From 438338381e671a9f699014764dc36b7b979986dd Mon Sep 17 00:00:00 2001 From: mworzala Date: Sun, 18 Jun 2023 07:54:50 -0400 Subject: [PATCH] hollow-cube/material-in-replacement-rule --- CHANGELOG.md | 1 + .../block/rule/BlockPlacementRule.java | 22 +++++++++++++------ .../listener/BlockPlacementListener.java | 7 +++--- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 187dc42f7..5c6f38f3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,3 +14,4 @@ Some of these are pending, some deserve PRs, others are just minor tweaks * This is a breaking change because it changes the signature of `Argument#parse`, but most use cases should not be affected. Support has been maintained for the old argument map signature, so only completely custom arguments will be affected. * **breaking** [Placement rule api changes](https://github.com/hollow-cube/minestom-ce/pull/20) +* Optionally use reworked chunk sending algorithm (`minestom.use-new-chunk-sending` system property) diff --git a/src/main/java/net/minestom/server/instance/block/rule/BlockPlacementRule.java b/src/main/java/net/minestom/server/instance/block/rule/BlockPlacementRule.java index c3229fd55..43f4c8db0 100644 --- a/src/main/java/net/minestom/server/instance/block/rule/BlockPlacementRule.java +++ b/src/main/java/net/minestom/server/instance/block/rule/BlockPlacementRule.java @@ -2,15 +2,13 @@ package net.minestom.server.instance.block.rule; import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Pos; -import net.minestom.server.entity.Player; import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.BlockFace; import net.minestom.server.item.ItemMeta; +import net.minestom.server.item.Material; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.concurrent.atomic.AtomicBoolean; - public abstract class BlockPlacementRule { protected final Block block; @@ -38,7 +36,7 @@ public abstract class BlockPlacementRule { */ public abstract @Nullable Block blockPlace(@NotNull PlacementState placementState); - public boolean isSelfReplaceable(@NotNull Block block, @NotNull BlockFace blockFace, @NotNull Point cursorPosition) { + public boolean isSelfReplaceable(@NotNull Replacement replacement) { return false; } @@ -55,9 +53,19 @@ public abstract class BlockPlacementRule { @NotNull Pos playerPosition, @NotNull ItemMeta usedItemMeta, boolean isPlayerShifting - ) {} + ) { + } public record UpdateState(@NotNull Block.Getter instance, - @NotNull Point blockPosition, - @NotNull Block currentBlock) {} + @NotNull Point blockPosition, + @NotNull Block currentBlock) { + } + + public record Replacement( + @NotNull Block block, + @NotNull BlockFace blockFace, + @NotNull Point cursorPosition, + @NotNull Material material + ) { + } } diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index 58f57e85e..a8bc73141 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -94,7 +94,8 @@ public class BlockPlacementListener { //todo it feels like it should be possible to have better replacement rules than this, feels pretty scuffed. Point placementPosition = blockPosition; var interactedPlacementRule = BLOCK_MANAGER.getBlockPlacementRule(interactedBlock); - if (interactedPlacementRule == null || !interactedPlacementRule.isSelfReplaceable(interactedBlock, blockFace, cursorPosition)) { + if (interactedPlacementRule == null || !interactedPlacementRule.isSelfReplaceable( + new BlockPlacementRule.Replacement(interactedBlock, blockFace, cursorPosition, useMaterial))) { // If the block is not replaceable, try to place next to it. final int offsetX = blockFace == BlockFace.WEST ? -1 : blockFace == BlockFace.EAST ? 1 : 0; final int offsetY = blockFace == BlockFace.BOTTOM ? -1 : blockFace == BlockFace.TOP ? 1 : 0; @@ -103,8 +104,8 @@ public class BlockPlacementListener { var placementBlock = instance.getBlock(placementPosition); var placementRule = BLOCK_MANAGER.getBlockPlacementRule(placementBlock); - if (!placementBlock.registry().isReplaceable() && (placementRule == null || - !placementRule.isSelfReplaceable(placementBlock, blockFace, cursorPosition))) { + if (!placementBlock.registry().isReplaceable() && (placementRule == null || !placementRule.isSelfReplaceable( + new BlockPlacementRule.Replacement(placementBlock, blockFace, cursorPosition, useMaterial)))) { // If the block is still not replaceable, cancel the placement canPlaceBlock = false; }