Jump to content

Register a Block in Minecraft 1.11


Nathansmash9000
 Share

Recommended Posts

I'm trying to make a block in Minecraft 1.11. Although, I'm having trouble registering it. Here's my code for my block:

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemBlock;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class BlockPower extends Block {
	
	public static final String name = "Power";
	
	public BlockPower()
    {
		super(Material.IRON);
        this.setCreativeTab(CreativeTabs.REDSTONE);
    }
}

 

Link to comment
Share on other sites

That's because this class is never ever even "seen" by Minecraft.

(You HAVE to call setRegistryName(string) in your block. Either directly in the constructor or later down the road when you register it. (setRegistryName returns the block itself, with the registryName set))

 

Create a new class, which you will be using for registration of blocks (and items (held blocks & blocks stored in inventories etc are item (technically ItemBlocks)))
Annotate this class with @EventBusSubscriber
Create a public static void method, and subscribe it to the Register<Block> event.

 

Now, I like to put all my blocks in a list/set collection, and for that collection, call forEach(event.getRegistry::register).

Of course, you can always just use a for-each loop and call event.getRegistry().register(block).

 

If you do not want to use a collection, use event.getRegistry().registerAll(), and supply the registerAll all your blocks. (block1, block2... blockn)

Now, create another public static void method, but subscribe to the Register<Item> event instead.
For each block you registered in just now, you will need to create a corresponding ItemBlock (so that it can exist in inventories & your hand etc)
Either for-each loop over the collection, create a new ItemBlock(block) & set it's RegistryName to the block's RegistryName and register it in the event, OR use another lambda expression:

blockCollection.stream().map(block -> new ItemBlock(block).setRegistryName(block.getRegistryName())).collect(Collectors.toList()).forEach(event.getRegistry()::register);

Wee bit messier than before. (for each block in this collection(that contains blocks!), create a new ItemBlock, and set it's RegistryName to the block's RegistryName. Put this new ItemBlock into a new list, and for each thing in this new list, register).

if you have any normal items, you can simply do the same thing we did with blocks, and call forEach(event.getRegistry::register) on the item-list.
(or as said stick to a simple for-each loop)

  • Like 2

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

15 hours ago, Nathansmash9000 said:

That's all my code.

Then you need to read up on how to structure a mod. You should also search out mods that have full source code at github so you can see examples.

 

For starters, you need a main mod class annotated with "@mod..."

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

15 hours ago, Matryoshika said:

Create a new class, which you will be using for registration of blocks (and items (held blocks & blocks stored in inventories etc are item (technically ItemBlocks)))
Annotate this class with @EventBusSubscriber
Create a public static void method, and subscribe it to the Register<Block> event.

Can you show me some code on how to do this? Sorry, I'm new at Java, and I'm not sure how to suscribe.

Link to comment
Share on other sites

On 6/20/2017 at 9:25 AM, Nathansmash9000 said:

Sorry, I'm new at Java

Ouch, that's the kiss of death around here. The forum even tells us "... this is not a Java school. You are expected to have basic knowledge of Java before posting here. "

 

Go learn Java (and programming in general, and find some Java references / help elsewhere online or in school or by recruiting a programmer friend ftf.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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
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.

 Share



×
×
  • Create New...

Important Information

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