Placing a block from code
From Sunrise's modding bookead8a1e
level.set_block(host, pos, state, flags) writes a state and nothing more. A block placed by an item does two things more, and a mod placing a block from code can ask for each of them. All three are server operations on the level in place.
level.update_from_neighbour_shapes(host, pos, state)isBlock.updateFromNeighbourShapes: it returns the state fitted to the blocks aroundpos(a fence gets its connections, a stair its corner). Nothing is written; pass the result toset_block. Neighbours that are not loaded read as air.level.place_block(host, pos, state, update_neighbour_shapes)writesstatewith a placement's flags (neighbours and clients told, and unlessupdate_neighbour_shapesis false their shapes updated) and then runsBlock.setPlacedBy, so a bed gets its head, a door its upper half and a tall plant its top. It returnsOk(true)when the block was written, andOk(false)when vanilla refused the write (an unchanged state, an unloaded or out-of-height position).
let fitted = level.update_from_neighbour_shapes(host, pos, door)?;
level.place_block(host, pos, fitted, true)?;place_block is player.place_block without a player: setPlacedBy gets no placer and no item. Nothing else of BlockItem.place runs: no survival or obstruction check, no sound, no game event, no advancement. Both refuse with LevelWriteError: Stale for a dead handle, Unavailable on a client or for a level that is not the one in place, and InvalidState for a state the world lacks.
Edit this page on GitHub · Spotted something wrong? Open an issue or ask on Discord.



