Jump to content

[1.8] [Solved] Block with ISmartModel not rendering as Item (TGG?)


SnowyEgret

Recommended Posts

I have a block with an ISmartModel that renders fine when placed. When in inventory, tossed, or in hand, it is being rendered as a block with a texture (with its .png in the textures folder). It is my understanding that if I want my smart model to render as an item, I have to do this (#initClientOnly):

 

https://github.com/TheGreyGhost/MinecraftByExample/blob/master/src/main/java/minecraftbyexample/mbe04_block_smartblockmodel1/StartupClientOnly.java

 

Here is my implementation:

 

public void registerItemBlockModels() {
	System.out.println("Registering ItemBlock models...");
	ItemModelMesher mesher = Minecraft.getMinecraft().getRenderItem().getItemModelMesher();
	String name = StringUtils.underscoreNameFor(BlockMaquette.class);
	Item item = GameRegistry.findItem(MoJo.MODID, name);
	ModelResourceLocation mrl = new ModelResourceLocation(MoJo.MODID + ":" + name, "inventory");
	mesher.register(item, 0, mrl);

	System.out.println("item=" + item);
	System.out.println("mrl=" + mrl);
	System.out.println("model=" + mesher.getModelManager().getModel(mrl));
}

 

 

And here is the console:

 

 

[17:46:13] [Client thread/INFO] [sTDOUT]: [org.snowyegret.mojo.MoJo:init:46]: Intializing------------------------------------------------------------------------------------------------------------
[17:46:13] [Client thread/INFO] [sTDOUT]: [org.snowyegret.mojo.CommonProxy:registerGuiHandler:206]: Registering gui handler...
[17:46:13] [Client thread/INFO] [sTDOUT]: [org.snowyegret.mojo.CommonProxy:registerEventHandlers:132]: Registering event handlers...
[17:46:13] [Client thread/INFO] [sTDOUT]: [org.snowyegret.mojo.CommonProxy:registerTileEntities:140]: Registering tile entities...
[17:46:13] [Client thread/INFO] [sTDOUT]: [org.snowyegret.mojo.ClientProxy:registerItemModels:30]: Registering Item models...
[17:46:13] [Client thread/INFO] [sTDOUT]: [org.snowyegret.mojo.ClientProxy:registerItemBlockModels:49]: Registering ItemBlock models...
[17:46:13] [Client thread/INFO] [sTDOUT]: [org.snowyegret.mojo.ClientProxy:registerItemBlockModels:56]: item=net.minecraft.item.ItemBlock@158f0af4
[17:46:13] [Client thread/INFO] [sTDOUT]: [org.snowyegret.mojo.ClientProxy:registerItemBlockModels:57]: mrl=mojo:block_maquette#inventory
[17:46:13] [Client thread/INFO] [sTDOUT]: [org.snowyegret.mojo.ClientProxy:registerItemBlockModels:58]: model=net.minecraftforge.client.model.IFlexibleBakedModel$Wrapper@3edea9e6

 

 

As you can see, the model is being registered in init and on the client side as instructed. I am assuming the json is set up properly because it is rendering with a texture in inventory, tossed, and in hand. The ISmartModel is rendering fine when placed.

 

Is there something I have missed?

 

Where can I insert printlns or breakpoints to debug this?

Link to comment
Share on other sites

Can someone suggest where I can put a breakpoint to debug my problem?

 

TGG, in MBE04, you suggest overriding #getActualState

 

 

@Override
  public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
  {
    return state;  //for debugging - useful spot for a breakpoint.  Not necessary.
  }

 

 

but this doesn't help me. It is not being called when the item model is being rendered.

Link to comment
Share on other sites

Hi

 

If your item is a ISmartItemModel then handleItemState() is good.

 

Otherwise, I've used this spot a lot:

ItemModelMesher.getItemModel()

 

getActualState is useful for blocks because there are so many other blocks in addition to yours that you get hundreds of nuisance breaks.  But with items you just put yours in the hotbar and it's the only one that reaches the breakpoint, so it's not an issue.

 

-TGG

 

 

 

 

Link to comment
Share on other sites

Thanks Sir Ghost for getting back to me  :)

 

I've got a breakpoint in ItemModelMesher.getItemModel(). For reference here is the method:

 

 

    public IBakedModel getItemModel(ItemStack stack)
    {
        Item item = stack.getItem();
        IBakedModel ibakedmodel = this.getItemModel(item, this.getMetadata(stack));

        if (ibakedmodel == null)
        {
            ItemMeshDefinition itemmeshdefinition = (ItemMeshDefinition)this.shapers.get(item);

            if (itemmeshdefinition != null)
            {
                ibakedmodel = this.modelManager.getModel(itemmeshdefinition.getModelLocation(stack));
            }
        }

        if(ibakedmodel instanceof net.minecraftforge.client.model.ISmartItemModel)
        {
            ibakedmodel = ((net.minecraftforge.client.model.ISmartItemModel)ibakedmodel).handleItemState(stack);
        }

        if (ibakedmodel == null)
        {
            ibakedmodel = this.modelManager.getMissingModel();
        }

        return ibakedmodel;
    }

 

 

item is an ItemBlock as expected, but ibakedmodel is an IFlexibleBakedModel. Seems I want it to be and ISmartItemModel.

 

In MB04 there is no mention of an ISmartItemModel, only the block's ISmartModel. What do I need to do so that this.getItemModel(item, this.getMetadata(stack)) returns an ISmartItemModel? Do I need an implementation of ISmartItemModel? If so, how do I register it in my ModelBakeEvent handler?

 

Here is my ModelBakeEvent handler:

 

 

public void onModelBake(ModelBakeEvent event) {
	IRegistry r = event.modelRegistry;
	r.putObject(ModelResourceLocations.forClass(BlockHighlight.class), new BlockHighlightSmartModel());

	// From MBE04 ModelBakeEventHandler:
	// IBakedModel existingModel = (IBakedModel)object;
	// CamouflageISmartBlockModelFactory customModel = new CamouflageISmartBlockModelFactory(existingModel);
	// event.modelRegistry.putObject(CamouflageISmartBlockModelFactory.modelResourceLocation, customModel);
	// From MBEO4 CamouflageISmartBlockModelFactory:
	// public static final ModelResourceLocation modelResourceLocation = new ModelResourceLocation(
	// "minecraftbyexample:mbe04_block_camouflage");

	r.putObject(new ModelResourceLocation("mojo:block_maquette"), new BlockMaquetteSmartModel());

[/spoiler]

 

 

Link to comment
Share on other sites

Hi, to create your ISmartItemModel, implement it where it is needed. To add it to the model registry (in ModelBakeEvent), define your modelresourcelocation in a field use it to create an object in your ModelBakeEvent. Check the instance of the object to IBakedModel and create a cast IBakedModel to the object. Create a new YourSmartItemModelClass object (the class created implementing ISmartItemModel) and then use the putObject() method to add the smartitemmodel object you just created. Sorry if my explanation was rubbish, here's a snippet of code that might help you out:

 

ModelBakeClass:

public void onEvent(ModelBakeEvent event)
{

    Object object = event.modelRegistry.getObject(YOURMODELRESOURCELOCATION);

    if(object instanceof IBakedModel)
    {

        IBakedModel bakedmodel = (IBakedModel) object;
        
        YourSmartItemModelClass smartmodel = new YourSmartItemModelClass(bakedmodel);

        event.modelRegistry.putObject(YOURMODELRESOURCELOCATION, smartmodel);

    }

}

Development of Plugins [2012 - 2014] Development of Mods [2012 - Current]

Link to comment
Share on other sites

What should the ModelResourceLocator look like?

 

I have already registered an ISmartModel for my block with mrl=mojo:block_maquette.

When I register the ISmartItemModel should the mrl be mojo:block_maquette#inventory?

 

Also, and this is a question for TGG, MB04 seems to suggest that an ISmartItemModel is not necessary and all we have to do is the extra work during the init stage on the client side. Have I misunderstood?

 

 

  public static void initClientOnly()
  {
    // This is currently necessary in order to make your block render properly when it is an item (i.e. in the inventory
    //   or in your hand or thrown on the ground).
    // Minecraft knows to look for the item model based on the GameRegistry.registerBlock.  However the registration of
    //  the model for each item is normally done by RenderItem.registerItems(), and this is not currently aware
    //   of any extra items you have created.  Hence you have to do it manually.  This will probably change in future.
    // It must be done in the init phase, not preinit, and must be done on client only.
    Item itemBlockCamouflage = GameRegistry.findItem("minecraftbyexample", "mbe04_block_camouflage");
    ModelResourceLocation itemModelResourceLocation = new ModelResourceLocation("minecraftbyexample:mbe04_block_camouflage", "inventory");
    final int DEFAULT_ITEM_SUBTYPE = 0;
    Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(itemBlockCamouflage, DEFAULT_ITEM_SUBTYPE, itemModelResourceLocation);
  }

 

 

Link to comment
Share on other sites

Ok, I understand.

 

My smart model must implement both ISmartModel and ISmartItemModel. ISmartItemModel adds one method #handleItemState.

Also, and this is a question for TGG, MB04 seems to suggest that an ISmartItemModel is not necessary and all we have to do is the extra work during the init stage on the client side. Have I misunderstood?

You only need ISmartItemModel if you want to customise your item's appearance based on the item state (for example - change the appearance if your item is damaged - since the damage information is stored in an ItemStack).

 

To summarise - if you want to customise the block appearance when placed, you need to implement ISmartBlockModel.  It sounds like you've got this bit working.

If you also want to customise the block appearance when in inventory or tossed or in your hand, you need to implement ISmartItemModel as well - but the trick here is that you can't use the same information to customise the appearance.  ISmartBlockModel gets to look at the IBlockState but there is no such thing for an item you're holding.  Items use the ItemStack 'damage' field and (if present) NBT information.

 

Could you describe a bit more what you're trying to do?  i.e. how should the Item appearance be customised?

 

-TGG

 

Link to comment
Share on other sites

The block in place should look the same as when in inventory, tossed, and in hand.

 

Here are #handleBlockState and #handleItemState:

 

 

@Override
public IBakedModel handleBlockState(IBlockState state) {

	String name = ((IExtendedBlockState) state).getValue(BlockMaquette.PROP_NAME);
	Iterable<Selection> selections = ((IExtendedBlockState) state).getValue(BlockMaquette.PROP_SELECTIONS);
	EnumFacing facing = (EnumFacing) state.getValue(BlockMaquette.PROP_FACING);

	generateQuads(name, selections, facing);
	return this;
}

@Override
public IBakedModel handleItemState(ItemStack stack) {

                // Instanciating te just to read from tag
	BlockMaquetteTileEntity te = new BlockMaquetteTileEntity();
	te.readFromNBT(stack.getTagCompound());

	String name = te.getName();
	Iterable<Selection> selections = te.getSelections();
	EnumFacing facing = te.getFacing();

	generateQuads(name, selections, facing);
	return this;
}

 

 

This is all working fine. Each method generates the same quads.

 

When I debug with a breakpoint in ItemModelMesher#getItemModel, stack.getItem() is still not returning an ISmartItemModel and this if statement evaluates false:

 

        if(ibakedmodel instanceof net.minecraftforge.client.model.ISmartItemModel)
        {
            ibakedmodel = ((net.minecraftforge.client.model.ISmartItemModel)ibakedmodel).handleItemState(stack);
        }

 

Now that my smart model implements both ISmartModel and ISmartItemModel, what should my ModelBakeEvent handler look like? Which ModelResourceLocation should be registered? or should both be registered?

 

public void onModelBake(ModelBakeEvent event) {
	event.modelRegistry.putObject(new ModelResourceLocation("mojo:block_maquette"), new BlockMaquetteSmartModel());
	event.modelRegistry.putObject(new ModelResourceLocation("mojo:block_maquette", "inventory"), new BlockMaquetteSmartModel());

Link to comment
Share on other sites

Now that my smart model implements both ISmartModel and ISmartItemModel, what should my ModelBakeEvent handler look like? Which ModelResourceLocation should be registered? or should both be registered?

 

public void onModelBake(ModelBakeEvent event) {
	event.modelRegistry.putObject(new ModelResourceLocation("mojo:block_maquette"), new BlockMaquetteSmartModel());
	event.modelRegistry.putObject(new ModelResourceLocation("mojo:block_maquette", "inventory"), new BlockMaquetteSmartModel());

Your code looks like it should work.  Yes you should register both of those ModelResourceLocations - the first is used for blocks, the second for inventory, so if you don't register the inventory it will substitute the missing model.

 

-TGG

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.