Jump to content

Recommended Posts

Posted

 

 I am working on a boar in Minecraft, and it was all going smoothly until I tried to code it to attack when it was attacked. I've spent about 6 hours of browsing the Internet and just toying with the tasks and AI to get something working, and nothing happened besides crashes when I summoned it, it not summoning at all, or it being summoned, but just as a passive mob. The Model and Render classes work just fine, the entity showed up in the game and worked fine up to the point when I added the walking and head animations. So, I will link the EntityBoar file only, unless you need to see the Model and Render classes. With this code in particular, the game continues to run even after attempting to summon the Boar, but Boar won't summon. I am working on Forge 1.12.2. Thanks

 

package com.dmh10s.morethingsmod.entity;
    
    import java.util.Set;
    import java.util.UUID;
    
    import javax.annotation.Nullable;
    
    import com.dmh10s.morethingsmod.init.ModItems;
    import com.google.common.collect.Sets;
    
    import net.minecraft.block.Block;
    import net.minecraft.enchantment.EnchantmentHelper;
    import net.minecraft.entity.Entity;
    import net.minecraft.entity.EntityAgeable;
    import net.minecraft.entity.EntityCreature;
    import net.minecraft.entity.EntityLiving;
    import net.minecraft.entity.EntityLivingBase;
    import net.minecraft.entity.IEntityLivingData;
    import net.minecraft.entity.SharedMonsterAttributes;
    import net.minecraft.entity.ai.EntityAIAttackMelee;
    import net.minecraft.entity.ai.EntityAIFollow;
    import net.minecraft.entity.ai.EntityAIFollowParent;
    import net.minecraft.entity.ai.EntityAIHurtByTarget;
    import net.minecraft.entity.ai.EntityAILookIdle;
    import net.minecraft.entity.ai.EntityAIMate;
    import net.minecraft.entity.ai.EntityAIMoveTowardsRestriction;
    import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
    import net.minecraft.entity.ai.EntityAIPanic;
    import net.minecraft.entity.ai.EntityAISwimming;
    import net.minecraft.entity.ai.EntityAITarget;
    import net.minecraft.entity.ai.EntityAITempt;
    import net.minecraft.entity.ai.EntityAIWanderAvoidWater;
    import net.minecraft.entity.ai.EntityAIWatchClosest;
    import net.minecraft.entity.ai.attributes.AttributeModifier;
    import net.minecraft.entity.ai.attributes.IAttributeInstance;
    import net.minecraft.entity.effect.EntityLightningBolt;
    import net.minecraft.entity.monster.EntityMob;
    import net.minecraft.entity.monster.EntityPigZombie;
    import net.minecraft.entity.monster.EntityZombie;
    import net.minecraft.entity.passive.EntityAnimal;
    import net.minecraft.entity.passive.EntityPig;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.init.Items;
    import net.minecraft.init.SoundEvents;
    import net.minecraft.inventory.EntityEquipmentSlot;
    import net.minecraft.item.Item;
    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.util.DamageSource;
    import net.minecraft.util.EnumHand;
    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.EnumDifficulty;
    import net.minecraft.world.World;
    import net.minecraft.world.storage.loot.LootTableList;
    
    public class EntityBoar extends EntityAnimal
    
    {
        private static final Set<Item> TEMPTATION_ITEMS = Sets.newHashSet(ModItems.CABBAGE, Items.CHICKEN, ModItems.RAW_TURKEY_BREAST);
        private EntityLivingBase attackTarget;
        boolean callInReinforcements = false;
    
        public EntityBoar(World worldIn)
        {
            super(worldIn);
            this.setSize(0.9F, 0.9F);
            this.stepHeight = 1.0F;
        }
        
        protected void initEntityAI()
        {
            this.tasks.addTask(5, new EntityAISwimming(this));
            this.tasks.addTask(2, new EntityAITempt(this, 1.2D, false, TEMPTATION_ITEMS));
            this.tasks.addTask(3, new EntityAIWanderAvoidWater(this, 1.0D));
            this.tasks.addTask(4, new EntityAIWatchClosest(this, EntityPlayer.class, 10.0F));
            this.tasks.addTask(6, new EntityAILookIdle(this));
        }
    
        protected void applyEntityAttributes()
        {
            super.applyEntityAttributes();
            this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(14.0D);
            this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.2D);
            this.getAttributeMap().registerAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(4.0D);
            this.getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).setBaseValue(25.0D);
        }
    
        public static void registerFixesBoar(DataFixer fixer)
        {
            EntityLiving.registerFixesMob(fixer, EntityBoar.class);
        }
        
        public boolean attackEntityFrom(DamageSource source, float amount)
        {
            if (super.attackEntityFrom(source, amount))
            {
                EntityLivingBase entitylivingbase = this.getAttackTarget();
    
                if (entitylivingbase == null && source.getTrueSource() instanceof EntityLivingBase)
                {
                    entitylivingbase = (EntityLivingBase)source.getTrueSource();
                }
                
                this.setAttackTarget(entitylivingbase);
    
                return true;
            }
            else
            {
                return false;
            }
        }
        
        @Override
        public boolean attackEntityAsMob(Entity entityIn)
        {
            float f = (float)this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).getAttributeValue();
            int i = 0;
    
            if (entityIn instanceof EntityLivingBase)
            {
                f += EnchantmentHelper.getModifierForCreature(this.getHeldItemMainhand(), ((EntityLivingBase)entityIn).getCreatureAttribute());
                i += EnchantmentHelper.getKnockbackModifier(this);
            }
    
            boolean flag = entityIn.attackEntityFrom(DamageSource.causeMobDamage(this), f);
    
            if (flag)
            {
                if (i > 0 && entityIn instanceof EntityLivingBase)
                {
                    ((EntityLivingBase)entityIn).knockBack(this, (float)i * 0.5F, (double)MathHelper.sin(this.rotationYaw * 0.017453292F), (double)(-MathHelper.cos(this.rotationYaw * 0.017453292F)));
                    this.motionX *= 0.6D;
                    this.motionZ *= 0.6D;
                }
    
                int j = EnchantmentHelper.getFireAspectModifier(this);
    
                if (j > 0)
                {
                    entityIn.setFire(j * 4);
                }
    
                if (entityIn instanceof EntityPlayer)
                {
                    EntityPlayer entityplayer = (EntityPlayer)entityIn;
                    ItemStack itemstack = this.getHeldItemMainhand();
                    ItemStack itemstack1 = entityplayer.isHandActive() ? entityplayer.getActiveItemStack() : ItemStack.EMPTY;
    
                    if (!itemstack.isEmpty() && !itemstack1.isEmpty() && itemstack.getItem().canDisableShield(itemstack, itemstack1, entityplayer, this) && itemstack1.getItem().isShield(itemstack1, entityplayer))
                    {
                        float f1 = 0.25F + (float)EnchantmentHelper.getEfficiencyModifier(this) * 0.05F;
    
                        if (this.rand.nextFloat() < f1)
                        {
                            entityplayer.getCooldownTracker().setCooldown(itemstack1.getItem(), 100);
                            this.world.setEntityState(entityplayer, (byte)30);
                        }
                    }
                }
    
                this.applyEnchantments(this, entityIn);
            }
            return flag;
        }
    
        protected SoundEvent getAmbientSound()
        {
            return SoundEvents.ENTITY_PIG_AMBIENT;
        }
    
        protected SoundEvent getHurtSound(DamageSource damageSourceIn)
        {
            return SoundEvents.ENTITY_PIG_HURT;
        }
    
        protected SoundEvent getDeathSound()
        {
            return SoundEvents.ENTITY_PIG_DEATH;
        }
    
        protected void playStepSound(BlockPos pos, Block blockIn)
        {
            this.playSound(SoundEvents.ENTITY_PIG_STEP, 0.15F, 1.0F);
        }
    
        public boolean processInteract(EntityPlayer player, EnumHand hand)
        {
            if (!super.processInteract(player, hand))
            {
                ItemStack itemstack = player.getHeldItem(hand);
    
                if (itemstack.getItem() == Items.NAME_TAG)
                {
                    itemstack.interactWithEntity(player, this, hand);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
    
        /**
         * Called when the mob's health reaches 0.
         */
        public void onDeath(DamageSource cause)
        {
            super.onDeath(cause);
        }
    
        @Nullable
        protected ResourceLocation getLootTable()
        {
            return null; //YOU NEED TO MAKE CUSTOM LOOT TABLES FOR THE BOAR
        }
    
        public EntityBoar createChild(EntityAgeable ageable)
        {
            return new EntityBoar(this.world);
        }
    
        public boolean isBreedingItem(ItemStack stack)
        {
            return TEMPTATION_ITEMS.contains(stack.getItem());
        }
        
        public float getEyeHeight()
        {
            return 0.7F;
        }
    }

   

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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