Jump to content

How to use BlockStateProvider to generate blockstate assets?


Darkorg69

Recommended Posts

So I am trying to generate crop blockstate.json files by using BlockStateProvider.

I'm not finding a method in the class handling crops.

I guess it has to be done with the VariantBuilder from getVariantBuilder(BLOCK).forAllStates(state -> ...), 

from what I understand the function takes returns ConfiguredModel[] but how I am supposed to add definitions for each variant?

Documentation is not very clear, can someone briefly explain how does this work.

I'm using Forge 40.1.86 for 1.18.2.

Generated JSON's should look like this:

{
  "variants": {
    "age=0": {
      "model": "examplemod:block/example_crop_stage0"
    },
    "age=1": {
      "model": "examplemod:block/example_crop_stage0"
    },
    "age=2": {
      "model": "examplemod:block/example_crop_stage1"
    },
    "age=3": {
      "model": "examplemod:block/example_crop_stage1"
    },
    "age=4": {
      "model": "examplemod:block/example_crop_stage2"
    },
    "age=5": {
      "model": "examplemod:block/example_crop_stage2"
    },
    "age=6": {
      "model": "examplemod:block/example_crop_stage2"
    },
    "age=7": {
      "model": "examplemod:block/example_crop_stage3"
    }
  }
}

 

Link to comment
Share on other sites

It's my bad I forgot about the online docs, I meant there was not enough documentation in the IDE when looking through the classes.

I got it to work now.

public class ModBlockStateProvider extends BlockStateProvider {
    public ModBlockStateProvider(DataGenerator gen, String modid, ExistingFileHelper exFileHelper) {
        super(gen, modid, exFileHelper);
    }

    @Override
    protected void registerStatesAndModels() {
        addCropBlock(ModBlocks.TOMATOES.get());
    }

    public void addCropBlock(Block pBlock) {
        this.getVariantBuilder(pBlock)
                .forAllStates(state -> 
                        ConfiguredModel.builder()
                                .modelFile(
                                        models().getExistingFile(
                                           modLoc(pBlock.getRegistryName().getPath() + "_stage" + state.getValue(((CropBlock) pBlock).getAgeProperty()))
                                        )
                                )
                                .build()
                );
    }
}
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
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.



×
×
  • Create New...

Important Information

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