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/08/06 20:52] – [A note about performance] fudge | tutorial:blockstate [2019/08/29 07:47] – [Adding models for your blockstates] JavaScript formatting, add file names jamieswhiteshirt | ||
---|---|---|---|
Line 11: | Line 11: | ||
<code java> | <code java> | ||
public class MyBlock extends Block { | public class MyBlock extends Block { | ||
- | public static final BooleanProperty | + | public static final BooleanProperty |
} | } | ||
</ | </ | ||
Line 20: | Line 20: | ||
@Override | @Override | ||
protected void appendProperties(StateFactory.Builder< | protected void appendProperties(StateFactory.Builder< | ||
- | stateFactory.add(MyBlockIsHard); | + | stateFactory.add(HARDENED); |
} | } | ||
| | ||
Line 31: | Line 31: | ||
public MyBlock(Settings settings) { | public MyBlock(Settings settings) { | ||
super(settings); | super(settings); | ||
- | setDefaultState(getStateFactory().getDefaultState().with(MyBlockIsHard, false)); | + | setDefaultState(getStateFactory().getDefaultState().with(HARDENED, false)); |
} | } | ||
| | ||
Line 47: | Line 47: | ||
@Override | @Override | ||
public boolean activate(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) { | public boolean activate(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) { | ||
- | world.setBlockState(pos, | + | world.setBlockState(pos, |
return true; | return true; | ||
} | } | ||
Line 58: | Line 58: | ||
@Override | @Override | ||
public float getHardness(BlockState blockState, BlockView blockView, BlockPos pos) { | public float getHardness(BlockState blockState, BlockView blockView, BlockPos pos) { | ||
- | boolean | + | boolean |
- | if(isHard) return 2.0f; | + | if(hardened) return 2.0f; |
else return 0.5f; | else return 0.5f; | ||
} | } | ||
Line 65: | Line 65: | ||
</ | </ | ||
- | You can also make the texture of you block change based on the block state. [additional information needed] | + | ==== Adding models for your blockstates ==== |
+ | |||
+ | You can also make the texture | ||
+ | |||
+ | Let's say you register an instance of '' | ||
+ | |||
+ | <code JavaScript resources/ | ||
+ | { | ||
+ | " | ||
+ | "": | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Let's break this simple example down. There are a couple important parts to this JSON: | ||
+ | |||
+ | - The ''" | ||
+ | - A variant named ''""'' | ||
+ | - The object assigned to the ''""'' | ||
+ | - The ''" | ||
+ | |||
+ | If you //do// want to have different models for each blockstate, you'd want to add multiple variants. For the same '' | ||
+ | |||
+ | <code JavaScript resources/ | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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 '' | ||
+ | |||
+ | 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