This is an old revision of the document!
−Table of Contents
Giving a block state
Every type of block in Minecraft is represented by a singular Block
instance.
This makes it impossible to change a specific block's state by simply changing the Block
instance's state,
as every other block of that type will be affected!
But, what if you do want to give a singular block state, so it can change based on some condition?
This is what BlockState
s are for.
Say we wanted a block that can summon lightning, but only when charged up.
First we define the boolean property of the block - whether or not it is charged (careful not to import the wrong BooleanProperty!):
public class MyBlock extends Block { public static final BooleanProperty CHARGED = BooleanProperty.of("charged"); }
Then we need to register the properties of the block by overriding appendProperties
, and then add the CHARGED
property:
public class MyBlock extends Block { [...] @Override protected void appendProperties(StateManager.Builder<Block, BlockState> stateManager) { stateManager.add(CHARGED); } }
Then we need to set the default state of our property in the block constructor (To set multiple properties, chain with()
calls):
public class MyBlock extends Block { [...] public MyBlock(Settings settings) { super(settings); setDefaultState(getStateManager().getDefaultState().with(CHARGED, false)); } }
Now, we need to be able to charge the block, through the onUse
function, with the world.setBlockState()
function inside of it (The playSound
is completely optional, but it helps us know that the block is charged).
public class MyBlock extends Block { [...] @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { player.playSound(SoundEvents.BLOCK_RESPAWN_ANCHOR_CHARGE, 1, 1); world.setBlockState(pos, state.with(CHARGED, true)); return ActionResult.SUCCESS; } }
And Finally, to use the CHARGED
property, we call onSteppedOn
, with world.getBlockState(pos).get(CHARGED)
inside of it:
public class MyBlock extends Block { [...] @Override public void onSteppedOn(World world, BlockPos pos, BlockState state, Entity entity) { if (world.getBlockState(pos).get(CHARGED)){ //Summoning the Lighting Bolt at the block LightningEntity lightningEntity = (LightningEntity) EntityType.LIGHTNING_BOLT.create(world); lightningEntity.refreshPositionAfterTeleport(Vec3d.ofBottomCenter(pos)); world.spawnEntity(lightningEntity); } world.setBlockState(pos, state.with(CHARGED, false)); super.onSteppedOn(world, pos, state, entity); } }
Adding models for your blockstates
You can also make the texture and model of your block change based on the state. This is done through a JSON file called a Blockstate JSON. All blocks need a blockstate JSON, whether they have multiple states or not, but the contents of the JSON can be as simple or complex as you like. If you want to change the textures of your block based on the state, you will need multiple models.
Let's say you register an instance of MyBlock
to the ID mymod:my_block
. Minecraft would look for a file at the location src/main/resources/assets/mymod/blockstates/my_block.json
to load the state from. If you don't want your block to change models between states, the blockstate JSON can be very simple. It would look something like this:
- resources/assets/mymod/blockstates/my_block.json
{ "variants": { "": { "model": "mymod:block/my_block" } } }
Let's break this simple example down. There are a couple important parts to this JSON:
- The
“variants”
block will be where all possible variations for your blockstate go. We'll explore variants more in a little. - A variant named
“”
will apply to every permutation of a blockstate. If you have a“”
variant, you shouldn't have any other variants in the JSON, or Minecraft will get upset. - The object assigned to the
“”
variant can have various properties added to it like rotation or texture manipulation. Check out the linked Model page below for more documentation on what properties can be added. All variants must contain a“model”
property. - The
“model”
property is always passed an ID of a model. In this case, the game will look at the locationsrc/main/resources/assets/mymod/models/block/my_block.json
. The ID here can be anything. It doesn't need to be the same as your block's ID, but if you only have one variant, it probably should. Block models have their own setup, which is documented very well on the Minecraft wiki page linked below. You can either write the JSON by hand or use a program like Blockbench to generate it more easily.
If you want to have different models for each blockstate, you should add multiple variants. For the same src/main/resources/assets/mymod/blockstates/my_block.json
location we used above, your model file would probably look like such:
- resources/assets/mymod/blockstates/my_block.json
{ "variants": { "charged=false": { "model": "mymod:block/my_block" }, "charged=true": { "model": "mymod:block/my_block_charged" } } }
In this JSON, there are two variants, one for each possibility of the CHARGED
property we defined above. Since we gave the property the string name of charged
in the Java, that's what we use here. Booleans only have two states, but if you use properties based on integers or enums, you'll have more variants.
Variants are based on possible permutations of the properties added to your block. A property can be totally ignored in the blockstate JSON if you want, like in the first blockstate JSON where we ignored the charged
property, but if you want to include a property in one variant, it must be included in all variants. If mymod:my_block
also had a boolean property called glowing
, and you wanted to change the model based on whether it was glowing and based on whether it was charged, you would need four variants: charged off and glowing off, charged on and glowing off, charged off and glowing on, and charged on and glowing on. The same model can be assigned to multiple variants if you need it to be.
This is only a simple introduction to blockstate JSONs. All of the tricks you can do with blockstate and model JSONs are documented on the Minecraft wiki, along with examples of how the features are used in vanilla. Best of luck!
A note about performance
Every possible state of a block is registered at the start of the game. This means that if you have 14 boolean properties, the block has 2^14 = 16384 different states and 2^14 states are registered. For this reason blocks should not contain too many blockstate properties. Rather, blockstates should be mostly reserved for visuals, and Block Entities should be used for more advanced state.