#Get the last value of all state values from the current block

1 messages · Page 1 of 1 (latest)

toxic sky
#

It seems that BlockStates.get(state).validValues is the only way to get the list of values of a given state, but the problem is that it gets it globally and not only from the block it's run from, so is there a way to get a list of valid state values from the block it's run from, or is there a solution to this?

woven python
toxic sky
#

Hmm, will try that.

#

If I know how to.. lol

stray merlin
toxic sky
#

Yea, but not values.

#

I.. I take it back.

#

But well, can't get the max stage of it right?

#

That's kinda the part I am stuck at rn lol

stray merlin
#
world.beforeEvents.playerBreakBlock.subscribe((event) => {
    const { block } = event;

    const states = Object.entries(block.permutation.getAllStates());

    console.warn(JSON.stringify(states));
});
``` so I can see both the states and their values
toxic sky
#

Yea, I know, but you can't get all values and return the last value in the list.

ornate maple
toxic sky
#

crop stages are in order, right. So, you can get all stages but not the final or max stage.

#

they're numbers.

#

from 0 - 3 or 0 - 7

shut tulip
woven python
#

If you go over it overflows back to the first stage which isnt ideal

shut tulip
shut tulip
ornate maple
#

@toxic sky

function getValidValuesForBlock(blockId) {
    const states = {};
    for (const stateName in BlockPermutation.resolve(blockId).getAllStates()) {
        const validValuesForState = BlockStates.get(stateName)?.validValues;
        if (validValuesForState) {
            validValuesForState.forEach((value) => {
                if (isBlockStateValid(blockId, stateName, value)) {
                    if (!states[stateName]) {
                        states[stateName] = [];
                    }
                    states[stateName].push(value);
                }
            });
        }
    }
    return states;
}
function isBlockStateValid(blockId, blockState, blockValue) {
    try {
        const state = {};
        state[blockState] = blockValue;
        const permutation = BlockPermutation.resolve(blockId, state);
        if (permutation.getState(blockState) === blockValue) {
            return true;
        }
        else {
            return false;
        }
    }
    catch (error) {
        return false;
    }
}

getValidValuesForBlock("cocoa") gives

{"age":[0,1,2],"direction":[0,1,2,3]}
toxic sky
toxic sky
toxic sky
# ornate maple <@345885507420553217> ```js function getValidValuesForBlock(blockId) { con...

Maybe you can help me understand something. This works, but let's say my block has states like [0,1,2,3,4,5], BlockStates returns [0,1,2,3]. It's like it can't go above 3? My guess would be that most of the blocks uses [0,1,2,3] so that's why it caps at 3? It works fine when 2 blocks has states like [0,1,2]. It's quite confusing but this is my guess for why it only goes up to 3 if that makes.. sense? Haha.

toxic sky
#

I figured it out.. kinda? It's odd, but one of my blocks has states from 0-7, and if that block has these states, it returns properly for other states. If this 1 block has states from 0-3, other blocks that has more than 3 states (well 4), it can't return the others and is capped at 3, the states of this 1 block..

#

I did more tests, and if I give another block the most states, it will still use states from this 1 block. This one block is a blueberry bush fyi.

#

Oh, all the blocks uses the same state with different "max values"

ornate maple
#

I haven't tried this with custom blocks that have custom values for block states if that's what you are asking?

In general it seems like BlockStates will return all the different values that any block can use. Like age can go up to 3 for cocoa but up to 15 for sugar cane.

There's a list on https://minecraft.fandom.com/wiki/Block_states

Minecraft Wiki

Block states (also known as block properties) are extra pieces of data that further define a block, such as how it appears or behaves.
In Bedrock Edition, there are also aux values (also known as Metadata) to define a block. Aux values are in the format of binary flags, which basically match the block states one-to-one. And they are accessible i...

toxic sky
#

I know this, but it's strange why it only takes all valid values from 1 block and not all of the blocks that uses this state, you know?

#

It's confusing if you ask me.

ornate maple
# toxic sky It's confusing if you ask me.

It's very confusing. I guess some crops just don't have as many stages. And it's weird that I have to write a function to just keep trying values and if they aren't valid it just sets them back to 0.

There really should be something built in to check

lunar junco
#

Looks like bug 🐛

toxic sky
#

I'll check if it's fixed in the latest preview

lunar junco
#

validValues not being "valid"

toxic sky
#

Yea.. odd

#

Just crazy why it uses the state from my blueberry bush

ornate maple
toxic sky
#

Basically yea. The screenshots shows the block and states. If I add or take values from the array, BlockStates will return this state from this block only.

#

if this block in the pic has [0,1,2,3] and my other block has [0,1,2,3,4], BlockStates returns [0,1,2,3] from the block in the pic

#

It like.. makes no sense..

ornate maple
# toxic sky It like.. makes no sense..

Oh. I didn't understand before. Yeah, that seems like a bug for sure. I guess to get around it you could have the growth stages namespaced in a way, like better_on_bedrock:blueberry_block_growth_stage. Then whatever kind of block you have you can just tack groqth_stage to the end of it.

If I had to guess I would think that the valid value list is being taken from either the first or last block that the game reads.

toxic sky
#

Could it be that this block is the first to be registered on world load with this state and all the values, which is why BlockStates chooses this block?

#

I thought it registered all blocks with this state... wild LOL

toxic sky
toxic sky
#

I assume that's what you meant.

woven python
#

I experienced this a while back

toxic sky
#

Yea. Wish it didn't, but I can live with this