Minecraft – Do buttons/levers above rails prevent zombie pigmen from spawning

minecraft-java-edition

I was watching a live stream of someone creating a railway through the Nether when it was suggested to him to prevent zombie pigmen from spawning on his rails by placing button on the space above the rails (either attached to the ceiling or the wall).

Having never seen buttons being used for that purpose I started researching it a bit. Buttons do count as transparent blocks, but mobs can spawn on pressure plates, so perhaps redstone components are an exception.
The relevant question on this topic on Arqade did not list them specifically in the answers (see Is there a way to keep Zombie Pigmen off of minecart tracks in the Nether?).

Best Answer

No, buttons do not affect mob spawning

The commenter probably misremembered that mobs cannot spawn if there is no air block. This is just false.

Mobs can spawn in blocks without a hitbox (and carpets) without any issues. They could so for quite a while now (maybe even always). Note that prior to 1.8 the spawning algorithm required an actual air block to start in, but mobs could always spawn in hitboxless blocks.

To test this, I used a superflat world with the following generator:

3;minecraft:bedrock,3*minecraft:stone,5*minecraft:sandstone,4*minecraft:stone_button:5;2;

minecraft:stone_button:5 is a floor button, but the orientation doesn't matter for this. I put 4 layers buttons there. The world looks pretty trippy:

ButtonlandMore buttonland

A quick /time set 18000 and mobs start spawning almost immediately, even though there are no solid surfaces not covered in 4 layers of buttons.

Mobs spawning in buttonland

This is also true for buttons over rails:

3;minecraft:bedrock,3*minecraft:stone,5*minecraft:sandstone,minecraft:rail,minecraft:stone_button:5;2;

Mobs spawning on rails under buttons

Note: While these are overworld mobs, I have no reason to assume that it'd be any different for zombie pigmen, seeing that they don't follow any special spawning conditions.