[Tutorial] Blockstates 1.17

Started by Latokike on

Topic category: User side tutorials

Last seen on 16:26, 22. Feb 2022
Joined Feb 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[Tutorial] Blockstates 1.17

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.

Last seen on 13:20, 25. Apr 2024
Joined Oct 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hello! It seems this code:  …
Tue, 04/19/2022 - 22:06

Hello! It seems this code: 

public static final IntegerProperty EXAMPLE_NUMBER_BLOCKSTATE = IntegerProperty.create("your_blockstate_name_here");

is outdated in 1.18.2, how do I fix it?

Last seen on 01:47, 18. Aug 2022
Joined Sep 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
endcollector, you can't just…
Mon, 07/04/2022 - 15:30

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!

Last seen on 17:56, 19. Apr 2024
Joined Nov 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@Override protected void…
Sat, 11/05/2022 - 16:21
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
	builder.add(FACING, EXAMPLE_BLOCKSTATE);
}

I cannot figure this out, it always gives me an error (1.18.2)
Last seen on 17:56, 19. Apr 2024
Joined Nov 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Please ignore my comment, I…
Sat, 11/05/2022 - 18:18

Please ignore my comment, I figured it out.

 

Last seen on 14:47, 26. Apr 2024
Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Do someone know how to get…
Mon, 11/28/2022 - 20:49

Do someone know how to get BlockState Property in Code?

Last seen on 18:30, 27. Mar 2023
Joined Feb 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
can you make an example…
Sun, 03/05/2023 - 09:14

can you make an example replacement of EXAMPLE_NUMBER_BLOCKSTATE

Last seen on 05:01, 29. Jan 2024
Joined Nov 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
This was very easy to follow…
Tue, 06/20/2023 - 08:10

This was very easy to follow, also thanks Wolverine64 for clarifying the create() function parameters. Ty!!