Skip to content

Block behavior

These types give a mod’s blocks what vanilla blocks do: wood sets, fire, stripping, composting, fuel, signs, saplings that grow trees, placing and breaking, water a block holds, blocks a player breaks one part at a time, blocks fire and a dispenser’s flint and steel light as they light TNT, blocks told an explosion destroyed them, a mod’s own blast that claim mods filter and that hurts, pushes, and breaks as vanilla’s does, and redstone. BlockBehaviors, WoodSet, and SaplingGrowers are set up during initialization and the rest run as the game plays; how a block is drawn is on Block rendering, its particles on Particles, and a block entity’s data on Block entities.

Type Summary Status Since Side Thread
Blast The explosion that destroyed a block, or a mod’s own blast: its center, source, and igniter. Experimental 2.17.0 both any
BlastPermit What the claim and protection mods let a mod’s own blast break and hurt. Experimental 2.17.0 server server
BlockBehaviors Vanilla behaviors for blocks and items. Experimental 2.1.0 both setup
BlockPlacement Block placement, breaking, and the checks around them. Experimental 2.10.0 both any
ExplosionBreakable A block told when an explosion destroys it, with the state it had. Experimental 2.17.0 server server
LitByFire A block that fire or a dispenser’s flint and steel lights as it lights TNT. Experimental 2.17.0 server server
PlayerBreakable A block that decides whether a player’s break removes it. Experimental 2.15.0 both any
RedstoneSignals Redstone signals and neighbor updates. Experimental 2.15.0 server server
SaplingGrowers Growers for a mod’s saplings. Experimental 2.4.0 both setup
Waterlogging The water tick, fluid state, and placement check of a block that holds water. Experimental 2.10.0 both any
WoodSet One wood’s blocks and items, the set vanilla gives oak, with oak’s properties and behaviors. Experimental 2.9.0 both setup

The examples on this page use the Registered handles a mod’s Registrar returns: MAPLE_LOG and STRIPPED_MAPLE_LOG (RotatedPillarBlock), MAPLE_PLANKS, MAPLE_LEAVES, and MAPLE_SAPLING. In your mod’s common initializer, after registering them:

Make a new wood burn, strip, and compost as vanilla wood does
BlockBehaviors.flammableAsOakLog(MAPLE_LOG);
BlockBehaviors.flammableAsOakPlanks(MAPLE_PLANKS);
BlockBehaviors.flammableAsOakLeaves(MAPLE_LEAVES);
BlockBehaviors.strippable(MAPLE_LOG, STRIPPED_MAPLE_LOG);
BlockBehaviors.compostableAsOakLeaves(MAPLE_LEAVES);
BlockBehaviors.compostableAsOakLeaves(MAPLE_SAPLING);

Experimental · Since 2.1.0 · Side: both · Thread: setup

BlockBehaviors gives a mod’s blocks and items the fire, stripping, composting, fuel, and sign behavior vanilla blocks have. With MAPLE_SIGN, MAPLE_WALL_SIGN, MAPLE_HANGING_SIGN, and MAPLE_WALL_HANGING_SIGN, the maple wood’s four sign blocks:

Let a wood's signs use vanilla's sign block entities
BlockBehaviors.sign(MAPLE_SIGN);
BlockBehaviors.sign(MAPLE_WALL_SIGN);
BlockBehaviors.sign(MAPLE_HANGING_SIGN);
BlockBehaviors.sign(MAPLE_WALL_HANGING_SIGN);
  • The quick start’s calls give a block or item outside a WoodSet oak’s values; BlockBehaviors#flammable and compostable take other numbers.
  • Flammability, stripping, and composting write vanilla’s own tables (FireBlock, AxeItem.STRIPPABLES, ComposterBlock.COMPOSTABLES), which Fabric API’s content registries and Forge read too, so other mods see the values.

Experimental · Since 2.9.0 · Side: both · Thread: setup

WoodSet registers a whole wood the way vanilla has oak, with the behaviors the quick start sets by hand. Use it when a wood matches oak block for block; for a wood that differs, register its blocks with Registrar and give them behaviors with BlockBehaviors. With REGISTRAR, the mod’s Registrar, and the MapleSaplingBlock class from the SaplingGrowers example, in your mod’s common initializer, in place of registering the maple blocks one by one as the other examples on this page do:

Register an oak-style maple wood
BoatSet boats = REGISTRAR.boats();
WoodSet<LeavesBlock, MapleSaplingBlock> maple =
WoodSet.register(boats, "maple", LeavesBlock::new, MapleSaplingBlock::new);
  • Pass the same BoatSet to each of the mod’s wood sets.
  • Add WoodSet#items to the mod’s creative tab as they come, and the wood lines up as oak’s does.

Experimental · Since 2.4.0 · Side: both · Thread: setup

The trees are configured features in the mod’s data, named by their keys. Vanilla’s SaplingBlock constructor is protected, and a mod’s common code compiles against vanilla, so the mod’s sapling is a subclass of its own that passes the grower to super. With REGISTRAR, the mod’s Registrar:

A sapling that grows a tree, or a giant from a 2 by 2 square
public final class MapleSaplingBlock extends SaplingBlock {
private static final ResourceKey<ConfiguredFeature<?, ?>> MAPLE =
RegistryLookups.configuredFeatureKey(id("maple"));
private static final ResourceKey<ConfiguredFeature<?, ?>> GIANT_MAPLE =
RegistryLookups.configuredFeatureKey(id("giant_maple"));
public MapleSaplingBlock(Properties properties) {
super(SaplingGrowers.withGiant(MAPLE, GIANT_MAPLE), properties);
}
}
// In the mod's block holder:
static final Registered<SaplingBlock> MAPLE_SAPLING = REGISTRAR.block("maple_sapling",
() -> new MapleSaplingBlock(BlockBehaviour.Properties.copy(Blocks.OAK_SAPLING)));

Experimental · Since 2.10.0 · Side: both · Thread: any

BlockPlacement places and breaks blocks as the game does, and answers what a block’s shape lets rest on it, fills, and covers. In a chimney block entity’s client tick, with above, a BlockPos.MutableBlockPos field so the tick makes no position, and REACH, how many blocks its smoke rises:

Count the open blocks above a chimney
int open = 0;
above.set(worldPosition);
while (open < REACH && !BlockPlacement.coversFromAbove(level, above.move(Direction.UP))) open++;
  • BlockPlacement#coversFromAbove is the question for what moves straight up or down; BlockPlacement#isFullCollisionBlock is the question for a block that fills its space, and BlockPlacement#sturdyFace for what can rest on a face.
  • BlockPlacement#place does not look for entities; ask BlockPlacement#isClearOfEntities first where a mob or a player may stand in the block’s space.

Experimental · Since 2.10.0 · Side: both · Thread: any

Waterlogging gives a block that holds water what a vanilla waterlogged block does. A block that keeps its water as a value of a property of its own, here FILL with Fill.WATER, asks for the water source when it is placed and reports its water as a fluid, and calls Waterlogging#scheduleWaterTick where vanilla’s waterlogged blocks schedule theirs:

Hold water in a block's own property
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockState placed = defaultBlockState();
return Waterlogging.isWaterSourceAt(context.getLevel(), context.getClickedPos())
? placed.setValue(FILL, Fill.WATER)
: placed;
}
@Override
public FluidState getFluidState(BlockState state) {
return Waterlogging.fluidState(state.getValue(FILL) == Fill.WATER);
}

Blast: Experimental · Since 2.17.0 · Side: both · Thread: any

BlastPermit: Experimental · Since 2.17.0 · Side: server · Thread: server

BlastPermit asks the claim and protection mods what a mod’s own blast may break and hurt, as they are asked of TNT, on Forge and Fabric. With level, the server level, center, the blast’s center, charge, the entity that exploded, lighter, the player who lit it, and blocks and entities, the blast’s own candidates:

Break and hurt only what the claim mods allow
Blast blast = new Blast(center, charge, lighter);
BlastPermit permit = BlastPermit.ask(level, blast, blocks, entities);
blast.breakBlocks(level, permit.blocks());
for (Entity entity : permit.entities()) {
permit.hurt(entity, 6.0f);
permit.push(entity, entity.position().subtract(center).normalize().scale(0.5));
}
  • The mod plays the blast’s sound and particles itself; the ask plays none.

Blast is the blast the ask is about, and the explosion an ExplosionBreakable block is told of. Blast#breakBlocks breaks the permitted blocks as a vanilla explosion breaks them, so TNT among them primes; Blast#resistanceAt reads the resistance a mod’s blast weakens by, as vanilla’s rays do, for a mod that shapes its blast by the blocks in the way.

Experimental · Since 2.15.0 · Side: both · Thread: any

A block of several parts implements PlayerBreakable so a player’s break takes the part the player aimed at and keeps the rest. In FittingBlock, with PARTS, its count of parts, Part, its enum of parts, partAt, the block’s method that names the part a hit point is in, withoutPart, the state left when one goes, and Part#drop, the item a part drops:

Take only the aimed part of a fitting
@Override
public boolean removeOnPlayerBreak(Level level, BlockState state, Player player, BlockHitResult hit) {
if (state.getValue(PARTS) == 1) return true;
if (level instanceof ServerLevel server) {
Part part = partAt(state, hit.getLocation().subtract(Vec3.atLowerCornerOf(hit.getBlockPos())));
BlockPlacement.place(server, hit.getBlockPos(), withoutPart(state, part));
// As vanilla's drops: none in creative mode or without the right tool.
if (!player.isCreative() && player.hasCorrectToolForDrops(state)) {
ItemDrops.dropAt(server, hit.getBlockPos(), part.drop());
}
}
return false;
}

Experimental · Since 2.15.0 · Side: server · Thread: server

RedstoneSignals reads redstone and starts it, as a wire or a lamp does. In a block entity, with SIDES, a private static final Direction[] SIDES = Direction.values() so the tick copies no array, neighbor, a BlockPos.MutableBlockPos field so the tick makes no position, wasPowered, a field it keeps, and BELL, the mod’s block, its server tick reads the signal from every side and, when that changes, tells its neighbors:

Read the strongest signal around a block, and tell its neighbors when it changes
if (!(level instanceof ServerLevel server)) return;
int strongest = 0;
for (Direction side : SIDES) {
neighbor.setWithOffset(worldPosition, side);
strongest = Math.max(strongest, RedstoneSignals.signal(server, neighbor, side));
}
if (strongest > 0 != wasPowered) {
wasPowered = strongest > 0;
RedstoneSignals.updateNeighbors(server, worldPosition, BELL.get());
}
  • Block entities: SyncedBlockEntity, BlockEntitySync, and BlockEntityItems: a block entity’s data on its clients and on its block’s item.
  • Block rendering: BlockRendering, Colormap, MimicModels, BlockAppearance, DataModels, PaintedBox, and BuiltinPacks: how a block is drawn.
  • Particles: SimpleParticles, ClientParticles, FallingLeaves, ParticleProviders, and ServerParticles: the particles a block shows.
  • Registration and ids: registering the blocks and items these calls take.
  • Features and loot: placing a mod’s trees in biomes.
  • Add a wood type with boats: a guide that builds a wood step by step.