Jump to content

[SOLVED] About ModelBakeEvent, ISmartItemModel and loading JSON models


Recommended Posts

Posted

Hello.

 

My goal is to merge 2 models together using the ModelBakeEvent and ISmartItemModel. The first model is used by an item so it has been registered @ItemModelMesher. The second one however, is not being used by any item (and not registered ofc). I figured out that the code works if I use 2 models that have been registered and are used by an item. So I guess I have to load those unused models as well, however, after deep google'ing I still have no idea how to do it.

Yes, I checked if all strings and file names are correct.

 

Code (might be little bit dirty, since Im still trying to get used to this new system):

 

ModelGun (ISmartItemModel)

 

public class ModelGun implements ISmartItemModel
{
private IBakedModel baseModel;
private HashMap<String, IBakedModel> map;

private ItemGun gun = null;

private ItemScope scope = null;
private ItemAccessory accessory = null;
private ItemBarrel barrel = null;
private ItemUnderbarrel underbarrel = null;
private ItemPaint paint = null;

public ModelGun(IBakedModel baseModel, HashMap map)
{
	this.baseModel = baseModel;
	this.map = map;
}

@SuppressWarnings("deprecation")
@Override
public IBakedModel handleItemState(ItemStack stack)
{
	if (stack != null && stack.getItem() instanceof ItemGun)
	{
		ItemGun gun = (ItemGun) stack.getItem();

		this.gun = gun;
		this.scope = gun.getScope(stack);
		this.accessory = gun.getAccessory(stack);
		this.barrel = gun.getBarrel(stack);
		this.underbarrel = gun.getUnderbarrel(stack);
		this.paint = gun.getPaint(stack);
	}

	return this;
}

@Override
public TextureAtlasSprite getTexture()
{
	return baseModel.getTexture();
}

@Override
public List getFaceQuads(EnumFacing enumFacing)
{
	List<BakedQuad> combinedQuadsList = new ArrayList(baseModel.getFaceQuads(enumFacing));

	if(this.scope != null)
	{
		IBakedModel model = this.map.get(this.scope.getUnlocalizedDirect());

		if(model != null)
		{
			combinedQuadsList.addAll(model.getFaceQuads(enumFacing));
		}
	}

	return combinedQuadsList;
}

@Override
public List getGeneralQuads()
{
	List<BakedQuad> combinedQuadsList = new ArrayList(baseModel.getGeneralQuads());

	if(this.scope != null)
	{
		IBakedModel model = this.map.get(this.scope.getUnlocalizedDirect());

		if(model != null)
		{
			combinedQuadsList.addAll(model.getGeneralQuads());
		}
	}

	return combinedQuadsList;
}

@Override
public boolean isAmbientOcclusion()
{
	return baseModel.isAmbientOcclusion();
}

@Override
public boolean isGui3d()
{
	return baseModel.isGui3d();
}

@Override
public boolean isBuiltInRenderer()
{
	return false;
}

@Override
public ItemCameraTransforms getItemCameraTransforms()
{
	return baseModel.getItemCameraTransforms();
}
}

 

 

ModelBakeHandler (ModelBakeEvent)

 

public class ModelBakeHandler
{
@SubscribeEvent
public void onModelBakeEvent(ModelBakeEvent event)
{
	for(ItemGun gun : ItemGun.guns)
	{
		ModelResourceLocation mrl = new ModelResourceLocation(GunCus.MOD_ID + ":" + gun.getUnlocalizedDirect(), "inventory");

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

		if(object instanceof IBakedModel)
		{
			HashMap<String, IBakedModel> modelMap = new HashMap<String, IBakedModel>();

			for(ItemAttachment attachment : ItemAttachment.attachments)
			{
// Trying to get a model that is not used by any item; does not work
				ModelResourceLocation mrl2 = new ModelResourceLocation(GunCus.MOD_ID + ":" + gun.getUnlocalizedDirect() + "_" + attachment.getUnlocalizedDirect(), "inventory");

// Getting a model used by an item; works
				ModelResourceLocation mrl2 = new ModelResourceLocation(GunCus.MOD_ID + ":" + attachment.getUnlocalizedDirect(), "inventory");

				Object object2 = event.modelManager.getModel(mrl2);

				if(object2 != null && object2 instanceof IBakedModel)
				{
					modelMap.put(attachment.getUnlocalizedDirect(), (IBakedModel) object2);
				}
			}

			event.modelRegistry.putObject(mrl, new ModelGun((IBakedModel) object, modelMap));
		}
	}
}
}

 

 

 

Note: I know Im still using the deprecated IBakedModel. Will change that in future ofc. 2 pages that helped me a lot:

https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe15_item_smartitemmodel (amazing)

http://www.minecraftforge.net/forum/index.php/topic,32309.0/nowap.html (helped me understanding the basics)

 

thanks

Posted

You may not need to bake them at all if you just want to do combine that model / texture with another one that is already baked:

// in your ISmartItemModel
IBakedModel model = Minecraft.getMinecraft().getRenderItem().getItemModelMesher().getModelManager().getModel(new ModelResourceLocation("yourmodid:name_of_the_model_you_want", "inventory"));

// now you can add the model's quads to the current smart item model, e.g.:
this.quads.addAll(model.getGeneralQuads());

I use something similar to combine the front and back of my shield models and only the front one is registered and baked.

 

Actually, that reminds me: I DID add those additional names as variants of my item, even though they are not themselves real items (they don't exist anywhere and can't be had by any means).

 

For one or all of your Attachment item classes, try adding the extra names as variants:

ModelBakery.addVariantName(YourAttachmentItem, real_attachment_name, gun_name_plus_attachment_name_1, etc.);

 

Also, instanceof checks for null, so the following are functionally equivalent:

if (object != null && object instanceof Something)
if (object instanceof Something)

Posted

You may not need to bake them at all if you just want to do combine that model / texture with another one that is already baked:

// in your ISmartItemModel
IBakedModel model = Minecraft.getMinecraft().getRenderItem().getItemModelMesher().getModelManager().getModel(new ModelResourceLocation("yourmodid:name_of_the_model_you_want", "inventory"));

// now you can add the model's quads to the current smart item model, e.g.:
this.quads.addAll(model.getGeneralQuads());

I use something similar to combine the front and back of my shield models and only the front one is registered and baked.

 

Actually, that reminds me: I DID add those additional names as variants of my item, even though they are not themselves real items (they don't exist anywhere and can't be had by any means).

 

For one or all of your Attachment item classes, try adding the extra names as variants:

ModelBakery.addVariantName(YourAttachmentItem, real_attachment_name, gun_name_plus_attachment_name_1, etc.);

 

Also, instanceof checks for null, so the following are functionally equivalent:

if (object != null && object instanceof Something)
if (object instanceof Something)

 

Thank you very much. Its working now.

 

For those who want to know: I run the following code right after registering all items to the item model mesher.

 

 

		for(ItemGun gun : ItemGun.guns)
	{
		ArrayList<String> list = new ArrayList<String>();
		list.add(GunCus.MOD_ID + ":" + gun.getUnlocalizedDirect());

		for(ItemAttachment attachment : ItemAttachment.attachments)
		{
			list.add(GunCus.MOD_ID + ":" + gun.getUnlocalizedDirect() + "_" + attachment.getUnlocalizedDirect());
		}

		ModelBakery.addVariantName(gun, list.toArray(new String[list.size()]));
	}

 

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.

×
×
  • Create New...

Important Information

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