User Tools

Site Tools


tutorial:stats

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
tutorial:stats [2024/11/18 16:45] – identifier constructor is private, Registries class bemotytutorial:stats [2026/01/08 21:31] (current) – Mojang mappings and fixes for incorrect identifier cassiancc
Line 4: Line 4:
 To add a custom statistic, create an identifier which will be used to register and increase the stat: To add a custom statistic, create an identifier which will be used to register and increase the stat:
 <code java> <code java>
-public static final Identifier INTERACT_WITH_COOL_BLOCK = Identifier.of("modid", "interact_with_cool_block");+public static final Identifier INTERACT_WITH_COOL_BLOCK = Identifier.fromNamespaceAndPath("example-mod", "interact_with_cool_block");
 </code> </code>
  
Line 11: Line 11:
 Then register the stat using the identifier: Then register the stat using the identifier:
 <code java> <code java>
-Registry.register(Registries.CUSTOM_STAT, "interact_with_cool_block", INTERACT_WITH_COOL_BLOCK);+Registry.register(BuiltInRegistries.CUSTOM_STAT, INTERACT_WITH_COOL_BLOCK, INTERACT_WITH_COOL_BLOCK);
 </code> </code>
  
Line 17: Line 17:
  
 <code java> <code java>
-Stats.CUSTOM.getOrCreateStat(INTERACT_WITH_COOL_BLOCK, StatFormatter.DEFAULT);+Stats.CUSTOM.get(INTERACT_WITH_COOL_BLOCK, StatFormatter.DEFAULT);
 </code> </code>
  
 ===== Using The Statistic ===== ===== Using The Statistic =====
  
-To increment the statistic, for example when a player interacts with a block, you can use ''PlayerEntity::incrementStat'':+To increment the statistic, for example when a player interacts with a block, you can use ''Player::awardStat'':
  
 <code java> <code java>
 @Override @Override
-public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { +public ActionResult onUse(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { 
- player.incrementStat(ModClass.INTERACT_WITH_COOL_BLOCK);+ player.awardStat(ModClass.INTERACT_WITH_COOL_BLOCK);
  
- return ActionResult.SUCCESS;+ return InteractionResult.SUCCESS;
 } }
 </code> </code>
  
-You can also use ''PlayerEntity.increaseStat(stat, amount)'' to increase the stat by an arbitrary amount.+You can also use ''Player.awardStat(stat, amount)'' to increase the stat by an arbitrary amount.
  
 ===== Translation ===== ===== Translation =====
  
-To translate the name, add an entry for ''stat.modid.statid'' (see [[tutorial:lang]] for how to translate the statistic.):+To translate the name, add an entry for ''stat.example-mod.statid'' (see [[tutorial:lang]] for how to translate the statistic.):
  
 <code json> <code json>
 { {
-  "stat.modid.interact_with_cool_block": "Interactions with Cool Block"+  "stat.example-mod.interact_with_cool_block": "Interactions with Cool Block"
 } }
 </code> </code>
  
tutorial/stats.txt · Last modified: 2026/01/08 21:31 by cassiancc