end_zonexd01

Player selects TNT Minecart in their hotbar.

Player places a rail.

The mod automatically uses the TNT Minecart from the player's hotbar and places it on the rail.

The system automatically uses a bow + arrow to activate/shoot.

Everything happens automatically on the server side.

Fabric 1.21.11. name tnt higlisht

Kodari AI
KodariAug 20 at 2:05 pm 2
TntRailAutoActivator
Server-side Fabric mod for 1.21.11. When a player places a rail while holding a TNT Minecart in their hotbar, the mod automatically places the minecart on the rail and consumes it from inventory. Additionally, if the player has a bow and arrow equipped, the mod automatically shoots the minecart to activate it, all handled server-side without client mods.
  1. Create a Fabric Loom 1.14 project targeting Minecraft 1.21.11/Yarn.

  2. Add server-side rail placement detection that waits for vanilla placement to succeed.

  3. Spawn and consume a TNT minecart from the hotbar, then consume an arrow and damage a hotbar bow while firing a flaming arrow at the minecart.

  4. Add the minimal mod metadata and localization resources.

Created the Fabric 1.21.11 project TNT Highlight.

  • Detects successful rail placement server-side.

  • Finds a TNT minecart in the player’s hotbar and places it on the new rail.

  • Finds a bow and arrow in the hotbar.

  • Consumes one arrow, damages the bow, and fires a flaming arrow at the TNT minecart.

  • Uses Yarn mappings, Fabric Loader 0.19.2, Fabric API 0.141.4, Java 21.

  • No external libraries or manifest.kod required.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 257
src/main/java/com/kodari/tnthighlight/TntHighlightMod.java
package com.kodari.tnthighlight;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.block.AbstractRailBlock;
import net.minecraft.block.BlockState;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
import net.minecraft.entity.vehicle.TntMinecartEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.item.BowItem;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.world.World;

public class TntHighlightMod implements ModInitializer {
    public static final String MOD_ID = "tnthighlight";

    private static final List<PendingRailPlacement> PENDING_PLACEMENTS = new ArrayList<>();

    @Override
    public void onInitialize() {
        UseBlockCallback.EVENT.register(TntHighlightMod::onUseBlock);
        ServerTickEvents.END_SERVER_TICK.register(server -> processPendingPlacements());
    }

    private static ActionResult onUseBlock(
            net.minecraft.entity.player.PlayerEntity player,
            World world,
            net.minecraft.util.Hand hand,
            BlockHitResult hitResult) {
        if (!(world instanceof ServerWorld serverWorld) || player.isSpectator()) {
            return ActionResult.PASS;
        }

        ItemStack heldStack = player.getStackInHand(hand);
        if (!(heldStack.getItem() instanceof BlockItem blockItem)
                || !(blockItem.getBlock() instanceof AbstractRailBlock)) {
            return ActionResult.PASS;
        }

        BlockPos clickedPos = hitResult.getBlockPos();
        BlockPos adjacentPos = clickedPos.offset(hitResult.getSide());
        queuePlacement(serverWorld, clickedPos, player.getUuid());
        if (!adjacentPos.equals(clickedPos)) {
            queuePlacement(serverWorld, adjacentPos, player.getUuid());
        }

        return ActionResult.PASS;
    }

    private static void queuePlacement(ServerWorld world, BlockPos pos, UUID playerId) {
        BlockState previousState = world.getBlockState(pos);
        if (previousState.getBlock() instanceof AbstractRailBlock) {
            return;
        }

        PENDING_PLACEMENTS.add(new PendingRailPlacement(world, pos.toImmutable(), previousState, playerId));
    }

    private static void processPendingPlacements() {
        for (int index = PENDING_PLACEMENTS.size() - 1; index >= 0; index--) {
            PendingRailPlacement placement = PENDING_PLACEMENTS.remove(index);
            BlockState currentState = placement.world().getBlockState(placement.pos());
            if (!(currentState.getBlock() instanceof AbstractRailBlock)
                    || placement.previousState().getBlock() instanceof AbstractRailBlock) {
                continue;
            }

            ServerPlayerEntity player = placement.world().getServer()
                    .getPlayerManager().getPlayer(placement.playerId());
            if (player == null || player.isSpectator() || player.getEntityWorld() != placement.world()) {
                continue;
            }

            placeAndFireTntMinecart(placement.world(), player, placement.pos());
        }
    }

    private static void placeAndFireTntMinecart(ServerWorld world, ServerPlayerEntity player, BlockPos railPos) {
        ItemStack minecartStack = findHotbarItem(player, Items.TNT_MINECART);
        ItemStack bowStack = findHotbarBow(player);
        ItemStack arrowStack = findHotbarItem(player, Items.ARROW);
        if (minecartStack == null || bowStack == null || arrowStack == null) {
            return;
        }

        Box minecartBox = new Box(railPos).expand(0.35, 0.25, 0.35);
        if (!world.getEntitiesByClass(AbstractMinecartEntity.class, minecartBox, entity -> true).isEmpty()) {
            return;
        }

        TntMinecartEntity minecart = EntityType.TNT_MINECART.create(world, SpawnReason.TRIGGERED);
        ArrowEntity arrow = EntityType.ARROW.create(world, SpawnReason.TRIGGERED);
        if (minecart == null || arrow == null) {
            return;
        }

        Vec3d minecartPosition = new Vec3d(
                railPos.getX() + 0.5,
                railPos.getY() + 0.0625,
                railPos.getZ() + 0.5);
        minecart.refreshPositionAndAngles(minecartPosition.x, minecartPosition.y, minecartPosition.z, 0.0f, 0.0f);
        if (!world.spawnEntity(minecart)) {
            return;
        }

        Vec3d direction = minecartPosition.subtract(player.getEyePos()).normalize();
        arrow.setOwner(player);
        arrow.refreshPositionAndAngles(player.getX(), player.getEyeY(), player.getZ(), player.getYaw(), player.getPitch());
        arrow.setVelocity(direction.x, direction.y, direction.z, 3.0f, 0.0f);
        arrow.setOnFireFor(100);
        world.spawnEntity(arrow);

        minecartStack.decrement(1);
        arrowStack.decrement(1);
        bowStack.damage(1, player, net.minecraft.entity.EquipmentSlot.MAINHAND);
    }

    private static ItemStack findHotbarItem(ServerPlayerEntity player, net.minecraft.item.Item item) {
        for (int slot = 0; slot < 9; slot++) {
            ItemStack stack = player.getInventory().getStack(slot);
            if (stack.isOf(item)) {
                return stack;
            }
        }
        return null;
    }

    private static ItemStack findHotbarBow(ServerPlayerEntity player) {
        for (int slot = 0; slot < 9; slot++) {
            ItemStack stack = player.getInventory().getStack(slot);
            if (stack.getItem() instanceof BowItem) {
                return stack;
            }
        }
        return null;
    }

    private record PendingRailPlacement(ServerWorld world, BlockPos pos, BlockState previousState, UUID playerId) {
    }
}