Skip to content

Instantly share code, notes, and snippets.

@Zamion101
Created January 23, 2019 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zamion101/2c24d38fc620e575c2037e4d09bce421 to your computer and use it in GitHub Desktop.
Save Zamion101/2c24d38fc620e575c2037e4d09bce421 to your computer and use it in GitHub Desktop.
My RegistryUtils
package me.Zamion101.HMM.utils;
import com.google.common.collect.Maps;
import me.Zamion101.HMM.HMM;
import me.Zamion101.HMM.blocks.impl.HBlock;
import me.Zamion101.HMM.blocks.impl.IHBlock;
import me.Zamion101.HMM.items.impl.IHItem;
import me.Zamion101.HMM.items.others.ItemRockPiece;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.registries.IForgeRegistryModifiable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Mod.EventBusSubscriber(modid = HMM.MOD_ID)
public class RegistryUtils {
private static Map<String,Block> blocks = Maps.newHashMap();
private static Map<String,Item> items = Maps.newHashMap();
private static List<ResourceLocation> recipes = new ArrayList<>();
private static Map<String,SoundEvent> sounds = Maps.newHashMap();
@SubscribeEvent
public static void registerBlocks(RegistryEvent.Register<Block> event){
HMM.LOGGER.info("---------------===============[REGISTER BLOCKS]===============---------------");
if(blocks.isEmpty()){ HMM.LOGGER.info("Blocks: SKIPPED");
HMM.LOGGER.info("---------------===============[REGISTER BLOCKS]===============---------------");return;}
HMM.LOGGER.info("_ Blocks");
for(Block hBlock : blocks.values()){
HMM.LOGGER.info("|-Blocks Info: Name '" + ((IHBlock)hBlock).getName() + "'");
event.getRegistry().register(hBlock);
}
HMM.LOGGER.info("Blocks: REGISTERED");
HMM.LOGGER.info("---------------===============[REGISTER BLOCKS]===============---------------");
}
@SubscribeEvent
public static void registerItems(RegistryEvent.Register<Item> event){
HMM.LOGGER.info("---------------===============[REGISTER ITEMS]===============---------------");
if(items.isEmpty()){ HMM.LOGGER.info("Items: SKIPPED");
HMM.LOGGER.info("---------------===============[REGISTER ITEMS]===============---------------");return;}
HMM.LOGGER.info("_ Items");
for(Item hItem : items.values()){
if(hItem instanceof IHItem) {
HMM.LOGGER.info("|-Item Info: Name '" + ((IHItem) hItem).getName() + "' isColored '" + ((IHItem) hItem).isColored() + "'");
}else{
HMM.LOGGER.info("|-Item Info: Name '" + hItem.getRegistryName() + "' isColored 'false'");
}
event.getRegistry().register(hItem);
}
HMM.LOGGER.info("Items: REGISTERED");
HMM.LOGGER.info("---------------===============[REGISTER ITEMS]===============---------------");
}
@SubscribeEvent
public static void registerSounds(RegistryEvent.Register<SoundEvent> event){
HMM.LOGGER.info("---------------===============[REGISTER SOUNDS]===============---------------");
if(sounds.isEmpty()){ HMM.LOGGER.info("Sounds: SKIPPED");
HMM.LOGGER.info("---------------===============[REGISTER SOUNDS]===============---------------");return;}
HMM.LOGGER.info("_ Sounds");
for(String key : sounds.keySet()){
SoundEvent sound = sounds.get(key);
HMM.LOGGER.info("|-Sound Info: Name '" + key + "'");
event.getRegistry().register(sound);
}
HMM.LOGGER.info("Sounds: REGISTERED");
HMM.LOGGER.info("---------------===============[REGISTER SOUNDS]===============---------------");
}
public static void addSound(String key, SoundEvent sound){
sound.setRegistryName(key);
sounds.put(key,sound);
}
public static SoundEvent getSound(String key){
if(!sounds.containsKey(key))return null;
return sounds.get(key);
}
@SubscribeEvent
public static void registerModels(ModelRegistryEvent event){
registerRender();
}
private static void registerRender() {
for(Item tmp : items.values()){
if(tmp == null || tmp == Items.AIR)continue;
if(tmp instanceof IHItem) {
IHItem item = (IHItem) tmp;
if (item.isColored()) {
for (int i = 0; i < item.getColorPalette().length; i++) {
ModelLoader.setCustomModelResourceLocation(tmp, i, new ModelResourceLocation(tmp.getRegistryName(), "inventory"));
}
continue;
}
}
if(tmp instanceof ItemRockPiece){
ItemRockPiece item = (ItemRockPiece)tmp;
for(int i = 0; i < item.getColorPalette().length; i++){
ModelLoader.setCustomModelResourceLocation(item,i,new ModelResourceLocation("hmm:rock_piece_"+i, "inventory"));
}
}
ModelLoader.setCustomModelResourceLocation(tmp,0,new ModelResourceLocation(tmp.getRegistryName(),"inventory"));
}
/** for(Block block : blocks){
if(block == null || block == Blocks.AIR)continue;
Item item = Item.getItemFromBlock(block);
ModelLoader.setCustomModelResourceLocation(item,0,new ModelResourceLocation(item.getRegistryName(),"inventory"));
}*/
}
@SubscribeEvent
public static void removeRecipes(RegistryEvent.Register<IRecipe> event){
IForgeRegistryModifiable reg = (IForgeRegistryModifiable) event.getRegistry();
for(ResourceLocation rl : recipes){
reg.remove(rl);
}
}
public static void registerOreDictionary(){
for(Item item : items.values()){
if(item == null || item == Items.AIR)continue;
if(item instanceof IHItem){
IHItem hItem = (IHItem) item;
if(hItem.isOreDictionary()){
OreDictionary.registerOre(hItem.getOreDictionaryName(),item);
}
}
}
}
public static void addBlock(HBlock block){
blocks.put(block.getName(),block);
}
public static void addItem(Item item){
System.out.println(Objects.requireNonNull(item.getRegistryName()).toString().substring(4));
items.put(item.getRegistryName().toString().substring(4),item);
}
public static Item getItem(String name){
return items.get(name);
}
public static Block getBlock(String name){
return blocks.get(name);
}
public static void removeRecipe(String name){
recipes.add(new ResourceLocation(name.contains(":") ? name : "minecraft:" + name));
}
public static void registerColors(){
for(Item item : items.values()){
if(item == null || item == Items.AIR)continue;
if(item instanceof IHItem){
IHItem hItem = (IHItem)item;
if (hItem.isColored()) {
Minecraft.getMinecraft().getItemColors().registerItemColorHandler(hItem, item);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment