Minecraft – How to create a stamina system using the hunger bar

minecraft-commandsminecraft-java-edition

I am trying to create a stamina system for a mini-game, but I just can't seem to get it to work properly. I can't use the exp bar, as that is already being used for a magic bar that I have up and running. I am trying to use the hunger bar. I have gotten the regen feature to work, but the problem is when it comes to draining it. Currently I have it so jumping and sprinting drain your stamina using the hunger effect, the problem though is that when the player jumps it gives them the hunger effect and won't take it away when the stop. The second issue is that it won't give the hunger effect for sprinting as I need to constantly reset it to make sure if the player is resting or sprinting. I want to know if there is a way to test if the player is not moving or just walking. Just testing for those two things would allow me to make the stamina system a lot easier to control.
The scoreboards I am using are:

staminaRegen dummy
staminaUsage stat.sprintOneCm
staminaUsageAir stat.jump
Resting stat.sneakTime

Best Answer

you should reset the objective every time you applied the hunger effect, like so:

/effect @a[score_staminaUsage_min=1] minecraft:hunger 1 1 true
/scoreboard players set @a[score_staminaUsage_min=1] staminaUsage 0

same for the staminaUsageAir.

this way you can give the hunger effect only when a player sprints or jumps.


Here is another tip: if you're okay with your player regenerating hunger whenever he's not loosing, you could for example put this command on a clock:

/effect @a minecraft:saturation 1 0

this will regenerate half a cone each time it is activated (20 times a second: full bar in one second. once per second: full bar in 40 seconds).
then just put the hunger effect above it while they run (with a high amplifier, like 200) and bam, system complete.