package now.nonsense; import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents; import net.minecraft.core.Registry; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.resources.Identifier; import net.minecraft.resources.ResourceKey; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.CreativeModeTabs; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.state.BlockBehaviour; import now.nonsense.blocks.WeirdBlock; import java.util.function.Function; public class ModBlocks { public static final Block TEST_BLOCK = register( "test_block", Block::new, BlockBehaviour.Properties.of().sound(SoundType.METAL), true ); public static final Block WEIRD_BLOCK = register( "weird_block", WeirdBlock::new, BlockBehaviour.Properties.of().sound(SoundType.CHAIN), true ); private static Block register(String name, Function blockFactory, BlockBehaviour.Properties properties, boolean shouldRegisterItem) { // Create a registry key for the block ResourceKey blockKey = keyOfBlock(name); // Create the block instance Block block = blockFactory.apply(properties.setId(blockKey)); // Sometimes, you may not want to register an item for the block. // Eg: if it's a technical block like `minecraft:moving_piston` or `minecraft:end_gateway` if (shouldRegisterItem) { // Items need to be registered with a different type of registry key, but the ID // can be the same. ResourceKey itemKey = keyOfItem(name); BlockItem blockItem = new BlockItem(block, new Item.Properties().setId(itemKey).useBlockDescriptionPrefix()); Registry.register(BuiltInRegistries.ITEM, itemKey, blockItem); } return Registry.register(BuiltInRegistries.BLOCK, blockKey, block); } private static ResourceKey keyOfBlock(String name) { return ResourceKey.create(Registries.BLOCK, Identifier.fromNamespaceAndPath(TestMod.MOD_ID, name)); } private static ResourceKey keyOfItem(String name) { return ResourceKey.create(Registries.ITEM, Identifier.fromNamespaceAndPath(TestMod.MOD_ID, name)); } public static void initialize(){ CreativeModeTabEvents.modifyOutputEvent(CreativeModeTabs.BUILDING_BLOCKS).register((creativeTab) -> { creativeTab.accept(ModBlocks.TEST_BLOCK.asItem()); }); CreativeModeTabEvents.modifyOutputEvent(CreativeModeTabs.FUNCTIONAL_BLOCKS).register((creativeTab) -> { creativeTab.accept(ModBlocks.WEIRD_BLOCK.asItem()); }); } }