x0pier

Make me a mod that can fill player inventory with 64 strings (if they inv almost just ignore) when do /string

Kodari AI
KodariJul 10 at 6:52 pm 5
StringRefill
StringRefill is a lightweight Fabric 1.21.11 utility mod that adds the /string command. When a player runs the command, the mod inserts one full stack of 64 Minecraft string items into their inventory. Any available inventory space is used automatically, while excess items are silently ignored if the inventory is full or nearly full. The command is player-only and requires no configuration, permissions setup, client installation, extra libraries, or custom items.
  • Create a Fabric 1.21.11 Gradle project using Java 21.

  • Add the /string command through Fabric’s command registration API.

  • Insert up to 64 vanilla string items into the executing player’s inventory.

  • Ignore any items that cannot fit.

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

import com.mojang.brigadier.Command;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StringRefill implements ModInitializer {
    public static final String MOD_ID = "stringrefill";
    public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

    @Override
    public void onInitialize() {
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
            dispatcher.register(CommandManager.literal("string")
                .requires(source -> source.isExecutedByPlayer())
                .executes(context -> {
                    ServerPlayerEntity player = context.getSource().getPlayerOrThrow();
                    ItemStack strings = new ItemStack(Items.STRING, 64);
                    player.getInventory().insertStack(strings);

                    int inserted = 64 - strings.getCount();
                    context.getSource().sendFeedback(
                        () -> Text.literal("Added " + inserted + " string."),
                        false
                    );

                    return Command.SINGLE_SUCCESS;
                })
            )
        );
    }
}