Merge remote-tracking branch 'origin/master'

This commit is contained in:
themode 2020-12-18 01:46:00 +01:00
commit 213599a649
2 changed files with 109 additions and 44 deletions

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 net.minestom.server.utils.block.BlockUtils;
import org.jetbrains.annotations.NotNull;
public class RedstonePlacementRule extends BlockPlacementRule {
@ -16,20 +17,13 @@ public class RedstonePlacementRule extends BlockPlacementRule {
@Override
public boolean canPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
// TODO check solid block
return true;
BlockUtils block = new BlockUtils(instance, blockPosition);
return block.below().getBlock().isSolid();
}
@Override
public short blockRefresh(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentId) {
int x = blockPosition.getX();
int y = blockPosition.getY();
int z = blockPosition.getZ();
if (isAir(instance, x, y - 1, z)) {
return Block.AIR.getBlockId();
}
BlockUtils block = new BlockUtils(instance, blockPosition);
String east = "none";
String north = "none";
@ -37,36 +31,64 @@ public class RedstonePlacementRule extends BlockPlacementRule {
String south = "none";
String west = "none";
if (isRedstone(instance, x + 1, y + 1, z)) {
east = "up";
} else if (isRedstone(instance, x + 1, y, z)) {
east = "side";
} else if (isRedstone(instance, x + 1, y - 1, z)) {
east = "side";
}
// TODO Block should have method isRedstone, as redstone connects to more than itself.
if (isRedstone(instance, x - 1, y + 1, z)) {
west = "up";
} else if (isRedstone(instance, x - 1, y, z)) {
west = "side";
} else if (isRedstone(instance, x - 1, y - 1, z)) {
west = "side";
}
final BlockUtils blockNorth = block.north();
final BlockUtils blockSouth = block.south();
final BlockUtils blockEast = block.east();
final BlockUtils blockWest = block.west();
int connected = 0;
if (isRedstone(instance, x, y + 1, z + 1)) {
south = "up";
} else if (isRedstone(instance, x, y, z + 1)) {
south = "side";
} else if (isRedstone(instance, x, y - 1, z + 1)) {
if (blockNorth.equals(Block.REDSTONE_WIRE) || blockNorth.below().equals(Block.REDSTONE_WIRE)) {
connected++;
north = "side";
}
if (blockSouth.equals(Block.REDSTONE_WIRE) || blockSouth.below().equals(Block.REDSTONE_WIRE)) {
connected++;
south = "side";
}
if (isRedstone(instance, x, y + 1, z - 1)) {
if (blockEast.equals(Block.REDSTONE_WIRE) || blockEast.below().equals(Block.REDSTONE_WIRE)) {
connected++;
east = "side";
}
if (blockWest.equals(Block.REDSTONE_WIRE) || blockWest.below().equals(Block.REDSTONE_WIRE)) {
connected++;
west = "side";
}
if (blockNorth.above().equals(Block.REDSTONE_WIRE)) {
connected++;
north = "up";
} else if (isRedstone(instance, x, y, z - 1)) {
north = "side";
} else if (isRedstone(instance, x, y - 1, z - 1)) {
}
if (blockSouth.above().equals(Block.REDSTONE_WIRE)) {
connected++;
south = "up";
}
if (blockEast.above().equals(Block.REDSTONE_WIRE)) {
connected++;
east = "up";
}
if (blockWest.above().equals(Block.REDSTONE_WIRE)) {
connected++;
west = "up";
}
if (connected == 0) {
north = "side";
south = "side";
east = "side";
west = "side";
} else if (connected == 1) {
if (!north.equals("none")) {
south = "side";
}
if (!south.equals("none")) {
north = "side";
}
if (!east.equals("none")) {
west = "side";
}
if (!west.equals("none")) {
east = "side";
}
}
// TODO power
@ -86,14 +108,4 @@ public class RedstonePlacementRule extends BlockPlacementRule {
return getBlockId();
}
private boolean isRedstone(Instance instance, int x, int y, int z) {
final short blockStateId = instance.getBlockStateId(x, y, z);
return Block.fromStateId(blockStateId) == Block.REDSTONE_WIRE;
}
private boolean isAir(Instance instance, int x, int y, int z) {
final short blockStateId = instance.getBlockStateId(x, y, z);
return Block.fromStateId(blockStateId) == Block.AIR;
}
}

View File

@ -0,0 +1,53 @@
package net.minestom.server.utils.block;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.utils.BlockPosition;
public class BlockUtils {
private final Instance instance;
private final BlockPosition position;
public BlockUtils(Instance instance, BlockPosition position) {
this.instance = instance;
this.position = position;
}
public BlockUtils getRelativeTo(int x, int y, int z) {
BlockPosition position = this.position.clone().add(x, y, z);
return new BlockUtils(instance, position);
}
public BlockUtils above() {
return getRelativeTo(0, 1, 0);
}
public BlockUtils below() {
return getRelativeTo(0, -1, 0);
}
public BlockUtils north() {
return getRelativeTo(0, 0, -1);
}
public BlockUtils east() {
return getRelativeTo(1, 0, 0);
}
public BlockUtils south() {
return getRelativeTo(0, 0, 1);
}
public BlockUtils west() {
return getRelativeTo(-1, 0, 0);
}
public Block getBlock() {
return Block.fromStateId(instance.getBlockStateId(position));
}
public boolean equals(Block block) {
return getBlock() == block;
}
}