Topic category: User side tutorials
DISCLAIMER: You might need to know a bit of java, depending on what you want for your block.
First, to make things easier, I recommend installing Blockstate procedures.
After you install it, you will have a couple more procedure blocks.
Depending on how your blockstate works you will need different blocks.
If you use numbers for the blockstates, then you will want the 'Set number property of block at xyz to 0'
If you use true/false, you will want the 'Set logic property of block at xyz to true'
The blocks look like this:
You can do whatever you want in your procedure, but make sure to use this block in it with your blockstate's name.
Next, assign the procedure to your block.
Now, we have to custom code.
Open up your block in the custom code editor
You'll need to add this import if you're using true/false for the blockstate:
import net.minecraft.world.level.block.state.properties.BooleanProperty;
and this import if you're using numbers for the blockstate:
import net.minecraft.world.level.block.state.properties.IntegerProperty;
Next, you'll create the state. Add this code to your block:
public static final BooleanProperty EXAMPLE_BOOL_BLOCKSTATE = BooleanProperty.create("your_blockstate_name_here");
Replace EXAMPLE_BOOL_BLOCKSTATE with an uppercase of your logic property, and replace your_blockstate_name_here with the logic property you created in the procedure.
If you use numbers, add this code instead:
public static final IntegerProperty EXAMPLE_NUMBER_BLOCKSTATE = IntegerProperty.create("your_blockstate_name_here");
Next add the value to the block's default values:
this.registerDefaultState(this.stateDefinition.any()
.setValue(FACING, Direction.NORTH)
.setValue(EXAMPLE_BLOCKSTATE, blockstate_default_value_here)
);
If your block doesn't have any rotation, the code would look like this:
this.registerDefaultState(this.stateDefinition.any()
.setValue(EXAMPLE_BLOCKSTATE, blockstate_default_value_here)
);
Next, we have to add the value to blockstates:
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING, EXAMPLE_BLOCKSTATE);
}
Now were done with our block's custom code.
Now we have to register the states.
In your explorer window, copy your original block's model and rename it to ORIGINAL BLOCK NAME + blockstate name, or if you already have a second model with a texture, you can skip this. You will find the block's model here:
The file name should look something like this: acacia_door_open.json
Next, go to the blockstates folder. You can find it here:
Open up your blocks file in the blockstates folder, the code should look like this:
{
"variants": {
"facing=north": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL"
},
"facing=east": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL"
"y": 90
},
"facing=south": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL"
"y": 180
},
"facing=west": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL"
"y": 270
}
}
}
You will edit it so it looks similar to this:
{
"variants": {
"facing=north,EXAMPLE_BLOCKSTATE=value": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL"
},
"facing=east,EXAMPLE_BLOCKSTATE=value": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL"
"y": 90
},
"facing=south,EXAMPLE_BLOCKSTATE=value": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL"
"y": 180
},
"facing=west,EXAMPLE_BLOCKSTATE=value": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL"
"y": 270
},
"facing=north,EXAMPLE_BLOCKSTATE=value2": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL_BLOCKSTATE"
},
"facing=east,EXAMPLE_BLOCKSTATE=value2": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL_BLOCKSTATE"
"y": 90
},
"facing=south,EXAMPLE_BLOCKSTATE=value2": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL_BLOCKSTATE"
"y": 180
},
"facing=west,EXAMPLE_BLOCKSTATE=value2": {
"model": "ex_mod:block/YOUR_BLOCK_MODEL_BLOCKSTATE"
"y": 270
}
}
}
And there you have it. Complete the condition you created in your procedure, and your block will switch states.
Hello! It seems this code:
is outdated in 1.18.2, how do I fix it?
endcollector, you can't just supply a string to the function. it requires a string, int, int.
use this code, and replace the values ive put in with whatever you want
public static final IntegerProperty EXAMPLE_NUMBER_BLOCKSTATE = IntegerProperty.create("your_blockstate_name_here", minimum_value, maximum_value);
dont add quotation marks to the min/max values, and dont use decimals!
Please ignore my comment, I figured it out.
Do someone know how to get BlockState Property in Code?
can you make an example replacement of EXAMPLE_NUMBER_BLOCKSTATE
This was very easy to follow, also thanks Wolverine64 for clarifying the create() function parameters. Ty!!