All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yungnickyoung.minecraft.yungsapi.module.ItemModuleForge Maven / Gradle / Ivy

There is a newer version: 1.21.1-NeoForge-5.1.2
Show newest version
package com.yungnickyoung.minecraft.yungsapi.module;

import com.yungnickyoung.minecraft.yungsapi.api.autoregister.AutoRegisterBlock;
import com.yungnickyoung.minecraft.yungsapi.api.autoregister.AutoRegisterItem;
import com.yungnickyoung.minecraft.yungsapi.autoregister.AutoRegisterField;
import com.yungnickyoung.minecraft.yungsapi.autoregister.AutoRegistrationManager;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.RegisterEvent;

/**
 * Registration of Items and BlockItems.
 */
public class ItemModuleForge {
    public static void processEntries() {
        FMLJavaModLoadingContext.get().getModEventBus().addListener(ItemModuleForge::registerItems);
    }

    private static void registerItems(RegisterEvent event) {
        event.register(Registries.ITEM, helper -> {
            // Register BlockItems
            AutoRegistrationManager.BLOCKS.forEach(data -> registerBlockItem(data, helper));

            // Register BlockItems for leftover blocks that depend on other blocks.
            // These will be things like Stairs, Slabs, Fences, Walls, etc.
            BlockModuleForge.EXTRA_BLOCKS.forEach(extraBlockData -> registerExtraBlockItem(extraBlockData, helper));

            // Register items
            AutoRegistrationManager.ITEMS.stream()
                    .filter(data -> !data.processed())
                    .forEach(data -> registerItem(data, helper));
        });
    }

    private static void registerBlockItem(AutoRegisterField data, RegisterEvent.RegisterHelper helper) {
        AutoRegisterBlock autoRegisterBlock = (AutoRegisterBlock) data.object();
        if (autoRegisterBlock.hasItemProperties()) {
            BlockItem blockItem = new BlockItem(autoRegisterBlock.get(), autoRegisterBlock.getItemProperties().get());
            helper.register(data.name(), blockItem);
        }
    }

    private static void registerExtraBlockItem(BlockModuleForge.ExtraBlockData extraBlockData, RegisterEvent.RegisterHelper helper) {
        BlockItem blockItem = new BlockItem(extraBlockData.block(), extraBlockData.itemProperties().get());
        helper.register(extraBlockData.blockRegisteredName(), blockItem);
    }

    private static void registerItem(AutoRegisterField data, RegisterEvent.RegisterHelper helper) {
        AutoRegisterItem autoRegisterItem = (AutoRegisterItem) data.object();
        Item item = autoRegisterItem.get();
        helper.register(data.name(), item);
        data.markProcessed();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy