0

我正在尝试使用 Java 代码为我的世界制作一个 mod,并且看到我不是在询问与我的世界直接相关的事情,我想我会在这里问它。

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

在该行的末尾,它调用位于文件 CopperIngot 中的函数 getName(),并返回字符串“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]:titaniumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:titaniumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63 ]: 钛块 [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: vanadiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: vanadiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init: 63]:钒块[com.hihellobyeoh.derptech.main.MainRegistry:init:63]: chromiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: chromiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: chromiumBlock [com.hihellobyeoh .derptech.main.MainRegistry:init:63]: manganeseIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: manganeseDust [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]: 铜块 [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: 铜矿 [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: 锌锭 [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]: 锆块 [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: 铌锭 [com.hihellobyeoh.derptech.main.MainRegistry:init:63] : niobiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: 铌块 [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: molybdenumIngot [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]: rutheniumIngot [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]: palladiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63 ]: palladiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: palladiumBlock [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]:cadmiumIngot [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]: 钽锭 [com.hihellobyeoh.derptech.main.MainRegistry:init:63 ]: tantalumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: 钽块 [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: tungstenIngothihellobyeoh.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]: tungstenIngothihellobyeoh.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]: tungstenIngot

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

这不是我想要的,我希望它在运行 getName 函数时读取变量名,然后将该变量名写入字符串名称。现在有了代码,我给了我所有的变量,我不知道如何隔离我需要找到的确切变量。

提前致谢。

4

2 回答 2

1

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

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

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

希望这可以帮助

于 2015-03-02T19:42:30.700 回答
0

您只想将其写入变量吗?

Field[] fields = MainRegistry.class.getDeclaredFields();
//gives no of fields
System.out.println(fields.length);         
for (Field field : fields) {
    //gives the names of the fields
    String name = field.getName().toLowerCase();
    if ( name.indexOf( "copper" ) > -1 ) { 
        System.out.println("you found copper: " + name);
    }   
}
于 2015-03-02T19:14:05.263 回答