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 [2022/12/16 01:42] – [A note about performance] solidblocktutorial:blockstate [2022/12/16 01:52] solidblock
Line 4: Line 4:
 This is what ''BlockState''s are for. Say we wanted a block that can summon lightning, but only when charged up.    This is what ''BlockState''s are for. 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 ''BooleanProperty''), and register the block within the mod initializer. (If you directly register the block in the static field in the ''ChargeableBlock'' class, the mod initializer may totally ignore it if the class is not initialized.) 
 + 
 +In fact, you can also use existing properties defined in vanilla, which can be found in ''Properties'' (''net.minecraft.state.property.Properties''). If you intend to define other types of properties, you may use ''IntProperty'' or ''EnumProperty''.
 <code java> <code java>
 public class ChargeableBlock extends Block { public class ChargeableBlock extends Block {
     public static final BooleanProperty CHARGED = BooleanProperty.of("charged");     public static final BooleanProperty CHARGED = BooleanProperty.of("charged");
          
-    // The block instance. You can place it anywhere. +    // The block instance. You can place it anywhere. Make the class is initialized
-    public static final ChargeableBlock CHARGEABLE_BLOCK = Registry.register+    public static final ChargeableBlock CHARGEABLE_BLOCK = new ChargeableBlock(FabricBlockSettings.copyOf(Blocks.STONE)); 
-        Registries.BLOCK, +
-        new Identifier("tutorial", "chargeable_block"), +</code> 
-        new ChargeableBlock( /* write anything appropriate here */ ));+<code java> 
 +public class ExampleMod implements ModInitializer { 
 +    @Override 
 +    public void onInitialize() { 
 +        Registry.register(Registries.BLOCK, new Identifier("tutorial", "chargeable_block"), ChargeableBlock.CHARGEABLE_BLOCK); 
 +        Registry.register(Registries.ITEM, new Identifier("tutorial", "chargeable_block"), new BlockItem(ChargeableBlock.CHARGEABLE_BLOCK, new FabricItemSettings()); 
 +    }
 } }
 </code> </code>
tutorial/blockstate.txt · Last modified: 2023/11/18 08:13 by solidblock