Jump to content

Recommended Posts

Posted

Hello. This is my first topic.

 

I am trying to add new block on forge 1.12.2. I Succeeded to add new Block with following code : 

// Main.java
package doph.niho.cookmod.system;

import java.util.Iterator;

import doph.niho.cookmod.init.BlockRegisterer;
import doph.niho.cookmod.init.CookModFood;
import doph.niho.cookmod.init.RecipeDeleter;
import net.minecraft.advancements.Advancement;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFood;
import net.minecraft.item.crafting.IRecipe;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@Mod(modid = Define.MODID, name = Define.MOD_NAME, version = Define.MOD_VERSION, acceptedMinecraftVersions = Define.MOD_MC_VERSION)
@EventBusSubscriber
public class Main {

	@SubscribeEvent
	public static void OnItemsRegistered(RegistryEvent.Register<Item> e) {
		CookModFood.RegisterFood();
		BlockRegisterer.Init();
		BlockRegisterer.RegisterItemBlock();
	}

	@SubscribeEvent
	public static void OnBlocksRegistered(RegistryEvent.Register<Block> e) {
		BlockRegisterer.RegisterFood();
	}

	@SubscribeEvent
	public static void OnRecipesRegistered(RegistryEvent.Register<IRecipe> e) {
		RecipeDeleter.DeleteRecipe();
	}

	// ファイルの読み込みやブロック等の登録
	@EventHandler
	public void PreInit(FMLPreInitializationEvent e) {
	}

	// レシピ追加、Mod用の各種データ設定
	@EventHandler
	public void Init(FMLInitializationEvent e) {
		BlockRegisterer.RegisterModel();
		CookModFood.RegisterModel();
	}

	// 他MODとの連携
	@EventHandler
	public void PostInit(FMLPostInitializationEvent e) {
	}
}
//BlockRegisterer.java
package doph.niho.cookmod.init;

import doph.niho.cookmod.block.BlockManaita;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.registry.ForgeRegistries;

public class BlockRegisterer {

	public static Block manaita;

	public static ItemBlock manaitaItem;

	public static void RegisterModel() {
		RegisterRender(manaita, "_oak", "_spruce", "_birch", "_jungle", "_acacia", "_dark_oak");
	}

	public static void RegisterFood() {
		ForgeRegistries.BLOCKS.register(manaita);
	}

	public static void RegisterItemBlock() {
		ForgeRegistries.ITEMS.register(manaitaItem);
	}

	public static void RegisterRender(Block block, String... subName) {
		int len = subName.length;
		if (len == 0) {
			ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0,
					new ModelResourceLocation(block.getRegistryName(), "inventory"));
		} else {
			for (String str : subName) {
				ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0,
						new ModelResourceLocation(block.getRegistryName() + str, "inventory"));
			}
		}
	}

	public static void Init() {
		manaita = new BlockManaita();
		manaitaItem = new ItemBlock(manaita);
	}
}

I could add Block, but when try to register ItemBlock, NullPointerException occur, so I can't register Block as Item.

How to register or create instance of ItemBlock?

Posted

Thank you for your advice.

I tried following codes, However NullPointerException occured.

package doph.niho.cookmod.init;

import doph.niho.cookmod.block.BlockManaita;
import doph.niho.cookmod.itemblock.BaseItemBlock;
import doph.niho.cookmod.itemblock.ItemBlockManaita;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.common.registry.ForgeRegistries;

public class BlockRegisterer {

	public static Block manaita;

	public static BaseItemBlock manaitaItem;

	public static void RegisterModel() {
		RegisterRender(manaita, "_oak", "_spruce", "_birch", "_jungle", "_acacia", "_dark_oak");
	}

	public static void RegisterFood() {
		ForgeRegistries.BLOCKS.register(manaita);
	}

	public static void RegisterItemBlock() {
		ForgeRegistries.ITEMS.register(manaitaItem);
	}

	public static void RegisterRender(Block block, String... subName) {
		int len = subName.length;
		if (len == 0) {
			ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0,
					new ModelResourceLocation(block.getRegistryName(), "inventory"));
		} else {
			for (String str : subName) {
				ModelLoader.setCustomModelResourceLocation(Item.getItemFromBlock(block), 0,
						new ModelResourceLocation(block.getRegistryName() + str, "inventory"));
			}
		}
	}

	public static void Init() {
		manaita = new BlockManaita();
		manaitaItem = new ItemBlockManaita();
	}
}

 

package doph.niho.cookmod.itemblock;

import doph.niho.cookmod.init.BlockRegisterer;
import doph.niho.cookmod.system.Define;
import net.minecraft.util.ResourceLocation;

public class ItemBlockManaita extends BaseItemBlock{

	public ItemBlockManaita() {
		super(BlockRegisterer.manaita);
		this.setRegistryName(new ResourceLocation(Define.MODID, "manaita"));
		this.setUnlocalizedName("manaita");
	}

}

 

Error Log :

java.lang.NullPointerException: Can't use a null-name for the registry, object null.
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:864) ~[guava-21.0.jar:?]
    at net.minecraftforge.registries.ForgeRegistry.add(ForgeRegistry.java:287) ~[ForgeRegistry.class:?]
    at net.minecraftforge.registries.ForgeRegistry.add(ForgeRegistry.java:281) ~[ForgeRegistry.class:?]
    at net.minecraftforge.registries.ForgeRegistry.register(ForgeRegistry.java:114) ~[ForgeRegistry.class:?]
    at doph.niho.cookmod.init.BlockRegisterer.RegisterFood(BlockRegisterer.java:23) ~[BlockRegisterer.class:?]
    at doph.niho.cookmod.system.Main.OnBlocksRegistered(Main.java:37) ~[Main.class:?]
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_4_Main_OnBlocksRegistered_Register.invoke(.dynamic) ~[?:?]
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?]
    at net.minecraftforge.fml.common.eventhandler.EventBus$1.invoke(EventBus.java:143) ~[EventBus$1.class:?]
    at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:179) [EventBus.class:?]
    at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:736) [GameData.class:?]
    at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:604) [Loader.class:?]
    at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:270) [FMLClientHandler.class:?]
    at net.minecraft.client.Minecraft.init(Minecraft.java:513) [Minecraft.class:?]
    at net.minecraft.client.Minecraft.run(Minecraft.java:421) [Minecraft.class:?]
    at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
    at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
    at GradleStart.main(GradleStart.java:26) [start/:?]

 

java.lang.NullPointerException: Can't use a null-name for the registry, object null.
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:864)
    at net.minecraftforge.registries.ForgeRegistry.add(ForgeRegistry.java:287)
    at net.minecraftforge.registries.ForgeRegistry.add(ForgeRegistry.java:281)
    at net.minecraftforge.registries.ForgeRegistry.register(ForgeRegistry.java:114)
    at doph.niho.cookmod.init.BlockRegisterer.RegisterFood(BlockRegisterer.java:23)
    at doph.niho.cookmod.system.Main.OnBlocksRegistered(Main.java:37)
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_4_Main_OnBlocksRegistered_Register.invoke(.dynamic)
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
    at net.minecraftforge.fml.common.eventhandler.EventBus$1.invoke(EventBus.java:143)
    at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:179)
    at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:736)
    at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:604)
    at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:270)
    at net.minecraft.client.Minecraft.init(Minecraft.java:513)
    at net.minecraft.client.Minecraft.run(Minecraft.java:421)
    at net.minecraft.client.main.Main.main(Main.java:118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)
    at GradleStart.main(GradleStart.java:26)

Posted

Thank you. I called method creates instance of ItemBlock at FMLPreInitializationEvent, Error disappered. Finally, I could add ItemBlock to minecraft!

Posted

There are some other things with your code you should probably change. There is a ModelRegistryEvent for the models. Also, each of the registries passes the actual registry in the event (in your case called "e") parameter so you should be passing that allong to your registerer methods and then those methods should obtain the registry from that.

 

For example, you have

23 hours ago, K10 said:

public static void RegisterFood() { ForgeRegistries.BLOCKS.register(manaita); }

But that should really be:

   public static void RegisterFood(RegistryEvent.Register<Block> e) { e.getRegistry().register(manaita); }

 

Basically, you're assuming that you know what the registry is but you should really pass the actual registry along.

 

Secondly, I think you're trying to be too clever by breaking up all the methods the way you are. I know it is nice to organize things into small dedicated methods but in this case you have to be very careful about the order of instantiation, registration, constructing and other initialization so it can be easier keep clear by keeping it together instead of hopping around between classes and methods.

 

Lastly, you should move to the @ObjectHolder injection system for instantiating your singleton fields.

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.