Fabric Documentation

repository·main·Indexed 41 days ago

https://github.com/fabricmc/fabric-docs

Official guides and API references for the Fabric modding toolchain for Minecraft. Includes documentation on unit testing with Fabric Loader JUnit, game testing with Fabric Loom, data generation for enchantments and world features, and command argument suggestions.

Tokens
131.1K
Snippets
346
Records
657
Agent score
46%

What's inside Fabric

  1. Overview of Loom plugin

    main

    Loom is a Gradle plugin for developing mods in the Fabric ecosystem. It handles the complexities of Minecraft development, including:

    • Installing Minecraft and mods into a development environment.
    • Managing Minecraft obfuscation and version/distribution differences.
    • Providing launch configurations for the Fabric Loader.
    • Handling Mixin compile processing.
    • Providing utilities for the Fabric Loader's jar-in-jar system.

    Loom supports all versions of Minecraft, including those not officially supported by the Fabric API, because it is version-independent.

  2. What is Fabric Loom?

    main
    Fabric Loom (or simply Loom) is a Gradle plugin used for developing mods in the Fabric ecosystem. It provides utilities for installing Minecraft and mods in a development environment, handling Minecraft obfuscation differences across versions, and providing run configurations for Fabric Loader, Mixin compilation processing, and the Fabric Loader jar-in-jar system. Loom is version-agnostic and supports all versions of Minecraft.
  3. Automated Testing Overview

    main

    There are two primary ways to automatically test a Fabric mod:

    1. Unit Testing with Fabric Loader JUnit: Used for testing individual code components like methods and helper classes. It uses a JUnit plugin to handle Minecraft's runtime byte-code modifications (like Mixin).
    2. Game Tests: Uses Minecraft's built-in game test framework to spin up an actual Minecraft client and server. This is suitable for testing gameplay features and end-to-end functionality.
  4. What is a SuggestionProvider?

    main

    A SuggestionProvider is a functional interface used to generate a list of values that are suggested to the user in the Minecraft command interface (e.g., when typing /give <player> <item>).

    It is used to make commands more user-friendly by allowing players to select from a list of valid arguments rather than typing them manually. Because suggestions might require asynchronous lookups (like querying a database or a remote server), the provider returns a CompletableFuture<Suggestions>.

    To implement it, you must provide a method that accepts a CommandContext and a SuggestionBuilder and returns the suggestions.

  5. What is provided by Fabric API?

    main

    Fabric API provides a wide range of APIs built upon vanilla Minecraft functionality to simplify both basic and advanced mod development. Key features include:

    • Hooks and Events: New entry points into the game logic.
    • Utilities: Tools to assist in common development tasks.
    • Transitive Access Widener: Support for accessing protected or private game elements.
    • Internal Registry Access: Access to internal registries, such as the composable item registry.
  6. What is a SuggestionProvider and how does it work?

    main

    A SuggestionProvider is a functional interface used to generate a list of suggested values for command arguments that are sent to the client. This makes commands more user-friendly by allowing players to select from a list of valid options (e.g., entity names, biomes, or sounds).

    Key technical details:

    • It is a functional interface that accepts a CommandContext and a SuggestionBuilder.
    • It returns a Suggestions object wrapped in a CompletableFuture, allowing for asynchronous suggestion generation if the data is not immediately available.
  7. Default Loom Configuration and Behavior

    main

    Loom applies several default configurations to your Gradle project to facilitate Fabric development:

    • Plugins: Automatically applies java and eclipse plugins.
    • Repositories: Adds Fabric, Mojang, and Maven Central repositories.
    • IDE Integration: If a .idea folder exists in the root project, Loom loads assets and sets up run configurations in .idea/runConfigurations.
    • Annotation Processing: Adds net.fabricmc:fabric-mixin-compile-extensions to the annotationProcessor configuration and configures all non-test JavaCompile tasks with Mixin annotation processor settings.
    • Task Dependencies:
      • The eclipse task depends on genEclipseRuns.
      • The build task depends on remapJar and remapSourcesJar.
    • Jar Remapping:
      • remapJar outputs a JAR with the same name as the jar task, while the original jar task is given the dev classifier.
      • remapSourcesJar processes the output of the sourcesJar task if it exists.
      • Both remapped tasks are added to the archives artifacts.
    • Maven Publishing: For each MavenPublication, Loom manually adds dependencies to the POM for modded dependency configurations, provided the configuration has Maven scope.
  8. Use Entrypoints for Mod Initialization

    main

    Entrypoints are specified in fabric.mod.json to tell Fabric which classes to run during game startup. There are two primary types:

    1. main entrypoint: Used for common code that runs on both client and server. The initialization class must implement ModInitializer.
    2. client entrypoint: Used for client-only code (e.g., rendering, input handling). The initialization class must implement ClientModInitializer.
  9. Understand the structure of an Advancement

    main

    An advancement in Minecraft is composed of several key components:

    • DisplayInfo: Defines how the advancement is visually presented to players.
    • AdvancementRequirements: A list of lists of criteria. At least one criterion from each sub-list must be satisfied to complete the advancement.
    • AdvancementRewards: The items or experience the player receives upon completion.
    • Strategy: Determines how multiple criteria are handled.
    • Parent Advancement: Defines the hierarchical relationship in the advancement tree.
    • Criterion: The specific requirement (trigger) that must be met.
  10. Create subcommands and command aliases

    main

    Subcommands

    To create subcommands, register a literal node for the parent command, and then append literal nodes for the subcommands. Subcommands can be optional; if a node is not marked as required, both the parent command and the full path (e.g., /command and /command sub) will be valid.

    Command Aliases (Redirection)

    You can redirect one command to another using aliases. This is useful for supporting multiple names for the same functionality.

    Note: Brigadier redirects command nodes using arguments. If you want to redirect a command without any arguments, you must provide an .executes() builder with the same logic as the target command.

  11. Understand the `Command` interface and `CommandContext`

    main

    In Brigadier (the command parser used by Minecraft), a command is represented by the com.mojang.brigadier.Command<S> functional interface.

    • Generic Type S: Represents the command source (e.g., ServerCommandSource).
    • run(CommandContext<S> context): The single method in the interface. It must return an integer.
      • A value of 0 or less typically indicates failure.
      • A positive value indicates success. Use the constant Command#SINGLE_SUCCESS for standard successful execution.
    • CommandContext<S>: Provides access to the command source via context.getSource(), parsed arguments, and the raw input string.
    • ServerCommandSource: A specific implementation of the command source in Minecraft that allows you to identify the entity, world, or server associated with the command execution.
    Command<ServerCommandSource> command = context -> {
        ServerCommandSource source = context.getSource();
        // Perform logic here
        return Command.SINGLE_SUCCESS;
    };