Minecraft – Creating a double-jump mechanic in Minecraft using commands

minecraft-commandsminecraft-java-edition

I am trying to make double jumping, i.e. jumping again in midair but only once before you hit the ground again. Hitting the ground should reset you to allow another double jump to happen.

What I've got so far doesn't quite work. Is anyone able to have a look?

Here is my code:

a repeating command block with

/scoreboard players set @a[scores={sneak=1}] sneak 0

leading into another repeating command block with conditional on with

/execute at @a[scores={sneak=1,double=0}] if block ~ ~-0.75 ~ air run effect give @p[scores={sneak=1}] minecraft:levitation 1 0 true

which finally leads into another conditional repeating command block with

execute at @a if block ~ ~-0.5 ~ air run effect clear @a minecraft:levitation

which ends that chain.

there are other commands than reset scores when needed, such as

execute at @a if block ~ ~-0.5 ~ air run scoreboard players set @p[scores={sneak=..1}] sneak 0

along with

execute at @a unless block ~ ~-0.5 ~ air run scoreboard players set @p[scores={double=..1}] double 0

But this isn't working. Can I have some help please?

Best Answer

You can detect if a player sneaks with the help of this command:

/scoreboard objectives add sneaks minecraft.custom:minecraft.sneak_time

Then run this command in a repeating command block to make every player that has sneaked for 1 tick and is not on the ground levitate (which seems to be the way how you want the second jump to work)

/execute as @e[scores={sneaks=1},nbt={OnGround:0b}] run effect give @s minecraft:levitation 1 2 true

You can use a chain command block to the run this command, it will reset the sneak time for any player who is standing on the ground:

/scoreboard players reset @a[nbt={OnGround:1b}] sneaks

If you sneak for a really short time, then you may have a sneak time of 1, which would lead to levitating indefinitely, as the effect would get set repeatedly. To prevent this you can use this command in a second chain command block:

/execute as @a[scores={sneaks=1}] run scoreboard players add @s sneaks 1

When the player hits their head, then you want them to stop levitating and fall again, you can achieve this with yet another chain command block and this command:

/execute as @a[scores={sneaks=2..}] at @s unless block ~ ~1.8 ~ minecraft:air run effect clear @s minecraft:levitation

Edit after some refining

You can put these commands into a repeating command block and 4 chain command blocks, it works similarly to what I explained above, but it feels more like actually jumping mid air, because the levitation effect is much stronger. I added a dummy scoreboard objective called "duration", which gets a value from the ActiveEffects-tag in the 4th command. The 5th command clears the levitation effect 17 ticks (0.85 seconds) before it would usually wear off:

/execute as @e[scores={sneaks=1},nbt={OnGround:0b}] run effect give @s minecraft:levitation 1 20 true
/scoreboard players reset @a[nbt={OnGround:1b}] sneaks
/execute as @a[scores={sneaks=1}] run scoreboard players add @s sneaks 1
/execute as @a store result score @s duration run data get entity @s ActiveEffects[{Id:25b}].Duration 1
/execute as @a[scores={duration=..17}] run effect clear @s

Note: I removed the command that checks if you hit your head, because the levitation effect will be so short that you hardly notice that you levitate against a ceiling.

Note 2: this has the side effect of adding some kind of super jump if you sneak and jump while sneaking, this jump is about 4 blocks high and you cannot perform a double jump from it. A double jump takes you about 3 blocks high.