NaturesAura/src/main/java/de/ellpeck/naturesaura/reg/ModRegistry.java

110 lines
3.8 KiB
Java
Raw Normal View History

2018-10-13 20:35:18 +02:00
package de.ellpeck.naturesaura.reg;
import de.ellpeck.naturesaura.NaturesAura;
import net.minecraft.block.Block;
2019-10-20 22:30:49 +02:00
import net.minecraft.client.renderer.model.ModelResourceLocation;
import net.minecraft.item.ItemGroup;
2018-10-13 20:35:18 +02:00
import net.minecraft.item.Item;
2019-10-20 22:30:49 +02:00
import net.minecraft.item.BlockItem;
2018-10-13 20:35:18 +02:00
import net.minecraft.item.ItemStack;
2019-10-20 22:30:49 +02:00
import net.minecraft.potion.Effect;
2018-10-13 20:35:18 +02:00
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public final class ModRegistry {
private static final List<IModItem> ALL_ITEMS = new ArrayList<>();
2018-11-29 17:58:47 +01:00
public static void add(IModItem item) {
2018-10-13 20:35:18 +02:00
ALL_ITEMS.add(item);
}
2019-10-20 22:30:49 +02:00
private static void registerPotion(Effect potion, String name) {
2018-11-29 17:58:47 +01:00
potion.setPotionName("potion." + NaturesAura.MOD_ID + "." + name + ".name");
potion.setRegistryName(NaturesAura.MOD_ID, name);
ForgeRegistries.POTIONS.register(potion);
}
2019-10-20 22:30:49 +02:00
private static void registerItem(Item item, String name, ItemGroup tab) {
2018-10-13 20:45:32 +02:00
item.setTranslationKey(NaturesAura.MOD_ID + "." + name);
2018-10-13 20:35:18 +02:00
item.setRegistryName(NaturesAura.MOD_ID, name);
ForgeRegistries.ITEMS.register(item);
2018-11-29 17:58:47 +01:00
item.setCreativeTab(tab);
2018-10-13 20:35:18 +02:00
}
2019-10-20 22:30:49 +02:00
private static void registerBlock(Block block, String name, BlockItem item, ItemGroup tab) {
2018-10-13 20:45:32 +02:00
block.setTranslationKey(NaturesAura.MOD_ID + "." + name);
2018-10-13 20:35:18 +02:00
block.setRegistryName(NaturesAura.MOD_ID, name);
ForgeRegistries.BLOCKS.register(block);
if (item != null) {
item.setRegistryName(block.getRegistryName());
ForgeRegistries.ITEMS.register(item);
}
2018-10-13 20:35:18 +02:00
block.setCreativeTab(tab);
2018-11-29 17:58:47 +01:00
}
2019-10-20 22:30:49 +02:00
private static ItemGroup getTab(IModItem item) {
2018-11-29 17:58:47 +01:00
if (item instanceof ICreativeItem)
return ((ICreativeItem) item).getTabToAdd();
return null;
2018-10-13 20:35:18 +02:00
}
public static void preInit(FMLPreInitializationEvent event) {
for (IModItem item : ALL_ITEMS) {
2018-11-29 17:58:47 +01:00
if (item instanceof Item) {
registerItem((Item) item, item.getBaseName(), getTab(item));
} else if (item instanceof Block) {
2018-10-13 20:35:18 +02:00
Block block = (Block) item;
2019-10-20 22:30:49 +02:00
BlockItem itemBlock;
if (item instanceof ICustomItemBlockProvider)
2018-10-13 20:35:18 +02:00
itemBlock = ((ICustomItemBlockProvider) item).getItemBlock();
else
2019-10-20 22:30:49 +02:00
itemBlock = new BlockItem(block);
2018-10-13 20:35:18 +02:00
2018-11-29 17:58:47 +01:00
registerBlock(block, item.getBaseName(), itemBlock, getTab(item));
2019-10-20 22:30:49 +02:00
} else if (item instanceof Effect)
registerPotion((Effect) item, item.getBaseName());
2018-10-13 20:35:18 +02:00
if (item instanceof IModelProvider) {
Map<ItemStack, ModelResourceLocation> models = ((IModelProvider) item).getModelLocations();
for (ItemStack stack : models.keySet())
NaturesAura.proxy.registerRenderer(stack, models.get(stack));
2018-10-13 20:35:18 +02:00
}
item.onPreInit(event);
}
}
public static void init(FMLInitializationEvent event) {
for (IModItem item : ALL_ITEMS) {
if (item instanceof IColorProvidingBlock)
2018-10-13 20:35:18 +02:00
NaturesAura.proxy.addColorProvidingBlock((IColorProvidingBlock) item);
if (item instanceof IColorProvidingItem)
2018-10-13 20:35:18 +02:00
NaturesAura.proxy.addColorProvidingItem((IColorProvidingItem) item);
if (item instanceof ITESRProvider)
NaturesAura.proxy.registerTESR((ITESRProvider) item);
2018-10-13 20:35:18 +02:00
item.onInit(event);
}
}
public static void postInit(FMLPostInitializationEvent event) {
for (IModItem item : ALL_ITEMS) {
item.onPostInit(event);
}
}
}