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 revision
Previous revision
tutorial:blockstate [2023/11/18 08:13] – [Adding models for your blockstates] update Minecraft Wiki link solidblocktutorial:blockstate [2024/10/27 14:57] (current) solidblock
Line 1: Line 1:
 +~~REDIRECT>https://docs.fabricmc.net/develop/blocks/blockstates~~
 +
 ====== Giving a block state ====== ====== Giving a block state ======
 Every type of block in Minecraft is represented by a singular ''Block'' instance. This makes it impossible to change a specific block's state by simply changing the ''Block'' instance's state, as every other block of that type will be affected! But, what if you //do// want to give a singular block state, so it can change based on some condition? Every type of block in Minecraft is represented by a singular ''Block'' instance. This makes it impossible to change a specific block's state by simply changing the ''Block'' instance's state, as every other block of that type will be affected! But, what if you //do// want to give a singular block state, so it can change based on some condition?
Line 7: Line 9:
  
 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''. 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''.
 +
 +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("charged");     public static final BooleanProperty CHARGED = BooleanProperty.of("charged");
          
-    // The block instance. You can place it anywhere. Make the class is initialized. +    public ChargeableBlock(Settings settings) { 
-    public static final ChargeableBlock CHARGEABLE_BLOCK = new ChargeableBlock(FabricBlockSettings.copyOf(Blocks.STONE));+      super(settings); 
 +    }
 } }
 </code> </code>
 +
 +We register the blocks in the form described in [[blocks]].
 <code java> <code java>
-public class ExampleMod implements ModInitializer +public final class TutorialBlocks 
-    @Override +    // For versions below 1.21.2: 
-    public void onInitialize() { +    public static final Chargeable CHARGEABLE_BLOCK = register("chargeable_block", new ChargeableBlock(Block.Settings.copy(Blocks.STONE))); 
-        Registry.register(Registries.BLOCK, new Identifier("tutorial", "chargeable_block"), ChargeableBlock.CHARGEABLE_BLOCK); +    // For versions since 1.21.2: 
-        Registry.register(Registries.ITEM, new Identifier("tutorial", "chargeable_block"), new BlockItem(ChargeableBlock.CHARGEABLE_BLOCK, new FabricItemSettings()); +    public static final Chargeable CHARGEABLE_BLOCK = register("chargeable_block", ChargeableBlock::new, Block.Settings.copy(Blocks.STONE)); 
-    }+     
 +    // [...]
 } }
 </code> </code>
Line 35: Line 43:
 } }
 </code> </code>
-Then we need to set the default state of our property in the block constructor (To set multiple propertieschain ''with()'' calls):+ 
 +Then we need to set the default state of our property. Go to the constructor of the class we created just nowand modify it like this:
 <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 51: Line 59:
     [...]     [...]
     @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, 1, 1);         player.playSound(SoundEvents.BLOCK_RESPAWN_ANCHOR_CHARGE, 1, 1);
         world.setBlockState(pos, state.with(CHARGED, true));         world.setBlockState(pos, state.with(CHARGED, true));
Line 67: Line 75:
         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) EntityType.LIGHTNING_BOLT.create(world);+            LightningEntity lightningEntity = EntityType.LIGHTNING_BOLT.create(world);
             lightningEntity.refreshPositionAfterTeleport(Vec3d.ofBottomCenter(pos));             lightningEntity.refreshPositionAfterTeleport(Vec3d.ofBottomCenter(pos));
             world.spawnEntity(lightningEntity);             world.spawnEntity(lightningEntity);
tutorial/blockstate.1700295207.txt.gz · Last modified: 2023/11/18 08:13 by solidblock