Posted January 14, 20178 yr In my world generator class I have been using World#getBiomeGenForCoords to get the current biome and check if it's a forest biome. However, I realized I need to detect which TYPE of forest it is (NORMAL, BIRCH, FLOWER ROOFED). I can see that BiomeForest has a Type enum, but I'm not sure how to access that value given that the current biome may or may not even be of the BiomeForest type. Does that make sense?
January 14, 20178 yr Did you try looking at the BiomeForest class? There's a field called type that stores the BiomeForest.Type value. There's no public getter method, so you'll need to access it with reflection. If you don't know whether a Biome is a BiomeForest , check with instanceof . Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
January 14, 20178 yr Author Thanks for your reply. I am already using instanceof to determine if the biome is a BiomeForest, so I'm good there. And yes, I looked at the BiomeForest class and saw the type field. I just didn't know how to access it. I've never used reflection, so I guess I have more reading to do. If there's a particularly good resource you can point me to online, I'd me much obliged. Unless someone has a quick explanation of how I can use it in this case.
January 14, 20178 yr Use the ReflectionHelper class. You'll need to know both the MCP name and the SRG name though. You'll have to use MCPBot to get the SRG name. Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
January 14, 20178 yr Jabelar has a brief explanation of reflection here. What he doesn't explain there is that each vanilla method/field has multiple names depending on the environment and that you need to check for these when accessing them via reflection. There are three types of names: Notch - Fully Obfuscated - These are the names produced by Mojang's obfuscation process, the vanilla JARs use these names. They change with each Minecraft version. They're usually a few letters long, e.g. a , bb or ff . SRG/Searge - Semi-obfuscated - These are auto-generated by MCP and FML deobfuscates to them at runtime outside of the development environment. ForgeGradle reobfuscates your code to these names when you run the build task. They remain stable between Minecraft versions. Examples include func_110121_a (a method), field_110285_bP (a field) and p_110121_0_ (a parameter). MCP - Deobfuscated - These are contributed by the community using MCPBot. They're the names you see in the development environment. Examples include isNullOrEmpty ( func_110121_a ), gallopTime ( field_110285_bP ) and str ( p_110121_0_ ). Instead of using Class#getDeclaredField or Class#getDeclaredMethod , use ReflectionHelper.findField or ReflectionHelper.findMethod to get the Field / Method object before unreflecting it into a MethodHandle (as Jabelar explains). You need to pass both the MCP and SRG names when accessing a vanilla field/method. I have a class that does this here. You can see it in use here, here, here and here. Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
January 14, 20178 yr Author Thank you kindly, good sirs. I'll read up on all that tomorrow. Hopefully it goes well. One thing, though. I noticed that the type field in BiomeForest is private final. Seems like that will block access to the field's value. Am I right? Or will reflection let me overcome that?
January 14, 20178 yr Thank you kindly, good sirs. I'll read up on all that tomorrow. Hopefully it goes well. One thing, though. I noticed that the type field in BiomeForest is private final. Seems like that will block access to the field's value. Am I right? Or will reflection let me overcome that? final means the field is read-only, i.e. you can get its value but you can't set a new value. There are ways around this (see FinalFieldHelper and EnumHelper ), but you generally shouldn't do this unless it's absolutely necessary. Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
January 14, 20178 yr There is no need to convert things into a MethodHandle unless you really need the utmost performance, which you rarely do. First measure that you actually have a performance problem with reflection. Is there any downside to using MethodHandle s instead of Method s/ Field s? Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
January 14, 20178 yr Author final means the field is read-only, i.e. you can get its value but you can't set a new value. There are ways around this (see FinalFieldHelper and EnumHelper ), but you generally shouldn't do this unless it's absolutely necessary. That's fine. I only need to read the value to find out what kind of biome it is. I was more concerned about it being private. Edit: Now that I've done more reading, I see that it is indeed possible to access private fields with reflection.
January 14, 20178 yr Edit: Now that I've done more reading, I see that it is indeed possible to access private fields with reflection. That's why it was suggested that you use reflection, because reflection can do that. Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
January 15, 20178 yr Author It seems to be working now. I didn't use MethodHandles, and performance seems to be OK. Thanks for posting the links to your example mod, Choonster; the examples were nice. I didn't find jabelar's info to be very helpful, but there was a link to a page that was super helpful: http://tutorials.jenkov.com/java-reflection/index.html It took me a minute to figure out how to use the MCPbot (http://mcpbot.bspk.rs/help). I discovered that there's yet another type of forest which is a subclass of BiomeForest that I had to account for. Here's the completed code from my world generator class, in case it helps anyone later. I'd be glad for feedback as well. Biome biome = world.getBiomeGenForCoords(pos); boolean rightForestType = false; if (biome instanceof BiomeForest && !(biome instanceof BiomeForestMutated)) { try { Class<?> biomeClass = biome.getClass(); Field typeField = ReflectionHelper.findField(biomeClass, "type", "field_150632_aF"); Object biomeType = typeField.get(biome); if (biomeType == BiomeForest.Type.NORMAL) rightForestType = true; } catch (IllegalAccessException x) { x.printStackTrace(); throw new ReflectionHelper.UnableToAccessFieldException(null, x); } if (rightForestType == true) { DO STUFF }
January 15, 20178 yr Author Ohhh, I see. I can get the field up front, because I know which class it's coming from. I don't even need to get the class itself, as long as I'm making sure to get the field value only when the biome being generated is of the right type. New code: public class PrimalWorldGen implements IWorldGenerator { static final Field typeField = ReflectionHelper.findField(BiomeForest.class, "type", "field_150632_aF"); ... private void generateTrees(WorldGenerator generator, World world, Random rand, int chunk_X, int chunk_Z, int chancesToSpawn) { Biome biome = world.getBiome(pos); boolean rightForestType = false; if (biome instanceof BiomeForest && !(biome instanceof BiomeForestMutated)) { try { Object biomeType = typeField.get(biome); if (biomeType == BiomeForest.Type.NORMAL) rightForestType = true; } catch (IllegalAccessException x) { x.printStackTrace(); throw new ReflectionHelper.UnableToAccessFieldException(null, x); } if (rightForestType == true) { DO STUFF } } }
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.