mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 16:37:38 +01:00
hollow-cube/material-in-replacement-rule
This commit is contained in:
parent
9f3ee89506
commit
438338381e
@ -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.
|
* 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.
|
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)
|
* **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)
|
||||||
|
@ -2,15 +2,13 @@ package net.minestom.server.instance.block.rule;
|
|||||||
|
|
||||||
import net.minestom.server.coordinate.Point;
|
import net.minestom.server.coordinate.Point;
|
||||||
import net.minestom.server.coordinate.Pos;
|
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.Block;
|
||||||
import net.minestom.server.instance.block.BlockFace;
|
import net.minestom.server.instance.block.BlockFace;
|
||||||
import net.minestom.server.item.ItemMeta;
|
import net.minestom.server.item.ItemMeta;
|
||||||
|
import net.minestom.server.item.Material;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
public abstract class BlockPlacementRule {
|
public abstract class BlockPlacementRule {
|
||||||
protected final Block block;
|
protected final Block block;
|
||||||
|
|
||||||
@ -38,7 +36,7 @@ public abstract class BlockPlacementRule {
|
|||||||
*/
|
*/
|
||||||
public abstract @Nullable Block blockPlace(@NotNull PlacementState placementState);
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,9 +53,19 @@ public abstract class BlockPlacementRule {
|
|||||||
@NotNull Pos playerPosition,
|
@NotNull Pos playerPosition,
|
||||||
@NotNull ItemMeta usedItemMeta,
|
@NotNull ItemMeta usedItemMeta,
|
||||||
boolean isPlayerShifting
|
boolean isPlayerShifting
|
||||||
) {}
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
public record UpdateState(@NotNull Block.Getter instance,
|
public record UpdateState(@NotNull Block.Getter instance,
|
||||||
@NotNull Point blockPosition,
|
@NotNull Point blockPosition,
|
||||||
@NotNull Block currentBlock) {}
|
@NotNull Block currentBlock) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public record Replacement(
|
||||||
|
@NotNull Block block,
|
||||||
|
@NotNull BlockFace blockFace,
|
||||||
|
@NotNull Point cursorPosition,
|
||||||
|
@NotNull Material material
|
||||||
|
) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.
|
//todo it feels like it should be possible to have better replacement rules than this, feels pretty scuffed.
|
||||||
Point placementPosition = blockPosition;
|
Point placementPosition = blockPosition;
|
||||||
var interactedPlacementRule = BLOCK_MANAGER.getBlockPlacementRule(interactedBlock);
|
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.
|
// 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 offsetX = blockFace == BlockFace.WEST ? -1 : blockFace == BlockFace.EAST ? 1 : 0;
|
||||||
final int offsetY = blockFace == BlockFace.BOTTOM ? -1 : blockFace == BlockFace.TOP ? 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 placementBlock = instance.getBlock(placementPosition);
|
||||||
var placementRule = BLOCK_MANAGER.getBlockPlacementRule(placementBlock);
|
var placementRule = BLOCK_MANAGER.getBlockPlacementRule(placementBlock);
|
||||||
if (!placementBlock.registry().isReplaceable() && (placementRule == null ||
|
if (!placementBlock.registry().isReplaceable() && (placementRule == null || !placementRule.isSelfReplaceable(
|
||||||
!placementRule.isSelfReplaceable(placementBlock, blockFace, cursorPosition))) {
|
new BlockPlacementRule.Replacement(placementBlock, blockFace, cursorPosition, useMaterial)))) {
|
||||||
// If the block is still not replaceable, cancel the placement
|
// If the block is still not replaceable, cancel the placement
|
||||||
canPlaceBlock = false;
|
canPlaceBlock = false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user