Jump to content

Recommended Posts

Posted

So I'm making a new ambient mob, a songbird, and I want it to have different textures/colors upon spawning in.  I have it working, except when I relog they all revert back to the "default" color. I've tried looking in the EntityRabbit file, but I'm not sure what to do. Maybe I have to sync the client side of things with the server side? Idk how to do any of this though. I'm honestly not the best at java coding, so any kind of help would be appreciated.

Entity

Spoiler

package xavier.welsanimals.init.mobs.entities;

import javax.annotation.Nullable;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAvoidEntity;
import net.minecraft.entity.passive.EntityAmbientCreature;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.DamageSource;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.datafix.DataFixer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.storage.loot.LootTableList;

public class EntityBird extends EntityAmbientCreature
{
    private static final DataParameter<Integer> BIRD_TYPE = EntityDataManager.<Integer>createKey(EntityBird.class, DataSerializers.VARINT);
    private EntityAIAvoidEntity<EntityPlayer> avoidEntity;
    
    
    private static final DataParameter<Byte> SITTING = EntityDataManager.<Byte>createKey(EntityBird.class, DataSerializers.BYTE);
    /** Coordinates of where the bird spawned. */
    private BlockPos spawnPosition;

    public EntityBird(World worldIn)
    {
        super(worldIn);
        this.setSize(0.25F, 0.25F);
        this.setIsBirdSitting(true);
    }

    protected void entityInit()
    {
        super.entityInit();
        this.dataManager.register(SITTING, Byte.valueOf((byte)0));
        this.dataManager.register(BIRD_TYPE, Integer.valueOf(0));
    }
    
    
    
    
    
    /** Returns the volume for the sounds this mob makes.
     **/
    protected float getSoundVolume()
    {
        return 0.1F;
    }

    /**
     * Gets the pitch of living sounds in living entities.
     */
    protected float getSoundPitch()
    {
        return super.getSoundPitch() * 0.95F;
    }

    @Nullable
    protected SoundEvent getAmbientSound()
    {
        return this.getIsBirdSitting() && this.rand.nextInt(4) != 0 ? null : SoundEvents.ENTITY_BAT_AMBIENT;
    }

    protected SoundEvent getHurtSound()
    {
        return SoundEvents.ENTITY_BAT_HURT;
    }

    protected SoundEvent getDeathSound()
    {
        return SoundEvents.ENTITY_BAT_DEATH;
    }

    /**
     * Returns true if this entity should push and be pushed by other entities when colliding.
     */
    public boolean canBePushed()
    {
        return false;
    }

    protected void collideWithEntity(Entity entityIn)
    {
    }

    protected void collideWithNearbyEntities()
    {
    }

    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(6.0D);
    }

    public boolean getIsBirdSitting()
    {
        return (((Byte)this.dataManager.get(SITTING)).byteValue() & 1) != 0;
    }

    public void setIsBirdSitting(boolean isSitting)
    {
        byte b0 = ((Byte)this.dataManager.get(SITTING)).byteValue();

        if (isSitting)
        {
            this.dataManager.set(SITTING, Byte.valueOf((byte)(b0 | 1)));
        }
        else
        {
            this.dataManager.set(SITTING, Byte.valueOf((byte)(b0 & -2)));
        }
    }

    /**
     * Called to update the entity's position/logic.
     */
    public void onUpdate()
    {
        super.onUpdate();

        if (this.getIsBirdSitting())
        {
            this.motionX = 0.0D;
            this.motionY = 0.0D;
            this.motionZ = 0.0D;
            this.posY = (double)MathHelper.floor(this.posY) + 1.0D - (double)this.height;
        }
        else
        {
            this.motionY *= 0.6000000238418579D;
        }
    }

    protected void updateAITasks()
    {
        super.updateAITasks();
        BlockPos blockpos = new BlockPos(this);
        BlockPos blockpos1 = blockpos.down();

        if (this.getIsBirdSitting())
        {
            if (this.world.getBlockState(blockpos1).isNormalCube())
            {
                if (this.rand.nextInt(200) == 0)
                {
                    this.rotationYawHead = (float)this.rand.nextInt(360);
                }

                if (this.world.getNearestPlayerNotCreative(this, 5.0D) != null)
                {
                    this.setIsBirdSitting(false);
                    this.world.playEvent((EntityPlayer)null, 1025, blockpos, 0);
                }
            }
            else
            {
                this.setIsBirdSitting(false);
                this.world.playEvent((EntityPlayer)null, 1025, blockpos, 0);
            }
        }
        else
        {
            if (this.spawnPosition != null && (!this.world.isAirBlock(this.spawnPosition) || this.spawnPosition.getY() < 1))
            {
                this.spawnPosition = null;
            }

            if (this.spawnPosition == null || this.rand.nextInt(30) == 0 || this.spawnPosition.distanceSq((double)((int)this.posX), (double)((int)this.posY), (double)((int)this.posZ)) < 4.0D)
            {
                this.spawnPosition = new BlockPos((int)this.posX + this.rand.nextInt(7) - this.rand.nextInt(7), (int)this.posY + this.rand.nextInt(6) - 2, (int)this.posZ + this.rand.nextInt(7) - this.rand.nextInt(7));
            }

            double d0 = (double)this.spawnPosition.getX() + 0.5D - this.posX;
            double d1 = (double)this.spawnPosition.getY() + 0.1D - this.posY;
            double d2 = (double)this.spawnPosition.getZ() + 0.5D - this.posZ;
            this.motionX += (Math.signum(d0) * 0.5D - this.motionX) * 0.10000000149011612D;
            this.motionY += (Math.signum(d1) * 0.699999988079071D - this.motionY) * 0.10000000149011612D;
            this.motionZ += (Math.signum(d2) * 0.5D - this.motionZ) * 0.10000000149011612D;
            float f = (float)(MathHelper.atan2(this.motionZ, this.motionX) * (180D / Math.PI)) - 90.0F;
            float f1 = MathHelper.wrapDegrees(f - this.rotationYaw);
            this.moveForward = 0.5F;
            this.rotationYaw += f1;

            if (this.rand.nextInt(100) == 0 && this.world.getBlockState(blockpos1).isNormalCube())
            {
                this.setIsBirdSitting(true);
            }
        }
    }

    /**
     * returns if this entity triggers Block.onEntityWalking on the blocks they walk on. used for spiders and wolves to
     * prevent them from trampling crops
     */
    protected boolean canTriggerWalking()
    {
        return false;
    }

    public void fall(float distance, float damageMultiplier)
    {
    }

    protected void updateFallState(double y, boolean onGroundIn, IBlockState state, BlockPos pos)
    {
    }
    
    /**
     * Return whether this entity should NOT trigger a pressure plate or a tripwire.
     */
    public boolean doesEntityNotTriggerPressurePlate()
    {
        return true;
    }

    /**
     * Called when the entity is attacked.
     */
    public boolean attackEntityFrom(DamageSource source, float amount)
    {
        if (this.isEntityInvulnerable(source))
        {
            return false;
        }
        else
        {
            if (!this.world.isRemote && this.getIsBirdSitting())
            {
                this.setIsBirdSitting(false);
            }

            return super.attackEntityFrom(source, amount);
        }
    }

    public static void registerFixesBird(DataFixer fixer)
    {
        EntityLiving.registerFixesMob(fixer, EntityBird.class);
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    public void readEntityFromNBT(NBTTagCompound compound)
    {
        super.readEntityFromNBT(compound);
        this.dataManager.set(SITTING, Byte.valueOf(compound.getByte("BirdFlags")));
       
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    public void writeEntityToNBT(NBTTagCompound compound)
    {
        super.writeEntityToNBT(compound);
        compound.setByte("BirdFlags", ((Byte)this.dataManager.get(SITTING)).byteValue());
        compound.setInteger("BirdType", this.getBirdType());
        
    }
    
    public void setBirdType(int birdTypeId)
    {
        this.dataManager.set(BIRD_TYPE, Integer.valueOf(birdTypeId));
    }

    /**
     * Checks if the entity's current position is a valid location to spawn this entity.
     */
    
    @Nullable
    public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata)
    {
        livingdata = super.onInitialSpawn(difficulty, livingdata);
        int i = this.getRandomBirdType();
        boolean flag = false;

        if (livingdata instanceof EntityBird.BirdTypeData)
        {
            i = ((EntityBird.BirdTypeData)livingdata).typeData;
            flag = true;
        }
        else
        {
            livingdata = new EntityBird.BirdTypeData(i);
        }

        this.setBirdType(i);
      
        
        return livingdata;
    }

    private int getRandomBirdType()
    {
        Biome biome = this.world.getBiome(new BlockPos(this));
        int i = this.rand.nextInt(6);
        return (i);
    }
    
    
    public float getEyeHeight()
    {
        return this.height / 0.5F;
    }
    
    public int getBirdType()
    {
        return ((Integer)this.dataManager.get(BIRD_TYPE)).intValue();
    }
    
    public static class BirdTypeData implements IEntityLivingData
    {
        public int typeData;

        public BirdTypeData(int type)
        {
            this.typeData = type;
        }
    }

    @Nullable
    protected ResourceLocation getLootTable()
    {
        return LootTableList.ENTITIES_BAT;
    }
}

 

 

and Render

Spoiler

package xavier.welsanimals.init.mobs.renderers;

import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.entity.RenderLiving;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import xavier.welsanimals.init.mobs.entities.EntityBird;
import xavier.welsanimals.init.mobs.models.ModelBird;
import xavier.welsanimals.main.Reference;

@SideOnly(Side.CLIENT)
public class RenderBird extends RenderLiving<EntityBird>
{    
    public static final ResourceLocation BLUE_BIRD_TEXTURE = new ResourceLocation(Reference.MODID, "textures/entity/bird/bluebird.png");
    public static final ResourceLocation RED_BIRD_TEXTURE = new ResourceLocation(Reference.MODID, "textures/entity/bird/redbird.png");
    public static final ResourceLocation WHITE_BIRD_TEXTURE = new ResourceLocation(Reference.MODID, "textures/entity/bird/whitebird.png");
    public static final ResourceLocation GREEN_BIRD_TEXTURE = new ResourceLocation(Reference.MODID, "textures/entity/bird/greenbird.png");
    public static final ResourceLocation BLACK_BIRD_TEXTURE = new ResourceLocation(Reference.MODID, "textures/entity/bird/blackbird.png");
    public static final ResourceLocation YELLOW_BIRD_TEXTURE = new ResourceLocation(Reference.MODID, "textures/entity/bird/yellowbird.png");

    public RenderBird(RenderManager renderManagerIn)
    {
        super(renderManagerIn, new ModelBird(), 0.25F);
    }

    /**
     * Returns the location of an entity's texture. Doesn't seem to be called unless you call Render.bindEntityTexture.
     */
    protected ResourceLocation getEntityTexture(EntityBird entity)
    {
        switch (entity.getBirdType())
        {
            case 0:
            default:
                return BLUE_BIRD_TEXTURE;
            case 1:
                return RED_BIRD_TEXTURE;
            case 2:
                return WHITE_BIRD_TEXTURE;
            case 3:
                return GREEN_BIRD_TEXTURE;
            case 4:
                return BLACK_BIRD_TEXTURE;
            case 5:
                return YELLOW_BIRD_TEXTURE;
        }
    }

    /**
     * Allows the render to do state modifications necessary before the model is rendered.
     */
    protected void preRenderCallback(EntityBird entitylivingbaseIn, float partialTickTime)
    {
        GlStateManager.scale(0.35F, 0.35F, 0.35F);
    }
}

 

 

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.