Jump to content

Recommended Posts

Posted

Hey there and good evening fellow devs. :)

 

I created a simple "corn" crop, also seeds.

When I went into the game and tried to plant the seed, the game immediately crashed.

Here's the log from the error:

  Reveal hidden contents

Also, here are the classes I used and made for the crops to work:

ItemCornSeed

  Reveal hidden contents

BlockCropCorn

  Reveal hidden contents

ModBlocks & ModItems to initialize everything:

  Reveal hidden contents

I think that would be all important classes, if you miss anything, please just tell me. :) Not only I, but also a friend of mine is having the exact same problem. I followed this tutorial on how to create crops:

https://shadowfacts.net/tutorials/forge-modding-112/crops/

 

Thanks for all the help!

Cheers and have a nice evening

Korlimann

Posted
  On 2/14/2018 at 9:35 PM, Korlimann said:

    @Override
    protected Item getCrop() {
        return ModItems.CORN_SEED;
    }

Expand  

This looks fine, let me go see where this item is created...

  On 2/14/2018 at 9:35 PM, Korlimann said:

public class ModItems {

    public static final List<Item> ITEMS = new ArrayList<Item>();
    public static final Item CORN = new ItemBaseFood("corn", 2, 0.3f, false);
    
}

Expand  

Uh...

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
  On 2/14/2018 at 10:01 PM, Draco18s said:

This looks fine, let me go see where this item is created...

Uh...

Expand  

Whoops, I actually had like 30 more items in there but I deleted them so you'll only see the right ones, and I actually deleted the Corn_seed item. xD

 

Here it is:

package com.korlimann.sushimod.init;

import java.util.ArrayList;
import java.util.List;

import com.korlimann.sushimod.items.ItemBase;
import com.korlimann.sushimod.items.ItemBaseFood;
import com.korlimann.sushimod.items.ItemCornSeed;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFood;

public class ModItems {

	public static final List<Item> ITEMS = new ArrayList<Item>();

	public static final Item CORN_SEED = new ItemCornSeed(ModBlocks.CROPCORN, Blocks.FARMLAND, "corn_seed");
	public static final Item CORN = new ItemBaseFood("corn", 2, 0.3f, false);
}

 

Posted
  On 2/14/2018 at 10:03 PM, Korlimann said:

new ItemCornSeed(ModBlocks.CROPCORN, Blocks.FARMLAND, "corn_seed");

Expand  

Because your blocks and items are created in static initializer blocks, your item is being created before your block, so ModBlocks.CROPCORN is null at this point.

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

Not use a static initializer?

Seriously, control the order your code gets executed in. You HAVE to insure that your blocks get created before your items. The only way to do this is to explicitly invoke the initializers yourself.

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
@EventBusSubscriber
public class ObjectRegistry {
	public static Block BLOCK_PATH;
	public static Block BLOCK_PATH_SLAB;
	public static Block BLOCK_GHOST;
	
	public static Item ITEM_PATHMAKER;
	public static Item ITEM_COPYTOOL;
	
	public static Set<Item> items = new HashSet<Item>();
	public static Set<Block> blocks = new HashSet<Block>();
	
	public static void prepareBlocks(){
		blocks.add(BLOCK_PATH = new BlockPath());
		blocks.add(BLOCK_PATH_SLAB = new BlockPathSlab());
		blocks.add(BLOCK_GHOST = new BlockGhost());
	}
	
	public static void prepareItems() {
		items.add(ITEM_PATHMAKER = new ItemPathMaker());
		items.add(ITEM_COPYTOOL = new ItemCopyTool());
	}
	
	//This method will be called without us calling it. This is because 
	//Forge calls it -for- us, when the RegistryEvent happens. This is why
	//we had to use the @Mod.EventBusSubscriber at the top of the class.
	@SubscribeEvent
	public static void registerBlocks(Register<Block> event){
		
		//We make sure that the list gets filled with our blocks.
		prepareBlocks();
		
		for(Block block : blocks){
			event.getRegistry().register(block);
		}
	}
	
	//We do not need to call prepareBlocks() in this method, because Blocks are registered before items.
	//Thus, our registerBlocks method has already happened.
	@SubscribeEvent
	public static void registerItems(Register<Item> event){
		
		for(Block block : blocks){
			ItemBlock iblock = new ItemBlock(block);
			iblock.setRegistryName(block.getRegistryName());
			event.getRegistry().register(iblock);
		}		
		
		prepareItems();
		
		for(Item item : items) {
			event.getRegistry().register(item);
		}
	}
}

 

A really nice registry class from a tutorial that I can sadly not find anymore, otherwise I would link it.

It's really compact and nice to use. Maybe something for you too. But it follows the Forge register order so you don't have to do it yourself hehe.

  • Like 1
Posted (edited)
  On 2/14/2018 at 11:31 PM, SatyPardus said:

A really nice registry class from a tutorial that I can sadly not find anymore, otherwise I would link it.

It's really compact and nice to use. Maybe something for you too. But it follows the Forge register order so you don't have to do it yourself hehe.

Expand  

Hi!

Sorry, I only had a chance to have a look at your answer now. :)

 

So, I tried using your class, but I think I went wrong somewhere...

This would be my "RegistryHandler", as I called it:

package com.korlimann.sushimod.util.handlers;

import com.korlimann.sushimod.init.ModBlocks;
import com.korlimann.sushimod.init.ModItems;
import com.korlimann.sushimod.util.IHasModel;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@EventBusSubscriber
public class RegistryHandler {
	
	@SubscribeEvent
	public static void onBlockRegister(RegistryEvent.Register<Block> event) {
		event.getRegistry().registerAll(ModBlocks.BLOCKS.toArray(new Block[0]));
	}
	
	@SubscribeEvent
	public static void onItemRegister(RegistryEvent.Register<Item> event) {
		event.getRegistry().registerAll(ModItems.ITEMS.toArray(new Item[0]));
	}	
	
	@SubscribeEvent
	public static void onModelRegister(ModelRegistryEvent event) {		
		for(Block block : ModBlocks.BLOCKS) {
			if(block instanceof IHasModel) {
				((IHasModel)block).registerModels();
			}
		}
		for(Item item : ModItems.ITEMS) {
			if(item instanceof IHasModel) {
				((IHasModel)item).registerModels();
			}
		}
	}
}
 

I changed all of my Block and Item classes, so they'll add themselves to the items and blocks list from your ObjectRegistry class. But I'm not sure if I have to do something else, because for now, nothing of the items or blocks gets shown. Do I have to create an instance of the ObjectRegistry class somewhere? I looked into my code, but I didn't find a single line, where my RegistryHandler would've been initialized.

 

As you can see, in my ModItems and my ModBlocks classes, I had an ArrayList of the blocks and items, that then would've been initialized by my RegistryHandler.

So, expect for that now, you have the lists in the ObjectRegistry class itself, I can't see any big differences between those two classes.

Edited by Korlimann
Forgot information that could've been important
Posted

The important part of the class was, that the lists get filled inside the register event. When you add them in the other classes, they are probably added randomly again.

Also, toArray(new Item[0]) will probably create a empty array. Didn't use toArray myself yet, but in C# that thing would be empty :D

  • Like 1
Posted
  On 2/15/2018 at 8:05 PM, SatyPardus said:

Also, toArray(new Item[0]) will probably create a empty array.

Expand  

Nope:

https://www.tutorialspoint.com/java/util/arraylist_toarray.htm

  Quote

a − This is the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

Expand  

 

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
  On 2/15/2018 at 8:11 PM, SatyPardus said:

this looks like it creates a empty array

Expand  

It does. It's just that that array gets tossed and a new one is created by toArray()

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
  On 2/15/2018 at 8:05 PM, SatyPardus said:

The important part of the class was, that the lists get filled inside the register event. When you add them in the other classes, they are probably added randomly again.

Also, toArray(new Item[0]) will probably create a empty array. Didn't use toArray myself yet, but in C# that thing would be empty :D

Expand  

THANK YOU! :D

 

I managed to get it to work.

However, the texture for the crops does not get displayed. I think it's a pathing error, but I can't find the exact error. But I'll figure it out somehow. Thanks a lot anyways. :3

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.