Fix BlockPlacementRule block state id update

This commit is contained in:
Felix Cravic 2020-12-12 04:27:43 +01:00
parent 77b4fe7287
commit 137273f05f
5 changed files with 29 additions and 17 deletions

View File

@ -331,10 +331,11 @@ public class InstanceContainer extends Instance {
final short neighborStateId = chunk.getBlockStateId(neighborX, neighborY, neighborZ);
final BlockPlacementRule neighborBlockPlacementRule = BLOCK_MANAGER.getBlockPlacementRule(neighborStateId);
if (neighborBlockPlacementRule != null) {
final BlockPosition neighborPosition = new BlockPosition(neighborX, neighborY, neighborZ);
final short newNeighborId = neighborBlockPlacementRule.blockRefresh(this,
new BlockPosition(neighborX, neighborY, neighborZ), neighborStateId);
neighborPosition, neighborStateId);
if (neighborStateId != newNeighborId) {
refreshBlockStateId(neighborX, neighborY, neighborZ, neighborStateId);
refreshBlockStateId(neighborPosition, newNeighborId);
}
}

View File

@ -5,6 +5,7 @@ import net.minestom.server.instance.Instance;
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;
public abstract class BlockPlacementRule {
@ -14,7 +15,7 @@ public abstract class BlockPlacementRule {
this.blockId = blockId;
}
public BlockPlacementRule(Block block) {
public BlockPlacementRule(@NotNull Block block) {
this(block.getBlockId());
}
@ -26,7 +27,7 @@ public abstract class BlockPlacementRule {
* @param blockPosition the position where the block is trying to get place
* @return true if the block placement position is valid
*/
public abstract boolean canPlace(Instance instance, BlockPosition blockPosition);
public abstract boolean canPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition);
/**
* Called when the block state id can be updated (for instance if a neighbour block changed).
@ -36,7 +37,7 @@ public abstract class BlockPlacementRule {
* @param currentStateID the current block state id of the block
* @return the updated block state id
*/
public abstract short blockRefresh(Instance instance, BlockPosition blockPosition, short currentStateID);
public abstract short blockRefresh(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentStateID);
/**
* Called when the block is placed.
@ -47,7 +48,9 @@ public abstract class BlockPlacementRule {
* @param pl the player who placed the block
* @return the block state id of the placed block
*/
public abstract short blockPlace(Instance instance, Block block, BlockFace blockFace, Player pl);
public abstract short blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace,
@NotNull Player pl);
public short getBlockId() {
return blockId;

View File

@ -6,6 +6,7 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
public class AxisPlacementRule extends BlockPlacementRule {
@ -17,17 +18,17 @@ public class AxisPlacementRule extends BlockPlacementRule {
}
@Override
public boolean canPlace(Instance instance, BlockPosition blockPosition) {
public boolean canPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
return true;
}
@Override
public short blockRefresh(Instance instance, BlockPosition blockPosition, short currentId) {
public short blockRefresh(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentId) {
return currentId;
}
@Override
public short blockPlace(Instance instance, Block block, BlockFace blockFace, Player pl) {
public short blockPlace(@NotNull Instance instance, @NotNull Block block, @NotNull BlockFace blockFace, @NotNull Player pl) {
String axis = "y";
if (blockFace == BlockFace.WEST || blockFace == BlockFace.EAST) {
axis = "x";

View File

@ -6,6 +6,7 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
public class RedstonePlacementRule extends BlockPlacementRule {
@ -14,13 +15,13 @@ public class RedstonePlacementRule extends BlockPlacementRule {
}
@Override
public boolean canPlace(Instance instance, BlockPosition blockPosition) {
public boolean canPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
// TODO check solid block
return true;
}
@Override
public short blockRefresh(Instance instance, BlockPosition blockPosition, short currentId) {
public short blockRefresh(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentId) {
int x = blockPosition.getX();
int y = blockPosition.getY();
int z = blockPosition.getZ();
@ -70,13 +71,18 @@ public class RedstonePlacementRule extends BlockPlacementRule {
// TODO power
final String[] properties = new String[]{
"east=" + east,
"north=" + north,
"power=" + power,
"south=" + south,
"west=" + west};
return Block.REDSTONE_WIRE.withProperties("east=" + east, "north=" + north,
"power=" + power, "south=" + south, "west=" + west);
return Block.REDSTONE_WIRE.withProperties(properties);
}
@Override
public short blockPlace(Instance instance, Block block, BlockFace blockFace, Player pl) {
public short blockPlace(@NotNull Instance instance, @NotNull Block block, @NotNull BlockFace blockFace, @NotNull Player pl) {
return getBlockId();
}

View File

@ -6,6 +6,7 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import org.jetbrains.annotations.NotNull;
public class WallPlacementRule extends BlockPlacementRule {
@ -17,12 +18,12 @@ public class WallPlacementRule extends BlockPlacementRule {
}
@Override
public boolean canPlace(Instance instance, BlockPosition blockPosition) {
public boolean canPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
return true;
}
@Override
public short blockRefresh(Instance instance, BlockPosition blockPosition, short currentId) {
public short blockRefresh(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentId) {
int x = blockPosition.getX();
int y = blockPosition.getY();
int z = blockPosition.getZ();
@ -56,7 +57,7 @@ public class WallPlacementRule extends BlockPlacementRule {
}
@Override
public short blockPlace(Instance instance, Block block, BlockFace blockFace, Player pl) {
public short blockPlace(@NotNull Instance instance, @NotNull Block block, @NotNull BlockFace blockFace, @NotNull Player pl) {
return getBlockId();
}