User Tools

Site Tools


tutorial:blockstate

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
tutorial:blockstate [2019/08/06 20:52] – [A note about performance] fudgetutorial:blockstate [2019/08/28 21:29] – [Giving a block state] Added description of blockstate json b0undarybreaker
Line 11: Line 11:
 <code java> <code java>
 public class MyBlock extends Block { public class MyBlock extends Block {
-    public static final BooleanProperty MyBlockIsHard = BooleanProperty.of("is_hard");+    public static final BooleanProperty HARDENED = BooleanProperty.of("hardened");
 } }
 </code> </code>
Line 20: Line 20:
     @Override     @Override
     protected void appendProperties(StateFactory.Builder<Block, BlockState> stateFactory) {     protected void appendProperties(StateFactory.Builder<Block, BlockState> stateFactory) {
-        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, MyBlocks.MY_BLOCK_INSTANCE.getDefaultState().with(MyBlockIsHard, true));+        world.setBlockState(pos, MyBlocks.MY_BLOCK_INSTANCE.getDefaultState().with(HARDENED, true));
         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 isHard = blockState.get(MyBlockIsHard); +        boolean hardened = blockState.get(HARDENED); 
-        if(isHard) return 2.0f;+        if(hardened) return 2.0f;
         else return 0.5f;         else return 0.5f;
     }     }
Line 65: Line 65:
 </code> </code>
  
-You can also make the texture of you block change based on the block state. [additional information needed]+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. 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: 
 + 
 +<code json> 
 +
 +    "variants":
 +        "": { "model": "mymod:block/my_block"
 +    } 
 +
 +</code> 
 + 
 +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 [[Minecraft wiki]](https://minecraft.gamepedia.com/Model#Block_states) for more documentation on what properties can be added.
 ==== 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:blockentity|Block Entities]] should be used for more advanced state. 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:blockentity|Block Entities]] should be used for more advanced state.
tutorial/blockstate.txt · Last modified: 2023/11/18 08:13 by solidblock