sandstone icon indicating copy to clipboard operation
sandstone copied to clipboard

Add type-safe Block State matching

Open ColinTimBarndt opened this issue 3 years ago • 12 comments

Some blocks have block states. A minecraft:sandstone_slab can for example be waterlogged and has a type (top, bottom, double). In mcfunction this can be done everywhere a block is placed or compared. There is currently no type-safe block-aware way to do this in Sandstone.

Using typescript it could be checked whether a block has the states that are specified. There is for example no type=bottom variant of a grass block.

ColinTimBarndt avatar Aug 29 '21 22:08 ColinTimBarndt

Manually adding this for all existing blocks would be insane, and generating typings is a difficult task. Not saying this is impossible, but its not easy.

MulverineX avatar Aug 29 '21 22:08 MulverineX

Manually adding this for all existing blocks would be insane, and generating typings is a difficult task. Not saying this is impossible, but its not easy.

Please take a look at this repository: https://github.com/Articdive/ArticData It has all the data required for generating the type definitions needed. It could for example be done with a function like this:

function block(id: string, state?: {[name: string]: string | number}) {
  if (state) {
    id += "[" + Object.entries(state).map(([name, val]) => `${name}=${val}`).join(",") + "]";
  }
  return id;
}

And then the overloads for blocks with states could be added to make it type-safe. The file "1_17_1_blocks.json" from the above mentioned repo is a list of all blocks and a list of their possible state names (eg. grass => ["SNOWY"]) and "1_17_1_block_properties.json" enumerates the possible values.

ColinTimBarndt avatar Aug 29 '21 22:08 ColinTimBarndt

A data source is not the problem because the community is well aware of several repositories of exported game data. Neither is the problem stringifying the states, that is fairly straightforward. The problem is the last part, because of having to make a sustainable & stable complex type generator. It's not simply a list of string literals.

MulverineX avatar Aug 29 '21 23:08 MulverineX

Couldn't this be repeated for every block that has a state?

function block(id: "minecraft:sandstone_slab", state?: {type?: "top"|"bottom"|"double"; waterlogged?: boolean});

Maybe I forgot something, but I don't see the complexity of writing a generator for this. I might try that tomorrow.

ColinTimBarndt avatar Aug 30 '21 00:08 ColinTimBarndt

I implemented this function in my sandstone-resourcepacks library. Here's a working example of the function:

blockWithState

Sources:

  • https://github.com/ColinTimBarndt/sandstone-resourcepacks/blob/master/src/blockstate.ts#L61-L72
  • https://github.com/ColinTimBarndt/sandstone-resourcepacks/blob/master/generate/blockstates.ts

ColinTimBarndt avatar Sep 03 '21 13:09 ColinTimBarndt