Jump to content

[1.7.10][SOLVED] Custom Item Renderer Issues


hilburn

Recommended Posts

I'm going to begin this right at the start because I think that'd be easier to understand overall.

 

I'm creating a method of disabling Items in certain dimensions - to facilitate the creation of magic/tech only dimensions, prevent people quarrying in the overworld, stuff like that.

 

The method I'm currently using detects if a disabled item is in the player's inventory and disables it from being used by removing it, then creating a new ItemStack of DisabledItem with the original ItemStack stored as an NBT tag. This Item has no interesting properties and so can't be placed in the world as a block, worn as armour, used as a weapon/tool etc.

 

What I would like to do however is to get the "stored" item to render normally, except in the inventory where I want to overlay a small padlock icon. Here is where I am having trouble, I cannot get blocks or items with custom or multi-pass renders to display properly.

 

If anyone could give some advice on how to approach this I'd be very appreciative.

 

Link to comment
Share on other sites

Hi

 

The Item Rendering topics on this page might give useful clues.

http://greyminecraftcoder.blogspot.com.au/p/list-of-topics.html

 

I think the easiest way is probably

1) make an ItemStack of the disabled Item

2) copy the vanilla code that prepares and calls the Item renderer, and use it to call the renderer methods of the disabled Item.

The hardest part will be getting your renderer to execute at the right time for multi-pass renderers; eg you might need to use one of the post-render events (eg RenderGameOverlayEvent, RenderWorldLastEvent) to render your stuff last; might need to experiment a bit.

 

-TGG

Link to comment
Share on other sites

Thanks for the advice - I've got closer (a lot closer) but I've hit a ridiculous wall - the easiest part of the whole thing, rendering basic items in the inventory doesn't work, no matter what method I use to try to do it: tesselator, GL, .renderIcon - all result in an empty looking inventory slot.

 

If you have any ideas about what's going wrong in this, that'd be awesome

 

package com.hilburn.dimensionguard.client;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.IItemRenderer;
import net.minecraftforge.client.MinecraftForgeClient;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

public class DisabledRenderer implements IItemRenderer {
    private static RenderItem renderItem = new RenderItem();
    private static ResourceLocation lockTexture = new ResourceLocation("dimensionguard","textures/items/lock.png");
    private static Minecraft mc = Minecraft.getMinecraft();
    private static RenderBlocks renderBlocksIr = new RenderBlocks();


    @Override
    public boolean handleRenderType (ItemStack item, ItemRenderType type)
    {
        switch (type)
        {
        case ENTITY:
        case EQUIPPED:
        case EQUIPPED_FIRST_PERSON:
        case INVENTORY:
            return true;
        default:
        case FIRST_PERSON_MAP:
            return false;
        }
    }
    
@Override
public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item, ItemRendererHelper helper) {
	return true;
}
    
    
    @Override
    public void renderItem (ItemRenderType type, ItemStack itemStack, Object... data)
    {
    	ItemStack thisStack = ItemStack.loadItemStackFromNBT((NBTTagCompound) itemStack.stackTagCompound.getTag("ItemStack"));

        boolean isInventory = type == ItemRenderType.INVENTORY;
        if (data.length > 1) {
	}
        Tessellator tess = Tessellator.instance;
        
        TextureManager texturemanager = mc.getTextureManager();
    	GL11.glPushMatrix();
        Item item = thisStack.getItem();
        Block block = Block.getBlockFromItem(item);

        if (thisStack != null && block != null && block.getRenderBlockPass() != 0){
            GL11.glEnable(GL11.GL_BLEND);
            GL11.glEnable(GL11.GL_CULL_FACE);
            OpenGlHelper.glBlendFunc(770, 771, 1, 0);
        }
	IItemRenderer storedRenderer = MinecraftForgeClient.getItemRenderer(thisStack, type);
	//======Handles Special Blocks and Items======
	if (storedRenderer!=null){
		storedRenderer.renderItem(type, thisStack, data); 
	}
	else{
		if (thisStack.getItemSpriteNumber() == 0 && item instanceof ItemBlock && RenderBlocks.renderItemIn3d(block.getRenderType()))
        { 													
			//=====Handles regular blocks======
            texturemanager.bindTexture(texturemanager.getResourceLocation(0));
            switch (type){
            case EQUIPPED_FIRST_PERSON:
            case EQUIPPED:
                GL11.glTranslatef(0.5F, 0.5F, 0.5F);
                break;
            case ENTITY:
            default:
            }
            if (thisStack != null && block != null && block.getRenderBlockPass() != 0)
            {
                GL11.glDepthMask(false);
                renderBlocksIr.renderBlockAsItem(block, thisStack.getItemDamage(), 1.0F);
                GL11.glDepthMask(true);
            }
            else
            {
                renderBlocksIr.renderBlockAsItem(block, thisStack.getItemDamage(), 1.0F);
            }
        }
		else{
			//=======Handles Regular Items======
			IIcon icon = thisStack.getIconIndex();
			float xMax;
	        float yMin;
	        float xMin;
	        float yMax;
	        float depth = 1f / 16f;

	        float width;
	        float height;
	        float xDiff;
	        float yDiff;
	        float xSub;
	        float ySub;

	        xMin = icon.getMinU();
	        xMax = icon.getMaxU();
	        yMin = icon.getMinV();
	        yMax = icon.getMaxV();
	        width = icon.getIconWidth();
	        height = icon.getIconHeight();
	        xDiff = xMin - xMax;
	        yDiff = yMin - yMax;
	        xSub = 0.5f * (xMax - xMin) / width;
	        ySub = 0.5f * (yMax - yMin) / height;
	        

	        if (isInventory)
	        {
	        	//=======Render Icon in inventory======== Doesn't Work
	           renderItem.renderIcon(0, 0, icon, 16, 16);
	        }
	        else
	        {
	            GL11.glEnable(GL12.GL_RESCALE_NORMAL);
	            switch (type)
	            {
	            case EQUIPPED_FIRST_PERSON:
	            	//TODO: get items to render in the right place
	            case EQUIPPED:
	            	GL11.glRotated(90F, 0F, 1F, 0F);
	            	GL11.glTranslatef(-0.8F, 0.7F, -0.8F);
	                //GL11.glTranslatef(0, -4 / 16f, 0);
	                break;
	            case ENTITY:
	                GL11.glTranslatef(-0.5f, 0f, depth); // correction of the rotation point when items lie on the ground
	            default:
	            }

	            //=====Front and back=====
	            tess.startDrawingQuads();
	            tess.setNormal(0, 0, 1);
                tess.addVertexWithUV(0, 0, 0, xMax, yMax);
                tess.addVertexWithUV(1, 0, 0, xMin, yMax);
                tess.addVertexWithUV(1, 1, 0, xMin, yMin);
                tess.addVertexWithUV(0, 1, 0, xMax, yMin);
	            tess.draw();

	            tess.startDrawingQuads();
	            tess.setNormal(0, 0, -1);
                tess.addVertexWithUV(0, 1, -depth, xMax, yMin);
                tess.addVertexWithUV(1, 1, -depth, xMin, yMin);
                tess.addVertexWithUV(1, 0, -depth, xMin, yMax);
                tess.addVertexWithUV(0, 0, -depth, xMax, yMax);
	            tess.draw();

	            // =========Sides============
	            tess.startDrawingQuads();
	            tess.setNormal(-1, 0, 0);
	            float pos;
	            float iconPos;
                float w = width, m = xMax, d = xDiff, s = xSub;
                for (int k = 0, e = (int) w; k < e; ++k)
                {
                    pos = k / w;
                    iconPos = m + d * pos - s;
                    tess.addVertexWithUV(pos, 0, -depth, iconPos, yMax);
                    tess.addVertexWithUV(pos, 0, 0, iconPos, yMax);
                    tess.addVertexWithUV(pos, 1, 0, iconPos, yMin);
                    tess.addVertexWithUV(pos, 1, -depth, iconPos, yMin);
                }
	            tess.draw();
	            tess.startDrawingQuads();
	            tess.setNormal(1, 0, 0);
	            float posEnd;
                w = width; 
                m = xMax;
                d = xDiff;
                s = xSub;
                float d2 = 1f / w;
                for (int k = 0, e = (int) w; k < e; ++k)
                {
                    pos = k / w;
                    iconPos = m + d * pos - s;
                    posEnd = pos + d2;
                    tess.addVertexWithUV(posEnd, 1, -depth, iconPos, yMin);
                    tess.addVertexWithUV(posEnd, 1, 0, iconPos, yMin);
                    tess.addVertexWithUV(posEnd, 0, 0, iconPos, yMax);
                    tess.addVertexWithUV(posEnd, 0, -depth, iconPos, yMax);
                }
	            tess.draw();
	            tess.startDrawingQuads();
	            tess.setNormal(0, 1, 0);
                float h = height;
                m = yMax;
                d = yDiff;
                s = ySub;
                d2 = 1f / h;
                for (int k = 0, e = (int) h; k < e; ++k)
                {
                    pos = k / h;
                    iconPos = m + d * pos - s;
                    posEnd = pos + d2;
                    tess.addVertexWithUV(0, posEnd, 0, xMax, iconPos);
                    tess.addVertexWithUV(1, posEnd, 0, xMin, iconPos);
                    tess.addVertexWithUV(1, posEnd, -depth, xMin, iconPos);
                    tess.addVertexWithUV(0, posEnd, -depth, xMax, iconPos);
	            }
	            tess.draw();
	            tess.startDrawingQuads();
	            tess.setNormal(0, -1, 0);
            	h = height; 
            	m = yMax;
            	d = yDiff;
            	s = ySub;
                for (int k = 0, e = (int) h; k < e; ++k)
                {
                    pos = k / h;
                    iconPos = m + d * pos - s;
                    tess.addVertexWithUV(1, pos, 0, xMin, iconPos);
                    tess.addVertexWithUV(0, pos, 0, xMax, iconPos);
                    tess.addVertexWithUV(0, pos, -depth, xMax, iconPos);
                    tess.addVertexWithUV(1, pos, -depth, xMin, iconPos);
                }
	            tess.draw();
	            GL11.glDisable(GL12.GL_RESCALE_NORMAL);
	        }
		}
	}

	if (thisStack != null && block != null && block.getRenderBlockPass() != 0)
        {
            GL11.glDisable(GL11.GL_BLEND);
        }
        
	//TODO: Overlay lock in inventory


        GL11.glPopMatrix();

        
    }
}

Link to comment
Share on other sites

Hi

 

Based on this diagram (1.6.4 but probably still correct):

http://greyminecraftcoder.blogspot.com.au/2013/08/rendering-inventory-items.html

 

.. I'd guess that you are missing some of the important setup steps from renderItemIntoGUI.

 

Perhaps you could call renderItemIntoGUI() instead of renderIcon?

 

-TGG

 

I have tried both renderItemIntoGUI and renderItemWithEffectsIntoGUI both to no avail unfortunately

 

Don't really know much about this, so a few stupid Qs:

 

Is the item actually in the inventory (do you assign it a slot and stuff)?

Could you post some images and stuff?

 

Don't worry - I am a huge fan of stupid questions when it comes to debugging annoying code

 

Here's a quick album (sorry for night screenshots)

Link to comment
Share on other sites

Odd, is there a way to get and call the item renderer or just see if there is a custom renderer and then otherwise just manually render the texture?

 

It looks like you're just using:

 

if (storedRenderer!=null){

storedRenderer.renderItem(type, thisStack, data);

}

 

to render normal items?

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Falling victim to a scam is a distressing reality many face. I found myself in such a predicament not too long ago, having invested a significant amount of money in what I believed to be a promising platform, only to discover it was nothing more than a fraudulent scheme. The devastating realization that my hard-earned funds had vanished and left me feeling helpless and vulnerable until I found a glimmer of hope in the form of TECHNOCRATE RECOVERY. Discovering TECHNOCRATE RECOVERY felt like stumbling upon a lifeline amidst the turbulent sea of financial fraud. Their reputation as a remarkable team of professionals specializing in assisting individuals in recovering funds lost to scams preceded them, offering a ray of hope in my darkest hour. With trepidation and a flicker of optimism, I reached out to them, hoping against hope for a chance at redemption. From the very first interaction, TECHNOCRATE RECOVERY proved themselves to be true lifesavers. Their compassion, professionalism, and unwavering dedication to my cause were evident from the outset, instilling a sense of trust and reassurance in their abilities. They wasted no time in springing into action, meticulously gathering the necessary information, and laying out a strategic plan for the recovery process. What followed was nothing short of miraculous. With precision and expertise, TECHNOCRATE RECOVERY navigated the complexities of cryptocurrency scams, uncovering the deceit and reclaiming what was rightfully mine. MAIL BOX: Technocratrecovery(@)contractor(.)net   WEBSITE INFO: www (.)technocraterecovery(.)site
    • Hello, I have been suffering with consistent crashes either during load up or during play-time. I have updated all drivers, updated and reinstalled java versions, and tried curseforge, GDLauncher, and PrismLauncher. I am struggling to find the issue of the crash as it is inhibiting me from playing. Please help, thanks. https://pastebin.com/GnQHr6sZ https://pastebin.com/6EyR1Sgk
    • Hello, I am trying to join a server with mods, specifically a Custom Profile Modpack I made. I have no idea how to identify the problem, please help. I attached the error, Modpack, and the Crash Report it gave me. Mods: https://ibb.co/N7p6Pgr Modpack: https://drive.google.com/file/d/1KCEyq_sGAF9q7tilKbdGOubpKeUdHRHx/view?usp=sharing Minecraft Crash Report: https://paste.ee/p/zISCN Minecraft Launcher Error Image: https://ibb.co/sbwQRb4
    • Thanks for the response. I ended up going with this:   public class PlayerDeathHandler { @SubscribeEvent public void playerKilled(LivingDeathEvent event) { if (event.getEntity() instanceof Player player) { ItemStack poppetStack = new ItemStack(ModItems.DEATH_PROTECTION_POPPET.get()); if (player.getInventory().contains(poppetStack)) { event.setCanceled(true); player.setHealth(player.getMaxHealth()); for (int i = 0; i < player.getInventory().getContainerSize(); i++) { ItemStack stackInSlot = player.getInventory().getItem(i); if (stackInSlot.getItem() == poppetStack.getItem()) { stackInSlot.shrink(1); if (stackInSlot.isEmpty()) { player.getInventory().setItem(i, ItemStack.EMPTY); } break; } } } } } } It works like a charm, but are there any issues that COULD arise using this? I don't have much experience with Forge, so I'm not sure about the common issues and whatnot.
  • Topics

×
×
  • Create New...

Important Information

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