Interface FabricBlock


public interface FabricBlock
General-purpose Fabric-provided extensions for Block subclasses.

Note: This interface is automatically implemented on all blocks via Mixin and interface injection.

  • Method Details

    • getAppearance

      default BlockState getAppearance(BlockState state, BlockRenderView renderView, BlockPos pos, Direction side, @Nullable @Nullable BlockState sourceState, @Nullable @Nullable BlockPos sourcePos)
      Return the current appearance of the block, i.e. which block state this block reports to look like on a given side.

      Common implementors are covers and facades, or any other mimic blocks that proxy another block's model. These will want to override this method. In that case, make sure to carefully read the implementation guidelines below.

      Common consumers are models with connected textures that wish to seamlessly connect to mimic blocks. These will want to check the apparent block state using FabricBlockState.getAppearance(net.minecraft.world.BlockRenderView, net.minecraft.util.math.BlockPos, net.minecraft.util.math.Direction, net.minecraft.block.BlockState, net.minecraft.util.math.BlockPos).

      Generally, the appearance will be queried from a nearby block, identified by the optional sourcePos and sourceState parameters.

      When a block changes appearance, it should trigger a chunk remesh for itself and the adjacent blocks, for example by calling World.updateListeners(net.minecraft.util.math.BlockPos, net.minecraft.block.BlockState, net.minecraft.block.BlockState, int).

      Note: Overriding this method for a block does not change how it renders. It's up to modded models to check for the appearance of nearby blocks and adjust accordingly.

      Implementation guidelines

      This can be called on the server, where block entity data can be safely accessed, and on the client, possibly in a meshing thread, where block entity data is not safe to access! Here is an example of how data from a block entity can be handled safely. The block entity should override RenderDataBlockEntity#getBlockEntityRenderData to return the necessary data. Refer to the documentation of RenderDataBlockEntity for more information.

      @Override
       public BlockState getAppearance(BlockState state, BlockRenderView renderView, BlockPos pos, Direction side, @Nullable BlockState sourceState, @Nullable BlockPos sourcePos) {
           if (renderView instanceof ServerWorld serverWorld) {
               // Server side; ok to use block entity directly!
               BlockEntity blockEntity = serverWorld.getBlockEntity(pos);
      
               if (blockEntity instanceof ...) {
                   // Get data from block entity
                   return ...;
               }
           } else {
               // Client side; need to use the block entity render data!
               Object data = renderView.getBlockEntityRenderData(pos);
      
               // Check if data is not null and of the correct type, and use that to determine the appearance
               if (data instanceof ...) {
                   // get appearance for side ...
                   return ...;
               }
           }
      
           // Example of varying the appearance based on the source pos
           if (sourcePos != null) {
               // get appearance for side ...
               return ...;
           }
      
           // If there is no other appearance, just return the original block state
           return state;
       });
       
      Parameters:
      state - state of this block, whose appearance is being queried
      renderView - the world this block is in
      pos - position of this block, whose appearance is being queried
      side - the side for which the appearance is being queried
      sourceState - (optional) state of the block that is querying the appearance, or null if unknown
      sourcePos - (optional) position of the block that is querying the appearance, or null if unknown
      Returns:
      the appearance of the block on the given side; the original state can be returned if there is no better option