bookssmp

can you make a mod that uses spear attribute swapping with a 1 tick delay when using also add a keybind so i can activate it

Kodari AI
KodariAug 27 at 6:12 pm 2
SpearSwap
A Fabric mod for Minecraft 1.21.11 that adds a spear attribute-swapping mechanic. When the player uses a spear, the mod swaps its attack attributes (damage, speed, reach) with a 1-tick delay before applying the new values. A customizable keybind lets the player activate or toggle this attribute-swapping feature at will, enabling dynamic combat style changes mid-battle.

I’ll create a Fabric 1.21.11 mod that adds a client keybind and swaps the player’s spear attack attribute for one tick while the spear is being used.

  1. Add the Fabric 1.21.11 project and split client/common source sets.

  2. Add the server-side one-tick spear attribute swap and C2S activation state.

  3. Add the client keybind, metadata, and translations.

  1. Created a Fabric 1.21.11 mod project using Yarn mappings and split client/common source sets.

  2. Added a default G keybind to toggle spear attribute swapping.

  3. Added a one-tick delay after spear use begins, then swaps the spear’s attack damage and attack speed modifiers for one tick.

  4. Added server synchronization through Fabric CustomPayload networking and translated status messages.

bookssmp

The compilation failed with this error:

code
Compilation failed with 8 error(s):

ERROR in src/main/java/com/kodari/spearswap/SpearAttributeSwapper.java at line 9: cannot find symbol
import net.minecraft.entity.attribute.AttributeModifierSlot;
                                     ^
  symbol: class AttributeModifierSlot
  location: package net.minecraft.entity.attribute

ERROR in src/main/java/com/kodari/spearswap/SpearAttributeSwapper.java at line 12: cannot find symbol
import net.minecraft.item.SpearItem;
                         ^
  symbol: class SpearItem
  location: package net.minecraft.item

ERROR in src/main/java/com/kodari/spearswap/SpearAttributeSwapper.java at line 67: cannot find symbol
        return !stack.isEmpty() && stack.getItem() instanceof SpearItem;
                                                              ^
  symbol: class SpearItem
  location: class SpearAttributeSwapper

ERROR in src/main/java/com/kodari/spearswap/SpearAttributeSwapper.java at line 106: cannot find symbol
        return builder.showInTooltip(original.showInTooltip()).build();
                                             ^
  symbol: method showInTooltip()
  location: variable original of type AttributeModifiersComponent

ERROR in   src/main/java/com/kodari/spearswap/SpearAttributeSwapper.java at line 9: cannot find symbol
  import net.minecraft.entity.attribute.AttributeModifierSlot;
                                       ^
  symbol: class AttributeModifierSlot
  location: package net.minecraft.entity.attribute

ERROR in   src/main/java/com/kodari/spearswap/SpearAttributeSwapper.java at line 12: cannot find symbol
  import net.minecraft.item.SpearItem;
                           ^
  symbol: class SpearItem
  location: package net.minecraft.item

ERROR in   src/main/java/com/kodari/spearswap/SpearAttributeSwapper.java at line 67: cannot find symbol
          return !stack.isEmpty() && stack.getItem() instanceof SpearItem;
                                                                ^
  symbol: class SpearItem
  location: class SpearAttributeSwapper

ERROR in   src/main/java/com/kodari/spearswap/SpearAttributeSwapper.java at line 106: cannot find symbol
          return builder.showInTooltip(original.showInTooltip()).build();
                                               ^
  symbol: method showInTooltip()
  location: variable original of type AttributeModifiersComponent


=== COMPILATION ERROR ANALYSIS ===

MISSING CLASSES:
  - AttributeModifierSlot
  - SpearItem
ACTION: Create these classes or check imports

MISSING METHODS:
  - showInTooltip()
ACTION: Check method names and signatures

FILES TO INVESTIGATE:
  - src/main/java/com/kodari/spearswap/SpearAttributeSwapper.java
  -   src/main/java/com/kodari/spearswap/SpearAttributeSwapper.java

Please fix this error by creating a new artifact with the corrected code.

Kodari AI
KodariAug 27 at 6:14 pm 1
  1. Remove the unavailable AttributeModifierSlot and SpearItem references.

  2. Detect spears through the vanilla spear item tag and preserve the existing one-tick swap behavior.

  3. Build the swapped component without the removed tooltip accessor.

Fixed the compilation errors in SpearAttributeSwapper.java:

  • Removed unavailable AttributeModifierSlot import.

  • Replaced missing SpearItem type checks with the vanilla ItemTags.SPEARS tag.

  • Removed the unavailable AttributeModifiersComponent.showInTooltip() call.

  • Preserved the one-tick delayed attribute swapping behavior.

The corrected artifact is ready for compilation.

bookssmp

can you make it 10 ticks instead of 1 tick

Kodari AI
KodariAug 27 at 6:18 pm 1

Changed the spear attribute swap delay from 1 tick to 10 ticks in SpearAttributeSwapper.java.

bookssmp

can you make it so it attacks when it switches to the spear

Kodari AI
KodariAug 27 at 6:21 pm 1
  1. Detect a hotbar transition onto a spear on the client.

  2. Trigger the normal Minecraft attack against the current targeted entity once per transition, while the feature is enabled.

  3. Keep the existing 10-tick attribute-swap behavior unchanged.

Updated the mod so that, when enabled, switching to a spear in the hotbar attacks the currently targeted entity once.

The existing 10-tick delayed attribute swap remains unchanged.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 362
src/main/java/com/kodari/spearswap/SpearAttributeSwapMod.java
package com.kodari.spearswap;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class SpearAttributeSwapMod implements ModInitializer {
    public static final String MOD_ID = "spear_attribute_swap";
    private static final Set<UUID> ACTIVE_PLAYERS = new HashSet<>();

    @Override
    public void onInitialize() {
        PayloadTypeRegistry.playC2S().register(TogglePayload.ID, TogglePayload.CODEC);

        ServerPlayNetworking.registerGlobalReceiver(TogglePayload.ID, (payload, context) -> {
            ServerPlayerEntity player = context.player();
            if (payload.enabled()) {
                ACTIVE_PLAYERS.add(player.getUuid());
            } else {
                ACTIVE_PLAYERS.remove(player.getUuid());
                SpearAttributeSwapper.reset(player);
            }

            player.sendMessage(
                    Text.translatable(payload.enabled()
                            ? "message.spear_attribute_swap.enabled"
                            : "message.spear_attribute_swap.disabled"),
                    true
            );
        });

        ServerTickEvents.END_SERVER_TICK.register(server -> {
            for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
                SpearAttributeSwapper.tick(player, ACTIVE_PLAYERS.contains(player.getUuid()));
            }
        });

        ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
            ServerPlayerEntity player = handler.getPlayer();
            ACTIVE_PLAYERS.remove(player.getUuid());
            SpearAttributeSwapper.reset(player);
        });
    }

    public record TogglePayload(boolean enabled) implements CustomPayload {
        public static final CustomPayload.Id<TogglePayload> ID =
                new CustomPayload.Id<>(Identifier.of(MOD_ID, "toggle"));
        public static final PacketCodec<RegistryByteBuf, TogglePayload> CODEC = PacketCodec.tuple(
                PacketCodecs.BOOLEAN,
                TogglePayload::enabled,
                TogglePayload::new
        );

        @Override
        public CustomPayload.Id<? extends CustomPayload> getId() {
            return ID;
        }
    }
}