Fix and Cleanup Redstone Placement Rule

This commit is contained in:
MichaelPriebe 2020-12-17 00:51:13 -05:00
parent 8b515e8686
commit 917c4ddfd6
2 changed files with 113 additions and 45 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.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule; import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.block.BlockUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class RedstonePlacementRule extends BlockPlacementRule { public class RedstonePlacementRule extends BlockPlacementRule {
@ -16,67 +17,81 @@ public class RedstonePlacementRule extends BlockPlacementRule {
@Override @Override
public boolean canPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition) { public boolean canPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
// TODO check solid block BlockUtils block = new BlockUtils(instance, blockPosition);
return true; return block.below().getBlock().isSolid();
} }
@Override @Override
public short blockRefresh(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentId) { public short blockRefresh(@NotNull Instance instance, @NotNull BlockPosition blockPosition, short currentId) {
int x = blockPosition.getX(); BlockUtils block = new BlockUtils(instance, blockPosition);
int y = blockPosition.getY();
int z = blockPosition.getZ();
if (isAir(instance, x, y - 1, z)) { String pEast = "none";
return Block.AIR.getBlockId(); String pNorth = "none";
}
String east = "none";
String north = "none";
String power = "0"; String power = "0";
String south = "none"; String pSouth = "none";
String west = "none"; String pWest = "none";
if (isRedstone(instance, x + 1, y + 1, z)) { int connected = 0;
east = "up";
} else if (isRedstone(instance, x + 1, y, z)) { BlockUtils north = block.north();
east = "side"; BlockUtils south = block.south();
} else if (isRedstone(instance, x + 1, y - 1, z)) { BlockUtils east = block.east();
east = "side"; BlockUtils west = block.west();
// TODO: Block should have method isRedstone, as redstone connects to more than itself.
if (north.equals(Block.REDSTONE_WIRE) || north.below().equals(Block.REDSTONE_WIRE)) {
connected++;
pNorth = "side";
} }
if (south.equals(Block.REDSTONE_WIRE) || south.below().equals(Block.REDSTONE_WIRE)) {
if (isRedstone(instance, x - 1, y + 1, z)) { connected++;
west = "up"; pSouth = "side";
} else if (isRedstone(instance, x - 1, y, z)) {
west = "side";
} else if (isRedstone(instance, x - 1, y - 1, z)) {
west = "side";
} }
if (east.equals(Block.REDSTONE_WIRE) || east.below().equals(Block.REDSTONE_WIRE)) {
if (isRedstone(instance, x, y + 1, z + 1)) { connected++;
south = "up"; pEast = "side";
} else if (isRedstone(instance, x, y, z + 1)) {
south = "side";
} else if (isRedstone(instance, x, y - 1, z + 1)) {
south = "side";
} }
if (west.equals(Block.REDSTONE_WIRE) || west.below().equals(Block.REDSTONE_WIRE)) {
if (isRedstone(instance, x, y + 1, z - 1)) { connected++;
north = "up"; pWest = "side";
} else if (isRedstone(instance, x, y, z - 1)) { }
north = "side"; if (north.above().equals(Block.REDSTONE_WIRE)) {
} else if (isRedstone(instance, x, y - 1, z - 1)) { connected++;
north = "side"; pNorth = "up";
}
if (east.above().equals(Block.REDSTONE_WIRE)) {
connected++;
pEast = "up";
}
if (south.above().equals(Block.REDSTONE_WIRE)) {
connected++;
pSouth = "up";
}
if (west.above().equals(Block.REDSTONE_WIRE)) {
connected++;
pWest = "up";
}
if (connected == 0) {
pNorth = "side";
pEast = "side";
pSouth = "side";
pWest = "side";
} else if (connected == 1) {
if (!pNorth.equals("none")) pSouth = "side";
if (!pSouth.equals("none")) pNorth = "side";
if (!pEast.equals("none")) pWest = "side";
if (!pWest.equals("none")) pEast = "side";
} }
// TODO power // TODO power
final String[] properties = { final String[] properties = {
"east=" + east, "east=" + pEast,
"north=" + north, "north=" + pNorth,
"power=" + power, "power=" + power,
"south=" + south, "south=" + pSouth,
"west=" + west}; "west=" + pWest};
return Block.REDSTONE_WIRE.withProperties(properties); return Block.REDSTONE_WIRE.withProperties(properties);
} }

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