Jump to content

[SOLVED] [1.10.2] Detect if a biome is a specific type of forest


Recommended Posts

Posted

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?

Posted

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.

Posted

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.

Posted

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.

Posted

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.

Posted

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?

Posted

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.

Posted

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.

Posted

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.

Posted

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.

Posted

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 }

Posted

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Please see https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/ for information on how to post your log correctly.
    • Hello!  The detailed description of how you got to where you are is certainly valuable.  But, at the end of the day (well, any time of the day actually), it is going to be the actual logs that going to provide the necessary details to hopefully solve your startup issue. Part of me wonders if you have installed a client-only mod on a dedicated server.  But I may very well be wrong, and it will be the logs that will tell that story.
    • Hello there! I didn't quite know where to go regarding this, but ended up deciding to post it here. I have been running a forge server with around 200 mods for me and some friends to play on casually, but have recently started to get an issue when booting the server. This all started after I decided to add some new mods to the server. Like usual, I add a mod, test run the server for any issues, and if all is well, I'll add a next one and so on until I have added all that I wanted to. After doing so, in all test runs, it all seemed to work just fine. However, the next day, after trying to boot the server, I kept getting an error regarding java.lang.NullPointerException, towards one of the mods I had recently added. So far so good, I removed the mod that was causing the issue, started up the server again, and here in when things took a turn for the worse. I received another java.lang.NullPointerException null error that wouldn't allow me to boot the server, but this time with a mod that wasn't part of the new ones I had recently added. I found this weird, but nonetheless, I removed it thinking it might be causing some conflicts with some of the new ones. Afterwards, booting the server again proved to be impossible, as it gave me another java.lang.NullPointerException null error with the 3rd mod I had ever installed on the server! This mod was there since the start, it added some biomes and had been just fine so far. This turn of events made me remove all the newer mods I had recently added in hopes to fix this whole ordeal, but alas, to no avail. Same error, with that same biome mod that had been there since day one. Reluctantly, I removed the biome mod, booted the server, and voila! The server was running, although without a major mod that had always been there to begin with. As I do not wish to part ways with this mod, specially since it had been working so far without any issues, I tried to bring everything back to how it was before I added those new mods, but kept on getting the same java.lang.NullPointerException null error for the biome mod. Even adding the newer mods won't cause me this error, with exception of the one that started it all, which I find quite odd since the mods I had been using without any issues are now giving me the same error the newer one that started it all gave me. Now, I have checked that everything is up to date regarding the mods, forge (forge-1.20.1-47.3.12) and java. The modpack runs perfectly fine when I start Minecraft itself, and play singleplayer, or even when I open a LAN world, everything works. Everything aside from the server. From what I could gather, this java.lang.NullPointerException null error would point to a missing value of sorts, for an item perhaps, within the mod that is causing the error, but aside from removing the whole mod, I lack the knowledge on how to fix this. With this in mind, if anyone would be so kind as to shine some light into this situation, with a way to fix all this blunder, I would be most grateful!
    • If you want a mana GUI, call Minecraft.getInstance().setScreen(new ManaScreen()); somewhere display your GUI. I would recommend creating a keybind and listening to key events, and if your key bind is pressed down, running that code.
    • You are creating an entire new screen, or GUI. What you probably want is an Overlay.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.