Jump to content

Recommended Posts

Posted

Hello, I am trying to code a horse. However, when the horse is ridden this happens;

tRvaO.png

and I end up in the horse, how can I change where the player sits on the entity?

 

Code:

 

 

EntityHorse:

package uk.co.toomuchminecraft.battlefoxcox;

import net.minecraft.src.AchievementList;
import net.minecraft.src.EntityAIFollowParent;
import net.minecraft.src.EntityAILookIdle;
import net.minecraft.src.EntityAIMate;
import net.minecraft.src.EntityAIPanic;
import net.minecraft.src.EntityAISwimming;
import net.minecraft.src.EntityAITempt;
import net.minecraft.src.EntityAIWander;
import net.minecraft.src.EntityAIWatchClosest;
import net.minecraft.src.EntityAnimal;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.World;

public class EntityHorse extends EntityAnimal
{
public String ownerName = "";

    public EntityHorse(World par1World)
    {
        super(par1World);
        this.texture = "/uk/co/toomuchminecraft/battlefoxcox/textures/entities/horse.png";
        this.setSize(5F, 0.9F);
        this.getNavigator().setAvoidsWater(true);
        float var2 = 0.25F;
        this.tasks.addTask(0, new EntityAISwimming(this));
        this.tasks.addTask(1, new EntityAIPanic(this, 0.38F));
        this.tasks.addTask(2, new EntityAIMate(this, var2));
        this.tasks.addTask(3, new EntityAITempt(this, 0.25F, Item.wheat.shiftedIndex, false));
        this.tasks.addTask(4, new EntityAIFollowParent(this, 0.28F));
        this.tasks.addTask(5, new EntityAIWander(this, var2));
        this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
    }

    /**
     * Returns true if the newer Entity AI code should be run
     */
    public boolean isAIEnabled()
    {
        return true;
    }

    public int getMaxHealth()
    {
        return 20;
    }
    
    public void setOwner(String playerName)
    {
    	if(playerName != null)
    	{
    		ownerName = playerName;
    	}
    }
    
    public String getOwner()
    {
    	return this.ownerName;
    }

    protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(16, Byte.valueOf((byte)0));
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.writeEntityToNBT(par1NBTTagCompound);
        par1NBTTagCompound.setString("OwnerName", this.ownerName);
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.readEntityFromNBT(par1NBTTagCompound);
        this.ownerName = (par1NBTTagCompound.getString("OwnerName"));
    }

    /**
     * Returns the sound this mob makes while it's alive.
     */
    protected String getLivingSound()
    {
        return null;
    }

    /**
     * Returns the sound this mob makes when it is hurt.
     */
    protected String getHurtSound()
    {
        return null;
    }

    /**
     * Returns the sound this mob makes on death.
     */
    protected String getDeathSound()
    {
        return null;
    }

    /**
     * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a horse.
     */
    public boolean interact(EntityPlayer par1EntityPlayer)
    {
    	ItemStack itemstack = par1EntityPlayer.inventory.getCurrentItem();
    	
        if (super.interact(par1EntityPlayer))
        {
            return true;
        }
        else if (!this.worldObj.isRemote && par1EntityPlayer.getEntityName().equalsIgnoreCase(ownerName))
        {
            par1EntityPlayer.mountEntity(this);
            return true;
        }
        else
        {
        	this.setBeenAttacked();
            return true;
        }
    }

    /**
     * Returns the item ID for the item the mob drops on death.
     */
    protected int getDropItemId()
    {
        return Item.leather.shiftedIndex;
    }

    /**
     * Drop 0-2 items of this living's type
     */
    protected void dropFewItems(boolean par1, int par2)
    {
        int var3 = this.rand.nextInt(3) + 1 + this.rand.nextInt(1 + par2);

        for (int var4 = 0; var4 < var3; ++var4)
        {
            if (this.isBurning())
            {
                this.dropItem(Item.porkCooked.shiftedIndex, 1);
            }
            else
            {
                this.dropItem(Item.porkRaw.shiftedIndex, 1);
            }
        }
    }

    /**
     * Returns true if the horse is saddled.
     */
    public boolean getSaddled()
    {
        return true;
    }

    /**
     * This function is used when two same-species animals in 'love mode' breed to generate the new baby animal.
     */
    public EntityAnimal spawnBabyAnimal(EntityAnimal par1EntityAnimal)
    {
        return null;
    }
}

ModelHorse:

package uk.co.toomuchminecraft.battlefoxcox.client;

import net.minecraft.src.Entity;
import net.minecraft.src.MathHelper;
import net.minecraft.src.ModelBase;
import net.minecraft.src.ModelQuadruped;
import net.minecraft.src.ModelRenderer;

public class ModelHorse extends ModelBase
{
  //fields
    ModelRenderer tail;
    ModelRenderer ear1;
    ModelRenderer neck;
    ModelRenderer head;
    ModelRenderer body;
    ModelRenderer leg1;
    ModelRenderer leg2;
    ModelRenderer leg3;
    ModelRenderer leg4;
    ModelRenderer ear2;
  
  public ModelHorse()
  {
    textureWidth = 128;
    textureHeight = 64;
    
    //tail
      tail = new ModelRenderer(this, 42, 15);
      tail.addBox(-1F, -1F, -1F, 2, 8, 2);
      tail.setRotationPoint(0F, 4F, 10F);
      tail.setTextureSize(128, 64);
      tail.mirror = false;
      setRotation(tail, 0.4461433F, 0F, 0F);
      
    //ear 1
      ear1 = new ModelRenderer(this, 10, 17);
      ear1.addBox(-5F, -16F, -5F, 2, 3, 1);
      ear1.setRotationPoint(0F, 4F, -8F);
      ear1.setTextureSize(128, 64);
      ear1.mirror = false;
      setRotation(ear1, -0.1047198F, -0.5934119F, 0.2268928F);
      
    //neck
      neck = new ModelRenderer(this, 22, 0);
      neck.addBox(-2F, -11F, 0F, 4, 11, 4);
      neck.setRotationPoint(0F, 4F, -8F);
      neck.setTextureSize(128, 64);
      neck.mirror = false;
      setRotation(neck, 0.4461433F, 0F, 0F);
    
    //head
      head = new ModelRenderer(this, 0, 0);
      head.addBox(-2.5F, -9F, -13F, 5, 11, 6);
      head.setRotationPoint(0F, 4F, -8F);
      head.setTextureSize(128, 64);
      head.mirror = false;
      setRotation(head, -0.669215F, 0F, 0F);
      
    //body
      body = new ModelRenderer(this, 14, 15);
      body.addBox(-4.5F, -10F, -7F, 9, 18, 10);
      body.setRotationPoint(0F, 5F, 2F);
      body.setTextureSize(128, 64);
      body.mirror = false;
      setRotation(body, 1.570796F, 0F, 0F);
      
    //leg1
      leg1 = new ModelRenderer(this, 0, 17);
      leg1.addBox(-3F, 0F, -2F, 3, 12, 4);
      leg1.setRotationPoint(-2F, 12F, 7F);
      leg1.setTextureSize(128, 64);
      leg1.mirror = false;
      setRotation(leg1, 0F, 0F, 0F);
      
    //leg2
      leg2 = new ModelRenderer(this, 0, 17);
      leg2.addBox(-1F, 0F, -2F, 3, 12, 4);
      leg2.setRotationPoint(3F, 12F, 7F);
      leg2.setTextureSize(128, 64);
      leg2.mirror = false;
      setRotation(leg2, 0F, 0F, 0F);
      
    //leg3
      leg3 = new ModelRenderer(this, 0, 17);
      leg3.addBox(-2F, 0F, -3F, 3, 12, 4);
      leg3.setRotationPoint(-3F, 12F, -5F);
      leg3.setTextureSize(128, 64);
      leg3.mirror = false;
      setRotation(leg3, 0F, 0F, 0F);
      
    //leg4
      leg4 = new ModelRenderer(this, 0, 17);
      leg4.addBox(-1F, 0F, -3F, 3, 12, 4);
      leg4.setRotationPoint(3F, 12F, -5F);
      leg4.setTextureSize(128, 64);
      leg4.mirror = false;
      setRotation(leg4, 0F, 0F, 0F);
      
    //ear2
      ear2 = new ModelRenderer(this, 10, 17);
      ear2.addBox(3F, -16F, -5F, 2, 3, 1);
      ear2.setRotationPoint(0F, 4F, -8F);
      ear2.setTextureSize(128, 64);
      ear2.mirror = true;
      setRotation(ear2, -0.1047198F, 0.5934119F, -0.2268928F);
  }
  
  public void render(Entity entity, float f, float f1, float f2, float f3, float f4, float f5)
  {
    super.render(entity, f, f1, f2, f3, f4, f5);
    setRotationAngles(f, f1, f2, f3, f4, f5);
    tail.render(f5);
    ear1.render(f5);
    neck.render(f5);
    head.render(f5);
    body.render(f5);
    leg1.render(f5);
    leg2.render(f5);
    leg3.render(f5);
    leg4.render(f5);
    ear2.render(f5);
  }
  
  private void setRotation(ModelRenderer model, float x, float y, float z)
  {
    model.rotateAngleX = x;
    model.rotateAngleY = y;
    model.rotateAngleZ = z;
  }
  
  /**
   * Sets the models various rotation angles.
   */
  public void setRotationAngles(float par1, float par2, float par3, float par4, float par5, float par6)
  {
      
      this.body.rotateAngleX = ((float)Math.PI / 2F);
      this.leg1.rotateAngleX = MathHelper.cos(par1 * 0.6662F) * 1.4F * par2;
      this.leg2.rotateAngleX = MathHelper.cos(par1 * 0.6662F + (float)Math.PI) * 1.4F * par2;
      this.leg3.rotateAngleX = MathHelper.cos(par1 * 0.6662F + (float)Math.PI) * 1.4F * par2;
      this.leg4.rotateAngleX = MathHelper.cos(par1 * 0.6662F) * 1.4F * par2;
  }

}

RenderHorse:

package uk.co.toomuchminecraft.battlefoxcox.client;

import org.lwjgl.opengl.GL11;

import uk.co.toomuchminecraft.battlefoxcox.EntityHorse;
import net.minecraft.client.Minecraft;
import net.minecraft.src.Entity;
import net.minecraft.src.EntityLiving;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.FontRenderer;
import net.minecraft.src.ModelBase;
import net.minecraft.src.RenderLiving;
import net.minecraft.src.Tessellator;

public class RenderHorse extends RenderLiving
{

    public RenderHorse(ModelBase modelbase, float f)
    {
        super(modelbase, f);
    }
    
    protected void renderName(EntityLiving par1EntityLiving, double par2, double par4, double par6)
    {
        if (Minecraft.isGuiEnabled())
        {
            float var8 = 1.6F;
            float var9 = 0.016666668F * var8;
            double var10 = par1EntityLiving.getDistanceSqToEntity(this.renderManager.livingPlayer);
            float var12 = par1EntityLiving.isSneaking() ? 32.0F : 64.0F;

            if (var10 < (double)(var12 * var12))
            {
            	if(par1EntityLiving instanceof EntityHorse)
            	{
            		EntityHorse horse = (EntityHorse)par1EntityLiving;
                    this.renderLivingLabel(par1EntityLiving, horse.getOwner(), par2, par4, par6, 64);
            	}
            }
        }
    }

    /**
     * Passes the specialRender and renders it
     */
    protected void passSpecialRender(EntityLiving par1EntityLiving, double par2, double par4, double par6)
    {
        this.renderName(par1EntityLiving, par2, par4, par6);
    }
    
    public void func_177_a(EntityHorse entityRandom, double d, double d1, double d2, 
            float f, float f1)
    {
        super.doRenderLiving(entityRandom, d, d1, d2, f, f1);
    }

    public void doRenderLiving(EntityLiving entityliving, double d, double d1, double d2, 
            float f, float f1)
    {
        func_177_a((EntityHorse)entityliving, d, d1, d2, f, f1);
    }

    public void doRender(Entity entity, double d, double d1, double d2, 
            float f, float f1)
    {
        func_177_a((EntityHorse)entity, d, d1, d2, f, f1);
    }
}

 

 

Posted

Put this method into your entity class. Play with the return value until it fits:

 

public double getMountedYOffset() {
return 1.1D;
}

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

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.