NeresVoid

The mod adds a legendary spear for each kill, it improves at level 10 to the spear shift Added the ability to throw up

Kodari AI
KodariNov 12 at 7:32 pm 7
LegendarySpearProgression
A Minecraft Fabric mod that introduces a legendary spear obtained from kills. The spear levels up with each kill, and at level 10 it gains the ability to be thrown. Features include kill tracking, level progression, enhanced spear mechanics, and a throwable projectile system with configurable damage and cooldown.
  1. Add a survival-obtainable legendary spear with a unique texture and item assets.

  2. Make kills with it advance its level, with a noticeable upgrade at level 10.

  3. Use the trident’s built-in throwing behavior for the spear’s throw ability.

Added the Legendary Spear mod for Fabric 1.21.11. Craft it with one Netherite Ingot, one Diamond, and one Stick. It can be thrown like a trident, tracks kills made with the spear, and gains +1 attack damage at level 10. Prismarine Shards repair it.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
10 210
src/main/java/com/kodari/legendaryspear/LegendarySpearMod.java
package com.kodari.legendaryspear;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.AttributeModifierSlot;
import net.minecraft.component.type.AttributeModifiersComponent;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroups;
import net.minecraft.item.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.item.TridentItem;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class LegendarySpearMod implements ModInitializer {
    public static final String MOD_ID = "legendary_spear";
    private static final String KILLS_KEY = "Kills";
    private static final int ASCENDED_LEVEL = 10;

    public static final Item LEGENDARY_SPEAR = registerSpear();

    private static Item registerSpear() {
        Identifier id = Identifier.of(MOD_ID, "legendary_spear");
        RegistryKey<Item> key = RegistryKey.of(RegistryKeys.ITEM, id);
        Item.Settings settings = new Item.Settings()
                .registryKey(key)
                .maxDamage(250)
                .repairable(Items.PRISMARINE_SHARD)
                .attributeModifiers(TridentItem.createAttributeModifiers());
        return Registry.register(Registries.ITEM, key, new TridentItem(settings));
    }

    @Override
    public void onInitialize() {
        ItemGroupEvents.modifyEntriesEvent(RegistryKey.of(
                RegistryKeys.ITEM_GROUP,
                Identifier.of("minecraft", "combat")
        )).register(entries -> entries.addAfter(Items.TRIDENT, LEGENDARY_SPEAR));

        ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> recordKill(damageSource));
    }

    private static void recordKill(DamageSource damageSource) {
        if (!(damageSource.getAttacker() instanceof ServerPlayerEntity player)) {
            return;
        }

        ItemStack weapon = damageSource.getWeaponStack();
        if (weapon == null || !weapon.isOf(LEGENDARY_SPEAR)) {
            return;
        }

        NbtCompound customData = weapon.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT).copyNbt();
        int kills = customData.getInt(KILLS_KEY, 0) + 1;
        NbtComponent.set(DataComponentTypes.CUSTOM_DATA, weapon, nbt -> nbt.putInt(KILLS_KEY, kills));

        if (kills == ASCENDED_LEVEL) {
            weapon.set(
                    DataComponentTypes.ATTRIBUTE_MODIFIERS,
                    weapon.getOrDefault(DataComponentTypes.ATTRIBUTE_MODIFIERS, AttributeModifiersComponent.DEFAULT)
                            .with(
                                    EntityAttributes.ATTACK_DAMAGE,
                                    new EntityAttributeModifier(
                                            Identifier.of(MOD_ID, "ascended_damage"),
                                            1.0,
                                            EntityAttributeModifier.Operation.ADD_VALUE
                                    ),
                                    AttributeModifierSlot.MAINHAND
                            )
            );
            player.sendMessage(Text.literal("Your Legendary Spear has ascended at level 10!"));
        } else {
            player.sendMessage(Text.literal("Legendary Spear level: " + kills + "/" + ASCENDED_LEVEL));
        }
    }
}