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;
}
}
}