hollow-cube/material-in-replacement-rule

This commit is contained in:
mworzala 2023-06-18 07:54:50 -04:00
parent 9f3ee89506
commit 438338381e
No known key found for this signature in database
GPG Key ID: B148F922E64797C7
3 changed files with 20 additions and 10 deletions

View File

@ -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)

View File

@ -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
) {
}
}

View File

@ -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;
}