From 1c6023be65e5cf1ca40d901ea779d8316ede7bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linnea=20Gr=C3=A4f?= Date: Mon, 15 Jul 2024 20:44:29 +0200 Subject: [PATCH] Clean up registry --- src/main/java/dev/exhq/ajarc/Ajar.java | 47 +----------- .../java/dev/exhq/ajarc/items/Register.java | 76 +++++++++++++++---- .../resources/assets/ajarc/lang/en_us.json | 3 + 3 files changed, 66 insertions(+), 60 deletions(-) create mode 100644 src/main/resources/assets/ajarc/lang/en_us.json diff --git a/src/main/java/dev/exhq/ajarc/Ajar.java b/src/main/java/dev/exhq/ajarc/Ajar.java index 8f9c69b..e56767a 100644 --- a/src/main/java/dev/exhq/ajarc/Ajar.java +++ b/src/main/java/dev/exhq/ajarc/Ajar.java @@ -40,45 +40,15 @@ import net.neoforged.neoforge.registries.DeferredRegister; @Mod(Ajar.MODID) public class Ajar { - // Define mod id in a common place for everything to reference public static final String MODID = "ajarc"; - // Directly reference a slf4j logger private static final Logger LOGGER = LogUtils.getLogger(); - // Create a Deferred Register to hold Blocks which will all be registered under the "examplemod" namespace - // Creates a creative tab with the id "examplemod:example_tab" for the example item, that is placed after the combat tab -// public static final DeferredHolder EXAMPLE_TAB = CREATIVE_MODE_TABS.register("example_tab", () -> CreativeModeTab.builder() -// .title(Component.translatable("itemGroup.examplemod")) //The language key for the title of your CreativeModeTab -// .withTabsBefore(CreativeModeTabs.COMBAT) -// .icon(() -> EXAMPLE_ITEM.get().getDefaultInstance()) -// .displayItems((parameters, output) -> { -// output.accept(EXAMPLE_ITEM.get()); // Add the example item to the tab. For your own tabs, this method is preferred over the event -// }).build()); - - // The constructor for the mod class is the first code that is run when your mod is loaded. - // FML will recognize some parameter types like IEventBus or ModContainer and pass them in automatically. public Ajar(IEventBus modEventBus, ModContainer modContainer) { - // Register the commonSetup method for modloading modEventBus.addListener(this::commonSetup); - // Register the Deferred Register to the mod event bus so blocks get registered - Register.BLOCKS.register(modEventBus); - // Register the Deferred Register to the mod event bus so items get registered - Register.ITEMS.register(modEventBus); - // Register the Deferred Register to the mod event bus so tabs get registered -// CREATIVE_MODE_TABS.register(modEventBus); - - // Register ourselves for server and other game events we are interested in. - // Note that this is necessary if and only if we want *this* class (ExampleMod) to respond directly to events. - // Do not add this line if there are no @SubscribeEvent-annotated functions in this class, like onServerStarting() below. - NeoForge.EVENT_BUS.register(this); - - // Register the item to a creative tab - modEventBus.addListener(this::addCreative); - - // Register our mod's ModConfigSpec so that FML can create and load the config file for us + Register.registerAll(modEventBus); modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC); } @@ -95,22 +65,7 @@ public class Ajar Config.items.forEach((item) -> LOGGER.info("ITEM >> {}", item.toString())); } - // Add the example block item to the building blocks tab - private void addCreative(BuildCreativeModeTabContentsEvent event) - { - if (event.getTabKey() == CreativeModeTabs.BUILDING_BLOCKS) - event.accept(Register.EXAMPLE_BLOCK_ITEM); - } - // You can use SubscribeEvent and let the Event Bus discover methods to call - @SubscribeEvent - public void onServerStarting(ServerStartingEvent event) - { - // Do something when the server starts - LOGGER.info("HELLO from server starting"); - } - - // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent @EventBusSubscriber(modid = MODID, bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) public static class ClientModEvents { diff --git a/src/main/java/dev/exhq/ajarc/items/Register.java b/src/main/java/dev/exhq/ajarc/items/Register.java index f4b84ba..907a585 100644 --- a/src/main/java/dev/exhq/ajarc/items/Register.java +++ b/src/main/java/dev/exhq/ajarc/items/Register.java @@ -2,37 +2,85 @@ package dev.exhq.ajarc.items; import dev.exhq.ajarc.items.blocks.NeaBlock; import net.minecraft.core.registries.Registries; +import net.minecraft.network.chat.Component; import net.minecraft.world.food.FoodProperties; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.CreativeModeTab; +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.state.BlockBehaviour; import net.minecraft.world.level.material.MapColor; +import net.neoforged.bus.api.IEventBus; import net.neoforged.neoforge.registries.DeferredBlock; +import net.neoforged.neoforge.registries.DeferredHolder; import net.neoforged.neoforge.registries.DeferredItem; import net.neoforged.neoforge.registries.DeferredRegister; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + import static dev.exhq.ajarc.Ajar.MODID; public class Register { - public static final DeferredRegister.Blocks BLOCKS = - DeferredRegister.createBlocks(MODID); - // Create a Deferred Register to hold Items which will all be registered under the "examplemod" namespace - public static final DeferredRegister.Items ITEMS = - DeferredRegister.createItems(MODID); - // Create a Deferred Register to hold CreativeModeTabs which will all be registered under the "examplemod" namespace + /// + private static final List> allRegistries = new ArrayList<>(); + + private static > T registry(T value) { + allRegistries.add(value); + return value; + } + + public static void registerAll(IEventBus eventBus) { + allRegistries.forEach(it -> it.register(eventBus)); + } + + public static final DeferredRegister.Blocks BLOCKS = registry(DeferredRegister.createBlocks(MODID)); + public static final DeferredRegister.Items ITEMS = registry(DeferredRegister.createItems(MODID)); public static final DeferredRegister CREATIVE_MODE_TABS = - DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MODID); + registry(DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MODID)); - // Creates a new Block with the id "examplemod:example_block", combining the namespace and path - public static final DeferredBlock EXAMPLE_BLOCK = BLOCKS.registerBlock("example_block", NeaBlock::new, BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_GREEN)); + /// - // Creates a new BlockItem with the id "examplemod:example_block", combining the namespace and path - public static final DeferredItem EXAMPLE_BLOCK_ITEM = ITEMS.registerSimpleBlockItem("example_block", EXAMPLE_BLOCK); + /// + private static DeferredBlock + block(String name, Function constructor, BlockBehaviour.Properties properties) { + DeferredBlock block = BLOCKS.registerBlock(name, constructor, properties); + ITEMS.registerSimpleBlockItem(block); + return block; + } + /// - // Creates a new food item with the id "examplemod:example_id", nutrition 1 and saturation 2 - public static final DeferredItem EXAMPLE_ITEM = ITEMS.registerSimpleItem("example_item", new Item.Properties().food(new FoodProperties.Builder() - .alwaysEdible().nutrition(1).saturationModifier(2f).build())); + /// + public static final DeferredBlock EXAMPLE_BLOCK = block( + "example_block", NeaBlock::new, BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_GREEN)); + public static final DeferredBlock OTHER_COMPUTER_BLOCK = block( + "other_block", NeaBlock::new, BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED)); + + /// + + /// + public static final DeferredItem EXAMPLE_ITEM = ITEMS.registerSimpleItem( + "example_item", + new Item.Properties().food(new FoodProperties.Builder() + .alwaysEdible().nutrition(1).saturationModifier(2f).build())); + + /// + + public static final DeferredHolder EXAMPLE_TAB = + CREATIVE_MODE_TABS.register( + "basic_tab", + () -> CreativeModeTab + .builder() + .title(Component.translatable("itemGroup.ajarcomputers.basic_tab")) + .withTabsBefore(CreativeModeTabs.COMBAT) + .icon(() -> EXAMPLE_ITEM.get().getDefaultInstance()) + .displayItems((parameters, output) -> { + ITEMS.getEntries() + .stream() + .map(DeferredHolder::get) + .forEach(output::accept); + }).build()); } diff --git a/src/main/resources/assets/ajarc/lang/en_us.json b/src/main/resources/assets/ajarc/lang/en_us.json new file mode 100644 index 0000000..0e0dcd2 --- /dev/null +++ b/src/main/resources/assets/ajarc/lang/en_us.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file