Jump to content

[1.9.4] Entity Rendering


DARKHAWX

Recommended Posts

So I recently decided I would finally create an entity. I had almost finished everything including the render but as I spawn it in game it doesn't seem to render right...

 

Its probably got to do with the fact that I'm using the old RenderingRegistry#registerEntityRenderingHandler() method that is deprecated... The entity is supposed to just be effectively a floating block...

 

Here are the relevant files:

 

EntityHeartFamiliar:

 

package com.darkhawx.planck.main.entity;

import com.darkhawx.planck.main.blocks.BlockHeart;
import com.darkhawx.planck.main.helpers.BlockPosHelper;
import com.darkhawx.planck.main.tileEntities.TileHeart;
import com.google.common.base.Optional;
import net.minecraft.block.BlockDoublePlant;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.EntityFlying;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.ai.EntityAIFollowOwner;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
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.server.management.PreYggdrasilConverter;
import net.minecraft.util.*;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.storage.loot.LootTableList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import javax.annotation.Nullable;
import java.util.Random;
import java.util.UUID;

/**
* Created by Bradley Gray on 22/07/16.
*/
public class EntityHeartFamiliar extends Entity {

    protected static final DataParameter<Optional<UUID>> OWNER_UNIQUE_ID = EntityDataManager.createKey(EntityHeartFamiliar.class, DataSerializers.OPTIONAL_UNIQUE_ID);
    private static final DataParameter<Float> DAMAGE = EntityDataManager.createKey(EntityHeartFamiliar.class, DataSerializers.FLOAT);

    private static final float TICK_INCREMENT = 0.05F;
    public static final float MAX_TICKS = 39.0F;
    private static final float MAX_SEARCH_DISTANCE = 10.0F;
    private static final float MIN_DISTANCE = 1.5F;
    private static final float VERTICAL_MOVEMENT = 0.05F;
    private static final Random RAND = new Random();
    private static final double PARTICLE_CHANCE = 0.01D;
    private static final double DROP = 0.0D;

    private float currentTick;
    private boolean healReady;

    public EntityHeartFamiliar(World worldIn, double x, double y, double z, EntityPlayer player) {
        super(worldIn);
        this.setOwnerId(player.getUniqueID());
        this.setPosition(x, y, z);
        this.motionX = 0.0D;
        this.motionY = 0.0D;
        this.motionZ = 0.0D;
        this.healReady = true;
    }

    public EntityHeartFamiliar(World worldIn, BlockPos pos, EntityPlayer player) {
        super(worldIn);
        this.setOwnerId(player.getUniqueID());
        this.setPosition(pos.getX(), pos.getY(), pos.getZ());
        this.motionX = 0.0D;
        this.motionY = 0.0D;
        this.motionZ = 0.0D;
    }

    public EntityHeartFamiliar(World worldIn) {
        super(worldIn);
    }

    public EntityHeartFamiliar(World worldIn, double x, double y, double z) {
        super(worldIn);
        this.setPosition(x, y, z);
        this.motionX = 0.0D;
        this.motionY = 0.0D;
        this.motionZ = 0.0D;
    }

    @Override
    protected void entityInit() {
        this.dataManager.register(OWNER_UNIQUE_ID, Optional.<UUID>absent());
        this.dataManager.register(DAMAGE, 0.0F);
    }

    /**
     * Called when the entity is attacked.
     */
    public boolean attackEntityFrom(DamageSource source, float amount)
    {
        if (!this.worldObj.isRemote && !this.isDead)
        {
            if (this.isEntityInvulnerable(source))
            {
                return false;
            }
            else
            {
                this.setBeenAttacked();
                this.setDamage(this.getDamage() + amount * 10.0F);
                boolean flag = source.getEntity() instanceof EntityPlayer && ((EntityPlayer)source.getEntity()).capabilities.isCreativeMode;

                if (flag || this.getDamage() > 40.0F)
                {
                    this.removePassengers();

                    this.setDead();
                }

                return true;
            }
        }
        else
        {
            return true;
        }
    }

    /**
     * Called to update the entity's position/logic.
     */
    public void onUpdate() {
//        boolean isRightHeight = BlockPosHelper.getLowestAirYWithRange(worldObj, getUnFloatedPos(), 2) == getUnFloatedPosY() - 1.0D;
        boolean isRightHeight = BlockPosHelper.getLowestAirYWithRange(worldObj, getPosition(), 2) == getPosition().getY() - 1.0D;
        EntityPlayer owner = (EntityPlayer)getOwner();

        this.currentTick += TICK_INCREMENT;
        if (this.currentTick > MAX_TICKS) {
            this.currentTick = 0.0F;
        }

        if (owner != null) {
            double distance = getPosition().distanceSq(owner.getPosition());

            if (distance >= MAX_SEARCH_DISTANCE) {
                setPosition(owner.posX + 0.5D, owner.eyeHeight + 1.0D, owner.posZ + 0.5D);
            } else if (distance > MIN_DISTANCE) {
                moveEntity(owner.posX + 0.5D, owner.eyeHeight, owner.posZ + 0.5D);
            }
        }

        //Particles
        if (worldObj.isRemote && RAND.nextDouble() < PARTICLE_CHANCE && isHealReady()) {
            worldObj.spawnParticle(EnumParticleTypes.HEART,
                    getPosition().getX() + rand.nextDouble(),
                    getPosition().getY() + rand.nextDouble(),
                    getPosition().getZ() + rand.nextDouble(),
                    0.0D,
                    DROP,
                    0.0D);
        }

        floatMove(isRightHeight);
    }

    public float getCurrentTick() {
        return this.currentTick;
    }

    public void floatMove(boolean isRightHeight) {
//        double currentPos = getCurrentPos();
//        float currentRot = this.currentTick / TileHeart.MAX_TICKS;
//
//        this.setRotation(currentRot, 0.0F);

        if (!isRightHeight) {
            double rightHeight = BlockPosHelper.getLowestAirYWithRange(worldObj, getPosition(), -1);
            if (getPosition().getY() > rightHeight) {
                this.posY -= VERTICAL_MOVEMENT;
            } else {
                this.posY += VERTICAL_MOVEMENT;
            }
        }

//        this.posY += currentPos;
    }

//    public double getCurrentPos() {
//        return Math.sin(this.currentTick) * 0.125D;
//    }
//
//    public BlockPos getUnFloatedPos() {
//        return this.getPosition().add(0.0D, -getCurrentPos(), 0.0D);
//    }
//
//    public double getUnFloatedPosY() {
//        return (this.getPosition().add(0.0D, -getCurrentPos(), 0.0D)).getY();
//    }

    public boolean isSilent() {
        return true;
    }

    /**
     * Setups the entity to do the hurt animation. Only used by packets in multiplayer.
     */
    @SideOnly(Side.CLIENT)
    public void performHurtAnimation()
    {
        this.setDamage(this.getDamage() + this.getDamage() * 10.0F);
    }

    /**
     * Sets the current amount of damage the minecart has taken. Decreases over time. The cart breaks when this is over
     * 40.
     */
    public void setDamage(float damage)
    {
        this.dataManager.set(DAMAGE, damage);
    }

    /**
     * Gets the current amount of damage the minecart has taken. Decreases over time. The cart breaks when this is over
     * 40.
     */
    public float getDamage()
    {
        return this.dataManager.get(DAMAGE);
    }

    /**
     * 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;
    }

    @Nullable
    public AxisAlignedBB getCollisionBox(Entity entityIn)
    {
        return BlockHeart.AABB;
    }

    @Nullable
    public AxisAlignedBB getCollisionBoundingBox()
    {
        return BlockHeart.AABB;
    }

    public boolean canBePushed()
    {
        return true;
    }

    public boolean isHealReady() {
        return this.healReady;
    }

    @Nullable
    public UUID getOwnerId()
    {
        return (UUID)((Optional)this.dataManager.get(OWNER_UNIQUE_ID)).orNull();
    }

    @Nullable
    public EntityLivingBase getOwner() {
        try
        {
            UUID uuid = this.getOwnerId();
            return uuid == null ? null : this.worldObj.getPlayerEntityByUUID(uuid);
        }
        catch (IllegalArgumentException var2)
        {
            return null;
        }
    }

    public boolean hasOwner() {
        return getOwnerId() != null;
    }

    public void setOwnerId(@Nullable UUID uuid)
    {
        this.dataManager.set(OWNER_UNIQUE_ID, Optional.fromNullable(uuid));
    }

    @Override
    public void readEntityFromNBT(NBTTagCompound compound) {
        String s;

        if (compound.hasKey("OwnerUUID", ) {
            s = compound.getString("OwnerUUID");
        } else {
            String s1 = compound.getString("Owner");
            s = PreYggdrasilConverter.convertMobOwnerIfNeeded(this.getServer(), s1);
        }

        if (!s.isEmpty()) {
            try {
                this.setOwnerId(UUID.fromString(s));
            } catch (Throwable var4) {

            }
        }
    }

    @Override
    public void writeEntityToNBT(NBTTagCompound compound) {
        if (this.getOwnerId() == null)
        {
            compound.setString("OwnerUUID", "");
        }
        else
        {
            compound.setString("OwnerUUID", this.getOwnerId().toString());
        }
    }
}

 

 

RendererHeartFamiliar:

 

package com.darkhawx.planck.main.entity.renderers;

import com.darkhawx.planck.main.PlanckMod;
import com.darkhawx.planck.main.entity.EntityHeartFamiliar;
import com.darkhawx.planck.main.init.ModBlocks;
import com.darkhawx.planck.main.tileEntities.TileHeart;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderArmorStand;
import net.minecraft.client.renderer.entity.RenderEnderman;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fml.client.registry.IRenderFactory;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.Random;

@SideOnly(Side.CLIENT)
public class RendererHeartFamiliar extends Render<EntityHeartFamiliar> {
    private static final ResourceLocation HEART_TEXTURES = new ResourceLocation(PlanckMod.MODID, "textures/block/heart.png");

    public RendererHeartFamiliar(RenderManager renderManager) {
        super(renderManager);
        this.shadowSize = 0.5F;
    }

    @Override
    protected ResourceLocation getEntityTexture(EntityHeartFamiliar entity) {
        return null;
    }

    public void doRender(EntityHeartFamiliar entity, double x, double y, double z, float entityYaw, float partialTicks) {
        GlStateManager.pushMatrix();
        this.bindEntityTexture(entity);


        float currentRot = entity.getCurrentTick() / TileHeart.MAX_TICKS;
        double currentPos = Math.sin(entity.getCurrentTick()) * 0.125D;
        double drop = 0.0D;

        GlStateManager.translate(x + 0.5D, y + 0.5D + currentPos, z + 0.5D);
        GlStateManager.rotate(currentRot * 360, 0.0F, 1.0F, 0.0F);
//        GlStateManager.scale(entity.scale, entity.scale, entity.scale);
//
        World world = entity.worldObj;
//
//        Minecraft.getMinecraft().getRenderItem().renderItem(new ItemStack(ModBlocks.heart), ItemCameraTransforms.TransformType.NONE);

        Minecraft.getMinecraft().getBlockRendererDispatcher().renderBlockBrightness(ModBlocks.heart.getDefaultState(), entity.getBrightness(partialTicks));
        GlStateManager.popMatrix();

        System.out.println("rendering");
    }

}

 

 

ModEntities:

 

package com.darkhawx.planck.main.init;

import com.darkhawx.planck.main.PlanckMod;
import com.darkhawx.planck.main.entity.EntityHeartFamiliar;
import com.darkhawx.planck.main.entity.renderers.RendererHeartFamiliar;
import com.darkhawx.planck.main.lib.LibEntities;
import net.minecraft.client.Minecraft;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.fml.common.registry.EntityRegistry;

/**
* Created by Bradley Gray on 30/07/16.
*/
public class ModEntities {

    public static void registerEntities() {
        EntityRegistry.registerModEntity(EntityHeartFamiliar.class, LibEntities.HEART_FAMILIAR, 0, PlanckMod.instance, 32, 5, true);
    }

    public static void registerRenderers() {
        RenderingRegistry.registerEntityRenderingHandler(EntityHeartFamiliar.class, new RendererHeartFamiliar(Minecraft.getMinecraft().getRenderManager()));
    }
}

 

 

ItemHeart:

 

package com.darkhawx.planck.main.items;

import com.darkhawx.planck.main.entity.EntityHeartFamiliar;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatList;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

/**
* Created by Bradley Gray on 30/07/16.
*/
public class ItemHeart extends ItemBasic {
    public ItemHeart(String itemName) {
        super(itemName);
    }

    public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {
        if (!playerIn.capabilities.isCreativeMode)
        {
            --stack.stackSize;
        }

        if (!worldIn.isRemote)
        {
            EntityHeartFamiliar entityHeart = new EntityHeartFamiliar(worldIn, pos, playerIn);
            worldIn.spawnEntityInWorld(entityHeart);
        }

        playerIn.addStat(StatList.getObjectUseStats(this));
        return EnumActionResult.SUCCESS;
    }
}

 

 

ModEntities#registerEntities() called in CommonProxy#preInit(). ModEntities#registerRenderers() is called in ClientProxy#init().

 

I don't really know what it is wrong, so if possible could someone help me.

If you need any more code, let me know.

No signature for you!

Link to comment
Share on other sites

Well I get that, but I don't know how not to use the deprecated method... I can't seem to find a tutorial or example that works...

 

Here's how:

RenderingRegistry.registerEntityRenderingHandler(EntityGreenGoblinGlider.class, new IRenderFactory<EntityGreenGoblinGlider>()
{
    @Override
    public Render<? super EntityGreenGoblinGlider> createRenderFor(RenderManager manager)
    {
        return new RenderGreenGoblinGlider(manager);
    }
});

width=620 height=260http://www.startrek.com/uploads/assets/articles/61c89a9d73c284bda486afaeaf01cdb27180359b.jpg[/img]

Till next time. Thank you for delivering funny scenes to Star Trek as Chekov :) . Will always remember you

Link to comment
Share on other sites

Here's how:
Just vomiting copy-pasta code into your post is not encouraged as a form of answering in this forum, thank you.

 

Oh sorry! I will keep that in mind in future posts :)

width=620 height=260http://www.startrek.com/uploads/assets/articles/61c89a9d73c284bda486afaeaf01cdb27180359b.jpg[/img]

Till next time. Thank you for delivering funny scenes to Star Trek as Chekov :) . Will always remember you

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.

Announcements



×
×
  • Create New...

Important Information

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