我试图使用Java代码为我的世界制作一个mod,并且看到我没有在问与我的世界直接相关的东西,我想我会在这里问它。

我的代码里面
renderItem.getItemModelMesher().register(copperIngot, 0, new ModelResourceLocation(RefStrings.MODID + ":" + ((CopperIngot) copperIngot).getName(), "inventory"));

在该行的最后,它调用函数getName(),该函数位于文件CopperIngot中,并返回字符串“ copperingot”。我想要做的是获取变量“ copperIngot”的名称,并将其设置为名为name的字符串内的小写字母。

我这样做的原因是,我可以拥有1个文件块,1个块文件等等。(块和块是在Minecraft基本代码中创建的变量类型Block和Item)

现在我正在使用tinker_fairy发布的以下代码

`Field[] fields = MainRegistry.class.getDeclaredFields();
//gives no of fields
System.out.println(fields.length);
for (Field field : fields) {
    //gives the names of the fields
    System.out.println(field.getName());
}`


这给了我这样的控制台读数:


[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:实例
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:代理
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:tabDerptech
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:scandiumIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:scandiumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:scandiumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:钛锭
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:钛尘
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:TitaniumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:钒锭
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:vanadiumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:vanadiumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:chromeIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:chromeDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:chromiumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:锰锭
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:锰尘
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:manganeseBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:ironDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:cobaltIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:cobaltDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:cobaltBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:nickelIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:nickelDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:nickelBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:copperIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:copperDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:copperBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:copperOre
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:zincIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:zincDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:zincBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:yttriumIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:yttriumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:yttriumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:锆锭
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:zirconiumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:zirconiumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:niobiumIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:niobiumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:niobiumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:钼锭
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:molybdenumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:molybdenumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:technetiumIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:technetiumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:technetiumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:钌锭
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:rutheniumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:rutheniumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:rhodiumIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:rhodiumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:rhodiumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:钯锭
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:钯粉尘
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:钯块
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:silverIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:silverDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:silverBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:镉锭
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:cadmiumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:cadmiumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:hafniumIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:hafniumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:hafniumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:tantalumIngot
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:tantalumDust
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:tantalumBlock
[com.hihellobyeoh.derptech.main.MainRegistry:init:63]:钨锭

等等...(我在那个文件中有113个变量atm)


这不是我想要的,我想要它,因为它运行getName函数,以读取变量名称,然后将该变量名称写入字符串名称。现在,使用代码,我已经为我提供了所有变量,我对于如何隔离需要查找的确切变量一无所知。

提前致谢。

最佳答案

如果我正确理解了您的问题,则想以小写形式返回类的名称,而不是硬编码的字符串。
如何在CopperIngot.java的getClass().getName().toLowerCase()方法中使用getName()

此外,您可以很好地使用反射来获取变量名称并设置新值。

Class aClass = MyObject.class;
Field field = aClass.getField("field");
MyObject objectInstance = new MyObject();
field.set(objetInstance, value);


希望这可以帮助

10-06 01:16