Jump to content

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


Recommended Posts

Posted (edited)

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

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