Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.7.10]Rendering a Throwable from Item Metadata


americanman
 Share

Recommended Posts

So I have been trying hard to render a throwable entity.  The item class has metadata on it and I am trying to the entity to render with a different icon based off of the item's metadata. The problem is that it seems to think that the metadata is always 0 even thought it could be that through 4.I also want it to behave differently based off of the spawning item's metadata but I want to fix the icon issue before that. I'm not particularly experienced with Java so if this is a Java thing let me know and I'll take it down. I don't want to be a nuisance here. Anyway, here's the code I have right now. Thank you to anyone willing to help.

 

The item class:

public class ItemGrenades extends Item 
{
public static String[] name = {"alien", "flash", "frag", "gas", "needle"};
public int metadata;

@SideOnly(Side.CLIENT)
public IIcon[] icons;

public ItemGrenades()
{	
	setCreativeTab(XcomMain.XcomTab);
	setHasSubtypes(true);
	setUnlocalizedName(Reference.MODID + "_" + "grenade" + "_" + name);
}

@SideOnly(Side.CLIENT)
@Override
public void registerIcons(IIconRegister iconregister)
{
	icons = new IIcon[name.length];

	for(int i = 0; i < icons.length; i++)
	{
		icons[i] = iconregister.registerIcon(Reference.MODID + ":" + "grenade" +  "_" + name[i]);
	}
}

@SideOnly(Side.CLIENT)
@Override
public IIcon getIconFromDamage(int par1)
{
	return icons[par1];
}	

@Override
public String getUnlocalizedName(ItemStack itemstack)
{
	int metadata = MathHelper.clamp_int(itemstack.getItemDamage(), 0, 15);

	return super.getUnlocalizedName() + "." + name[metadata];
}

@SuppressWarnings({"unchecked", "rawtypes"})
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(Item item, CreativeTabs creativeTab, List list)
{
	for(int i = 0; i < name.length; i++)
	{
		list.add(new ItemStack(this, 5 , i));
	}
}

@Override
public ItemStack onItemRightClick(ItemStack stackL, World world, EntityPlayer entityPlayer)
{
	int l = stackL.getItemDamage();
	metadata = l;

	if(world.isRemote == false)
	{
		world.spawnEntityInWorld(new XcomEntityGrenade(world, entityPlayer, l));
	}

	//Debug
	System.out.println(l);
	System.out.println(metadata);

	return stackL;
}
}

 

The entity class (not done yet)

 

public class XcomEntityGrenade extends EntityThrowable
{		
int x, y, z;
Block block;
int ticksInGround;
int ticksInAir;
int metadata;

public XcomEntityGrenade(World world) 
{
	super(world);
}

public XcomEntityGrenade(World world, EntityLivingBase entityLivingBase, int metadataL)
{
	super(world, entityLivingBase);
	this.metadata = metadataL;
}

public XcomEntityGrenade(World world, double posX, double posY, double posZ)
{
	super(world, posX, posY, posZ);
}

@Override
protected void onImpact(MovingObjectPosition moveObjPos) 
{
	x = moveObjPos.blockX;
	y = moveObjPos.blockY;
	z = moveObjPos.blockZ;
}

}

 

The Render class

 

public class XcomEntityGrenade extends EntityThrowable
{		
int x, y, z;
Block block;
int ticksInGround;
int ticksInAir;
int metadata;

public XcomEntityGrenade(World world) 
{
	super(world);
}

public XcomEntityGrenade(World world, EntityLivingBase entityLivingBase, int metadataL)
{
	super(world, entityLivingBase);
	this.metadata = metadataL;
}

public XcomEntityGrenade(World world, double posX, double posY, double posZ)
{
	super(world, posX, posY, posZ);
}

@Override
protected void onImpact(MovingObjectPosition moveObjPos) 
{
	x = moveObjPos.blockX;
	y = moveObjPos.blockY;
	z = moveObjPos.blockZ;
}

}

 

My Client Proxy

 

public class ClientProxy extends CommonProxy
{
public static ISimpleBlockRenderingHandler meld_canister_renderer;
public static ISimpleBlockRenderingHandler alien_computer_renderer;	

@Override
public void registerBlockRendering()
{
	meld_canister_renderer = new RenderBlockMeldCanister();
	RenderingRegistry.registerBlockHandler(RenderBlockMeldCanister.meld_canister_renderid, meld_canister_renderer);
	alien_computer_renderer = new RenderBlockAlienComputer();
	RenderingRegistry.registerBlockHandler(RenderBlockAlienComputer.xenocomputer_renderID, alien_computer_renderer);

}

@Override
public void registerEntityRendering()
{
	RenderingRegistry.registerEntityRenderingHandler(EntitySectoid.class, new RenderSectoid(new ModelSectoid(), 0.3F));
}

@Override
public void registerItemsRendering()
{
	RenderingRegistry.registerEntityRenderingHandler(XcomEntityGrenade.class, new RenderGrenade());
}
}

Link to comment
Share on other sites

First of all, remove that "metadata" field in you Item class. There is only ever one instance of your Item, meaning that value would be shared across all of your items, which you really do not want, it defeats the purpose of metadata.

 

Your issue is most likely (although you did not post your render class) that the clientside entity does not know about the metadata (fields in entities are not synced by magic). You need to implement IEntityAdditionalSpawnData on your entity and send any additional data that is needed on the client that way.

Link to comment
Share on other sites

Sorry to bother people again, but I was having some of the same troubles with the IEntityAdditionalSpawnData in terms of the value having 0 no matter what. If this is a java question I won't push you on it but I was wondering how I would get the classes to sync. Also, is there anything else I'm doing wrong here? Here is the render class (embarrassed that happened) and the new entity class.

 

 

Entity class:

package com.littlepup.xcom_mod.entities;

import io.netty.buffer.ByteBuf;

import java.util.Iterator;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

import com.littlepup.xcom_mod.items.ItemGrenades;
import com.littlepup.xcom_mod.items.ItemsMain;
import com.littlepup.xcom_mod.other.Utilities;

import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;

public class XcomEntityGrenade extends EntityThrowable implements IEntityAdditionalSpawnData
{		
int x, y, z;
Block block;
int ticksInGround;
int ticksInAir;
int metadata;

public XcomEntityGrenade(World world) 
{
	super(world);
}

public XcomEntityGrenade(World world, EntityLivingBase entityLivingBase, int metadataL)
{
	super(world, entityLivingBase);
	this.metadata = metadataL;
}

public XcomEntityGrenade(World world, double posX, double posY, double posZ)
{
	super(world, posX, posY, posZ);
}

@Override
protected void onImpact(MovingObjectPosition moveObjPos) 
{
	x = moveObjPos.blockX;
	y = moveObjPos.blockY;
	z = moveObjPos.blockZ;
}



@Override
public void writeSpawnData(ByteBuf addedData) 
{
	addedData.capacity(addedData.capacity() + 10);
	addedData.writerIndex(addedData.capacity() - 10);
	addedData.writeInt(this.metadata);
	addedData.writerIndex(addedData.writerIndex() - 1);
	System.out.println("entity written data " + metadata);
}

@Override
public void readSpawnData(ByteBuf addedData) 
{
	addedData.readerIndex(addedData.capacity() - 10);
	int l = addedData.getInt(addedData.readerIndex());
	System.out.println("entity written data " + l);
}

}

 

Render class (basically the same as render snowball)

 

package com.littlepup.xcom_mod.renderers;

import io.netty.buffer.ByteBuf;
import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.Entity;
import net.minecraft.entity.projectile.EntityPotion;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionHelper;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;

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

import com.littlepup.xcom_mod.items.ItemGrenades;
import com.littlepup.xcom_mod.items.ItemsMain;
import com.littlepup.xcom_mod.other.Utilities;

@SideOnly(Side.CLIENT)
public class RenderGrenade extends Render
{
    private int metadata;
    Item grenades = ItemsMain.grenades;
    public RenderGrenade()
    {
    	
    }
    
    public void doRender(Entity entity, double d1, double d2, double d3, float f, float f1)
    {	
        IIcon iicon = grenades.getIconFromDamage(metadata);
        
        System.out.println("metadata from render class" + metadata);
        		
        if (iicon != null)
        {
            GL11.glPushMatrix();
            GL11.glTranslatef((float)d1, (float)d2, (float)d3);
            GL11.glEnable(GL12.GL_RESCALE_NORMAL);
            GL11.glScalef(0.5F, 0.5F, 0.5F);
            this.bindEntityTexture(entity);
            Tessellator tessellator = Tessellator.instance;    
            this.func_77026_a(tessellator, iicon);
            GL11.glDisable(GL12.GL_RESCALE_NORMAL);
            GL11.glPopMatrix();
        }
    }

    /**
     * Returns the location of an entity's texture. Doesn't seem to be called unless you call Render.bindEntityTexture.
     */
    protected ResourceLocation getEntityTexture(Entity entity)
    {
        return TextureMap.locationItemsTexture;
    }

    private void func_77026_a(Tessellator p_77026_1_, IIcon p_77026_2_)
    {
        float f = p_77026_2_.getMinU();
        float f1 = p_77026_2_.getMaxU();
        float f2 = p_77026_2_.getMinV();
        float f3 = p_77026_2_.getMaxV();
        float f4 = 1.0F;
        float f5 = 0.5F;
        float f6 = 0.25F;
        GL11.glRotatef(180.0F - this.renderManager.playerViewY, 0.0F, 1.0F, 0.0F);
        GL11.glRotatef(-this.renderManager.playerViewX, 1.0F, 0.0F, 0.0F);
        p_77026_1_.startDrawingQuads();
        p_77026_1_.setNormal(0.0F, 1.0F, 0.0F);
        p_77026_1_.addVertexWithUV((double)(0.0F - f5), (double)(0.0F - f6), 0.0D, (double)f, (double)f3);
        p_77026_1_.addVertexWithUV((double)(f4 - f5), (double)(0.0F - f6), 0.0D, (double)f1, (double)f3);
        p_77026_1_.addVertexWithUV((double)(f4 - f5), (double)(f4 - f6), 0.0D, (double)f1, (double)f2);
        p_77026_1_.addVertexWithUV((double)(0.0F - f5), (double)(f4 - f6), 0.0D, (double)f, (double)f2);
        p_77026_1_.draw();
    }
}

Link to comment
Share on other sites

Well first of all, you need to make sure that you actually set the data BEFORE you spawn the entity. I notice only one of your constructors sets the metadata value - is that the one you are using when creating your entity?

 

Second, why are you messing around with the ByteBuf capacity and current index? Don't do that - just write what you need, and read back what you need in the same order.

public void writeSpawnData(ByteBuf buffer) {
   buffer.writeInt(this.metadata);
}

public void readSpawnData(ByteBuf buffer) {
   this.metadata = buffer.readInt();
}

That's it. You should never mess with the writer or reader index unless you really know what you're doing.

Link to comment
Share on other sites

Thank you for the help. Although I'm still confused on how to get the metadata value from the entity class to the render class I am using. If this is just a java question I won't push anyone, but I really am stumped on how to get that value into the render class.

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.