veveninactive

a command gives a vanilla fishing rod that strikes lightning at the exact location of the enemy and has 10 hearts damage and 30 second cooldown. ported to 1.21.11 and fabric

Kodari AI
KodariOct 31 at 4:20 pm 1
StormRod
Adds a custom fishing rod weapon to Minecraft Fabric 1.21.11. When used, the rod strikes lightning at the targeted enemy's exact location, dealing 10 hearts of damage. The ability has a 30-second cooldown, enforced per player. The mod registers a custom item, handles cooldown tracking, and integrates with Fabric's API for item use and entity targeting.
  1. Add a /lightningrod command that gives the executing player a marked vanilla fishing rod.

  2. Make right-clicking an entity with that rod call lightning at the target’s position, deal 10 hearts, and apply a 30-second cooldown.

Added a Fabric 1.21.11 mod with /lightningrod, which gives the player a vanilla fishing rod. Right-click an entity with it to strike lightning at its current position and deal 10 hearts of damage; the rod then has a 30-second cooldown.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 160
src/main/java/com/kodari/lightningrod/LightningRodMod.java
package com.kodari.lightningrod;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LightningEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.world.World;

public class LightningRodMod implements ModInitializer {
    private static final String MARKER_KEY = "lightningrod";

    @Override
    public void onInitialize() {
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
                dispatcher.register(CommandManager.literal("lightningrod").executes(context -> {
                    ServerPlayerEntity player = context.getSource().getPlayerOrThrow();
                    ItemStack rod = new ItemStack(Items.FISHING_ROD);
                    NbtCompound customData = new NbtCompound();
                    customData.putBoolean(MARKER_KEY, true);
                    rod.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(customData));
                    player.giveOrDropStack(rod);
                    return 1;
                })));

        UseEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
            ItemStack stack = player.getStackInHand(hand);
            if (!isLightningRod(stack)) {
                return ActionResult.PASS;
            }

            if (world.isClient()) {
                return ActionResult.SUCCESS;
            }

            if (!(world instanceof ServerWorld serverWorld) || !(player instanceof ServerPlayerEntity serverPlayer)) {
                return ActionResult.FAIL;
            }

            if (serverPlayer.getItemCooldownManager().isCoolingDown(stack)) {
                return ActionResult.FAIL;
            }

            LightningEntity lightning = new LightningEntity(EntityType.LIGHTNING_BOLT, serverWorld);
            lightning.setCosmetic(true);
            lightning.setPosition(entity.getX(), entity.getY(), entity.getZ());
            serverWorld.spawnEntity(lightning);

            entity.damage(serverWorld, entity.getDamageSources().lightningBolt(), 20.0F);
            serverPlayer.getItemCooldownManager().set(stack, 600);
            return ActionResult.SUCCESS;
        });
    }

    private static boolean isLightningRod(ItemStack stack) {
        NbtComponent customData = stack.get(DataComponentTypes.CUSTOM_DATA);
        return customData != null && customData.copyNbt().getBoolean(MARKER_KEY, false);
    }
}