Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 167
src/client/java/com/kodari/spearlunge/SpearLungeClient.java
package com.kodari.spearlunge;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.tag.ItemTags;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
public class SpearLungeClient implements ClientModInitializer {
private static final double LUNGE_STRENGTH = 1.15;
private static final double MAX_UPWARD_IMPULSE = 0.18;
private static final double MAX_DOWNWARD_IMPULSE = -0.08;
private static final int LUNGE_COOLDOWN_TICKS = 10;
private static KeyBinding lungeKey;
@Override
public void onInitializeClient() {
// Register a normal Controls-menu binding; the key's state is read from this object afterward.
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("spearlunge", "controls"));
lungeKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.spearlunge.lunge", GLFW.GLFW_KEY_J, category)
);
ClientTickEvents.END_CLIENT_TICK.register(SpearLungeClient::onEndClientTick);
}
private static void onEndClientTick(MinecraftClient client) {
if (client.currentScreen != null || client.player == null) {
// Discard queued presses so a press made in a menu cannot fire after returning to gameplay.
while (lungeKey.wasPressed()) {
// Intentionally consume input while gameplay input is unavailable.
}
return;
}
// wasPressed consumes discrete presses; isPressed would remain true and repeat the lunge each tick.
while (lungeKey.wasPressed()) {
tryLunge(client);
}
}
private static void tryLunge(MinecraftClient client) {
var player = client.player;
if (player == null) {
return;
}
// Detect a spear in either hand, preferring the main hand when both hands hold spears.
ItemStack spearStack = player.getMainHandStack();
if (!spearStack.isIn(ItemTags.SPEARS)) {
spearStack = player.getOffHandStack();
if (!spearStack.isIn(ItemTags.SPEARS)) {
return;
}
}
// Check the selected spear stack's cooldown, then apply a brief cooldown after a successful lunge.
var cooldownManager = player.getItemCooldownManager();
if (cooldownManager.isCoolingDown(spearStack)) {
return;
}
// Use the current look vector so the lunge follows the player's aim, while bounding vertical force.
var look = player.getRotationVec(1.0F);
double verticalImpulse = Math.max(MAX_DOWNWARD_IMPULSE,
Math.min(MAX_UPWARD_IMPULSE, look.y * 0.25));
// Add an impulse rather than replacing velocity, preserving existing movement; addVelocity marks it dirty.
player.addVelocity(look.x * LUNGE_STRENGTH, verticalImpulse, look.z * LUNGE_STRENGTH);
cooldownManager.set(spearStack, LUNGE_COOLDOWN_TICKS);
}
}