Fix a few errors related to placement rules

This commit is contained in:
TheMode 2021-05-22 23:41:55 +02:00
parent 9fe34cc32c
commit 21df05605d
6 changed files with 83 additions and 96 deletions

View File

@ -6,30 +6,27 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class BlockPlacementRule {
public static final int CANCEL_CODE = -1;
private final short blockId;
public BlockPlacementRule(short blockId) {
this.blockId = blockId;
}
private final Block block;
public BlockPlacementRule(@NotNull Block block) {
this(block.getBlockId());
this.block = block;
}
/**
* Called when the block state id can be updated (for instance if a neighbour block changed).
*
* @param instance the instance of the block
* @param blockPosition the block position
* @param currentStateID the current block state id of the block
* @return the updated block state id
* @param instance the instance of the block
* @param blockPosition the block position
* @param currentBlock the current block
* @return the updated block
*/
public abstract short blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentStateID);
public abstract @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Block currentBlock);
/**
* Called when the block is placed.
@ -39,14 +36,13 @@ public abstract class BlockPlacementRule {
* @param blockFace the block face
* @param blockPosition the block position
* @param pl the player who placed the block
* @return the block state id of the placed block,
* {@link #CANCEL_CODE} to prevent the placement
* @return the block to place, {@code null} to cancel
*/
public abstract short blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull BlockPosition blockPosition,
@NotNull Player pl);
public abstract @Nullable Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull BlockPosition blockPosition,
@NotNull Player pl);
public short getBlockId() {
return blockId;
public @NotNull Block getBlock() {
return block;
}
}

View File

@ -4,35 +4,32 @@ import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.BlockProperties;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
public class AxisPlacementRule extends BlockPlacementRule {
protected final Block block;
public AxisPlacementRule(Block block) {
public AxisPlacementRule(@NotNull Block block) {
super(block);
this.block = block;
}
@Override
public short blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentId) {
return currentId;
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Block block) {
return block;
}
@Override
public short blockPlace(@NotNull Instance instance,
public Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull BlockPosition blockPosition,
@NotNull Player pl) {
String axis = "y";
String axis = "Y";
if (blockFace == BlockFace.WEST || blockFace == BlockFace.EAST) {
axis = "x";
axis = "X";
} else if (blockFace == BlockFace.SOUTH || blockFace == BlockFace.NORTH) {
axis = "z";
axis = "Z";
}
return block.withProperties("axis=" + axis);
return block.withProperty(BlockProperties.AXIS, axis);
}
}

View File

@ -4,6 +4,8 @@ import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.BlockProperties;
import net.minestom.server.instance.block.Blocks;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.block.BlockUtils;
@ -12,56 +14,56 @@ import org.jetbrains.annotations.NotNull;
public class RedstonePlacementRule extends BlockPlacementRule {
public RedstonePlacementRule() {
super(Block.REDSTONE_WIRE);
super(Blocks.REDSTONE_WIRE);
}
@Override
public short blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentId) {
BlockUtils block = new BlockUtils(instance, blockPosition);
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Block block) {
BlockUtils blockUtils = new BlockUtils(instance, blockPosition);
String east = "none";
String north = "none";
String power = "0";
int power = 0;
String south = "none";
String west = "none";
// TODO Block should have method isRedstone, as redstone connects to more than itself.
final BlockUtils blockNorth = block.north();
final BlockUtils blockSouth = block.south();
final BlockUtils blockEast = block.east();
final BlockUtils blockWest = block.west();
final BlockUtils blockNorth = blockUtils.north();
final BlockUtils blockSouth = blockUtils.south();
final BlockUtils blockEast = blockUtils.east();
final BlockUtils blockWest = blockUtils.west();
int connected = 0;
if (blockNorth.equals(Block.REDSTONE_WIRE) || blockNorth.below().equals(Block.REDSTONE_WIRE)) {
if (blockNorth.equals(Blocks.REDSTONE_WIRE) || blockNorth.below().equals(Blocks.REDSTONE_WIRE)) {
connected++;
north = "side";
}
if (blockSouth.equals(Block.REDSTONE_WIRE) || blockSouth.below().equals(Block.REDSTONE_WIRE)) {
if (blockSouth.equals(Blocks.REDSTONE_WIRE) || blockSouth.below().equals(Blocks.REDSTONE_WIRE)) {
connected++;
south = "side";
}
if (blockEast.equals(Block.REDSTONE_WIRE) || blockEast.below().equals(Block.REDSTONE_WIRE)) {
if (blockEast.equals(Blocks.REDSTONE_WIRE) || blockEast.below().equals(Blocks.REDSTONE_WIRE)) {
connected++;
east = "side";
}
if (blockWest.equals(Block.REDSTONE_WIRE) || blockWest.below().equals(Block.REDSTONE_WIRE)) {
if (blockWest.equals(Blocks.REDSTONE_WIRE) || blockWest.below().equals(Blocks.REDSTONE_WIRE)) {
connected++;
west = "side";
}
if (blockNorth.above().equals(Block.REDSTONE_WIRE)) {
if (blockNorth.above().equals(Blocks.REDSTONE_WIRE)) {
connected++;
north = "up";
}
if (blockSouth.above().equals(Block.REDSTONE_WIRE)) {
if (blockSouth.above().equals(Blocks.REDSTONE_WIRE)) {
connected++;
south = "up";
}
if (blockEast.above().equals(Block.REDSTONE_WIRE)) {
if (blockEast.above().equals(Blocks.REDSTONE_WIRE)) {
connected++;
east = "up";
}
if (blockWest.above().equals(Block.REDSTONE_WIRE)) {
if (blockWest.above().equals(Blocks.REDSTONE_WIRE)) {
connected++;
west = "up";
}
@ -87,26 +89,21 @@ public class RedstonePlacementRule extends BlockPlacementRule {
// TODO power
final String[] properties = {
"east=" + east,
"north=" + north,
"power=" + power,
"south=" + south,
"west=" + west};
return Block.REDSTONE_WIRE.withProperties(properties);
return Blocks.REDSTONE_WIRE.withProperty(BlockProperties.REDSTONE_WIRE.EAST_REDSTONE, east)
.withProperty(BlockProperties.REDSTONE_WIRE.NORTH_REDSTONE, north)
.withProperty(BlockProperties.REDSTONE_WIRE.SOUTH_REDSTONE, south)
.withProperty(BlockProperties.REDSTONE_WIRE.WEST_REDSTONE, west)
.withProperty(BlockProperties.REDSTONE_WIRE.POWER, power);
}
@Override
public short blockPlace(@NotNull Instance instance,
public Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull BlockPosition blockPosition,
@NotNull Player pl) {
final short belowBlockId = instance.getBlockStateId(blockPosition.getX(), blockPosition.getY() - 1, blockPosition.getZ());
if (!Block.fromStateId(belowBlockId).isSolid()) {
return CANCEL_CODE;
if (!Block.fromStateId(belowBlockId).getData().isSolid()) {
return null;
}
return getBlockId();
return block;
}
}

View File

@ -4,8 +4,8 @@ import it.unimi.dsi.fastutil.Pair;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockAlternative;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.BlockProperties;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
@ -18,26 +18,26 @@ public class StairsPlacementRule extends BlockPlacementRule {
}
@Override
public short blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentStateID) {
return currentStateID;
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Block block) {
return block;
}
@Override
public short blockPlace(@NotNull Instance instance, @NotNull Block block, @NotNull BlockFace blockFace, @NotNull BlockPosition blockPosition, @NotNull Player player) {
public Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace,
@NotNull BlockPosition blockPosition, @NotNull Player player) {
Facing facing = this.getFacing(player);
Shape shape = this.getShape(instance, blockPosition, facing);
BlockFace half = BlockFace.BOTTOM; // waiting for new block faces to be implemented
boolean waterlogged = false; // waiting for water to be implemented
return block.withProperties(
"facing=" + facing.toString().toLowerCase(),
"half=" + half.toString().toLowerCase(),
"shape=" + shape.toString().toLowerCase(),
"waterlogged=" + waterlogged
);
return block.withProperty(BlockProperties.FACING, facing.toString())
.withProperty(BlockProperties.HALF, half.toString())
.withProperty(BlockProperties.STAIRS_SHAPE, shape.toString())
.withProperty(BlockProperties.WATERLOGGED, waterlogged);
}
private enum Shape {
STRAIGHT,
OUTER_LEFT,
OUTER_RIGHT,
@ -46,7 +46,6 @@ public class StairsPlacementRule extends BlockPlacementRule {
}
private enum Facing {
NORTH(
new BlockPosition(0, 0, 1),
new BlockPosition(0, 0, -1)
@ -89,10 +88,10 @@ public class StairsPlacementRule extends BlockPlacementRule {
return Pair.of(null, null);
}
short stateId = instance.getBlockStateId(blockPosition);
BlockAlternative alternative = block.getAlternative(stateId);
Block state = Block.fromStateId(stateId);
try {
Shape shape = Shape.valueOf(alternative.getProperty("shape").toUpperCase());
Facing facing = Facing.valueOf(alternative.getProperty("facing").toUpperCase());
Shape shape = Shape.valueOf(state.getProperty("shape").toUpperCase());
Facing facing = Facing.valueOf(state.getProperty("facing").toUpperCase());
return Pair.of(shape, facing);
} catch (Exception ex) {
return Pair.of(null, null);
@ -163,4 +162,4 @@ public class StairsPlacementRule extends BlockPlacementRule {
return Facing.WEST;
}
}
}
}

View File

@ -4,21 +4,19 @@ import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.BlockProperties;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
public class WallPlacementRule extends BlockPlacementRule {
Block block;
public WallPlacementRule(Block block) {
public WallPlacementRule(@NotNull Block block) {
super(block);
this.block = block;
}
@Override
public short blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentId) {
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Block block) {
final int x = blockPosition.getX();
final int y = blockPosition.getY();
final int z = blockPosition.getZ();
@ -26,8 +24,8 @@ public class WallPlacementRule extends BlockPlacementRule {
String east = "none";
String north = "none";
String south = "none";
String up = "true";
String waterlogged = "false";
boolean up = true;
boolean waterlogged = false;
String west = "none";
if (isBlock(instance, x + 1, y, z)) {
@ -45,22 +43,24 @@ public class WallPlacementRule extends BlockPlacementRule {
if (isBlock(instance, x, y, z - 1)) {
north = "low";
}
return block.withProperties("east=" + east, "north=" + north, "south=" + south, "up=" + up,
"waterlogged=" + waterlogged, "west=" + west);
return block
.withProperty(BlockProperties.NORTH_WALL, north)
.withProperty(BlockProperties.EAST_WALL, east)
.withProperty(BlockProperties.SOUTH_WALL, south)
.withProperty(BlockProperties.WEST_WALL, west)
.withProperty(BlockProperties.UP, up)
.withProperty(BlockProperties.WATERLOGGED, waterlogged);
}
@Override
public short blockPlace(@NotNull Instance instance,
public Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull BlockPosition blockPosition,
@NotNull Player pl) {
return getBlockId();
return block;
}
private boolean isBlock(Instance instance, int x, int y, int z) {
final short blockStateId = instance.getBlockStateId(x, y, z);
return Block.fromStateId(blockStateId).isSolid();
return Block.fromStateId(blockStateId).getData().isSolid();
}
}

View File

@ -1,7 +1,6 @@
package net.minestom.server.world.biomes;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockAlternative;
import net.minestom.server.item.ItemStack;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;
@ -35,18 +34,17 @@ public class BiomeParticles {
//TODO also can be falling_dust
private static final String type = "block";
private final BlockAlternative block;
private final Block block;
public BlockParticle(BlockAlternative block) {
public BlockParticle(Block block) {
this.block = block;
}
@Override
public NBTCompound toNbt() {
NBTCompound nbtCompound = new NBTCompound();
Block block1 = Block.fromStateId(block.getId());
nbtCompound.setString("type", type);
nbtCompound.setString("Name", block1.getName());
nbtCompound.setString("Name", block.getName());
Map<String, String> propertiesMap = block.createPropertiesMap();
if (propertiesMap.size() != 0) {
NBTCompound properties = new NBTCompound();