Skip to content

Block States API - #84

Open
SpaceWalkerRS wants to merge 5 commits into
gen2from
blockstates
Open

Block States API#84
SpaceWalkerRS wants to merge 5 commits into
gen2from
blockstates

Conversation

@SpaceWalkerRS

Copy link
Copy Markdown
Member

This API adds block states to Minecraft versions 1.7.10 and below.

  • Many methods in Block that take in int parameters for block data values, directions and coordinates, are deprecated in favor of equivalent methods that take in BlockState, Direction, and BlockPos parameters.
  • The World, WorldChunk, and WorldChunkSection classes have been extended with BlockState getters and setters.
  • A StatefulBlock implementation is provided that forwards calls to the deprecated methods to the equivalent methods that use block states.
  • The chunk format is not modified whatsoever. The block state getters and setters convert block states to/from block IDs and block data values and call the existing block ID and block data value getters and setters.

Block states in 1.8 and above have also been extended with methods that exist in later versions.

  • In 1.8, a bunch of methods are added that forward calls to equivalent methods in Block (Vanilla did not do this until 1.9)
  • In 1.12 and below, several methods are added that do exist in 1.13+, like isAir, ticksRandomly, canBePlaced, onAdded, onRemoved, and more.

Usage

To make use of block states, extend StatefulBlock and implement the three methods to define the block states and metadata conversion.

package com.example;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.world.World;

import net.ornithemc.osl.blockstates.api.block.StatefulBlock;
import net.ornithemc.osl.blockstates.api.state.BlockState;
import net.ornithemc.osl.blockstates.api.state.StateDefinition;
import net.ornithemc.osl.blockstates.api.state.property.BooleanProperty;
import net.ornithemc.osl.blockstates.api.state.property.IntegerProperty;
import net.ornithemc.osl.core.api.util.math.BlockPos;

public class CookieBlock extends StatefulBlock {

	public static final IntegerProperty BITES_TAKEN = IntegerProperty.of("bites_taken", 0, 7);
	public static final BooleanProperty BAKED = BooleanProperty.of("baked");

	public CookieBlock() {
		super(Material.COOKIE);
	}

	@Override
	public void buildStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
		builder.add(BITES_TAKEN, BAKED);
	}

	@Override
	public BlockState getStateFromMetadata(int metadata) {
		int bitesTaken = metadata & 0x7
		boolean baked = (metadata & 0x8) != 0;

		return this.defaultState()
					.set(BITES_TAKEN, bitesTaken)
					.set(BAKED, baked);
	}

	@Override
	public int getMetadataFromState(BlockState state) {
		int bitesTaken = state.get(BITES_TAKEN);
		int baked = state.get(BAKED) ? 0x8 : 0;

		return baked | bitesTaken;
	}

	@Override
	public void onAdded(World world, BlockPos pos, BlockState state) {
		...
	}

	@Override
	public void onRemoved(World world, BlockPos pos, BlockState state) {
		...
	}
}

@SpaceWalkerRS SpaceWalkerRS added the enhancement New feature or request label Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant