User Tools

Site Tools


zh_cn:tutorial:modding_tips

This is an old revision of the document!


模组编写技巧

以下是与Fabric有关的编写模组的小技巧。这些技巧是从API的使用经验中积累的。

基本(API)

  • 由于 Fabric API 开发的基于注入的方法,我们不需要以用户可见的方式彻底修改类。发现一些你做不到的东西时,你有时只需要找原版类的 Fabric 扩展。比如:
    • Block.Settings → FabricBlockSettings
    • EntityType.Builder → FabricEntityTypeBuilder
  • 对于内建的资源包或者数据包,请确保分别存在“assets/模组id”或“data/模组id”的目录!IDEA用户可能会发现他们偶然创建了“assets.模组id”目录——这不起作用。

Mixins

  • 要将类强制转换为它未实现的接口,或强制转换为最终类,或将mixin强制转换为目标类,你可以使用“(TargetClass) (Object) sourceClassObject”的技巧。
  • @Redirect 和 @ModifyConstant mixins 目前无法嵌套(多个模组同时应用在同一个地方)。这在开发中可能以后会改变——然而,目前,同@Overwrite一样,请尽量避免使用(or discuss bringing the hook over to Fabric's API, or - for more niche things - consider putting it in a small JAR-in-JAR API).
  • If you're adding non-private custom fields or methods, prefix them with “[modid]$” or another unique string and annotate them with @Unique. Essentially, “mymod$secretValue” instead of “secretValue”. This is to avoid conflicts between mods adding a field or method named the same way.

网络

  • Packets always begin execution on the network thread, however accesses to most Minecraft things are not thread-safe. In general, if you're not exactly sure what you're doing, you want to parse the packet on the network thread (read all the values out), then use the task queue to perform additional operations on the main server/client thread.

Pitfalls

  • Avoid using the java.awt package and its subpackages. AWT does not work well on all systems. Several users have reported that it hangs Minecraft.
zh_cn/tutorial/modding_tips.1643512314.txt.gz · Last modified: 2022/01/30 03:11 by solidblock