I am developing a Minecraft Fabric mod with custom player stats. Here is how I created a custom player stat:
public class ModStats {
public static final Identifier MINING_STAT = new Identifier(ExampleMod.MOD_ID, "mining_stat");
public static void registerModStats() {
Registry.register(Registries.CUSTOM_STAT, "mining_stat", MINING_STAT);
Stats.CUSTOM.getOrCreateStat(MINING_STAT, StatFormatter.DEFAULT);
}
}
registerModStats()
is called in the Mod main class.
I increase the stat using a MiningToolItemMixin
:
@Mixin(MiningToolItem.class)
public class MiningToolItemMixin {
@Inject(at = @At("TAIL"), method = "postMine")
private void postMine(ItemStack stack, World world, BlockState state, BlockPos pos, LivingEntity miner,
CallbackInfoReturnable<Boolean> info) {
if (miner instanceof PlayerEntity player) {
player.incrementStat(ModStats.MINING_STAT);
}
System.out.println("MiningToolItemMixin");
}
}
Everything works finde but how can I get the stat value in other parts of the code?
I tired to get the stat via the PlayerEntitiy
object but there are only methods to increase stats and to retreive stats.