我正在尝试使用Java代码为我的世界制作一个mod,并且因为我不是在询问与我的世界直接相关的东西,我想我会在这里问它。
我的代码里面有
renderItem.getItemModelMesher().register(copperIngot, 0, new ModelResourceLocation(RefStrings.MODID + ":" + ((CopperIngot) copperIngot).getName(), "inventory"));
在该行的末尾,它调用函数getName(),它位于文件CopperIngot中,并返回字符串" copperingot"。我想要做的是得到变量的名称" copperIngot"并将其设置为名为name的字符串中的小写。
我这样做的原因是我可以拥有1个文件锭,1个文件用于块等等(块和锭是变量类型Block和Item,它们是在Minecraft基本代码中创建的)
现在我正在使用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]:instance [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:proxy [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]:titaniumBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:vanadiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:vanadiumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:vanadiumBlock [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]:锰英语 [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]:zirconiumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]:锆粉尘 [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]: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]:铑粉末 [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]: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函数,读取变量名称,然后将该变量名称写入String名称。现在我的代码我给了我所有的变量我不知道如何隔离我需要找到的确切变量。
提前致谢。
答案 0 :(得分:1)
如果我正确理解您的问题,而不是硬编码字符串,您希望以小写的形式返回该类的名称。
如何在 CopperIngot.java 的getClass().getName().toLowerCase()
方法中使用getName()
。
此外,您可以很好地使用反射来获取变量名称并设置新值。
Class aClass = MyObject.class;
Field field = aClass.getField("field");
MyObject objectInstance = new MyObject();
field.set(objetInstance, value);
希望这有帮助
答案 1 :(得分: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);
}
}