tutorial:blockstate
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:blockstate [2022/12/15 16:47] – solidblock | tutorial:blockstate [2025/04/01 09:21] (current) – [Adding models and blockstates definitions for your blockstates] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~REDIRECT> | ||
+ | |||
====== Giving a block state ====== | ====== Giving a block state ====== | ||
- | Every type of block in Minecraft is represented by a singular '' | + | Every type of block in Minecraft is represented by a singular '' |
- | This makes it impossible to change a specific block' | + | |
- | as every other block of that type will be affected! | + | ===== Block state properties ===== |
- | But, what if you //do// want to give a singular block state, so it can change based on some condition? | + | |
- | This is what '' | + | Each block can have zero, one or multiple **block state properties**. Each block state property can have at lease two values. Vanilla properties, can be found in '' |
+ | |||
+ | Several examples: | ||
+ | * [[directionalblock|Directional block]], the block state property used is '' | ||
+ | * [[waterloggable|Waterloggable block]], the block state property used is '' | ||
+ | |||
+ | ===== Custom block state property: a chargeable block as an example ===== | ||
Say we wanted a block that can summon lightning, but only when charged up. | 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!): | + | First we define the boolean property of the block - whether or not it is charged (careful not to import the wrong '' |
+ | |||
+ | If you intend to define other types of properties, you may use '' | ||
+ | |||
+ | Create the class first: | ||
<code java> | <code java> | ||
public class ChargeableBlock extends Block { | public class ChargeableBlock extends Block { | ||
public static final BooleanProperty CHARGED = BooleanProperty.of(" | public static final BooleanProperty CHARGED = BooleanProperty.of(" | ||
| | ||
- | // The block instance. You can place it anywhere. | + | |
- | public static final ChargeableBlock CHARGEABLE_BLOCK = Registry.register( | + | super(settings); |
- | | + | } |
- | new Identifier(" | + | } |
- | new ChargeableBlock( | + | </ |
+ | |||
+ | We register the blocks in the form described in [[blocks]]. | ||
+ | <code java> | ||
+ | public final class TutorialBlocks { | ||
+ | | ||
+ | public static final ChargeableBlock CHARGEABLE_BLOCK = register(" | ||
+ | |||
+ | // For versions since 1.21.2: | ||
+ | | ||
+ | |||
+ | // [...] | ||
} | } | ||
</ | </ | ||
Line 30: | Line 54: | ||
} | } | ||
</ | </ | ||
- | Then we need to set the default state of our property | + | |
+ | Then we need to set the default state of our property. Go to the constructor | ||
<code java> | <code java> | ||
public class ChargeableBlock extends Block { | public class ChargeableBlock extends Block { | ||
- | [...] | ||
public ChargeableBlock(Settings settings) { | public ChargeableBlock(Settings settings) { | ||
super(settings); | super(settings); | ||
Line 46: | Line 70: | ||
[...] | [...] | ||
@Override | @Override | ||
- | public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { | + | public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) { |
player.playSound(SoundEvents.BLOCK_RESPAWN_ANCHOR_CHARGE, | player.playSound(SoundEvents.BLOCK_RESPAWN_ANCHOR_CHARGE, | ||
world.setBlockState(pos, | world.setBlockState(pos, | ||
Line 62: | Line 86: | ||
if (world.getBlockState(pos).get(CHARGED)){ | if (world.getBlockState(pos).get(CHARGED)){ | ||
// Summoning the Lighting Bolt at the block | // Summoning the Lighting Bolt at the block | ||
- | LightningEntity lightningEntity = (LightningEntity) | + | LightningEntity lightningEntity = EntityType.LIGHTNING_BOLT.create(world, SpawnReason.MOB_SUMMONED); |
lightningEntity.refreshPositionAfterTeleport(Vec3d.ofBottomCenter(pos)); | lightningEntity.refreshPositionAfterTeleport(Vec3d.ofBottomCenter(pos)); | ||
world.spawnEntity(lightningEntity); | world.spawnEntity(lightningEntity); | ||
Line 73: | Line 97: | ||
</ | </ | ||
- | ==== Adding models for your blockstates ==== | + | ==== Adding models |
- | 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. | + | 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 "**block states definition**". All blocks need a blockstates definition, whether they have multiple states or not, but the contents of it 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 '' | + | Let's say you register an instance of '' |
<code JavaScript resources/ | <code JavaScript resources/ | ||
Line 105: | Line 129: | ||
</ | </ | ||
- | In this JSON, there are two variants, one for each possibility of the '' | + | In this blockstates definition, there are two variants, one for each possibility of the '' |
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 '' | 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 '' | ||
- | 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 [[https:// | + | This is only a simple introduction to blockstates definitions. You can visit Minecraft Wiki for all of the tricks you can do with [[https:// |
==== A note about performance ==== | ==== A note about performance ==== | ||
- | Every possible | + | Every possible |
+ | |||
+ | As all possible states have been built, an equal state for a block is a same object, and the '' |
tutorial/blockstate.1671122843.txt.gz · Last modified: 2022/12/15 16:47 by solidblock