Table of Contents
Adding Player Statistics
Statistics track time spend doing something, distance traveled or jumped, and how often the player interacted with blocks or performed certain actions.
To add a custom statistic, create an identifier which will be used to register and increase the stat:
public static final Identifier INTERACT_WITH_COOL_BLOCK = Identifier.fromNamespaceAndPath("example-mod", "interact_with_cool_block");
Registration
Then register the stat using the identifier:
Registry.register(BuiltInRegistries.CUSTOM_STAT, INTERACT_WITH_COOL_BLOCK, INTERACT_WITH_COOL_BLOCK);
Then add the stat to the statistic screen, where you can also specify the stat formatter. It determines how the number is shown in the stat list. You can use DEFAULT, DIVIDE_BY_TEN, DISTANCE or TIME.
Stats.CUSTOM.get(INTERACT_WITH_COOL_BLOCK, StatFormatter.DEFAULT);
Using The Statistic
To increment the statistic, for example when a player interacts with a block, you can use Player::awardStat:
@Override public ActionResult onUse(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { player.awardStat(ModClass.INTERACT_WITH_COOL_BLOCK); return InteractionResult.SUCCESS; }
You can also use Player.awardStat(stat, amount) to increase the stat by an arbitrary amount.
Translation
To translate the name, add an entry for stat.example-mod.statid (see lang for how to translate the statistic.):
{
"stat.example-mod.interact_with_cool_block": "Interactions with Cool Block"
}