Jump to content

1.8: Registering textures for metadata-based items


endreman0

Recommended Posts

I am attempting to create an item that will have a different texture for each metadata value.

ItemBase

(the item):

 

 

package io.github.endreman0.endermagnetics.item;

public class ItemBase extends ItemMetadataEM{
public ItemBase(){
	super("machineBase", "tier1", "tier2");
}
}

 

 

ItemMetadataEM

(base class for every item that uses metadata):

 

 

package io.github.endreman0.endermagnetics.item;

import java.util.List;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class ItemMetadataEM extends ItemEM{
protected final String[] variants;
public ItemMetadataEM(String name, String... variants){
	super(name);
	this.variants = variants;
	setHasSubtypes(true);
}
public ItemMetadataEM(String name, int stackSize, String... variants){
	super(name, stackSize);
	this.variants = variants;
}
public int variants(){return variants.length;}
public String variant(int index){return variants[index];}
@Override public String getUnlocalizedName(ItemStack stack){return super.getUnlocalizedName(stack) + "_" + variants[stack.getItemDamage()];}
@Override @SideOnly(Side.CLIENT)
public void getSubItems(Item item, CreativeTabs tab, List list){
	for(int i=0; i<variants.length; i++) list.add(new ItemStack(item, 1, i));
}
}

 

 

ItemEM

(base class for all items in the mod):

 

 

package io.github.endreman0.endermagnetics.item;

import io.github.endreman0.endermagnetics.EnderMagnetics;
import io.github.endreman0.endermagnetics.util.Utility;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;

public class ItemEM extends Item{
protected final String name;
public ItemEM(String name){this(name, 64);}
public ItemEM(String name, int stackSize){
	super();
	this.name = name;
	setUnlocalizedName(EnderMagnetics.MOD_ID + ":" + name);
	this.maxStackSize = stackSize;
	setCreativeTab(Utility.CREATIVE_TAB);
}
public String getName(){return name;}
}

 

 

ItemsEM

(holds the item instance and registers items in preinit phase):

 

 

package io.github.endreman0.endermagnetics.item;

import net.minecraft.client.resources.model.ModelBakery;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class ItemsEM{
public static final ItemWrench wrench = new ItemWrench();
public static final ItemBase base = new ItemBase();
public static void init(){
	register(wrench);
	register(base);
}
private static void register(ItemEM item){
	GameRegistry.registerItem(item, item.getName());
	if(item instanceof ItemMetadataEM){
		ItemMetadataEM metaItem = (ItemMetadataEM)item;
		String[] variants = new String[metaItem.variants()];
		for(int i=0; i<variants.length; i++) variants[i] = metaItem.getUnlocalizedName() + "_" + metaItem.variant(i);
		ModelBakery.addVariantName(metaItem, variants);
	}
}
}

 

 

ClientProxy

(registers item renders in init phase):

 

 

package io.github.endreman0.endermagnetics;

import io.github.endreman0.endermagnetics.block.BlockEM;
import io.github.endreman0.endermagnetics.block.BlocksEM;
import io.github.endreman0.endermagnetics.item.ItemEM;
import io.github.endreman0.endermagnetics.item.ItemMetadataEM;
import io.github.endreman0.endermagnetics.item.ItemsEM;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;

public class ClientProxy extends CommonProxy{
@Override
public void renders(){
	render(ItemsEM.wrench);
	render(ItemsEM.base);
	render(BlocksEM.blockPearl);
}
private void render(ItemEM item){
	if(item instanceof ItemMetadataEM){
		ItemMetadataEM itemMeta = (ItemMetadataEM)item;
		for(int i=0; i<itemMeta.variants(); i++){
			EnderMagnetics.logger().info("Registering texture for {}#{}", EnderMagnetics.MOD_ID + ":" + item.getName() + "_" + itemMeta.variant(i), "inventory");
			Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, i,
					new ModelResourceLocation(EnderMagnetics.MOD_ID + ":" + item.getName() + "_" + itemMeta.variant(i), "inventory"));
			EnderMagnetics.logger().info("Texture registered");
		}
	}else{
		Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0,//Item, metadata,
				new ModelResourceLocation(EnderMagnetics.MOD_ID + ":" + item.getName(), "inventory"));//location (file name, render type)
	}
}
private void render(BlockEM block){
	Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(block), 0,
			new ModelResourceLocation(EnderMagnetics.MOD_ID + ":" + block.getName(), "inventory"));
}
}

 

 

machineBase_tier1.json

(in

assets/endermagnetics/models/item

):

 

 

{
"parent": "builtin/generated",
"textures": {
	"layer0": "endermagnetics:items/machineBase_tier1"
},
    "display": {
        "thirdperson": {
            "rotation": [ -90, 0, 0 ],
            "translation": [ 0, 1, -3 ],
            "scale": [ 0.55, 0.55, 0.55 ]
        },
        "firstperson": {
            "rotation": [ 0, -135, 25 ],
            "translation": [ 0, 4, 2 ],
            "scale": [ 1.7, 1.7, 1.7 ]
        }
    }
}

 

 

machineBase_tier2.json

is the same, except the texture link is to

machineBase_tier2

. Both textures exist in

assets/endermagnetics/textures/items

, and are named exactly as their JSON counterparts are (not including the extension, of course).

When the game loads, the log shows my texture registration happening immediately after Minecraft searches for (and can't find the textures). Here's that section of the log:

 

 

[12:14:37] [Client thread/ERROR] [FML]: Model definition for location item.endermagnetics:machineBase_tier1#inventory not found

[12:14:37] [Client thread/ERROR] [FML]: Model definition for location item.endermagnetics:machineBase_tier2#inventory not found

[12:14:38] [Client thread/INFO] [endermagnetics]: Registering texture for endermagnetics:machineBase_tier1#inventory

[12:14:38] [Client thread/INFO] [endermagnetics]: Texture registered

[12:14:38] [Client thread/INFO] [endermagnetics]: Registering texture for endermagnetics:machineBase_tier2#inventory

[12:14:38] [Client thread/INFO] [endermagnetics]: Texture registered

 

 

The two "not found" lines are also shown farther down in the log.

 

 

[12:14:43] [Client thread/INFO] [FML]: Max texture size: 8192

[12:14:44] [Client thread/INFO]: Created: 512x512 textures-atlas

[12:14:44] [Client thread/ERROR] [FML]: Model definition for location item.endermagnetics:machineBase_tier1#inventory not found

[12:14:44] [Client thread/ERROR] [FML]: Model definition for location item.endermagnetics:machineBase_tier2#inventory not found

 

 

Am I registering the textures at the wrong time? The wrong way?

Link to comment
Share on other sites

Sorry to not give straight answer, but that's a lot of text to go through.

 

Since you know your code it might be faster to just give example:

https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe11_item_variants

 

Check out resources:

https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/resources/assets/minecraftbyexample

 

You sure you got all variants in json files? Hm, just look at example, pretty much perfect one.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Thanks, that helped me fix it! The issue was using

item.getUnlocalizedName()

. This prepends

"item."

to the actual name, which isn't supposed to be there for model registry. I swapped it for

MOD_ID + ":" + item.getName()

and it worked.

 

Also, that looks like a great reference. Thanks for showing it to me!

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.