Clean up registry

This commit is contained in:
Linnea Gräf 2024-07-15 20:44:29 +02:00
parent 3d19d949ce
commit 1c6023be65
No known key found for this signature in database
GPG key ID: AA563E93EB628D91
3 changed files with 66 additions and 60 deletions

View file

@ -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<CreativeModeTab, CreativeModeTab> 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
{

View file

@ -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
/// <editor-fold desc="Registries" defaultstate="collapsed">
private static final List<DeferredRegister<?>> allRegistries = new ArrayList<>();
private static <T extends DeferredRegister<?>> 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<CreativeModeTab> 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<Block> EXAMPLE_BLOCK = BLOCKS.registerBlock("example_block", NeaBlock::new, BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_GREEN));
/// </editor-fold>
// Creates a new BlockItem with the id "examplemod:example_block", combining the namespace and path
public static final DeferredItem<BlockItem> EXAMPLE_BLOCK_ITEM = ITEMS.registerSimpleBlockItem("example_block", EXAMPLE_BLOCK);
/// <editor-fold desc="Registration helpers" defaultstate="collapsed">
private static <T extends NeaBlock> DeferredBlock<T>
block(String name, Function<BlockBehaviour.Properties, ? extends T> constructor, BlockBehaviour.Properties properties) {
DeferredBlock<T> block = BLOCKS.registerBlock(name, constructor, properties);
ITEMS.registerSimpleBlockItem(block);
return block;
}
/// </editor-fold>
// Creates a new food item with the id "examplemod:example_id", nutrition 1 and saturation 2
public static final DeferredItem<Item> EXAMPLE_ITEM = ITEMS.registerSimpleItem("example_item", new Item.Properties().food(new FoodProperties.Builder()
/// <editor-fold desc="Blocks">
public static final DeferredBlock<NeaBlock> EXAMPLE_BLOCK = block(
"example_block", NeaBlock::new, BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_GREEN));
public static final DeferredBlock<NeaBlock> OTHER_COMPUTER_BLOCK = block(
"other_block", NeaBlock::new, BlockBehaviour.Properties.of().mapColor(MapColor.COLOR_RED));
/// </editor-fold>
/// <editor-fold desc="Items">
public static final DeferredItem<Item> EXAMPLE_ITEM = ITEMS.registerSimpleItem(
"example_item",
new Item.Properties().food(new FoodProperties.Builder()
.alwaysEdible().nutrition(1).saturationModifier(2f).build()));
/// </editor-fold>
public static final DeferredHolder<CreativeModeTab, CreativeModeTab> 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());
}

View file

@ -0,0 +1,3 @@
{
}