User Tools

Site Tools


ru:tutorial:introduction

This is an old revision of the document!


Введение в разработку модов с помощью Fabric

Это руководство призвано объяснить и показать некоторые вещи, которые вы сможете использовать при разработке модов. Чтобы создавать моды для Minecraft, вам часто придётся сталкиваться с написанием необычного кода, который поймёт Minecraft Так как изменения в Minecraft превносятся достаточно быстро, он, по своей сути, не предназначен для модификаций.

В отличии от других библиотек для создания модов, Fabric не является надстройкой над игрой, чтобы дать разработчикам больше функций. Вместо этого, код встроен с помощьюбиблиотеку Mixin.

Mixin'ы временами работают ненадёжно, нестабильно, и даже могут вызывать конфликты. Тем не менее, некоторые базовые функции уже встроены в Fabric API. Если нужной вам функции в Fabric API нет, зачастую для этого можно найти сторонние библиотеки. Практически в любой ситуации рекомендуется использовать отличающиеся от Fabric API библиотеки вместо того, чтобы импортировать Mixin самостоятельно.

Однако, это может и не понадобиться вовсе. Да, Minecraft не предназначен для разработки модов , но тем не менее, он содержит в себе достаточно много всего, с чем можно работать без каких-либо изменений извне.

Эта статья расскажет о способах взаимодействия с Minecraft, в порядке предпочтительности.

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 also uses JSON data files for various data-driven features. You can add JSON files to your mod, which are then injected by the Fabric API. 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 in the same way as the core Fabric API can. You should use these instead of writing your own mixins where possible to minimize the possibility for conflicts.

You can find an incomplete lists of third party libraries on this wiki.

Mixins

Finally, you can use mixins. Mixins are a powerful feature that let you change any of Minecraft's own code. Some mixins can cause conflict, but used responsibly these are key to adding unique behavior to your mod.

Mixins come in a variety of flavors, in rough order of preference:

  • Adding Interfaces
  • Callback Injectors
  • Redirect Injectors
  • Overwrites, you should never use these

This is not a complete list, but rather a quick overview. Some mixin types are omitted here.

Adding Interfaces

This is probably one of the safest ways to use mixins. New interface implementations can be added to any Minecraft class. You can then access the interface by casting the class to it. This doesn't change anything about the class, it only adds new things, and is therefore very unlikely to conflict.

One caveat is that the function signature (name + parameter types) you inject must be unique. So if you use common parameter types, be sure to give it a very unique name.

Callback Injectors

Callback injectors let you add callback hooks to existing methods, as well as on specific method calls within that method. They also let you intercept and change the return value of a method, and return early. Callback injects can stack, and are therefore unlikely to cause conflicts between mods.

Redirect Injectors

Redirects let you wrap method calls or variable access within a target method with your own code. Use these very sparingly, a target call or access can only be redirected once between all mods. If two mods redirect the same value, that will cause a conflict. Consider callback injects first.

Overwrite

Avoid overwrites completely. They replace a method entirely, removing all existing code and conflicting with any other types of mixins on the method. They are extremely likely to conflict not just with other mods, but also with changes to Minecraft itself. You most likely do not need an overwrite to do what you want to do, please use something else.

ru/tutorial/introduction.1598185321.txt.gz · Last modified: 2020/08/23 12:22 by magicsweet