Jump to content

3D Item wont render in Inventory


Thornack

Recommended Posts

I have made a custom 3D ball and wish to render it in my inventory. It is an item that throws like the snowball but in the ItemRenderBall.java class the following switch statement wants to return an entity but when I replace null with (Entity) data[1] I get an out of bounds error.

 

problem code

switch (type) {
	case INVENTORY:
		GL11.glPushMatrix();
		// rotates the item and translates the item
		GL11.glRotatef(0, 0, 0, 1);
		GL11.glRotatef(0, 0, 1, 0);
		GL11.glRotatef(180, 1, 0, 0);
		GL11.glTranslatef(0f, 0f, 0f);
		GL11.glScalef(0.5F, 0.5F, 0.5F);
		Minecraft.getMinecraft().renderEngine.bindTexture(this.itemBallTexture);
		// renders the item
		modelBall.render(null, 0.0f, 0.0f, 0.0f, 0.0f,
				0.0f, 0.0225f);
		GL11.glPopMatrix();
	default:
		break;
	}

 

related mod classes pertaining to this item

 

 

main mod class soccer.java

import soccer.entity.EntityBall;
import soccer.item.CustomItem;
import soccer.item.Ball;
import soccer.render.ItemRenderBall;
import soccer.render.RenderBall;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraftforge.client.IItemRenderer;
import net.minecraftforge.client.MinecraftForgeClient;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.EntityRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

//import cpw.mods.fml.common.registry.EntityRegistry;

@Mod(modid = Soccer.modid, name = "Soccer Mod", version = Soccer.version)
public class Soccer {

public static final String modid = "soccer";
public static final String version = "0.0.1";

// The instance of your mod that Forge uses.
@Instance(modid)
public static Soccer instance;

@SidedProxy(clientSide = "soccer.ClientProxy", serverSide = "soccer.CommonProxy")
public static CommonProxy proxy;

/** declare creative tabs here */
public static CreativeTabs soccerTab;
/** declare 3D Items here */
public static Item Ball;
/** declare Blocks here */
/** declare MobEntities here */

@EventHandler
public void preInit(FMLPreInitializationEvent preEvent) {
	this.instance = this;
	/** initialize and register Tabs here */
	soccerTab = new CreativeTabs("Soccer") {
		@SideOnly(Side.CLIENT)
		public Item getTabIconItem() {
			return Ball /**
			 * returns
			 * creative tab logo
			 */
			;
		}
	};
	/** initialize and register non 3D Items here */
	/** initialize and register 3D Items here */
	Ball = new Ball();
	GameRegistry.registerItem(Ball,
			"soccer" + Ball.setUnlocalizedName("Ball"));
	MinecraftForgeClient.registerItemRenderer(Ball,
			(IItemRenderer) new ItemRenderBall());
	/** initialize and register blocks here */
	/** initialize and register Ores here */
	/** initialize and register TileEntities here */
	/** initialize and register MobEntities here */

}

@EventHandler
public void init(FMLInitializationEvent event) {
	proxy.init();

	int soccerMobEntityID = 0;
	/**soccer Entities */
	EntityRegistry.registerModEntity(EntityBall.class, "Ball", ++soccerMobEntityID, this, 80, 1, true);
       // proxy.registerRenderInformation();
}

@EventHandler
public void postInit(FMLPostInitializationEvent event) {
		}

}

Common Proxy.java

import soccer.entity.*;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;
import cpw.mods.fml.common.registry.EntityRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
public class CommonProxy implements IGuiHandler{

//do not remove. This is overridden in ClientProxy to add rendering data for the models (client side only).
public void registerRenderInformation(){}

public void init(){
	registerEntities();
	registerRenderInformation();
}

//registers all soccer entities in the game
public void registerEntities(){
	//register ball
	EntityRegistry.registerGlobalEntityID(EntityBall.class, "Ball", EntityRegistry.findGlobalUniqueEntityId());
	}
public Object getServerGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z) {
			return null;
}


public Object getClientGuiElement(int ID, EntityPlayer player, World world,
		int x, int y, int z) {
			return null;
}
}

Client Proxy.java

import net.minecraft.client.model.ModelBase;
import net.minecraft.entity.Entity;
import net.minecraftforge.client.IItemRenderer;
import net.minecraftforge.client.MinecraftForgeClient;
import soccer.entity.*;
import soccer.model.*;
import soccer.render.ItemRenderBall;
import soccer.render.RenderBall;
import cpw.mods.fml.client.registry.RenderingRegistry;

public class ClientProxy extends CommonProxy{
public void registerRenderInformation(){
	RenderingRegistry.registerEntityRenderingHandler(EntityBall.class, new RenderBall());
}
	}

EntityBall.java

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

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.EntityBlaze;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class EntityBall extends EntityThrowable {

private static final String __OBFID = "CL_00001722";


    
public EntityBall(World world) {
	super(world);
	this.setSize(0.9F, 0.9F); // hit-box size?

}
public EntityBall(World world, EntityLivingBase par2EntityLivingBase)
    {
        super(world, par2EntityLivingBase);
    }

    public EntityBall(World world, double par2, double par4, double par6)
    {
        super(world, par2, par4, par6);
    }

public EntityBall(World par1World, EntityLiving par2EntityLiving) {
	super(par1World, par2EntityLiving);
}

/**
 * Called when this EntityThrowable hits a block or entity.
 */
@Override
protected void onImpact(MovingObjectPosition movingItemPosition) {
	if (movingItemPosition.entityHit != null)
        {
            float thrownItemDamage = 0;//how much damage the thrown item will inflict

            movingItemPosition.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), thrownItemDamage);
        }

        for (int i = 0; i < 8; ++i)
        {
            this.worldObj.spawnParticle("crit", this.posX, this.posY, this.posZ, 0.0D, 0.0D, 0.0D);
        }
    
        // this if statement is required or it will keep updating forever and you'll end up with lots of leftover entities in the world
        if (!this.worldObj.isRemote) 
        {
            this.setDead();}
		}
protected void entityInit() {
}}

Ball.java

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

import soccer.Soccer;
import soccer.entity.EntityBall;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;

public class Ball extends CustomItem {

public static int itemID;

public Ball() {
	super();
	setUnlocalizedName("Ball");
	setCreativeTab(Soccer.soccerTab);
	setFull3D();

}
@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world,
		EntityPlayer player) {
	if (!player.capabilities.isCreativeMode) {
		--itemStack.stackSize;
	}
	world.playSoundAtEntity(player, "random.bow", 0.5F,
			0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
	if (!world.isRemote) {
		world.spawnEntityInWorld(new EntityBall(world, player));
	}
	return itemStack;
}

/**
 * Called when a player right clicks an entity with an item.
 */
public boolean itemInteractionForEntity(ItemStack ItemStack,
		EntityLiving EntityLiving) {
	return false;
}

/**
 * returns the action that specifies what animation to play when the items
 * is being used
 */
public EnumAction getItemUseAction(ItemStack ItemStack) {
	return EnumAction.none;
}

/**
 * Determines if this Item has a special entity for when they are in the
 * world. Is called when a EntityItem is spawned in the world, if true and
 * Item#createCustomEntity returns non null, the EntityItem will be
 * destroyed and the new Entity will be added to the world.
 * 
 * @param stack
 *            The current item stack
 * @return True of the item has a custom entity, If true,
 *         Item#createCustomEntity will be called
 */
public boolean hasCustomEntity(ItemStack stack) {
	return false;
}

/**
 * This function should return a new entity to replace the dropped item.
 * Returning null here will not kill the EntityItem and will leave it to
 * function normally. Called when the item it placed in a world.
 * 
 * @param world
 *            The world object
 * @param location
 *            The EntityItem object, useful for getting the position of the
 *            entity
 * @param itemstack
 *            The current item stack
 * @return A new Entity object to spawn or null
 */
public Entity createEntity(World world, Entity location, ItemStack itemstack) {
	return null;
}
}

CustomItem.java

import soccer.Soccer;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;

public class CustomItem extends Item {

public CustomItem() {
	super();

	this.setCreativeTab(getCreativeTab().tabMisc);
	setMaxStackSize(64);

}

public String getUnlocalizedName2() {
	return getUnlocalizedName().substring(4);
}

@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister iconRegister) {
	this.itemIcon = iconRegister.registerIcon(Soccer.modid + ":"
			+ this.getUnlocalizedName().substring(5));
}
}

ItemRenderBall.java ( This class renders the 3D Ball model on the ground and in the hand as a 3D item and currently doesn’t show an icon for the item in the inventory or creative tab )

import org.lwjgl.opengl.GL11;

import soccer.Soccer;
import soccer.model.ItemModelBall;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.IItemRenderer;

public class ItemRenderBall implements IItemRenderer {

protected ItemModelBall modelBall;
private static final ResourceLocation itemBallTexture = new ResourceLocation(
		"soccer:textures/items/ball.png");
public ItemRenderBall() {
	modelBall = new ItemModelBall();
}

@Override
public boolean handleRenderType(ItemStack item, ItemRenderType type) {
	switch (type) {
	case EQUIPPED:
	case EQUIPPED_FIRST_PERSON:
	case ENTITY:
	case INVENTORY:
		return true;
	default:
		return false;
	}
}

@Override
public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item,
		ItemRendererHelper helper) {
	return false;
}
//Renders the Ball in your hand
@Override
public void renderItem(ItemRenderType type, ItemStack item, Object... data) {
	switch (type) {
	case EQUIPPED:
	case EQUIPPED_FIRST_PERSON:
		GL11.glPushMatrix();
		// rotates the item
		GL11.glRotatef(90, 0, 0, 1);
		GL11.glRotatef(90, 0, 1, 0);
		GL11.glRotatef(230, 1, 0, 0);
		GL11.glTranslatef(0, 0.2f, -0.6f);
		GL11.glScalef(1.5F, 1.5F, 1.5F);
		Minecraft.getMinecraft().renderEngine.bindTexture(this.itemBallTexture);
		// renders the item
		modelBall.render((Entity) data[1], 0.0f, 0.0f, 0.0f, 0.0f,
				0.0f, 0.0225f);
		GL11.glPopMatrix();

	default:
		break;
		//Renders the Ball on ground
	}
	switch (type) {
	case ENTITY:

		GL11.glPushMatrix();
		// rotates the item and translates the item
		GL11.glRotatef(0, 0, 0, 1);
		GL11.glRotatef(0, 0, 1, 0);
		GL11.glRotatef(180, 1, 0, 0);
		GL11.glTranslatef(0, 0f, 0f);
		GL11.glScalef(1.5F, 1.5F, 1.5F);
		Minecraft.getMinecraft().renderEngine.bindTexture(this.itemBallTexture);
		// renders the item
		modelBall.render((Entity) data[1], 0.0f, 0.0f, 0.0f, 0.0f,
				0.0f, 0.0225f);
		GL11.glPopMatrix();
	default:
		break;
	}
	switch (type) {
	case INVENTORY:
		GL11.glPushMatrix();
		// rotates the item and translates the item
		GL11.glRotatef(0, 0, 0, 1);
		GL11.glRotatef(0, 0, 1, 0);
		GL11.glRotatef(180, 1, 0, 0);
		GL11.glTranslatef(0, 0f, 0f);
		GL11.glScalef(1.5F, 1.5F, 1.5F);
		Minecraft.getMinecraft().renderEngine.bindTexture(this.itemBallTexture);
		/* renders the item except here inputting null doesn’t crash Minecraft whereas replacing null with (Entity) data[1] does crash Minecraft*/

		modelBall.render(null, 0.0f, 0.0f, 0.0f, 0.0f,
				0.0f, 0.0225f);
		GL11.glPopMatrix();
	default:
		break;
	}
}
 /**
     * Returns the location of an entity's texture. Doesn't seem to be called unless you call Render.bindEntityTexture.
     */
    protected ResourceLocation getEntityTexture(Entity Ball)
    {
        return this.itemBallTexture;
    }
}

RenderBall.java ( This class renders the 3D ball model mid flight upon right click but currently the ball doesn't render properly and only flies in one plane)

import net.minecraft.client.Minecraft;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import org.lwjgl.opengl.GL11;

import soccer.Soccer;
import soccer.model.ItemModelBall;

@SideOnly(Side.CLIENT)
public class RenderBall extends Render
{

private static final ResourceLocation itemBallTexture = new ResourceLocation("soccer:textures/items/ball.png");
   /**
     * instance of ModelBall for rendering
     */
    protected ModelBase modelBall;
    public RenderBall()
    {
        this.shadowSize = 0.5F;
        this.modelBall = new ItemModelBall();
    }

   public void doRender(Entity renderflyingball, double x, double y, double z, float yaw, float partialTickTime)
    {
        GL11.glPushMatrix();
        GL11.glScalef(-0.5F, -0.5F, 0.5F);
        GL11.glTranslated(x,y,z);
        GL11.glRotatef(180.0F - yaw, 0.0F, 1.0F, 0.0F);
        this.bindEntityTexture(renderflyingball);
        this.modelBall.render(renderflyingball, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);
        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 par1Entity)
    {
        return this.itemBallTexture;
    }
}

 

Link to comment
Share on other sites

here is the one that i have do  your renaming and rendirng stuff

 

shooter

package thrizzo.minibots.items;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import thrizzo.minibots.entities.throwable.EntityMiniJBullet;
import thrizzo.minibots.main.MiniBotsMain;

public class ItemBlasterRifle extends Item
{

  public ItemBlasterRifle(int par1)
  {
	 super();

 this.maxStackSize = 1;
  }
  @Override
  public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
  {
  if (par3EntityPlayer.capabilities.isCreativeMode || par3EntityPlayer.inventory.hasItem(MiniBotsMain.MiniRifleAmmo)){
	  
	  if (!par3EntityPlayer.capabilities.isCreativeMode)
      		{
              par3EntityPlayer.inventory.consumeInventoryItem(MiniBotsMain.MiniRifleAmmo);
      		}
      	par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
      	if (!par2World.isRemote)
      		{
    	  	par2World.spawnEntityInWorld(new EntityMiniJBullet(par2World, par3EntityPlayer));
      		}
      	return par1ItemStack;
  }
 	return par1ItemStack;
  }
}

 

 

shooten entity

 


package thrizzo.minibots.entities.throwable;

import java.util.Random;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

public class EntityMiniJBullet extends EntityThrowable
{    
private Random rand ; 
int speed = 2;
    private double damage = 7.5D ;

   public EntityMiniJBullet(World par1World)
   {
       super(par1World);
   }
   
   
   
   public void setDamage(double par1)
   {
       this.damage = par1;
   }

   public double getDamage()
   {
       return this.damage;
   }

   
   public EntityMiniJBullet(World par1World, EntityLivingBase par2EntityLivingBase)
   {
       super(par1World, par2EntityLivingBase);
       this.motionX*=speed;
       this.motionY*=speed;
       this.motionZ*=speed;
   }
   public EntityMiniJBullet(World par1World, double par2, double par4, double par6)
   {
       super(par1World, par2, par4, par6);
   }
   @Override
   protected void onImpact(MovingObjectPosition movingobjectposition) 
   {
 this.worldObj.createExplosion(this, this.posX, this.posY, this.posZ,(float) 0.25, true);		


    
 this.setDead();

 if (movingobjectposition.entityHit != null)
     {
	 movingobjectposition.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), (float) damage);
     }

   }
   @Override
   protected float getGravityVelocity()
   {
       return 0F;
   }

   
}

 

hope it works for you, it does for 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.



×
×
  • Create New...

Important Information

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