media_te6h
๐Ÿ“Ž 1 file 4,480 chars
๐Ÿ“„
Create a Minecraft Java.txt
txt 139 lines
โ–ถ
Create a Minecraft Java Edition Fabric mod for Minecraft 1.21.11.

The mod should automatically perform the spear's lunge action when the player presses a configurable keybind. Set the default keybind to J.

Requirements:

1. Mod platform

- Minecraft Java Edition 1.21.11
- Fabric Loader
- Fabric API
- Client-side mod only
- Use Java
- Provide complete, compilable source code
- Include the required fabric.mod.json, Gradle configuration, mixins only if actually required, and all Java source files.

2. Keybind

- Add a client keybind named something like "Lunge Spear".
- Default key: J.
- Make the keybind configurable through Minecraft's Controls menu.
- Register the keybind using Fabric's client keybinding system.
- Do not hardcode J after registration. Use the registered keybind's state.

3. Spear detection

- Only activate the lunge when the player is holding a spear in the appropriate hand.
- Check both the main hand and offhand if necessary.
- Do not trigger while the player is holding another item.
- Use the correct Minecraft 1.21.11 item/API names rather than outdated mappings.

4. Cooldown
   Use the player's item cooldown manager to prevent triggering the lunge while the spear is cooling down.

Use this logic as a reference:

client.player.getItemCooldownManager().isCoolingDown(...)

Make sure the code passes the correct spear item to the cooldown check for Minecraft 1.21.11.

5. Key handling
   The lunge should happen when the J key is pressed.

Prefer a press-event style check so one key press causes one lunge. For example:

while (dashKey.wasPressed())

Do not repeatedly trigger the lunge every client tick while the key is being held unless Minecraft's input behavior specifically requires it.

Also explain the difference between:

dashKey.isPressed()

and:

dashKey.wasPressed()

and use the appropriate one for the requested behavior.

6. Lunge direction
   The spear lunge should move the player in the direction the player is looking.

Use the player's rotation vector, with the relevant Minecraft 1.21.11 method, such as:

player.getRotationVec(1.0F)

or the appropriate equivalent for the current mappings.

The movement should account for the player's current look direction rather than using fixed X, Y, or Z values.

7. Velocity
   Apply the lunge by modifying the player's velocity using the correct 1.21.11 API.

The provided reference code includes:

player.velocityModified

Determine whether velocityModified is still required in Minecraft 1.21.11 and use the correct modern API if it has changed.

Do not blindly copy outdated code. Explain any API changes.

8. Lunge behavior
   When J is pressed:

- Check that the player exists.
- Check that the player is holding a spear.
- Check that the spear is not on cooldown.
- Get the player's current rotation/look vector.
- Apply a forward lunge based on that vector.
- Preserve reasonable existing movement where appropriate.
- Avoid launching the player uncontrollably upward.
- Make the lunge feel similar to a spear charge/lunge rather than a generic jump.
- Do not activate while the player is in a menu or screen where gameplay input should not apply.
- Avoid repeatedly activating from a single key press.

9. Code quality
   Use clean, readable Java.

Separate keybind registration and lunge logic when appropriate.

Add comments explaining:

- Keybind registration
- Key press detection
- Spear detection
- Cooldown checking
- Rotation vector calculation
- Velocity modification

10. Version accuracy
    This is specifically for Minecraft 1.21.11 Fabric.

Do not provide code based on old Minecraft versions without checking whether the relevant classes, methods, or mappings changed.

Pay particular attention to these reference APIs:

client.player.getItemCooldownManager().isCoolingDown(...)
dashKey.isPressed()
dashKey.wasPressed()
player.getRotationVec(1.0F)
player.velocityModified

If any of these are outdated or incorrect for 1.21.11, replace them with the correct 1.21.11 Fabric/Yarn equivalent and clearly explain the replacement.

11. Output
    Return:

- Complete project folder structure
- build.gradle
- gradle.properties
- settings.gradle
- fabric.mod.json
- Java source files
- Any required mixin files
- Exact instructions for building with ./gradlew build
- Exact instructions for putting the resulting JAR into the Minecraft mods folder
- A short explanation of how the lunge works

The final code must be complete and compile-ready, not pseudocode.
Kodari AI
KodariOct 29 at 1:30 pm 3
SpearLungeKeybind
A client-side Fabric mod for Minecraft 1.21.11 that adds a configurable keybind (default J) to perform a spear lunge. When pressed, the mod checks if the player holds a spear in either hand, verifies the spear isn't on cooldown, calculates the player's look direction vector, and applies a forward velocity impulse. Uses wasPressed() for single-press detection, respects item cooldowns via getItemCooldownManager(), and prevents activation while in menus. Includes clean separation of keybind registration and lunge logic with comprehensive comments.
  1. Verify the 1.21.11 spear, keybind, tick-event, cooldown, and movement APIs from the mounted source.

  2. Create the complete Fabric client-only project and its entrypoint/resources.

  3. Summarize the one-press lunge behavior and Kodariโ€™s compile/download steps.

Created a client-only Fabric 1.21.11 project with the J keybind, spear detection in either hand, a 10-tick cooldown, and a look-directed lunge.

Plain Text
spearlunge/
โ”œโ”€โ”€ build.gradle
โ”œโ”€โ”€ gradle.properties
โ”œโ”€โ”€ settings.gradle
โ””โ”€โ”€ src/
    โ”œโ”€โ”€ client/java/com/kodari/spearlunge/SpearLungeClient.java
    โ””โ”€โ”€ main/resources/
        โ”œโ”€โ”€ fabric.mod.json
        โ””โ”€โ”€ assets/spearlunge/lang/en_us.json

No mixins or third-party libraries are needed. wasPressed() consumes discrete presses, so one press triggers one lunge; isPressed() reports the held state and could repeat the action on successive ticks. The lunge checks ItemTags.SPEARS, adds an impulse in the playerโ€™s look direction, and preserves existing velocity. Vanilla 1.21.11 spears have no built-in item cooldown, so the mod applies a 10-tick cooldown through the playerโ€™s cooldown manager. velocityModified is not used: addVelocity marks velocity dirty.

This is a keybound velocity lunge, not the vanilla Lunge enchantment effect, which triggers after a piercing attack.

Compile and download in Kodari: On desktop, click Compile in the top bar; on phone, tap โ‹ฎ โ†’ Compile Project. Click Compile in the window, then Download on the successful build. Put the downloaded JAR in the mods folder of the Fabric instance you want to use; Fabric API must also be installed.

Kodari can make mistakes. Review important code before using.
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);
    }
}