Skip to content

How to make a block that changes over time with random ticks (Minecraft mod)

Every tick the game picks a few random blocks in each loaded chunk section and calls randomTick on them (the randomTickSpeed game rule, 3 by default, sets how many). A block only takes part if its properties say .randomTicks() (on 1.12.2, setTickRandomly(true) in its constructor). Override randomTick(state, level, pos, random) to change something (updateTick on 1.12.2), and use the random source it gives you for chances. It runs on the server only.

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Flag the block for random ticks: .randomTicks() in its properties.
  2. Override randomTick in your block class.
  3. Use the random source to act only some of the time.
  4. Change the world with level.setBlockAndUpdate(pos, newState).

The example uses these values. Change them in the code.

Setting In the example What it does
odds 4 It changes on one random tick in this many.
becomes GRAVEL The block it turns into.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Fabric and NeoForge 1.20.1 to 1.20.4, Fabric 1.20, Forge 1.20 to 1.20.1

Section titled “Fabric and NeoForge 1.20.1 to 1.20.4, Fabric 1.20, Forge 1.20 to 1.20.1”

Uses:

  • BlockBehaviour.Properties.randomTicks
  • BlockBehaviour.randomTick
  • RandomSource.nextInt
  • Level.setBlockAndUpdate

src/main/java/com/example/examplemod/block/CrumblingBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
// Crumbling Block: on about one random tick in 4 it turns into GRAVEL. The block's properties must include .randomTicks().
public class CrumblingBlock extends Block {
public CrumblingBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (random.nextInt(4) == 0) {
level.setBlockAndUpdate(pos, Blocks.GRAVEL.defaultBlockState());
}
}
}

It’s registered like any other block, as CrumblingBlock: see a new block.

BlockBehaviour.Properties.of().mapColor(MapColor.STONE).strength(1.0F).randomTicks()

Uses:

  • BlockBehaviour.Properties.randomTicks
  • BlockBehaviour.randomTick
  • RandomSource.nextInt
  • Level.setBlockAndUpdate

src/main/java/com/example/examplemod/block/CrumblingBlock.java

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
// Crumbling Block: on about one random tick in 4 it turns into GRAVEL. The block's properties must include .randomTicks().
public class CrumblingBlock extends Block {
public static final MapCodec<CrumblingBlock> CODEC = simpleCodec(CrumblingBlock::new);
public CrumblingBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends Block> codec() {
return CODEC;
}
@Override
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (random.nextInt(4) == 0) {
level.setBlockAndUpdate(pos, Blocks.GRAVEL.defaultBlockState());
}
}
}

It’s registered like any other block, as CrumblingBlock: see a new block.

BlockBehaviour.Properties.of().mapColor(MapColor.STONE).strength(1.0F).randomTicks()

Uses:

  • BlockBehaviour.Properties.randomTicks
  • BlockBehaviour.randomTick
  • RandomSource.nextInt
  • Level.setBlockAndUpdate

A block’s properties need its id set before the block is made (setId); register the block with the same id, or the game crashes at startup.

src/main/java/com/example/examplemod/block/CrumblingBlock.java

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
// Crumbling Block: on about one random tick in 4 it turns into GRAVEL. The block's properties must include .randomTicks().
public class CrumblingBlock extends Block {
public static final MapCodec<CrumblingBlock> CODEC = simpleCodec(CrumblingBlock::new);
public CrumblingBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends Block> codec() {
return CODEC;
}
@Override
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (random.nextInt(4) == 0) {
level.setBlockAndUpdate(pos, Blocks.GRAVEL.defaultBlockState());
}
}
}

It’s registered like any other block, as CrumblingBlock: see a new block.

BlockBehaviour.Properties.of().setId(ResourceKey.create(Registries.BLOCK, ResourceLocation.fromNamespaceAndPath("examplemod", "crumbling_block"))).mapColor(MapColor.STONE).strength(1.0F).randomTicks()

Uses:

  • BlockBehaviour.Properties.randomTicks
  • BlockBehaviour.randomTick
  • RandomSource.nextInt
  • Level.setBlockAndUpdate

A block’s properties need its id set before the block is made (setId); register the block with the same id, or the game crashes at startup.

src/main/java/com/example/examplemod/block/CrumblingBlock.java

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
// Crumbling Block: on about one random tick in 4 it turns into GRAVEL. The block's properties must include .randomTicks().
public class CrumblingBlock extends Block {
public static final MapCodec<CrumblingBlock> CODEC = simpleCodec(CrumblingBlock::new);
public CrumblingBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends Block> codec() {
return CODEC;
}
@Override
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (random.nextInt(4) == 0) {
level.setBlockAndUpdate(pos, Blocks.GRAVEL.defaultBlockState());
}
}
}

It’s registered like any other block, as CrumblingBlock: see a new block.

BlockBehaviour.Properties.of().setId(ResourceKey.create(Registries.BLOCK, Identifier.fromNamespaceAndPath("examplemod", "crumbling_block"))).mapColor(MapColor.STONE).strength(1.0F).randomTicks()

Uses:

  • BlockBehaviour.Properties.randomTicks
  • BlockBehaviour.randomTick
  • RandomSource.nextInt
  • Level.setBlockAndUpdate

A block’s properties need its id set before the block is made (setId); register the block with the same id, or the game crashes at startup. Since 26.3 a Block no longer has a codec: there is no CODEC field and no codec() method to write.

src/main/java/com/example/examplemod/block/CrumblingBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
// Crumbling Block: on about one random tick in 4 it turns into GRAVEL. The block's properties must include .randomTicks().
public class CrumblingBlock extends Block {
public CrumblingBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (random.nextInt(4) == 0) {
level.setBlockAndUpdate(pos, Blocks.GRAVEL.defaultBlockState());
}
}
}

It’s registered like any other block, as CrumblingBlock: see a new block.

BlockBehaviour.Properties.of().setId(ResourceKey.create(Registries.BLOCK, Identifier.fromNamespaceAndPath("examplemod", "crumbling_block"))).mapColor(MapColor.STONE).strength(1.0F).randomTicks()

Forge 1.17.1 to 1.18.2, Fabric 1.16.5 to 1.18.2

Section titled “Forge 1.17.1 to 1.18.2, Fabric 1.16.5 to 1.18.2”

Uses:

  • BlockBehaviour.Properties.randomTicks
  • BlockBehaviour.randomTick
  • RandomSource.nextInt
  • Level.setBlockAndUpdate

src/main/java/com/example/examplemod/block/CrumblingBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import java.util.Random;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
// Crumbling Block: on about one random tick in 4 it turns into GRAVEL. The block's properties must include .randomTicks().
public class CrumblingBlock extends Block {
public CrumblingBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public void randomTick(BlockState state, ServerLevel level, BlockPos pos, Random random) {
if (random.nextInt(4) == 0) {
level.setBlockAndUpdate(pos, Blocks.GRAVEL.defaultBlockState());
}
}
}

It’s registered like any other block, as CrumblingBlock: see a new block.

BlockBehaviour.Properties.of(Material.STONE).strength(1.0F).randomTicks()

Forge 1.19 to 1.19.4, Fabric 1.19 to 1.19.4

Section titled “Forge 1.19 to 1.19.4, Fabric 1.19 to 1.19.4”

Uses:

  • BlockBehaviour.Properties.randomTicks
  • BlockBehaviour.randomTick
  • RandomSource.nextInt
  • Level.setBlockAndUpdate

src/main/java/com/example/examplemod/block/CrumblingBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
// Crumbling Block: on about one random tick in 4 it turns into GRAVEL. The block's properties must include .randomTicks().
public class CrumblingBlock extends Block {
public CrumblingBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public void randomTick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (random.nextInt(4) == 0) {
level.setBlockAndUpdate(pos, Blocks.GRAVEL.defaultBlockState());
}
}
}

It’s registered like any other block, as CrumblingBlock: see a new block.

BlockBehaviour.Properties.of(Material.STONE).strength(1.0F).randomTicks()

Uses:

  • AbstractBlock.Properties.randomTicks
  • AbstractBlock.randomTick
  • Random.nextInt
  • World.setBlockAndUpdate

src/main/java/com/example/examplemod/block/CrumblingBlock.java

package com.example.examplemod.block;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.server.ServerWorld;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.BlockState;
// Crumbling Block: on about one random tick in 4 it turns into GRAVEL. The block's properties must include .randomTicks().
public class CrumblingBlock extends Block {
public CrumblingBlock(AbstractBlock.Properties properties) {
super(properties);
}
@Override
public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
if (random.nextInt(4) == 0) {
world.setBlockAndUpdate(pos, Blocks.GRAVEL.defaultBlockState());
}
}
}

It’s registered like any other block, as CrumblingBlock: see a new block.

AbstractBlock.Properties.of(Material.STONE).strength(1.0F).randomTicks()

Uses:

  • Block.setTickRandomly
  • Block.updateTick

In 1.12.2 the constructor calls setTickRandomly(true), the method is updateTick(world, pos, state, rand), and the change is world.setBlockState.

src/main/java/com/example/examplemod/block/CrumblingBlock.java

package com.example.examplemod.block;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
// Crumbling Block: on about one random tick in 4 it turns into GRAVEL. The constructor must call setTickRandomly(true).
public class CrumblingBlock extends Block {
public CrumblingBlock() {
super(Material.ROCK);
setTickRandomly(true);
}
@Override
public void updateTick(World world, BlockPos pos, IBlockState state, Random random) {
if (random.nextInt(4) == 0) {
world.setBlockState(pos, Blocks.GRAVEL.getDefaultState());
}
}
}

It’s registered like any other block, as CrumblingBlock: see a new block.

Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.