7 nov. 2020, 14:32

Bonjour.
Je veux faire une entité (ici un dragon) qui attaque avec un projectile.
Or, pour faire ca, il faut que cette entité regarde l’entité à attaquer.
J’ai fait ce code mais le jeu crash sans rien dire dans la console.
Merci d’avance 🙂

public class EntityBabyDragonBase extends EntityTameable
{
    public EntityBabyDragonBase(World worldIn)
    {
        super(worldIn);
        this.setSize(1.0F, 0.5F);
        this.setTamed(false);
    }

    public EntityBabyDragonBase(World worldIn, double x, double y, double z)
    {
        super(worldIn);
        this.setPosition(x, y, z);
    }

    @Override
    protected void initEntityAI()
    {
        this.aiSit = new EntityAISit(this);
        this.tasks.addTask(1, new EntityAISwimming(this));
        this.tasks.addTask(2, this.aiSit);
        // this.tasks.addTask(3, new EntityWolf.AIAvoidEntity(this, EntityLlama.class, 24.0F, 1.5D, 1.5D));
        this.tasks.addTask(4, new EntityAILeapAtTarget(this, 0.4F));
        // this.tasks.addTask(5, new EntityAIAttackMelee(this, 1.0D, true));
        this.tasks.addTask(6, new EntityAIFollowOwner(this, 1.0D, 10.0F, 2.0F));
        this.tasks.addTask(7, new EntityAIMate(this, 1.0D));
        this.tasks.addTask(8, new EntityAIWanderAvoidWater(this, 1.0D));
        // this.tasks.addTask(9, new EntityAIBeg(this, 8.0F));
        this.tasks.addTask(10, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
        this.tasks.addTask(10, new EntityAILookIdle(this));
        this.targetTasks.addTask(1, new EntityAIOwnerHurtByTarget(this));
        this.targetTasks.addTask(2, new EntityAIOwnerHurtTarget(this));
        this.targetTasks.addTask(3, new EntityAIHurtByTarget(this, true, new Class[0]));

        // this.targetTasks.addTask(5, new EntityAINearestAttackableTarget(this, AbstractSkeleton.class, false));
    }

    @Override
    public void onUpdate()
    {
        super.onUpdate();
        if(this.getAttackTarget() != null)
        {
            for(int i = 1000; i > 0; i--)
            {
                if(i == 1)
                {
                    this.getLookHelper().setLookPosition(this.getAttackTarget().posX, this.getAttackTarget().posY + (double)this.getAttackTarget().getEyeHeight(), this.getAttackTarget().posZ, (float)this.getHorizontalFaceSpeed(), (float)this.getVerticalFaceSpeed());

                    EntitySnowball entitysnowball = new EntitySnowball(this.world, this);
                    entitysnowball.shoot(this, this.rotationPitch, this.rotationYaw, 0.0F, 1.5F, 1.0F);
                    this.world.spawnEntity(entitysnowball);
                    i=1000;
                }
            }
        }
    }

    @Override
    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        if(this.getDragonType() == EnumDragonType.FIRE)
        {
            this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.29000001192092896D);

            if(this.isTamed())
            {
                this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(35.0D);
            }
            else
            {
                this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(25.0D);
            }

            this.getAttributeMap().registerAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(17.0D);
        }
        else if(this.getDragonType() == EnumDragonType.ICE)
        {
            this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.3500001192092896D);

            if(this.isTamed())
            {
                this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(33.0D);
            }
            else
            {
                this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(25.0D);
            }

            this.getAttributeMap().registerAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(12.0D);
        }
        else if(this.getDragonType() == EnumDragonType.EARTH)
        {
            this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.2500001192092896D);

            if(this.isTamed())
            {
                this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(50.0D);
            }
            else
            {
                this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(25.0D);
            }

            this.getAttributeMap().registerAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(15.0D);
        }
    }

    public EnumDragonType getDragonType()
    {
        return EnumDragonType.NONE;
    }

    public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
        ItemStack itemstack = player.getHeldItem(hand);

        if(this.isTamed())
        {
            if(!itemstack.isEmpty())
            {
                if(itemstack.getItem() instanceof ItemFood)
                {
                    ItemFood itemfood = (ItemFood)itemstack.getItem();

                    if(itemfood.isWolfsFavoriteMeat() && this.getHealth() < 20.0f)
                    {
                        if(!player.capabilities.isCreativeMode)
                        {
                            itemstack.shrink(1);
                        }

                        this.heal((float)itemfood.getHealAmount(itemstack));
                        return true;
                    }
                }
            }

            if(this.isOwner(player) && !this.world.isRemote && !this.isBreedingItem(itemstack))
            {
                this.aiSit.setSitting(!this.isSitting());
                this.isJumping = false;
                this.navigator.clearPath();
                this.setAttackTarget((EntityLivingBase)null);
            }
        }
        else
        {
            if(this.getDragonType() == EnumDragonType.ICE)
            {
                if(itemstack.getItem() == Item.getItemFromBlock(Blocks.PACKED_ICE))
                {
                    if(!player.capabilities.isCreativeMode)
                    {
                        itemstack.shrink(1);
                    }

                    if(!this.world.isRemote)
                    {
                        if(this.rand.nextInt(3) == 0 && !net.minecraftforge.event.ForgeEventFactory.onAnimalTame(this, player))
                        {
                            this.setTamedBy(player);
                            this.navigator.clearPath();
                            this.setAttackTarget((EntityLivingBase)null);
                            this.aiSit.setSitting(true);
                            this.setHealth(35.0F);
                            this.playTameEffect(true);
                            this.world.setEntityState(this, (byte)7);
                        }
                        else
                        {
                            this.playTameEffect(false);
                            this.world.setEntityState(this, (byte)6);
                        }
                    }
                }
            }
            else if(this.getDragonType() == EnumDragonType.FIRE)
            {
                if(itemstack.getItem() == Item.getItemFromBlock(Blocks.MAGMA))
                {
                    if(!player.capabilities.isCreativeMode)
                    {
                        itemstack.shrink(1);
                    }

                    if(!this.world.isRemote)
                    {
                        if(this.rand.nextInt(3) == 0 && !net.minecraftforge.event.ForgeEventFactory.onAnimalTame(this, player))
                        {
                            this.setTamedBy(player);
                            this.navigator.clearPath();
                            this.setAttackTarget((EntityLivingBase)null);
                            this.aiSit.setSitting(true);
                            this.setHealth(33.0F);
                            this.playTameEffect(true);
                            this.world.setEntityState(this, (byte)7);
                        }
                        else
                        {
                            this.playTameEffect(false);
                            this.world.setEntityState(this, (byte)6);
                        }
                    }
                }
            }
            else if(this.getDragonType() == EnumDragonType.EARTH)
            {
                if(itemstack.getItem() == Item.getItemFromBlock(Blocks.DIRT))
                {
                    if(!player.capabilities.isCreativeMode)
                    {
                        itemstack.shrink(1);
                    }

                    if(!this.world.isRemote)
                    {
                        if(this.rand.nextInt(3) == 0 && !net.minecraftforge.event.ForgeEventFactory.onAnimalTame(this, player))
                        {
                            this.setTamedBy(player);
                            this.navigator.clearPath();
                            this.setAttackTarget((EntityLivingBase)null);
                            this.aiSit.setSitting(true);
                            this.setHealth(50.0F);
                            this.playTameEffect(true);
                            this.world.setEntityState(this, (byte)7);
                        }
                        else
                        {
                            this.playTameEffect(false);
                            this.world.setEntityState(this, (byte)6);
                        }
                    }
                }
            }
        }

        return super.processInteract(player, hand);
    }

    @Override
    public EntityAgeable createChild(EntityAgeable ageable)
    {
        EntityWolf entitywolf = new EntityWolf(this.world);
        UUID uuid = this.getOwnerId();

        if(uuid != null)
        {
            entitywolf.setOwnerId(uuid);
            entitywolf.setTamed(true);
        }

        return entitywolf;
    }
}