2020-04-12 10:24:25 +02:00
|
|
|
package fr.themode.minestom.instance.block.rule.vanilla;
|
|
|
|
|
|
|
|
import fr.themode.minestom.instance.Instance;
|
|
|
|
import fr.themode.minestom.instance.block.Block;
|
|
|
|
import fr.themode.minestom.instance.block.rule.BlockPlacementRule;
|
|
|
|
import fr.themode.minestom.utils.BlockPosition;
|
|
|
|
|
|
|
|
public class RedstonePlacementRule extends BlockPlacementRule {
|
|
|
|
|
|
|
|
public RedstonePlacementRule() {
|
|
|
|
super(Block.REDSTONE_WIRE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public short blockRefresh(Instance instance, BlockPosition blockPosition) {
|
|
|
|
int x = blockPosition.getX();
|
|
|
|
int y = blockPosition.getY();
|
|
|
|
int z = blockPosition.getZ();
|
|
|
|
|
2020-04-15 13:20:28 +02:00
|
|
|
if (isAir(instance, x, y - 1, z)) {
|
|
|
|
return Block.AIR.getBlockId();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-12 10:24:25 +02:00
|
|
|
String east = "none";
|
|
|
|
String north = "none";
|
|
|
|
String power = "0";
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
south = "side";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isRedstone(instance, x, y + 1, z - 1)) {
|
|
|
|
north = "up";
|
|
|
|
} else if (isRedstone(instance, x, y, z - 1)) {
|
|
|
|
north = "side";
|
|
|
|
} else if (isRedstone(instance, x, y - 1, z - 1)) {
|
|
|
|
north = "side";
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:17:21 +02:00
|
|
|
// TODO power
|
|
|
|
|
2020-04-12 10:24:25 +02:00
|
|
|
|
|
|
|
return Block.REDSTONE_WIRE.withProperties(east, north, power, south, west);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isRedstone(Instance instance, int x, int y, int z) {
|
|
|
|
short blockId = instance.getBlockId(x, y, z);
|
|
|
|
return Block.getBlockFromId(blockId) == Block.REDSTONE_WIRE;
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:20:28 +02:00
|
|
|
private boolean isAir(Instance instance, int x, int y, int z) {
|
|
|
|
short blockId = instance.getBlockId(x, y, z);
|
|
|
|
return Block.getBlockFromId(blockId) == Block.AIR;
|
|
|
|
}
|
|
|
|
|
2020-04-12 10:24:25 +02:00
|
|
|
}
|