Jump to content

[1.12.2]I am having a lot of trouble understanding registry events and actually getting them to work


Recommended Posts

Posted

Hello. I am an old modder from the days of 1.6 and 1.7. A lot of stuff has changed since then so I guess I have to re-learn a lot of mcforge stuff.

 

I am having a lot of trouble getting a custom block added. Earlier today, I believed that I had the block class files set up correctly but couldn't get them to initialize.

From what I understand, the blocks have to be registered differently than they used to.  At first I thought I couldn't get the blocks to register but if I put System.out.println lines in my register function, it will display in the console and if I change my block's registry name, Minecraft gives an error about it indicating that there was something there.

 

So now I think that there is a problem with the way I am making the blocks. I tried 2 methods of getting a block to work. I made a bunch of custom classes as seen in various tutorials and I also made a block which is basically a direct extension of BlockStone which in theory should mean it has no excuse not to work and should at least show a copy of the stone block. That isn't the case though because of my 2 blocks, none of them show up in the creative menu. I also tried using the give command (/give *player* supertnt:testblock to attempt to force the game to show them but it still doesn't work. It just gives me an error saying "There is no such item with name supertnt:testblock".

 

Here is what I came up with in my main class file to register the blocks:

public static final BlockTestBlock WHATEVER_TEST = new BlockTestBlock("testblock",
			Material.ROCK,
			CreativeTabs.BUILDING_BLOCKS,
			5F,
			15F,
			"pickaxe",
			1);
    
    public static final BlockStoneCopy WHATEVER_TEST_SECOND = new BlockStoneCopy("testblock2",
			Material.ROCK,
			CreativeTabs.BUILDING_BLOCKS,
			5F,
			15F,
			"pickaxe",
			1);
    
    @Mod.EventBusSubscriber(modid = SuperTNTMod.MODID)
	public static class RegistrationHandler {
		public static final Set<ItemBlock> ITEM_BLOCKS = new HashSet<>();

		/**
		 * Register this mod's {@link Block}s.
		 *
		 * @param event The event
		 */
	@SubscribeEvent
	public static void registerBlocks(final RegistryEvent.Register<Block> event) {
		final IForgeRegistry<Block> registry = event.getRegistry();
		System.out.println("register blocks event executed");

		final Block[] blocks = {WHATEVER_TEST, WHATEVER_TEST_SECOND};
		registry.registerAll(blocks);
		System.out.println("register all function executed");
	}
	}

 

Here are the contents of my "BlockTestBlock" file:

package com.xeraster.supertnt.blocks;

import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;

public class BlockTestBlock extends BlockBase{
	public BlockTestBlock(String name, Material mat, CreativeTabs tab, float hardness, float resistance, String tool, int harvest) {
		super(name, mat, tab, hardness, resistance, tool, harvest);
	}
	//public BlockTestBlock() {
	//	super("testblock", Material.ICE, CreativeTabs.BUILDING_BLOCKS, 10.0F, 10.0F, "pickaxe", 1);
	//}

}

 

and then, here are the contents of the "BlockBase" file:

package com.xeraster.supertnt.blocks;
import net.minecraft.block.*;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;

public class BlockBase extends Block{
	public BlockBase(String name, Material mat, CreativeTabs tab, float hardness, float resistance, String tool, int harvest) {
		this(name, mat, tab, hardness, resistance);
		setHarvestLevel(tool, harvest);
	}
	
	public BlockBase(String name, Material mat, CreativeTabs tab, float hardness, float resistance, float light) {
		this(name, mat, tab, hardness, resistance);
		setLightLevel(light);
	}
	
	public BlockBase(String name, Material mat, CreativeTabs tab, float hardness, float resistance) {
		super(mat);
		setUnlocalizedName(name);
		setRegistryName(name);
		setHardness(hardness);
		setResistance(resistance);
		setCreativeTab(tab);
	}

}

If someone could point out why the above posted code doesn't produce the desired effect of putting a block in my game, that would be greatly appreciated.

Another thing I would like to post though is the contents of my "BlockStoneCopy" file. This is just a fundamentally uncomplicated thing and it just baffles me why it doesn't work. I expected to be able to just do the following and get a clone of the stone block to show up but it doesn't actually do anything.

package com.xeraster.supertnt.blocks;

import net.minecraft.block.BlockStone;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;

public class BlockStoneCopy extends BlockStone{
	public BlockStoneCopy(String name, Material mat, CreativeTabs tab, float hardness, float resistance, String tool, int harvest) {
		//this.(name, mat, tab, hardness, resistance, tool, harvest);
		this.setRegistryName(name);
		this.setUnlocalizedName(name);
		this.setCreativeTab(tab);
		this.setHardness(hardness);
		this.setResistance(resistance);
		this.setHarvestLevel(tool, harvest);
	}

}

If anyone could point out where my code is wrong and how to change it to get the desired effects, I would be really grateful.

Posted

Sorry to reply to my own topic but I randomly got lucky and found in TestMod3 Choonster's TestMod3 that I have to add this to my RegistrationHandler class under the registerBlocks subscribe event function:

@SubscribeEvent
	public static void registerItemBlocks(final RegistryEvent.Register<Item> event) {
		final ItemBlock[] items = {
				new ItemBlock(WHATEVER_TEST),
				new ItemBlock(WHATEVER_TEST_SECOND),
		};

		final IForgeRegistry<Item> registry = event.getRegistry();

		for (final ItemBlock item : items) {
			final Block block = item.getBlock();
			final ResourceLocation registryName = Preconditions.checkNotNull(block.getRegistryName(), "Block %s has null registry name", block);
			registry.register(item.setRegistryName(registryName));
			ITEM_BLOCKS.add(item);
		}

		//registerTileEntities();
}

 

I guess I was confused because I thought that Blocks and Items were different things and that it wasn't possible for a block to also be an item. But in 1.12, I presume anything that could be in a player's inventory is considered an "item" which is why it has to be initialized as an item to be holdable by the player.

Posted

Blocks have always had ItemBlock variants, at the very least since 1.2... (~6 years & counting)
However, previous to the 1.8 rendering update, the models for the ItemBlocks were made for you, from the blocks' models', if I remember correctly.

And Blocks & Items are different. An ItemBlock is an Item that represents a block. They do not equal eachother in any way.

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.

Posted (edited)

this muchAs you're coming from 1.7, my registration method may be of interst to you.

Take these two files:

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/hardlib/EasyRegistry.java

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/hardlib/client/ClientEasyRegistry.java

Set them as the common proxy/client proxy of a library mod (just need this much and this line), update the references to HardLib to point to your own lib main class.

 

And you're good to go. You just call it like this.

 

There's other variant registry methods too, e.g. blocks with no item (cake), items, blocks with custom items (when you need to subclass ItemBlock), items with custom mesh definitions (nbt-driven models), custom state mappers (where you want properties on your blocks but want to handle the blockstate stuff differently than standard, an example: 2D_ITEM isn't in the blockstate, and FLOWER_STALK actually changes the blockstate file entirely).

 

But largely speaking you won't be using those variations much. registerBlockWithItem, registerBlockWithCustomItem, registerItem are the main three you'll be using.

 

If you want to understand how the registry system works, look for the registry events within the two EasyRegistry classes.

Edited by Draco18s

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.

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.