Generalized Model Registry so that it works for other people wanting to use it

This commit is contained in:
oitsjustjose 2016-05-29 09:56:38 -05:00
commit f8659ad2f3
3 changed files with 49 additions and 43 deletions

View file

@ -25,7 +25,7 @@ public class ItemModMaterial extends Item
this.setCreativeTab(TinkerRegistry.tabGeneral); this.setCreativeTab(TinkerRegistry.tabGeneral);
GameRegistry.registerItem(this, "modifier_item"); GameRegistry.registerItem(this, "modifier_item");
registerRecipes(); registerRecipes();
Lib.add(this); Lib.MOD_ITEMS.add(this);
} }
@Override @Override

View file

@ -2,7 +2,6 @@ package com.oitsjustjose.tinkers_addons.lib;
import java.util.ArrayList; import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.item.Item; import net.minecraft.item.Item;
public class Lib public class Lib
@ -18,26 +17,5 @@ public class Lib
public static final String TAG_AUTO_REPAIR_COOLDOWN = "Last Auto-Repair"; public static final String TAG_AUTO_REPAIR_COOLDOWN = "Last Auto-Repair";
public static final String TAG_REDSTONE_FLUX = "Redstone Flux"; public static final String TAG_REDSTONE_FLUX = "Redstone Flux";
private static final ArrayList<Item> allItems = new ArrayList<Item>(); public static final ArrayList<Item> MOD_ITEMS = new ArrayList<Item>();
private static final ArrayList<Block> allBlocks = new ArrayList<Block>();
public static ArrayList<Item> getModItems()
{
return allItems;
}
public static ArrayList<Block> getModBlocks()
{
return allBlocks;
}
public static void add(Item item)
{
allItems.add(item);
}
public static void add(Block block)
{
allBlocks.add(block);
}
} }

View file

@ -1,5 +1,20 @@
package com.oitsjustjose.tinkers_addons.util; package com.oitsjustjose.tinkers_addons.util;
/**
* @author oitsjustjose with asiekierka's help (and others)
*
* This code is free to be used to help you out. Just change the assignment of the variables
* MODID and tab to be your proper MODID and CreativeTab, then this class will work for you!
*
* NOTE: It is assumed that you initialize your items' / blocks' unlocalized names using
* .setUnlocalizedName(MODID + ".<youDesiredNameHere>"); - this is critical for function below
*
* Item models will need to be placed in assets/<your_modid_all_lowercase_no_spaces>/models/items
* Block models will need to be placed in assets/<your_modid_all_lowercase_no_spaces>/models/blocks
* Block models will need BlockState files too, in assets/<your_modid_all_lowercase_no_spaces>/blockstates
*
*/
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -9,6 +24,7 @@ import net.minecraft.block.Block;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelBakery; import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelResourceLocation; import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
@ -18,55 +34,67 @@ import slimeknights.tconstruct.library.TinkerRegistry;
public class ClientProxy extends CommonProxy public class ClientProxy extends CommonProxy
{ {
static CreativeTabs tab = TinkerRegistry.tabParts;
static String MODID = Lib.MODID;
/**
* Grabs all the items from my stored arraylist and registers them Super convenient, I'd suggest doing similar things, and adding a similar block variant if you need it (I didn't)
*/
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public static void init() public static void init()
{ {
ArrayList<Block> blocks = Lib.getModBlocks(); ArrayList<Item> items = Lib.MOD_ITEMS;
ArrayList<Item> items = Lib.getModItems();
for (Block b : blocks)
register(b, "");
for (Item i : items) for (Item i : items)
register(i, ""); register(i);
} }
/**
* @param item
* The Item to register a model registry for. You still have to make the model file, but now MC will know where to look
*/
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
private static void register(Item item, String customName) public static void register(Item item)
{ {
int meta = 0; int meta = 0;
List<ItemStack> subItems = new ArrayList<ItemStack>(); List<ItemStack> subItems = new ArrayList<ItemStack>();
item.getSubItems(item, TinkerRegistry.tabParts, subItems); item.getSubItems(item, tab, subItems);
for (ItemStack sub : subItems) for (ItemStack sub : subItems)
{ {
String name = item.getUnlocalizedName(sub).substring(19).toLowerCase(); String name = item.getUnlocalizedName(sub).substring(MODID.length() + 6).toLowerCase();
ModelBakery.registerItemVariants(item, new ResourceLocation(Lib.MODID.toLowerCase(), name)); ModelBakery.registerItemVariants(item, new ResourceLocation(MODID.toLowerCase(), name));
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, meta, new ModelResourceLocation(Lib.MODID.toLowerCase() + ":" + name, "inventory")); Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, meta, new ModelResourceLocation(MODID.toLowerCase() + ":" + name, "inventory"));
meta++; meta++;
} }
} }
/**
* @param block
* The Item to register a model registry for. You still have to make the model file, but now MC will know where to look
*/
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
private static void register(Block block, String customName) public static void register(Block block)
{ {
int meta = 0; int meta = 0;
Item itemBlock = Item.getItemFromBlock(block); Item itemBlock = Item.getItemFromBlock(block);
// Checks if the block has metadata / subtypes
if (itemBlock.getHasSubtypes()) if (itemBlock.getHasSubtypes())
{ {
List<ItemStack> subItems = new ArrayList<ItemStack>(); List<ItemStack> subItems = new ArrayList<ItemStack>();
itemBlock.getSubItems(itemBlock, TinkerRegistry.tabParts, subItems); itemBlock.getSubItems(itemBlock, tab, subItems);
for (ItemStack sub : subItems) for (ItemStack sub : subItems)
{ {
String name = itemBlock.getUnlocalizedName(sub).toLowerCase().replace("tinkersaddons.", "").replace("tile.", ""); String name = itemBlock.getUnlocalizedName(sub).toLowerCase().replace(MODID.toLowerCase() + ".", "").replace("tile.", "");
ModelBakery.registerItemVariants(itemBlock, new ResourceLocation(Lib.MODID.toLowerCase(), name)); ModelBakery.registerItemVariants(itemBlock, new ResourceLocation(MODID.toLowerCase(), name));
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(itemBlock, meta, new ModelResourceLocation(Lib.MODID.toLowerCase() + ":" + name, "inventory")); Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(itemBlock, meta, new ModelResourceLocation(MODID.toLowerCase() + ":" + name, "inventory"));
meta++; meta++;
} }
} }
else if (customName == "")
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(itemBlock, 0, new ModelResourceLocation(Lib.MODID.toLowerCase() + ":" + block.getUnlocalizedName().substring(19).toLowerCase(), "inventory"));
else else
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(itemBlock, 0, new ModelResourceLocation(Lib.MODID.toLowerCase() + ":" + customName, "inventory")); {
Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(itemBlock, 0, new ModelResourceLocation(MODID.toLowerCase() + ":" + block.getUnlocalizedName().substring(MODID.length() + 6).toLowerCase(), "inventory"));
}
} }
} }