Jump to content

[1.8][solved-ish, bug, fix-ish in thread] GameRegistry.findblock returning null?


Tschallacka

Recommended Posts

When I do

 

block = GameRegistry.findBlock("minecraft", "chest");

 

it returns null.

 

For all I can see in the GameData class is that it should work. Getting the UniqueIdentifier from a blockstate works perfectly so they are in there.. Yet they don't match when I try to get the block later.

 

Does anyone have a workaround for it?

How much wood could a woodchuck chuck if a wood chuck could chuck wood - Guybrush Treepwood

 

I wrote my own mod ish... still a few bugs to fix. http://thaumcraft.duckdns.org/downloads/MagicCookies-1.0.6.4.jar

Link to comment
Share on other sites

if I look at the logs the chest does get added, which the unique id implies to by giving back the name and modid.

 

[17:55:18] [Client thread/TRACE] [FML/]: Registry add: minecraft:chest 54 net.minecraft.block.BlockChest@1f440e7 (req. id 54)

 

so it is in the registry, but the findblock doesnt find it in the minecraft:chest combo. Going to try to open up by reflection the class now to see whats going on...

How much wood could a woodchuck chuck if a wood chuck could chuck wood - Guybrush Treepwood

 

I wrote my own mod ish... still a few bugs to fix. http://thaumcraft.duckdns.org/downloads/MagicCookies-1.0.6.4.jar

Link to comment
Share on other sites

For those interested I found out the error occurs in the containsKey method. that always returns false.

 

to get an object out of it though, you can use this.

 

 

              Class gdClass = GameData.class;
		try {
		Method method = gdClass.getDeclaredMethod("getMain");
		method.setAccessible(true);
		GameData gamedata = (GameData)method.invoke(null);
		FMLControlledNamespacedRegistry<Block> b = gamedata.getBlockRegistry();
		Loggingthingy.log.warn(b.getKeys());// All the keys are there...
		Block found = b.getObject("minecraft:chest");
		if(found != Blocks.air) {// maybe add a check if you want an air block...
			// Do something with found, whatever..
		}
	}
	catch(Exception ex) {
		ex.printStackTrace();
	}

 

Now, to figure out where to submit a bug report....

How much wood could a woodchuck chuck if a wood chuck could chuck wood - Guybrush Treepwood

 

I wrote my own mod ish... still a few bugs to fix. http://thaumcraft.duckdns.org/downloads/MagicCookies-1.0.6.4.jar

Link to comment
Share on other sites

Figured out how to submit a bug ish ;-)

 

http://www.minecraftforge.net/forum/index.php/topic,29993.0.html

 

for those who just want to stay in the findBlock flow, I made this method.

 

Tweak as you see fit

 

/**
 * Tries to approach all legit ways to get a block from the game registry
 * but will go hackish when needed to to get the desired result.
 * @param modid String The modname
 * @param blockname String the block name
 * @return Blocks.air on not finding anything or the desired block.
 */
public static Block findBlock(String modid,String blockname) {
	Block find = GameRegistry.findBlock(modid,blockname);
	// legit way fails?
	if(find == null) {
		String searchkey = modid+":"+blockname;
		// Lets fire up the reflection...
		// You might want to put the map somewhere so you
		// don't have to reflect every time. saves a lot of cpu time.
		Class x = GameData.class;
			try {
				Method method = x.getDeclaredMethod("getMain");
				method.setAccessible(true);
				GameData gamedata = (GameData)method.invoke(null);
				// and I mean saving the below list b
				FMLControlledNamespacedRegistry<Block> b = gamedata.getBlockRegistry();
				// lets be gracious and give it a chance to find it this way
				if(b.containsKey(searchkey)) {
					find = b.getObject(searchkey);
				}
				else {
					// take a wild stab. returns air if nothing found
					find = b.getObject(searchkey);
					if(find != Blocks.air) {
						WhoTookMyCookies.log.warn("Chest found: "+GameRegistry.findUniqueIdentifierFor(b.getObject("minecraft:chest")).name);
					}
					else {
						if(!searchkey.equals("minecraft:air")) {
							// this is where we go and cry.
							// you can choose to leave find air which it is at this point
							// or set it to null...
						}
					}
				}
			}
			catch(Exception ex) {
				// your error handling here
			}
		}

	return find;
}

How much wood could a woodchuck chuck if a wood chuck could chuck wood - Guybrush Treepwood

 

I wrote my own mod ish... still a few bugs to fix. http://thaumcraft.duckdns.org/downloads/MagicCookies-1.0.6.4.jar

Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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