Skip to content

Wear a path where players walk

This guide makes grass that players cross often turn into a dirt path, with each block’s step count kept in the world save so a path keeps wearing in across restarts.

Add PathWear, the class from the saved data quick start, to your mod’s common code. PathWear#get returns a dimension’s counts, PathWear#step adds a step to a block and returns its new count, and PathWear#clear forgets a block. The world saves the counts with each dimension, so they survive a restart.

In your mod’s common initializer, register the footfall listener from the PlayerFootfallCallback example. It counts each step on grass with PathWear, leaves out sneaking players, and wears the grass to a path at the 40th step. When the event fires, and which block it reports, are in that section.

A block that becomes something else (a player digs the grass, or the footfall listener makes it a path) should start again from zero if it ever turns back into grass. Beside the footfall listener, register the listener from the BlockChangedCallback example, which clears a block’s count when it becomes another block.

Start a survival world and walk back and forth over one grass block: on the 40th step it becomes a dirt path. Sneak across another grass block as often as you like, and it stays grass. Quit and reload the world partway through: the count carries on from where it was.

To check the count without walking, write a GameTest, as Test behavior with GameTest does for PathWear.