sandstone
sandstone copied to clipboard
Add type-safe Block State matching
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.
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.
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.
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.
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.
I implemented this function in my sandstone-resourcepacks library. Here's a working example of the function:
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