zh_cn:tutorial:enum_adding
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
zh_cn:tutorial:enum_adding [2021/10/10 01:33] – created solidblock | zh_cn:tutorial:enum_adding [2022/08/18 02:37] (current) – removed solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 添加到枚举 ====== | ||
- | ===== 介绍 ===== | ||
- | 要恰当地添加到枚举,我们需要编辑一些内部字段。不确保这应用于所有的 Java 版本。因为确保正确地添加到枚举是十分重要的,比如,如果你需要制作盔甲,你不应该添加到 '' | ||
- | |||
- | ===== 创建容器类 ===== | ||
- | 这样做的目的是包含你的所有自定义枚举项。项会包含在静态的**非**常量字段中。 | ||
- | <code java> | ||
- | public class CustomAxolotlVariant { | ||
- | static { | ||
- | AxolotlEntity.Variant.values(); | ||
- | } | ||
- | | ||
- | public static AxolotlEntity.Variant PURPLE; // 你可以随便添加几个字段 | ||
- | } | ||
- | </ | ||
- | |||
- | ===== 往目标中 mixin ===== | ||
- | 如果你要往枚举中添加,建议你了解 mixin 的基本原理。 | ||
- | |||
- | ==== 访问构造器 ==== | ||
- | 我们需要构造器的调用器。前两个参数永远是内部名称和id。然后,我们有可见构造器的参数。 | ||
- | <code java> | ||
- | @SuppressWarnings(" | ||
- | @Invoker("< | ||
- | private static AxolotlEntity.Variant newVariant(String internalName, | ||
- | throw new AssertionError(); | ||
- | } | ||
- | </ | ||
- | |||
- | ==== 访问值字段 ==== | ||
- | 如果我们处理一个 Minecraft 类,则字段名称会是中间映射。要查找其名称我们需要查看字节码。要查看字节码,可以将鼠标指针指向这个类,然后到视图 -> 查看字节码。向下滚动,直到看到像这样的一行: | ||
- | <code java> | ||
- | private final static synthetic [Lnet/ | ||
- | </ | ||
- | 这意味着,在这个例子中,字段名称是 '' | ||
- | <code java> | ||
- | @SuppressWarnings(" | ||
- | @Shadow | ||
- | @Mutable | ||
- | private static @FinalAxolotlEntity.Variant[] field_28350; | ||
- | </ | ||
- | |||
- | ==== 注入项 ==== | ||
- | We are going inject after the values field is assigned. To do this we need the correct target for the for the '' | ||
- | ''< | ||
- | In our case it would be '' | ||
- | Now we can create the inject: | ||
- | <code java> | ||
- | @SuppressWarnings(" | ||
- | @Inject(method = "< | ||
- | opcode = Opcodes.PUTSTATIC, | ||
- | target = " | ||
- | shift = At.Shift.AFTER)) | ||
- | private static void addCustomVariant(CallbackInfo ci) { | ||
- | |||
- | } | ||
- | </ | ||
- | Now finally we can add our entries: | ||
- | <code java> | ||
- | var variants = new ArrayList<> | ||
- | var last = variants.get(variants.size() - 1); | ||
- | |||
- | // This means our code will still work if other mods or Mojang add more variants! | ||
- | // Repeat this section if you need more than one entry. Just remember to have unique ordinals! | ||
- | var purple = newVariant(" | ||
- | CustomAxolotlVariant.PURPLE = purple; | ||
- | variants.add(purple); | ||
- | |||
- | field_28350 = variants.toArray(new AxolotlEntity.Variant[0]); | ||
- | </ | ||
- | |||
- | ===== 访问项 ===== | ||
- | 要访问枚举项,只需要从容器类中获得字段,比如: | ||
- | <code java> | ||
- | System.out.println(CustomAxolotlVariant.PURPLE); | ||
- | </ | ||
- | |||
- | |||
- | 本教程基于lamalad的[[https:// |
zh_cn/tutorial/enum_adding.1633829596.txt.gz · Last modified: 2021/10/10 01:33 by solidblock