Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type-safe Block State matching #97

Open
ColinTimBarndt opened this issue Aug 29, 2021 · 12 comments
Open

Add type-safe Block State matching #97

ColinTimBarndt opened this issue Aug 29, 2021 · 12 comments
Assignees
Labels
enhancement New feature or request

Comments

@ColinTimBarndt
Copy link

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.

@MulverineX
Copy link
Member

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.

@ColinTimBarndt
Copy link
Author

ColinTimBarndt commented Aug 29, 2021

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.

@MulverineX
Copy link
Member

MulverineX commented Aug 29, 2021

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.

@ColinTimBarndt
Copy link
Author

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
Copy link
Author

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

blockWithState

Sources:

@MulverineX
Copy link
Member

@ColinTimBarndt this can be added in a future release, and you can be the one to add it to the upcoming generation script.

@ColinTimBarndt
Copy link
Author

@MulverineX how and where should code generation and loading the game data be handled? Downloading gama data could for example be done in a separate package as it does not depend on any Sandstone code.

@MulverineX
Copy link
Member

MulverineX commented Jan 30, 2023

@ColinTimBarndt we will be using https://github.com/misode/mcmeta to source all game data. Currently code generation is not fully setup, here's the WIP script: https://github.com/sandstone-mc/sandstone/blob/ast/scripts/generateLists.js

Once #184 is merged you can start work on the block state piece of this.

@ColinTimBarndt
Copy link
Author

@ColinTimBarndt we will be using https://github.com/misode/mcmeta.

I've taken a look, but there seems to be no data on block data/the block palette. This can be exported by running the client/server jar with specific arguments, but the output seems to not be included here. I need this information in order to generate the types.

Also, which version/branch is going to be used? Always picking the latest snapshot doesn't seem like a good idea because Sanstone projects targeting an older version of the game may no longer work. It would be good to let the user pick a target game version.

@ColinTimBarndt
Copy link
Author

ColinTimBarndt commented Jan 30, 2023

As an example, I need something equivalent to the following data:

@MulverineX
Copy link
Member

Here's the full data on blocks: https://github.com/misode/mcmeta/blob/summary/blocks/data.json

As for game versions, I think a sandstone-cli command to generate those files for a specific version would be great. In the sandstone source though it will be up to date. For now let's just get it working for the latest snapshot though.

@MulverineX
Copy link
Member

Now that ast is "merged" (merge conflicts are "fun"), you can begin work on this, if it's completed before the release of 0.14 I'll roll it into that version, but for now, this Issue is set to Future Release. Thanks, and good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants