简体   繁体   中英

Java: How do i put the name of a variable into a string?

I am trying to make a mod for minecraft using Java code, and seeing as i am not asking about something directly related to minecraft i thought i would ask it here.

Inside of my code i have renderItem.getItemModelMesher().register(copperIngot, 0, new ModelResourceLocation(RefStrings.MODID + ":" + ((CopperIngot) copperIngot).getName(), "inventory"));

At the end of that line it calls for the function getName(), which is located in the file CopperIngot, and it returns the string "copperingot". what i want to do is to get the name of the variable "copperIngot" and have it set it to lowercase inside of a string called name.

My reasons for doing this is so i can have 1 file ingots, 1 file for blocks, etc. (blocks and ingots being of the variable types Block and Item which are created in the Minecraft base code)

Right now i am using the below code posted by 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());   
}`

Which gives me a console readout like this:

[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]: 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]: copperBlock [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: copperOre [com.hihellobyeoh.derptech.main.MainRegistry:i nit: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]: 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]: molybdenumIngot [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: molybdenumDust [com.hihellobyeoh.derptech.main.MainRegistry:init:63]: molybdenumBlock [com.hihellobyeoh.derptec h.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]: silv erBlock [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

etc.....(i have 113 variables atm in that one file)

which is not what i want, i want it to, as it runs the getName function, to read the variable name and then write that variable name to the String name. right now with the code i have giving me all of the variables i am at a loss for how to isolate the exact variable i need to find.

Thanks in advance.

If I understand your question correctly, instead of hardcoded string, you wanted to return the name of the class in small case. How about using getClass().getName().toLowerCase() in getName() method of CopperIngot.java .

Moreover, you can very well use reflection to get the variable name and set the new value.

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

Hope this helps

You just want to write it to a variable?

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);
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM