Shxrpz

make me a fabric 1.21.11 mod that has a toggle-able auto tnt minecart place after placing a rail, it needs to be instant, but not too instant so it is fast but also looks legit

Kodari AI
KodariNov 8 at 10:58 am 1
AutoTNTMinecartPlacer
A Fabric mod for Minecraft 1.21.11 that automatically places a TNT minecart on a rail immediately after the rail is placed. The mod listens for rail placement events and instantly spawns a TNT minecart on top of the newly placed rail, with a slight delay to appear natural. Features a toggle command to enable or disable the auto-placement functionality, allowing players to control when the automation is active. The mod ensures fast placement while maintaining a realistic appearance.

Plan:

  1. Register a client-only toggle and detect a newly placed rail from the selected rail stack.

  2. After a short 3-tick delay, place a TNT minecart from the hotbar on that rail, then restore the selected slot.

  3. Create the minimal Fabric 1.21.11 project files and mod metadata.

Created RailCart for Fabric 1.21.11. Press G to toggle it. When you place a rail, it waits 3 ticks, then uses a TNT minecart from your hotbar on that rail through the normal block-interaction path, and restores your previous hotbar slot. It won’t place one if you aren’t still looking at the rail or don’t have a TNT minecart in your hotbar.

In survival, use vanilla rails and a crafted TNT minecart.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 220
src/client/java/com/kodari/railcart/RailCartClient.java
package com.kodari.railcart;

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.block.AbstractRailBlock;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.tag.ItemTags;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;

public class RailCartClient implements ClientModInitializer {
    private static final KeyBinding TOGGLE_KEY = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.railcart.toggle",
            71,
            KeyBinding.Category.create(Identifier.of("railcart", "general"))
    ));

    private static boolean enabled;
    private static int previousSelectedSlot = -1;
    private static int previousRailCount;
    private static int pendingTicks;
    private static net.minecraft.util.math.BlockPos pendingRailPos;
    private static int slotToRestore = -1;
    private static int autoSelectedSlot = -1;
    private static int restoreTicks;

    @Override
    public void onInitializeClient() {
        ClientTickEvents.END_CLIENT_TICK.register(RailCartClient::onClientTick);
    }

    private static void onClientTick(MinecraftClient client) {
        if (TOGGLE_KEY.wasPressed()) {
            enabled = !enabled;
            pendingTicks = 0;
            pendingRailPos = null;
            previousSelectedSlot = -1;
            previousRailCount = 0;

            if (client.player != null) {
                client.player.sendMessage(Text.literal("Auto TNT minecart: " + (enabled ? "ON" : "OFF")), true);
            }
        }

        if (client.player == null || client.world == null || client.interactionManager == null) {
            previousSelectedSlot = -1;
            previousRailCount = 0;
            pendingTicks = 0;
            pendingRailPos = null;
            return;
        }

        PlayerInventory inventory = client.player.getInventory();
        if (restoreTicks > 0 && --restoreTicks == 0) {
            if (inventory.getSelectedSlot() == autoSelectedSlot) {
                inventory.setSelectedSlot(slotToRestore);
            }
            slotToRestore = -1;
            autoSelectedSlot = -1;
        }

        if (pendingTicks > 0 && --pendingTicks == 0 && enabled) {
            placeTntMinecart(client, pendingRailPos);
            pendingRailPos = null;
        }

        int selectedSlot = inventory.getSelectedSlot();
        ItemStack selectedStack = inventory.getMainStacks().get(selectedSlot);
        int railCount = selectedStack.isIn(ItemTags.RAILS) ? selectedStack.getCount() : 0;

        if (enabled && pendingTicks == 0 && previousSelectedSlot == selectedSlot
                && previousRailCount > 0 && railCount < previousRailCount) {
            BlockHitResult hit = getTargetedRail(client);
            if (hit != null) {
                pendingRailPos = hit.getBlockPos();
                pendingTicks = 3;
            }
        }

        previousSelectedSlot = selectedSlot;
        previousRailCount = railCount;
    }

    private static void placeTntMinecart(MinecraftClient client, net.minecraft.util.math.BlockPos railPos) {
        if (railPos == null) {
            return;
        }

        BlockHitResult hit = getTargetedRail(client);
        if (hit == null || !hit.getBlockPos().equals(railPos)) {
            return;
        }

        PlayerInventory inventory = client.player.getInventory();
        int cartSlot = -1;
        for (int slot = 0; slot < PlayerInventory.HOTBAR_SIZE; slot++) {
            if (inventory.getMainStacks().get(slot).isOf(Items.TNT_MINECART)) {
                cartSlot = slot;
                break;
            }
        }

        if (cartSlot < 0) {
            return;
        }

        int originalSlot = inventory.getSelectedSlot();
        if (cartSlot != originalSlot) {
            inventory.setSelectedSlot(cartSlot);
        }
        client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hit);
        client.player.swingHand(Hand.MAIN_HAND);

        if (cartSlot != originalSlot) {
            slotToRestore = originalSlot;
            autoSelectedSlot = cartSlot;
            restoreTicks = 1;
        }
    }

    private static BlockHitResult getTargetedRail(MinecraftClient client) {
        if (!(client.crosshairTarget instanceof BlockHitResult hit)) {
            return null;
        }

        return AbstractRailBlock.isRail(client.world.getBlockState(hit.getBlockPos())) ? hit : null;
    }
}