User Tools

Site Tools


tutorial:introduction

This is an old revision of the document!


Introduction to Modding with Fabric (DRAFT)

This is a quick introduction to some common techniques you can use while making Fabric mods. To make mods for Minecraft, you'll often have to interact in non-standard ways with Minecraft's code. While Minecraft has increasingly become flexible to changes, it's still not inherently built to be modded.

Unlike other modding APIs, the Fabric Loader does not overwrite Minecraft's class files to add more functionality. Instead, code is injected using the Mixin library. However, most of the time you will not use this library directly.

Mixins can be fragile, and at times can cause conflicts. Therefore, some common functionality has already been implemented by the Fabric API for you. If it doesn't exist in the core Fabric API, often it will exist in a third party library. In almost every situation it's preferable to use either the Fabric API, or a third party library over implementing a mixin yourself.

Sometimes though, you don't need any of that. While Minecraft's not made for modding, it still contains a lot of features you can access without any additional changes at all.

This article will go over all the ways you can affect Minecraft, in order of preference.

Native Minecraft APIs

If Minecraft already lets you do something, don't re-invent the wheel. A good example of this is the “Registry” class, which lets you add blocks and items without any modifications to Minecraft's code.

Minecraft will also load in the JSON data files from your mod. These files are used for anything data-driven, where code isn't necessary in the first place. For example, block models and loot tables are implemented through JSON files.

The Fabric API

Fabric itself, as installed into a client, is split up into two parts.

  1. The Fabric Loader, which loads your mod and calls your entry point.
  2. The Fabric API, an optional library that provides some common useful APIs.

The API is intentionally kept relatively small, to make porting Fabric to newer Minecraft versions faster.

You can find out what's included in the Fabric API by looking over its source code on GitHub. The Fabric API contains a lot of common event hooks, and general utilities for things like networking and rendering.

Third Party APIs

Because the Fabric API is intentionally kept small and focused, third party APIs exist to fill in the gaps. Mixins allow any third party library to affect Minecraft's code just as much as the core Fabric API can. You should use these instead of writing your own mixins where possible to minimize the possibility for conflicts.

A good example of this is Cotton, which provides a variety of common utilities, as well as common resource ores, items and blocks.

Mixins

Finally, if all other methods have failed, you can use mixins. These are not to be taken lightly, they can make debugging a lot harder. But, if you need to affect Minecraft's core functionality in a way no other previously mentioned method lets you, this is the way to do it.

Mixins allow you to add any code you want to existing Minecraft classes and methods. The example project already comes with an example mixin that injects itself into the “MinecraftClient#init” method.

If you can, prefer adding interfaces to classes over injecting into existing methods. This will let you access the class in ways not previously allowed by casting an instance of it to your interface. By preferring adding interfaces, you minimize the risk of conflicts by purely adding something to the class rather than changing it.

If you do have to use injections, strongly prefer additions over overwrites. Overwrites are the most likely to conflict with other mods and in most situations you do not need them. You can inject code into any part of a method, including wrapping inner method calls and canceling them.

tutorial/introduction.1572789787.txt.gz · Last modified: 2019/11/03 14:03 by layl