#Get the last value of all state values from the current block
1 messages · Page 1 of 1 (latest)
I personally couldn't find a way to do this. There is the trySetPermutation but I couldn't get that to work for my use case. You could create a map that maps the block to the valid values manually
maybe
block.permutation.getAllStates()
for this it returns the list of all of the block states that the permutation has. https://jaylydev.github.io/scriptapi-docs/1.20.80/classes/_minecraft_server_1_10_0.BlockPermutation.html#getAllStates
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
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
Yea, I know, but you can't get all values and return the last value in the list.
What do you mean by "last" there is no order to block state values. It's a json object
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
Not really possible... But why you want to do that?
Useful for crops bonemealing to know what the last stage is
If you go over it overflows back to the first stage which isnt ideal
This is why I suggested this: https://discord.com/channels/1138536747932864532/1247270208519929967 so that all you have to do is trigger the event but no one seems to understand that
Oh yeah it resets back.
Where you will use this btw?
@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]}
I'll give it a shot and let you know.
Crops
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.
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"
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
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...
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.
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
Looks like bug 🐛
I'll check if it's fixed in the latest preview
validValues not being "valid"
Are you saying that when you create a custom blueberry bush block using age with valid values 0,1,2,3 then BlockStates won't return 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 for age anymore?
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..
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.
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
I'll give it a try and let you know.
I appear to have trouble getting the function to get any state that ends with _stage
I assume that's what you meant.
That is correct. Valid values takes the values of the first block with that state
I experienced this a while back
Yea. Wish it didn't, but I can live with this