Old Ff

Create a Minecraft Java Fabric 1.21.11 mod. The player's default movement speed should be 50% faster than vanilla. The longer the player walks, the faster they become (momentum system). If the player stops moving completely for 5 seconds, the speed must reset back to the modded default fast speed.

Kodari AI
KodariSep 26 at 6:43 am 1
MomentumRunner
MomentumRunner is a Fabric mod for Minecraft 1.21.11 that enhances player movement with a dynamic momentum system. The base movement speed is increased by 50% over vanilla. As the player continuously walks or runs, their speed gradually increases, rewarding sustained movement. If the player remains completely stationary for 5 seconds, the accumulated momentum resets, returning the player to the modded default fast speed. The mod tracks player input and movement state to seamlessly adjust speed, providing a fluid and engaging traversal experience.
  1. Create the Fabric 1.21.11 project files and mod entrypoint.

  2. Add a server-tick momentum system: 50% default speed bonus, progressive speed while moving, and reset after 100 stationary ticks.

Created the Fabric 1.21.11 mod project. Players get a 50% movement-speed bonus, gain momentum while moving up to an additional 100% bonus, and return to the default boosted speed after 100 stationary ticks (5 seconds).

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 188
src/main/java/com/kodari/swiftstride/SwiftStride.java
package com.kodari.swiftstride;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;

import java.util.Map;
import java.util.WeakHashMap;

public class SwiftStride implements ModInitializer {
    private static final Identifier DEFAULT_SPEED_ID = Identifier.of("swiftstride", "default_speed");
    private static final Identifier MOMENTUM_SPEED_ID = Identifier.of("swiftstride", "momentum_speed");
    private static final EntityAttributeModifier DEFAULT_SPEED_MODIFIER = new EntityAttributeModifier(
            DEFAULT_SPEED_ID,
            0.5,
            EntityAttributeModifier.Operation.ADD_MULTIPLIED_BASE
    );
    private static final double MOMENTUM_GAIN_PER_TICK = 0.005;
    private static final double MAX_MOMENTUM_BONUS = 1.0;
    private static final double MOVEMENT_EPSILON_SQUARED = 1.0E-6;
    private static final int STATIONARY_RESET_TICKS = 100;
    private static final Map<ServerPlayerEntity, MovementState> PLAYER_STATES = new WeakHashMap<>();

    @Override
    public void onInitialize() {
        ServerTickEvents.END_WORLD_TICK.register(SwiftStride::updatePlayers);
    }

    private static void updatePlayers(ServerWorld world) {
        for (ServerPlayerEntity player : world.getPlayers()) {
            EntityAttributeInstance movementSpeed = player.getAttributeInstance(EntityAttributes.MOVEMENT_SPEED);
            if (movementSpeed == null) {
                continue;
            }

            MovementState state = PLAYER_STATES.computeIfAbsent(
                    player,
                    ignored -> new MovementState(player.getX(), player.getZ())
            );
            double x = player.getX();
            double z = player.getZ();
            double deltaX = x - state.lastX;
            double deltaZ = z - state.lastZ;
            boolean moved = deltaX * deltaX + deltaZ * deltaZ > MOVEMENT_EPSILON_SQUARED;
            state.lastX = x;
            state.lastZ = z;

            if (moved) {
                state.stationaryTicks = 0;
                state.momentum = Math.min(MAX_MOMENTUM_BONUS, state.momentum + MOMENTUM_GAIN_PER_TICK);
            } else if (state.stationaryTicks < STATIONARY_RESET_TICKS) {
                state.stationaryTicks++;
                if (state.stationaryTicks == STATIONARY_RESET_TICKS) {
                    state.momentum = 0.0;
                }
            }

            if (!movementSpeed.hasModifier(DEFAULT_SPEED_ID)) {
                movementSpeed.addTemporaryModifier(DEFAULT_SPEED_MODIFIER);
            }

            if (Double.compare(state.appliedMomentum, state.momentum) != 0) {
                movementSpeed.removeModifier(MOMENTUM_SPEED_ID);
                if (state.momentum > 0.0) {
                    movementSpeed.addTemporaryModifier(new EntityAttributeModifier(
                            MOMENTUM_SPEED_ID,
                            state.momentum,
                            EntityAttributeModifier.Operation.ADD_MULTIPLIED_BASE
                    ));
                }
                state.appliedMomentum = state.momentum;
            }
        }
    }

    private static final class MovementState {
        private double lastX;
        private double lastZ;
        private double momentum;
        private double appliedMomentum = Double.NaN;
        private int stationaryTicks;

        private MovementState(double x, double z) {
            this.lastX = x;
            this.lastZ = z;
        }
    }
}