Jump to content

Recommended Posts

Posted

I'm trying to spawn entities with the same model, but different textures (just like boats), but what is used to dictate the texture must get nulled out as it transfers from item to render. Here's my classes, in order of progression:

First, the items are initialized here. This works fine, and the items appear as intended.

Spoiler

public class RaceCartItems {

	public static Item[] items = new Item[] {
			new ItemRaceCart("race_cart", EntityRaceCart.Driver.NONE),
			new ItemRaceCart("onesto_cart", EntityRaceCart.Driver.DRIVER62),
			new ItemRaceCart("cittadino_cart", EntityRaceCart.Driver.DRIVER72)
	};
	
	public static void registerItems(final IForgeRegistry<Item> registry)
	{
		for(Item i : items)
		{
			i.setCreativeTab(CreativeTabs.MISC);
			registry.register(i);
			ModelLoader.setCustomModelResourceLocation(i, 0, new ModelResourceLocation(i.getUnlocalizedName().substring(5)));
		}
	}
}

 

 

Then, the onRightClick method in the Item class should assign the item's type to the entity. (Lower half of method)

Spoiler

package mourningstar.mourningstarmod.items.racecart;

import java.util.List;

import mourningstar.mourningstarmod.ModLogger;
import mourningstar.mourningstarmod.entity.EntityRaceCart;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatList;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

public class ItemRaceCart extends Item {

    private final EntityRaceCart.Driver type;
    
	public ItemRaceCart(String rName, EntityRaceCart.Driver driver)
	{
		type = driver;
		setRegistryName("mourningstar", rName);
		setUnlocalizedName(getRegistryName().toString());
	}

	public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
    {
        ItemStack itemstack = playerIn.getHeldItem(handIn);
        float f = 1.0F;
        float f1 = playerIn.prevRotationPitch + (playerIn.rotationPitch - playerIn.prevRotationPitch) * 1.0F;
        float f2 = playerIn.prevRotationYaw + (playerIn.rotationYaw - playerIn.prevRotationYaw) * 1.0F;
        double d0 = playerIn.prevPosX + (playerIn.posX - playerIn.prevPosX) * 1.0D;
        double d1 = playerIn.prevPosY + (playerIn.posY - playerIn.prevPosY) * 1.0D + (double)playerIn.getEyeHeight();
        double d2 = playerIn.prevPosZ + (playerIn.posZ - playerIn.prevPosZ) * 1.0D;
        Vec3d vec3d = new Vec3d(d0, d1, d2);
        float f3 = MathHelper.cos(-f2 * 0.017453292F - (float)Math.PI);
        float f4 = MathHelper.sin(-f2 * 0.017453292F - (float)Math.PI);
        float f5 = -MathHelper.cos(-f1 * 0.017453292F);
        float f6 = MathHelper.sin(-f1 * 0.017453292F);
        float f7 = f4 * f5;
        float f8 = f3 * f5;
        double d3 = 5.0D;
        Vec3d vec3d1 = vec3d.addVector((double)f7 * 5.0D, (double)f6 * 5.0D, (double)f8 * 5.0D);
        RayTraceResult raytraceresult = worldIn.rayTraceBlocks(vec3d, vec3d1, true);

        if (raytraceresult == null) return new ActionResult<ItemStack>(EnumActionResult.PASS, itemstack);
        
        else
        {
            Vec3d vec3d2 = playerIn.getLook(1.0F);
            boolean flag = false;
            List<Entity> list = worldIn.getEntitiesWithinAABBExcludingEntity(playerIn, playerIn.getEntityBoundingBox().expand(vec3d2.x * 5.0D, vec3d2.y * 5.0D, vec3d2.z * 5.0D).grow(1.0D));

            for (int i = 0; i < list.size(); ++i)
            {
                Entity entity = list.get(i);

                if (entity.canBeCollidedWith())
                {
                    AxisAlignedBB axisalignedbb = entity.getEntityBoundingBox().grow((double)entity.getCollisionBorderSize());

                    if (axisalignedbb.contains(vec3d)) flag = true;
                }
            }

            if (flag) return new ActionResult<ItemStack>(EnumActionResult.PASS, itemstack);
            
            else if (raytraceresult.typeOfHit != RayTraceResult.Type.BLOCK)
                return new ActionResult<ItemStack>(EnumActionResult.PASS, itemstack);
            
            else
            {
                Block block = worldIn.getBlockState(raytraceresult.getBlockPos()).getBlock();
                boolean flag1 = block == Blocks.WATER || block == Blocks.FLOWING_WATER;
                EntityRaceCart entityRaceCart = new EntityRaceCart(worldIn, raytraceresult.hitVec.x, flag1 ? raytraceresult.hitVec.y - 0.12D : raytraceresult.hitVec.y, raytraceresult.hitVec.z);
                
                // Set driver to entity
                entityRaceCart.setDriver(type);
                entityRaceCart.rotationYaw = playerIn.rotationYaw;

                if (!worldIn.getCollisionBoxes(entityRaceCart, entityRaceCart.getEntityBoundingBox().grow(-0.1D)).isEmpty())
                    return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
                else
                {
                    if (!worldIn.isRemote) worldIn.spawnEntity(entityRaceCart);
                    if (!playerIn.capabilities.isCreativeMode) itemstack.shrink(1);
                    return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
                }
            }
        }
    }
}

 

 

The two getters and setters for the driver enum in the Entity class:
 

Spoiler

    public void setDriver(EntityRaceCart.Driver d) { driver = d; }
    public EntityRaceCart.Driver getDriver() { return driver; }

 

And the Render class *should* pull the driver from the entity passed in, using getEntityTexture(), but I get a Null exception instead.

Spoiler

package mourningstar.mourningstarmod.client.render;

import mourningstar.mourningstarmod.ModLogger;
import mourningstar.mourningstarmod.MourningstarMod;
import mourningstar.mourningstarmod.client.model.ModelRaceCart;
import mourningstar.mourningstarmod.entity.EntityRaceCart;
import net.minecraft.client.model.IMultipassModel;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityBoat;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.client.registry.IRenderFactory;

public class RenderRaceCart extends Render<EntityRaceCart> {

	private static ResourceLocation[] SCHEMES = new ResourceLocation[] {
			new ResourceLocation(MourningstarMod.MOD_ID, "textures/entity/driverdefault.png"),
			new ResourceLocation(MourningstarMod.MOD_ID, "textures/entity/driver62.png"),
			new ResourceLocation(MourningstarMod.MOD_ID, "textures/entity/driver72.png")
	};
	
	private static ResourceLocation TEXTURE = new ResourceLocation(MourningstarMod.MOD_ID, "textures/entity/driverdefault.png"); 
	
	protected ModelBase modelRaceCart = new ModelRaceCart();
	
	public RenderRaceCart(RenderManager renderManager) {
		super(renderManager);
	}

	
	@Override protected ResourceLocation getEntityTexture(EntityRaceCart entity) {
  		// Causes NullPointerException
		return SCHEMES[entity.getDriver().ordinal()];
	}

	@Override
    public void doRender(EntityRaceCart entity, double x, double y, double z, float entityYaw, float partialTicks)
    {
		GlStateManager.pushMatrix();
        this.setupTranslation(x, y, z);
        this.setupRotation(entity, entityYaw, partialTicks);
        this.bindEntityTexture(entity);

        if (this.renderOutlines)
        {
            GlStateManager.enableColorMaterial();
            GlStateManager.enableOutlineMode(this.getTeamColor(entity));
        }
        
        this.modelRaceCart.render(entity, partialTicks, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);

        if (this.renderOutlines)
        {
            GlStateManager.disableOutlineMode();
            GlStateManager.disableColorMaterial();
        }

        GlStateManager.popMatrix();
        super.doRender(entity, x, y, z, entityYaw, partialTicks);
    }

    public void setupRotation(EntityRaceCart entity, float p_188311_2_, float p_188311_3_)
    {
        GlStateManager.rotate(180.0F - p_188311_2_, 0.0F, 1.0F, 0.0F);
        GlStateManager.scale(-1.0F, -1.0F, 1.0F);
    }

    public void setupTranslation(double x, double y, double z)
    {
        GlStateManager.translate((float)x, (float)y+ 0.375F, (float)z);
    }
}

 

 

Here's the stack trace of the error

Spoiler

net.minecraft.util.ReportedException: Rendering entity in world
	at net.minecraft.client.renderer.entity.RenderManager.doRenderEntity(RenderManager.java:432) ~[RenderManager.class:?]
	at net.minecraft.client.renderer.entity.RenderManager.renderEntityStatic(RenderManager.java:374) ~[RenderManager.class:?]
	at net.minecraft.client.renderer.RenderGlobal.renderEntities(RenderGlobal.java:654) ~[RenderGlobal.class:?]
	at net.minecraft.client.renderer.EntityRenderer.renderWorldPass(EntityRenderer.java:1389) ~[EntityRenderer.class:?]
	at net.minecraft.client.renderer.EntityRenderer.renderWorld(EntityRenderer.java:1303) ~[EntityRenderer.class:?]
	at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1106) ~[EntityRenderer.class:?]
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1192) ~[Minecraft.class:?]
	at net.minecraft.client.Minecraft.run(Minecraft.java:436) [Minecraft.class:?]
	at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
	at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
	at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
	at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97) [start/:?]
	at GradleStart.main(GradleStart.java:26) [start/:?]
Caused by: java.lang.NullPointerException
	at mourningstar.mourningstarmod.client.render.RenderRaceCart.getEntityTexture(RenderRaceCart.java:36) ~[RenderRaceCart.class:?] // getEntityTexture()
	at mourningstar.mourningstarmod.client.render.RenderRaceCart.getEntityTexture(RenderRaceCart.java:1) ~[RenderRaceCart.class:?] // package declaration
	at net.minecraft.client.renderer.entity.Render.bindEntityTexture(Render.java:115) ~[Render.class:?]
	at mourningstar.mourningstarmod.client.render.RenderRaceCart.doRender(RenderRaceCart.java:45) ~[RenderRaceCart.class:?]
	at mourningstar.mourningstarmod.client.render.RenderRaceCart.doRender(RenderRaceCart.java:1) ~[RenderRaceCart.class:?]
	at net.minecraft.client.renderer.entity.RenderManager.doRenderEntity(RenderManager.java:390) ~[RenderManager.class:?]

 

 

I'm pretty sure I built this exactly how the boat object handles this, but I also did this at 4 in the morning and may have missed something simple.

Posted (edited)
9 minutes ago, jonesto95 said:

entity.getDriver()

Obviously this is null. And interestingly enough you have posted every file you could but the most important one - your entity class. 

 

9 minutes ago, jonesto95 said:

public void setDriver(EntityRaceCart.Driver d) { driver = d; } public EntityRaceCart.Driver getDriver() { return driver; }

The client is not aware of the parameter change if you do it like this. If you want the client to be aware of some kind of data changes you need to use EntityDataManager and DataParameters. Kinda how boats do that with the BOAT_TYPE parameter.

 

As a side note:

9 minutes ago, jonesto95 said:

public static void registerItems(final IForgeRegistry<Item> registry) {

for(Item i : items)

{

i.setCreativeTab(CreativeTabs.MISC);

registry.register(i);

ModelLoader.setCustomModelResourceLocation(i, 0, new ModelResourceLocation(i.getUnlocalizedName().substring(5)));

}

}

ModelLoader is client-side only, you must call it from your proxy/client-only class or you will crash the server. 

Models must be registered in the appropriate ModelRegistryEvent.

Unlocalized names have nothing to do with anything but displaying the name of the item, stop using them. Just use Item::getRegistryName().

Edited by V0idWa1k3r
Posted
On 7/8/2017 at 0:28 PM, V0idWa1k3r said:

ModelLoader is client-side only, you must call it from your proxy/client-only class or you will crash the server. 

Models must be registered in the appropriate ModelRegistryEvent

Ok, so how would I go about fixing this? As you said it's causing issues lol. Should I use the @SideOnly(Side.CLIENT) tag for those methods?

Posted
On 7/8/2017 at 9:28 PM, V0idWa1k3r said:

Models must be registered in the appropriate ModelRegistryEvent.

A ModelRegistryEvent is a normal forge event you can subscribe to just like to any other event.

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.