Jump to content

[SOLVED][1.8]Attempting to create dynamic item textures, failing.


Sgmjscorp

Recommended Posts

Hello there!  I'm trying to make an item that changes its texture dynamically but have so far made no progress into figuring out how, even after going through a few tutorial codes that are supposed to do "animation". No luck, so I want to see if anyone out there can see what I'm missing and help me fix it.  Posted is the relevant coding, and if you need anything more please let me know.

 

Main Class

package com.scors4.bioframe.common;

import java.util.ArrayList;
import java.util.List;

import com.scors4.bioframe.common.items.*;
import com.scors4.bioframe.common.proxy.CommonProxy;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemModelMesher;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

@Mod(modid=References.MODID, name=References.MODNAME, version=References.VERSION)
public class BioFrame {

@Instance("bioframe")
public static BioFrame instance = new BioFrame();

@SidedProxy(clientSide = References.CLIENT_PROXY_CLASS,  serverSide = References.COMMON_PROXY_CLASS)
public static CommonProxy proxy;

public static final String modID = "bioframe";
public static boolean active = false;

public static List<String> enabledMods = new ArrayList<String>();

public static Item itemExtractor;

static public CreativeTabs tabBioFrame = null;

public static void registerMod(String st)
{
	if(!enabledMods.contains(st))
	{
		enabledMods.add(st);
		System.out.println("Frame is now adding "+st);
	}
}

@EventHandler
public void preInit(FMLPreInitializationEvent preEvent)
{

}

@EventHandler
public void init(FMLInitializationEvent event)
{
	if(enabledMods.size() > 0)
	{
		BioFrame.active = true;

		initObjects();

		registerObjects();

		proxy.registerModels();
	}
}

@EventHandler
public void postInit(FMLPostInitializationEvent postEvent)
{

}

private void initObjects()
{
	tabBioFrame = new CreativeTabs("tabBioFrame") {

		@Override
		public Item getTabIconItem() {
			return itemExtractor;
		}
	};

	initBlocks();
	initItems();
}

private void initBlocks()
{

}

private void initItems()
{
	itemExtractor = new ItemExtractor().setUnlocalizedName("extractor").setCreativeTab(tabBioFrame);
}

private void registerObjects()
{
	registerBlocks();
	registerItems();
}

private void registerBlocks()
{

}

private void registerItems()
{
	GameRegistry.registerItem(itemExtractor, "extractor");
}

}

 

Client Proxy

package com.scors4.bioframe.client.proxy;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemModelMesher;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;

import com.scors4.bioframe.common.BioFrame;
import com.scors4.bioframe.common.proxy.CommonProxy;

public class ClientProxy extends CommonProxy {

public void registerBakery()
{
	ModelBakery.addVariantName(BioFrame.itemExtractor,
			BioFrame.modID+":extractor",
			BioFrame.modID+":extractor1",
			BioFrame.modID+":extractor2",
			BioFrame.modID+":extractor3",
			BioFrame.modID+":extractor4");
}

@Override
public void registerModels()
{
	registerModelItem(BioFrame.itemExtractor);
}

public void registerModelItem(Item item)
{
	ModelResourceLocation model = new ModelResourceLocation(BioFrame.modID+":"+item.getUnlocalizedName().substring(5),"inventory");
	System.out.println("Registering: "+model);
	Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, model);
}

public void registerModelSubsets(Item item, String[] subSets)
{
	ModelBakery.addVariantName(item, subSets);
}
}

 

Item Class

package com.scors4.bioframe.common.items;

import java.util.List;

import com.scors4.bioframe.common.BioFrame;
import com.scors4.bioframe.common.References;
import com.scors4.bioframe.common.elements.EnumElement;

import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class ItemExtractor extends Item {

private static int maxLoad = 100;

public ItemExtractor() {
	super();

	this.setMaxDamage(0);
	this.setMaxStackSize(1);
	this.setHasSubtypes(false);
}

public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)	{
	if(!isLoaded(stack))
	{
		setLoaded(stack, true);
		setElement(stack, EnumElement.FIRE);
	}

	loadAmount(stack, 25);

	return stack;
}

@Override
@SideOnly(Side.CLIENT)
public ModelResourceLocation getModel(ItemStack stack, EntityPlayer player, int useRemaining)
{
	if(!isLoaded(stack))
	{
		return null;
	}
	else
	{
		System.out.println(ItemMultiModels.getInstance().getExtractor(0));
		return ItemMultiModels.getInstance().getExtractor(0);
	}
}

public void addInformation(ItemStack stack, EntityPlayer player, List tooltip, boolean advanced)
{
	tooltip.add("Element loaded: "+getElement(stack).getLocalName());
	tooltip.add("Amount: "+getAmountLoaded(stack));
}

public boolean isLoaded(ItemStack stack)
{
	NBTTagCompound tag = getTag(stack);
	return tag.getBoolean("IsLoaded");
}

public boolean setLoaded(ItemStack stack, boolean value)
{
	NBTTagCompound tag = getTag(stack);
	tag.setBoolean("IsLoaded", value);
	writeTag(stack, tag);
	return value;
}

public void loadAmount(ItemStack stack, int amt)
{
	NBTTagCompound tag = getTag(stack);
	int lAmt = tag.getInteger("Amount");
	if(lAmt + amt > 100){tag.setInteger("Amount",100);}
	else
		tag.setInteger("Amount", lAmt + amt);
	writeTag(stack, tag);
}

public void onCreated(ItemStack stack, World world, EntityPlayer player)
{
	NBTTagCompound tag = getTag(stack);
	tag.setBoolean("IsLoaded", false);
	writeTag(stack, tag);
}

public int getColorFromItemStack(ItemStack stack, int renderPass)
{
	NBTTagCompound tag = getTag(stack);
	if(renderPass > 0 && tag.getBoolean("IsLoaded"))
	{
		EnumElement element = EnumElement.valueOf(tag.getString("Element"));
		return element.getHexColor();
	}
	else
		return 0xFFFFFF;
}

public void setElement(ItemStack stack, EnumElement element)
{
	NBTTagCompound tag = getTag(stack);

	tag.setString("Element", element.toString());
}

public EnumElement getElement(ItemStack stack)
{
	NBTTagCompound tag = getTag(stack);

	if(tag.getBoolean("IsLoaded"))
	{
		return EnumElement.valueOf(tag.getString("Element"));
	}
	else
		return EnumElement.BASIC;
}

public int getAmountLoaded(ItemStack stack)
{
	NBTTagCompound tag = getTag(stack);
	if(isLoaded(stack))
		return tag.getInteger("Amount");
	else
		return 0;
}

public NBTTagCompound getTag(ItemStack stack)
{
	NBTTagCompound tag = stack.getTagCompound();
	if(tag == null)
	{
		tag = new NBTTagCompound();
		stack.setTagCompound(tag);
	}
	return tag;
}

public void writeTag(ItemStack stack, NBTTagCompound tag)
{
	stack.setTagCompound(tag);
}
}

 

MultiModel storage based on MinecraftByExample

package com.scors4.bioframe.common.items;

import java.util.ArrayList;

import com.scors4.bioframe.common.References;

import net.minecraft.client.resources.model.ModelResourceLocation;

public class ItemMultiModels {

public static ItemMultiModels getInstance(){
	if(instance==null){instance = new ItemMultiModels();}
	return instance;
}

private static ItemMultiModels instance;
private ArrayList<ModelResourceLocation> extractors = new ArrayList<ModelResourceLocation>();

public ItemMultiModels()
{
	extractors.add(new ModelResourceLocation(BioFrame.modID+":extractor1", "inventory"));
}

public ModelResourceLocation getExtractor(int id)
{
	return extractors.get(id);
}
}

 

Base JSON file - Extractor

# THIS IS THE ITEM FILE. IT GOES IN THE assets/bioframe/models/item/ folder.
# Generated using sheenrox82's JSON File Generator for Minecraft 1.8.

{
    "parent": "builtin/generated",
    "textures": {
        "layer0": "bioframe:items/extractor"
    },
    "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 ]
        }
    }
}

 

Example JSON file - Extractor 25% filled

# THIS IS THE ITEM FILE. IT GOES IN THE assets/bioframe/models/item/ folder.
# Generated using sheenrox82's JSON File Generator for Minecraft 1.8.

{
    "parent": "builtin/generated",
    "textures": {
        "layer0": "bioframe:items/extractor"
        "layer1": "bioframe:items/extractor1"
    },
    "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 ]
        }
    }
}

 

I would like to have multi-layer rendering as the second layer is going to have a dynamic color based on... well, you can see that in the item code.  ::)

If anyone could lend a hand or point out where my stupid struck, please let me know!  (Preferably in detail O.o. I'm bright, but a little dense.)

Link to comment
Share on other sites

So the held texture animates incorrectly?  Does it cycle through the frames?  In one of the screenshots I see it looks fine.

 

Does your error console get any 'missing texture' or 'missing model' messages?

 

-TGG

 

It's not an animation, it's supposed to change its texture based on how filled it is, thus why I showed the tooltips in the screenshots as well.  I based my code off of animation because that was the closest example I could find to a dynamic texture.

 

I receive no missing texture or missing model messages, and even put in a bit of code to verify that.  They load fine!  Just not when rendering .-.

Link to comment
Share on other sites

Well one of your models renders fine, the rest don't, which suggests to me that you haven't registered the other names properly, perhaps your paste-names-together-from-bits code has a subtle bug in it.  The only way I know to debug this is to trace into the vanilla code and figure out which bit isn't working as you expect.

 

For example, trace into

ModelBakery.addVariantName(BioFrame.itemExtractor, BioFrame.modID+":extractor",

 

or trace out of

public ModelResourceLocation getModel(ItemStack stack, EntityPlayer player, int useRemaining)

and see why your ModelResourceLocation is not found in the registry

etc

 

Unfortunately it's pretty complicated in there so it might take a while unless you're lucky.  I can't tell what's wrong just from looking at your code, except that it's almost certainly a registration problem and not a json file problem because you're not getting any error messages.

 

-TGG

 

Link to comment
Share on other sites

Thank you, GreyGhost!  It was registration, as such I forgot to actually register the alternate textures...

 

registerBakery(), when I did a call hierarchy, had nothing calling it.  Thank you, as I doubt I'd have noticed that without your pointing out it was a registration error.  Now to try to make the actual item texture change...  That's gonna be fun >:)

 

Thank you again!

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.



×
×
  • Create New...

Important Information

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