Skip to content

Instantly share code, notes, and snippets.

@Lanse505
Last active July 20, 2023 17:44
Show Gist options
  • Save Lanse505/4daf1e60e7a2582e2f2c7dae9f5889ca to your computer and use it in GitHub Desktop.
Save Lanse505/4daf1e60e7a2582e2f2c7dae9f5889ca to your computer and use it in GitHub Desktop.
public class CulinariumBlockRegistry {
private static final Item.Properties DEFAULT_ITEM_BLOCK_PROPERTIES = new Item.Properties();
// DeferredRegisters
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, Culinarium.MODID);
private static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, Culinarium.MODID);
private static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, Culinarium.MODID);
// Blocks
public static final RegistryObject<CulinariumCropBlock> RYE =
registerBlock("rye",
() -> new CulinariumCropBlock(BlockBehaviour.Properties.copy(Blocks.WHEAT)),
CulinariumBlockRegistry::getDefaultBlockItem);
public static final RegistryObject<RotatedPillarBlock> STRAW_BALE =
registerBlock("straw_bale",
() -> new RotatedPillarBlock(BlockBehaviour.Properties.copy(Blocks.HAY_BLOCK)),
(block) -> () -> new BlockItem(block.get(), DEFAULT_ITEM_BLOCK_PROPERTIES));
// Blocks /w BlockEntities
public static final BlockHolderWithTile<MillstoneBlock, BlockItem, MillstoneTile, BlockEntityType<MillstoneTile>> MILLSTONE =
registerBlockWithTile("millstone",
() -> new MillstoneBlock(BlockBehaviour.Properties.copy(Blocks.IRON_BLOCK).noOcclusion()),
CulinariumBlockRegistry::getDefaultBlockItem,
MillstoneTile::new,
CulinariumBlockRegistry::getDefaultType);
public static final BlockHolderWithTile<ChoppingBoardBlock, BlockItem, ChoppingBoardTile, BlockEntityType<ChoppingBoardTile>> CHOPPING_BOARD =
registerBlockWithTile("chopping_block",
() -> new ChoppingBoardBlock(BlockBehaviour.Properties.copy(Blocks.OAK_PLANKS).noOcclusion()),
CulinariumBlockRegistry::getDefaultBlockItem,
ChoppingBoardTile::new,
CulinariumBlockRegistry::getDefaultType);
public static final BlockHolderWithTile<BrewingBarrelBlock, BlockItem, BrewingBarrelTile, BlockEntityType<BrewingBarrelTile>> BREWING_BARREL =
registerBlockWithTile("brewing_barrel",
() -> new BrewingBarrelBlock(BlockBehaviour.Properties.copy(Blocks.BARREL).noOcclusion()),
CulinariumBlockRegistry::getDefaultBlockItem,
BrewingBarrelTile::new,
CulinariumBlockRegistry::getDefaultType);
// Registration Methods
public static void register(IEventBus bus) {
BLOCKS.register(bus);
ITEMS.register(bus);
BLOCK_ENTITY_TYPES.register(bus);
bus.addListener((CulinariumBlockRegistry::registerAdditionalBlockModels));
}
public static <BLOCK extends Block, BLOCKITEM extends BlockItem> RegistryObject<BLOCK> registerBlock
(String id, Supplier<BLOCK> block, Function<RegistryObject<BLOCK>, Supplier<BLOCKITEM>> item) {
RegistryObject<BLOCK> b = BLOCKS.register(id, block);
ITEMS.register(id, item.apply(b));
return b;
}
public static <BLOCK extends Block, BLOCKITEM extends BlockItem, T extends BlockEntity, TYPE extends BlockEntityType<T>>
BlockHolderWithTile<BLOCK, BLOCKITEM, T, TYPE> registerBlockWithTile(String id,
Supplier<BLOCK> block,
Function<RegistryObject<BLOCK>, Supplier<BLOCKITEM>> item,
BlockEntityType.BlockEntitySupplier<T> factory,
BiFunction<BlockEntityType.BlockEntitySupplier<T>, RegistryObject<BLOCK>, Supplier<TYPE>> type)
{
RegistryObject<BLOCK> b = BLOCKS.register(id, block);
RegistryObject<BLOCKITEM> i = ITEMS.register(id, item.apply(b));
RegistryObject<TYPE> t = BLOCK_ENTITY_TYPES.register(id, type.apply(factory, b));
return new BlockHolderWithTile<>(b, i, t);
}
private static <BLOCK extends Block> Supplier<BlockItem> getDefaultBlockItem(RegistryObject<BLOCK> block) {
return () -> new BlockItem(block.get(), DEFAULT_ITEM_BLOCK_PROPERTIES);
}
private static <BLOCK extends Block, T extends BlockEntity, TYPE extends BlockEntityType<T>> Supplier<BlockEntityType<T>> getDefaultType
(BlockEntityType.BlockEntitySupplier<T> factory, RegistryObject<BLOCK> block) {
return () -> new BlockEntityType<>(factory, Sets.newHashSet(block.get()), null);
}
private static void registerAdditionalBlockModels(ModelEvent.RegisterAdditional event) {
event.register(new ResourceLocation(Culinarium.MODID, "block/millstone_top"));
event.register(new ResourceLocation(Culinarium.MODID, "block/millstone_bottom"));
}
public record BlockHolderWithTile<BLOCK extends Block, BLOCKITEM extends BlockItem, T extends BlockEntity, TYPE extends BlockEntityType<T>>
(RegistryObject<BLOCK> block, RegistryObject<BLOCKITEM> item, RegistryObject<TYPE> type) implements ItemLike {
public BLOCK getBlock() {
return block.get();
}
public BLOCKITEM getItem() {
return item.get();
}
public TYPE getType() {
return type.get();
}
@Override
public Item asItem() {
return item.get();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment