Skip to content

Instantly share code, notes, and snippets.

@Daomephsta
Last active October 16, 2022 16:46
Show Gist options
  • Save Daomephsta/445947cd68164d2b8d8eb258dec6d28f to your computer and use it in GitHub Desktop.
Save Daomephsta/445947cd68164d2b8d8eb258dec6d28f to your computer and use it in GitHub Desktop.
/** Subinterface of BlockEntityProvider that reduces type checking and conversion boilerplate related to tickers.<br>
* This interface should be implemented by the {@code Block}.<br>
* The {@code BlockEntity} must implement {@link BlockEntityHost.Hosted}.
* @param <T> class of the block entity hosted by this block
* @author Daomephsta
*/
public interface BlockEntityHost<T extends BlockEntity & BlockEntityHost.Hosted> extends BlockEntityProvider
{
@Override
public default <U extends BlockEntity> BlockEntityTicker<U> getTicker(
World world, BlockState state, BlockEntityType<U> actualType)
{
if (actualType == getBlockEntityType())
return (a, b, c, blockEntity) -> ((Hosted) blockEntity).tick();
return null;
}
public BlockEntityType<T> getBlockEntityType();
/**
* Implement this interface on any {@code BlockEntity} hosted by a
* {@code Block} that implements {@code BlockEntityHost}
* @author Daomephsta
*/
public interface Hosted
{
public void tick();
}
}
public class ExampleBlockEntity extends BlockEntity implements BlockEntityHost.Hosted
{
/*
* SNIP
*/
public ExampleBlockEntity(BlockPos pos, BlockState state)
{
super(ExampleBlockEntityTypes.EXAMPLE_BLOCK_ENTITY, pos, state);
}
@Override
public void tick()
{
}
/*
* SNIP
*/
}
public class ExampleBlockEntityHost extends Block implements BlockEntityHost<ExampleBlockEntity>
{
/*
* SNIP
*/
@Override
public BlockEntityType<ExampleBlockEntity> getBlockEntityType()
{
return ExampleBlockEntityTypes.EXAMPLE_BLOCK_ENTITY;
}
@Override
public BlockEntity createBlockEntity(BlockPos blockPos, BlockState blockState)
{
return new ExampleBlockEntity(blockPos, blockState);
}
/*
* SNIP
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment