Fixed model loading errors on startup

This commit is contained in:
Adubbz 2016-12-04 10:55:44 +11:00
parent 8a7c42d024
commit 1f0140167f
2 changed files with 45 additions and 35 deletions

View file

@ -10,6 +10,7 @@ package biomesoplenty.common.init;
import static biomesoplenty.api.block.BOPBlocks.*; import static biomesoplenty.api.block.BOPBlocks.*;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import biomesoplenty.api.enums.BOPWoods; import biomesoplenty.api.enums.BOPWoods;
@ -132,8 +133,8 @@ public class ModBlocks
//Stone Slabs //Stone Slabs
// need to register items at the same time really so that they can be mapped to each other - bit messy this // need to register items at the same time really so that they can be mapped to each other - bit messy this
other_slab = registerBlock( new BlockBOPHalfOtherSlab(), "other_slab"); other_slab = registerBlock( new BlockBOPHalfOtherSlab(), "other_slab", CreativeTabBOP.instance, false);
double_other_slab = registerBlock( new BlockBOPDoubleOtherSlab(), "double_other_slab", null ); // no creative tab for double slab double_other_slab = registerBlock( new BlockBOPDoubleOtherSlab(), "double_other_slab", null, false ); // no creative tab for double slab
BOPItems.other_slab = ModItems.registerItem( new ItemSlab(other_slab, (BlockSlab)other_slab, (BlockSlab)double_other_slab), "other_slab"); BOPItems.other_slab = ModItems.registerItem( new ItemSlab(other_slab, (BlockSlab)other_slab, (BlockSlab)double_other_slab), "other_slab");
// Logs // Logs
@ -182,12 +183,12 @@ public class ModBlocks
// need to register items at the same time really so that they can be mapped to each other - bit messy this // need to register items at the same time really so that they can be mapped to each other - bit messy this
BlockBOPDoubleWoodSlab.createAllPages(); BlockBOPDoubleWoodSlab.createAllPages();
BlockBOPHalfWoodSlab.createAllPages(); BlockBOPHalfWoodSlab.createAllPages();
wood_slab_0 = registerBlock( BlockBOPHalfWoodSlab.paging.getBlock(0), "wood_slab_0"); wood_slab_0 = registerBlock( BlockBOPHalfWoodSlab.paging.getBlock(0), "wood_slab_0", CreativeTabBOP.instance, false);
double_wood_slab_0 = registerBlock( BlockBOPDoubleWoodSlab.paging.getBlock(0), "double_wood_slab_0", null ); // no creative tab for double slab double_wood_slab_0 = registerBlock( BlockBOPDoubleWoodSlab.paging.getBlock(0), "double_wood_slab_0", null, false ); // no creative tab for double slab
BOPItems.wood_slab_0 = ModItems.registerItem( new ItemSlab(wood_slab_0, (BlockSlab)wood_slab_0, (BlockSlab)double_wood_slab_0), "wood_slab_0"); BOPItems.wood_slab_0 = ModItems.registerItem( new ItemSlab(wood_slab_0, (BlockSlab)wood_slab_0, (BlockSlab)double_wood_slab_0), "wood_slab_0");
wood_slab_1 = registerBlock( BlockBOPHalfWoodSlab.paging.getBlock(1), "wood_slab_1"); wood_slab_1 = registerBlock( BlockBOPHalfWoodSlab.paging.getBlock(1), "wood_slab_1", CreativeTabBOP.instance, false);
double_wood_slab_1 = registerBlock( BlockBOPDoubleWoodSlab.paging.getBlock(1), "double_wood_slab_1", null ); // no creative tab for double slab double_wood_slab_1 = registerBlock( BlockBOPDoubleWoodSlab.paging.getBlock(1), "double_wood_slab_1", null, false ); // no creative tab for double slab
BOPItems.wood_slab_1 = ModItems.registerItem( new ItemSlab(wood_slab_1, (BlockSlab)wood_slab_1, (BlockSlab)double_wood_slab_1), "wood_slab_1"); BOPItems.wood_slab_1 = ModItems.registerItem( new ItemSlab(wood_slab_1, (BlockSlab)wood_slab_1, (BlockSlab)double_wood_slab_1), "wood_slab_1");
// fences have no variant metadata, use a new BlockBOPFence instance for each (note there's no giant_flower_fence or dead_fence) // fences have no variant metadata, use a new BlockBOPFence instance for each (note there's no giant_flower_fence or dead_fence)
@ -319,14 +320,14 @@ public class ModBlocks
// use a separate function for registering doors because the door block and item need to be registered together // use a separate function for registering doors because the door block and item need to be registered together
public static Block registerDoor(BlockBOPDoor door_block, String name, Item door_item) public static Block registerDoor(BlockBOPDoor door_block, String name, Item door_item)
{ {
Block block = registerBlock( door_block, name + "_block", null ); Block block = registerBlock( door_block, name + "_block", null, false );
door_item = ModItems.registerItem( new ItemDoor(block), name ); door_item = ModItems.registerItem( new ItemDoor(block), name );
door_block.setDoorItem(door_item); door_block.setDoorItem(door_item);
return block; return block;
} }
public static void registerBlockVariant(Block block, String stateName, int stateMeta) public static void registerBlockItemModel(Block block, String stateName, int stateMeta)
{ {
Item item = Item.getItemFromBlock(block); Item item = Item.getItemFromBlock(block);
BiomesOPlenty.proxy.registerItemVariantModel(item, stateName, stateMeta); BiomesOPlenty.proxy.registerItemVariantModel(item, stateName, stateMeta);
@ -339,10 +340,15 @@ public class ModBlocks
// by default, set the creative tab for all blocks added in BOP to CreativeTabBOP.instance // by default, set the creative tab for all blocks added in BOP to CreativeTabBOP.instance
return registerBlock(block, blockName, CreativeTabBOP.instance); return registerBlock(block, blockName, CreativeTabBOP.instance);
} }
public static Block registerBlock(Block block, String blockName, CreativeTabs tab)
{
public static Block registerBlock(Block block, String blockName,CreativeTabs tab)
{
return registerBlock(block, blockName, tab, true);
}
public static Block registerBlock(Block block, String blockName, CreativeTabs tab, boolean registerItemModels)
{
Preconditions.checkNotNull(block, "Cannot register a null block");
block.setUnlocalizedName(blockName); block.setUnlocalizedName(blockName);
block.setCreativeTab(tab); block.setCreativeTab(tab);
@ -359,24 +365,27 @@ public class ModBlocks
if (defaultState == null) if (defaultState == null)
{ {
defaultState = block.getBlockState().getBaseState(); defaultState = block.getBlockState().getBaseState();
BiomesOPlenty.logger.error("missing default state for " + block.getUnlocalizedName()); BiomesOPlenty.logger.error("Missing default state for " + block.getUnlocalizedName());
} }
// get the preset blocks variants // Some blocks such as doors and slabs register their items after the blocks (getItemClass returns null)
ImmutableSet<IBlockState> presets = BlockStateUtils.getBlockPresets(block); if (registerItemModels)
if (presets.isEmpty())
{ {
// block has no sub-blocks to register // get the preset blocks variants
registerBlockVariant(block, blockName, 0); ImmutableSet<IBlockState> presets = BlockStateUtils.getBlockPresets(block);
} if (presets.isEmpty())
else
{
// register all the sub-blocks
for (IBlockState state : presets)
{ {
String stateName = bopBlock.getStateName(state); // block has no sub-blocks to register
int stateMeta = block.getMetaFromState(state); registerBlockItemModel(block, blockName, 0);
registerBlockVariant(block, stateName, stateMeta); } else
{
// register all the sub-blocks
for (IBlockState state : presets)
{
String stateName = bopBlock.getStateName(state);
int stateMeta = block.getMetaFromState(state);
registerBlockItemModel(block, stateName, stateMeta);
}
} }
} }
} }
@ -384,7 +393,7 @@ public class ModBlocks
{ {
// for vanilla blocks, just register a single variant with meta=0 and assume ItemBlock for the item class // for vanilla blocks, just register a single variant with meta=0 and assume ItemBlock for the item class
registerBlockWithItem(block, blockName, ItemBlock.class); registerBlockWithItem(block, blockName, ItemBlock.class);
registerBlockVariant(block, blockName, 0); registerBlockItemModel(block, blockName, 0);
} }
return block; return block;

View file

@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import biomesoplenty.api.item.BOPItems; import biomesoplenty.api.item.BOPItems;
@ -46,7 +47,6 @@ import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.particle.Particle; import net.minecraft.client.particle.Particle;
import net.minecraft.client.renderer.ItemMeshDefinition; import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.ItemModelMesher; import net.minecraft.client.renderer.ItemModelMesher;
import net.minecraft.client.renderer.block.model.ModelBakery;
import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.IStateMapper; import net.minecraft.client.renderer.block.statemap.IStateMapper;
import net.minecraft.client.renderer.block.statemap.StateMap; import net.minecraft.client.renderer.block.statemap.StateMap;
@ -57,6 +57,7 @@ import net.minecraft.client.resources.AbstractResourcePack;
import net.minecraft.client.resources.IResourcePack; import net.minecraft.client.resources.IResourcePack;
import net.minecraft.client.resources.LegacyV2Adapter; import net.minecraft.client.resources.LegacyV2Adapter;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.init.Items;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumParticleTypes; import net.minecraft.util.EnumParticleTypes;
@ -98,7 +99,7 @@ public class ClientProxy extends CommonProxy
replaceForgeResources(); replaceForgeResources();
ModelBakery.registerItemVariants(ForgeModContainer.getInstance().universalBucket, bucketModelLocations); ModelLoader.registerItemVariants(ForgeModContainer.getInstance().universalBucket, bucketModelLocations);
} }
@Override @Override
@ -119,13 +120,13 @@ public class ClientProxy extends CommonProxy
} }
@Override @Override
public void registerItemVariantModel(Item item, String name, int metadata) public void registerItemVariantModel(Item item, String name, int metadata)
{ {
if (item != null) Preconditions.checkNotNull(item, "Cannot register models for null item " + name);
{ Preconditions.checkArgument(item != Items.AIR, "Cannot register models for air (" + name + ")");
ModelBakery.registerItemVariants(item, new ResourceLocation("biomesoplenty:" + name));
ModelLoader.setCustomModelResourceLocation(item, metadata, new ModelResourceLocation(BiomesOPlenty.MOD_ID + ":" + name, "inventory")); ModelLoader.registerItemVariants(item, new ResourceLocation("biomesoplenty:" + name));
} ModelLoader.setCustomModelResourceLocation(item, metadata, new ModelResourceLocation(BiomesOPlenty.MOD_ID + ":" + name, "inventory"));
} }
@Override @Override
@ -166,7 +167,7 @@ public class ClientProxy extends CommonProxy
String subItemName = item.getUnlocalizedName(subItem); String subItemName = item.getUnlocalizedName(subItem);
subItemName = subItemName.substring(subItemName.indexOf(".") + 1); // remove 'item.' from the front subItemName = subItemName.substring(subItemName.indexOf(".") + 1); // remove 'item.' from the front
ModelBakery.registerItemVariants(item, new ResourceLocation(BiomesOPlenty.MOD_ID, subItemName)); ModelLoader.registerItemVariants(item, new ResourceLocation(BiomesOPlenty.MOD_ID, subItemName));
ModelLoader.setCustomModelResourceLocation(item, subItem.getMetadata(), new ModelResourceLocation(BiomesOPlenty.MOD_ID + ":" + subItemName, "inventory")); ModelLoader.setCustomModelResourceLocation(item, subItem.getMetadata(), new ModelResourceLocation(BiomesOPlenty.MOD_ID + ":" + subItemName, "inventory"));
} }
} }