Jump to content

Recommended Posts

Posted

Hey fellow minecraft developers, I am working on a crop (Lemon Crop. P.S. I know it sounds wierd). I have the Lemon Seed in-game and the Lemon but, when i place the seed it crashes the game. This is extremely strange because it is giving a NullPointerExeption. I believe that it is because the Items might be registered into the game before the crops. If this is the case how can i change it. Heres is the crash log: https://pastebin.com/eZLpWgGP

 

Posted

Not strange. Almost everyone who creates a custom crop runs into this. It is because the block instance is null when you construct the seed item to your seed item tries to plan a null. Just control the order. In 1.12.2 you should be using the registry event and @ObjectHolder system for registering and instantiating. In that case, the item registration should happen after the blocks get their object holder injection.

 

Anyway, yes your suspicious is correct. It has to do with the order of registration and construction of the related classes. Just think through it and it should become obvious.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted
6 minutes ago, jabelar said:

Not strange. Almost everyone who creates a custom crop runs into this. It is because the block instance is null when you construct the seed item to your seed item tries to plan a null. Just control the order. In 1.12.2 you should be using the registry event and @ObjectHolder system for registering and instantiating. In that case, the item registration should happen after the blocks get their object holder injection.

 

Anyway, yes your suspicious is correct. It has to do with the order of registration and construction of the related classes. Just think through it and it should become obvious.

so how do i do this??

Posted (edited)

Sorry I know it can be frustrating to not get quick answers but remember we have lives. Today I had to get my daughter to the airport, have lunch with friends and now have to put in a couple hours at work. I can't look at it on my work break because my work blocks paste bin site. Ill probably be able to look at it this evening. Otherwise hopefully someone else can respond. But generally it is considered rude to bump your topic unless a full day has passed.

Edited by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Okay, I was able to find way to look at paste bin. You are not using the recommended way of using the registry events and @ObjectHolder annotation. The way you are doing it will not work as you cannot control their order of the static initialization between the two classes.

 

Please look how too use the new system and implement that. 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Okay, now I'm home and I have some time to give more specific advice. It took me a while to get into the @ObjectHolder annotation but the way you should do it is as follows:

 

In your InitBlocks and InitItems classes you should actually initialize them to null. Then you should give each class the @ObjectHolder annotation. What this does is during mod loading there are several passes after each registry event to "inject" the actual instance into the fields. The fields are matched up by the field name matching the registry name (or you can add additional annotation if you want a different field name).

 

Next you need methods that handle the registry events. I personally put those in an inner class of the class that has the @ObjectHolder instances. Those require methods regular event handling meaning the class needs the @EventBusSubscriber annotation and the methods need to be static and have the @SubscribeEvent annotation.

 

The registry events are generic so you have to handle each valid type. Such as RegistryEvent.Register<Block> and RegistryEvent.Register<Item>.  (Note you should also be using this for entities, biomes and some other things.)

 

Lastly you do the actual constructing of your singleton instances in the registry handling event.

 

You can see my example mod classes for ideas on how to put it all together: 

Now the cool thing is that if you do all this, you'll also solve your original problem because the Item registry event will happen after the Block is registered and injected into the object holder. So then when you instantiate your seed it will get the plantable block rather than null.

 

Hope that helps.

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted
9 hours ago, jabelar said:

Okay, now I'm home and I have some time to give more specific advice. It took me a while to get into the @ObjectHolder annotation but the way you should do it is as follows:

 

In your InitBlocks and InitItems classes you should actually initialize them to null. Then you should give each class the @ObjectHolder annotation. What this does is during mod loading there are several passes after each registry event to "inject" the actual instance into the fields. The fields are matched up by the field name matching the registry name (or you can add additional annotation if you want a different field name).

 

Next you need methods that handle the registry events. I personally put those in an inner class of the class that has the @ObjectHolder instances. Those require methods regular event handling meaning the class needs the @EventBusSubscriber annotation and the methods need to be static and have the @SubscribeEvent annotation.

 

The registry events are generic so you have to handle each valid type. Such as RegistryEvent.Register<Block> and RegistryEvent.Register<Item>.  (Note you should also be using this for entities, biomes and some other things.)

 

Lastly you do the actual constructing of your singleton instances in the registry handling event.

 

You can see my example mod classes for ideas on how to put it all together: 

Now the cool thing is that if you do all this, you'll also solve your original problem because the Item registry event will happen after the Block is registered and injected into the object holder. So then when you instantiate your seed it will get the plantable block rather than null.

 

Hope that helps.

 

this would take forever tho is there not a quicker way?

 

Posted (edited)

I do admit that keeping up with latest version can be a bit of work, but it is worth doing and while a number of big things have been overhauled in recent versions I think it will settle down and everything is getting more modern/professional in their implementation.

Edited by jabelar

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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



×
×
  • Create New...

Important Information

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