Solved Cannot place torch on wall

thomerow

New member
Nov 13, 2024
2
0
1
Hello everyone! I cannot for the life of me figure out how to place a torch on a wall programmatically. What I tried so far is the following, but in both cases the torches just hover in mid air in an upright position right in front of the wall I want to place them on.

The first idea was doing the following (for simplicity just assume that I determined the correct block face beforehand):

Java:
Block blkWall = world.getBlockAt(x, y, z);
Block blkTorch = blkWall.getRelative(BlockFace.WEST);
blkTorch.setType(Material.TORCH);

The second idea was to set the "facing" of the torch block to the opposite value of the wall face, but that also doesn't help:

Java:
Block blkWall = world.getBlockAt(x, y, z);
BlockFace bfWall = BlockFace.WEST;
BlockFace bfTorch = BlockFace.EAST;
Block blkTorch = blkWall.getRelative(bfWall);
blkTorch.setType(Material.TORCH);
BlockData blockData = blkTorch.getBlockData();
if (blockData instanceof Directional dir) {
    dir.setFacing(bfTorch);
    blkTorch.setBlockData(dir);
}

What is the general procedure of placing a torch on a wall?
 
Last edited:

thomerow

New member
Nov 13, 2024
2
0
1
I figured it out myself! First of all I didn'tknow that there also is a WALL_TORCH material which you have to use instead of TORCH. Secondly there seems to be an odd quirk with the correct "facing" values for wall torches. For example, if the block the torch should appear on faces WEST, the torch has to face NORTH, when the block faces EAST, the torch has to face SOUTH etc.

My code ended up looking like this:

Java:
Block blkWall = world.getBlockAt(x, y, z);
Block blkTorch = blkWall.getRelative(BlockFace.WEST);
blkTorch.setType(Material.WALL_TORCH);
BlockData blockData = blkTorch.getBlockData();
if (blockData instanceof Directional dir) {
    dir.setFacing(BlockFace.NORTH);
    blkTorch.setBlockData(dir);
}

In reality the absolute values that appear in this short example are calculated beforehand, of course.

Hope this helps someone in the future.