Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help with Registering Custom Blocks (that extends Blocks) using Deferred Registries

Featured Replies

Posted

I'm tinkering with some stuff while trying to learn modding, I made a class for a Block that extends net.minecraft.Block, but I get an error when registering it

This is the block class:

public class ExplodingBlock extends Block {
    public ExplodingBlock(Properties props) {
        super(props);

        this.setDefaultState(
            this.stateContainer.getBaseState()
                .with(CHARGE, 0)
        );

        Minecraft.getInstance().player.sendChatMessage("Block Placed with CHARGE: 0");
    }

    static final IntegerProperty CHARGE = IntegerProperty.create("charge", 0, 5);

    @Override
    protected void fillStateContainer(StateContainer.Builder<Block, BlockState> container) {
        super.fillStateContainer(container);

        container.add(new Property[]{CHARGE});
    }

    public ActionResultType onBlockActivated(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockRayTraceResult rayRes) {
        world.setBlockState(blockPos, blockState.with(CHARGE, (Integer)blockState.get(CHARGE) + 1));

        Minecraft.getInstance().player.sendChatMessage("Block Placed with CHARGE: " + (Integer)blockState.get(CHARGE));

        return ActionResultType.SUCCESS;
    }
}

 

And this is the code for registering it:

public static final RegistryObject<ExplodingBlock> EXPLODING_BLOCK = register("exploding_block",  () ->
	new ExplodingBlock(AbstractBlock.Properties.create(Material.TNT)
		.hardnessAndResistance(3, 10)
		.sound(SoundType.STONE))
);

 

I am registering other blocks that use simply RegistryObject<Block>, and the supplier is a new Block(), and they work. The code also worked when I changed the registering to Use of the Block class instead of the inheriting ExplondingBlock class, but without the functionally I wanted, of course.

 

I get the following error in the minecraft window after loading:

Tutorial Mod (tutorial) encountered an error during the load_registries event phase

Tutorial Mod (tutorial) encountered an error during the error event phase
java.lang.NullPointerException: Registry Object not present: tutorial:exploding_block

 

Edited by kiou.23
forgot to add the error message

  • Author
2 hours ago, poopoodice said:

Minecraft.getInstance().player.sendChatMessage("Block Placed with CHARGE: " + (Integer)blockState.get(CHARGE));

This will crash on server https://mcforge.readthedocs.io/en/latest/concepts/sides/, use the player provided instead.

 

You need to show more of your code

I was running Client, and I just added that for debugging purposes, is it still the cause of the error?

and what other part of the code I should add? There isn't much else, the problem I thought would be in the ExplodingBlock class

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.