Jump to content

Create blockstate from string


Recommended Posts

I've searched for a few hours but can't find a way to create a blockstate from a string.  I want the user to specify a blockstate in the configuration file and then load that into the mod to create that block.


For example I want them to be able to create a grass block with snow on top (blockstate property 'snowy' = true).  There's more to the design but that's the gist of this specific problem.


I can convert from blockstate to NBTTagCompound but can't go that extra step of converting a string into NBT.  I considered allowing the user to write out separate key + value pairs in the configuration but it's also difficult to work out the type needed for each property (something that NBTTagCompound.readBlockState() seems able to do).


I'm surprised there isn't some common method to do this as I imagine other mods would need to read blockstate that's written the user.


Link to comment
Share on other sites

The way I do it is to simply use the toString method on blockstates to turn them into strings, and then use the command block parser to parse them. I use it so that users can copy blockstates from their debug overlay and add them manually. It works pretty well, but if you can use a better system, do. 


  • Like 1

About Me


My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

That also looks good.


I ended up using JsonToNBT class as suggested and it turned out fairly straightforward.


Took me a while to realize quotes are needed for some values but not others.  So i decided to just add quotes around every value and allow the user to enter key value pairs without quotes.  Otherwise they'd have to escape the quotes in the mod's JSON configuration file.



    // logs errors and returns default state
    protected IBlockState blockStateDerive(String customBlockStateText, IBlockState defaultBlockState, Log log) {
        // deserialize block state text to nbt
        NBTTagCompound customTag = null;

        Optional<String> customBlockStateTextFull = blockStateTextDecorate(customBlockStateText, log);
        if (!customBlockStateTextFull.isPresent()) {
            // malformed
            return defaultBlockState;

        try {
            customTag = JsonToNBT.getTagFromJson(customBlockStateTextFull.get());
        } catch (NBTException e) {
            // log and ignore
            log.error("Invalid block state \"" + customBlockStateText + "\" for custom fill block \""
                    + defaultBlockState.getBlock().getLocalizedName() + "\".");

            return defaultBlockState;

        assert customTag != null;

        // merge with default nbt
        NBTTagCompound defaultTag = NBTUtil.writeBlockState(new NBTTagCompound(), defaultBlockState);

        // deserialize nbt to block state
        IBlockState newBlockState = NBTUtil.readBlockState(defaultTag);

        return newBlockState;


customBlockStateText is text such as "facing:east" and blockStateTextDecorate() is a little method that formats it to the liking of JsonToNBT.  Turns it into {Properties:{facing:"east"}} .

Link to comment
Share on other sites

  • Guest locked this topic
This topic is now closed to further replies.


  • Create New...

Important Information

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