tutorial:blockstate
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:blockstate [2019/11/14 11:53] – fudge | tutorial:blockstate [2021/07/18 03:06] – The function which the old article was using, getHardness(), was incorrect. I changed what the blockstates did and how they did them. Instead of changing the hardness, I changed the "charge" value, which summons lightning. tsunaminal | ||
---|---|---|---|
Line 5: | Line 5: | ||
But, what if you //do// want to give a singular block state, so it can change based on some condition? | But, what if you //do// want to give a singular block state, so it can change based on some condition? | ||
This is what '' | This is what '' | ||
- | Say we wanted a block to have a hardness of '' | + | Say we wanted a block that can summon lightning, but only when charged up. |
- | it would become harder and gain a hardness of '' | + | |
| | ||
- | First we define the boolean property of the block - whether or not it is hard (careful not to import the wrong BooleanProperty!): | + | First we define the boolean property of the block - whether or not it is charged |
<code java> | <code java> | ||
public class MyBlock extends Block { | public class MyBlock extends Block { | ||
- | public static final BooleanProperty | + | public static final BooleanProperty |
} | } | ||
</ | </ | ||
- | Then we need to register the property | + | Then we need to register the properties of the block by overriding '' |
<code java> | <code java> | ||
public class MyBlock extends Block { | public class MyBlock extends Block { | ||
[...] | [...] | ||
@Override | @Override | ||
- | protected void appendProperties(StateFactory.Builder< | + | protected void appendProperties(StateManager.Builder< |
- | | + | |
} | } | ||
| | ||
} | } | ||
</ | </ | ||
- | Then we need to set the default state of our property in the block constructor: | + | Then we need to set the default state of our property in the block constructor |
<code java> | <code java> | ||
public class MyBlock extends Block { | public class MyBlock extends Block { | ||
Line 31: | Line 30: | ||
public MyBlock(Settings settings) { | public MyBlock(Settings settings) { | ||
super(settings); | super(settings); | ||
- | setDefaultState(getStateFactory().getDefaultState().with(HARDENED, false)); | + | setDefaultState(getStateManager().getDefaultState().with(CHARGED, false)); |
} | } | ||
| | ||
} | } | ||
</ | </ | ||
- | (To set multiple properties, chain `with()` calls) | + | Now, we need to be able to charge the block, through the '' |
- | + | ||
- | Now, to set the property | + | |
- | + | ||
- | (Replace | + | |
<code java> | <code java> | ||
public class MyBlock extends Block { | public class MyBlock extends Block { | ||
[...] | [...] | ||
@Override | @Override | ||
- | public | + | public |
- | world.setBlockState(pos, | + | player.playSound(SoundEvents.BLOCK_RESPAWN_ANCHOR_CHARGE, |
- | return | + | world.setBlockState(pos, |
+ | return | ||
} | } | ||
} | } | ||
</ | </ | ||
- | And to use the property we call '' | + | |
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | And Finally, | ||
<code java> | <code java> | ||
public class MyBlock extends Block { | public class MyBlock extends Block { | ||
[...] | [...] | ||
@Override | @Override | ||
- | public | + | public |
- | | + | |
- | if(hardened) return 2.0f; | + | //Summoning the Lighting Bolt at the block |
- | | + | LightningEntity lightningEntity = (LightningEntity) EntityType.LIGHTNING_BOLT.create(world); |
+ | | ||
+ | world.spawnEntity(lightningEntity); | ||
+ | | ||
+ | |||
+ | world.setBlockState(pos, | ||
+ | super.onSteppedOn(world, | ||
} | } | ||
} | } | ||
Line 85: | Line 100: | ||
- The ''" | - The ''" | ||
- | If you // | + | If you want to have different models for each blockstate, you should |
<code JavaScript resources/ | <code JavaScript resources/ | ||
{ | { | ||
" | " | ||
- | "hardened=false": | + | "charged=false": |
- | "hardened=true": | + | "charged=true": |
} | } | ||
} | } | ||
</ | </ | ||
- | In this JSON, there are two variants, one for each possibility of the '' | + | In this JSON, 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 blockstate JSONs. All of the tricks you can do with blockstate and model JSONs are documented on the [[https:// | ||
==== A note about performance ==== | ==== 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 [[tutorial: | 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 [[tutorial: |
tutorial/blockstate.txt · Last modified: 2023/11/18 08:13 by solidblock