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.
Keep the step counts in saved data
Section titled “Keep the step counts in saved data”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.
Count steps on grass
Section titled “Count steps on grass”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.
Forget the count when the block changes
Section titled “Forget the count when the block changes”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.
Try it in game
Section titled “Try it in game”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.
See also
Section titled “See also”- Movement and block events: the footfall and block change events, their ordering, and their edge cases.
MobFootfallCallback: wearing paths under herd animals as well, with a higher threshold and no wear under pets.- Saved data and reloaders:
PathWear, and reading wear rules from data packs.