(SOLVED)How to make a entity attack everything except their own kind?

Started by Zach The Modder on Tue, 06/08/2021 - 04:44

Topic category: Help with modding (Java Edition)

Last seen on 07:33, 13. Jun 2021
Joined Feb 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
(SOLVED)How to make a entity attack everything except their own kind?
Tue, 06/08/2021 - 16:08 (edited)

Currently, im having trouble to make a condition return false if the entity they targeting it is their own kind. a lot of condition that i`ve already made, and none of them work they keep attacking each other.

My Entity Code:

package net.mcreator.outerrealm.entity;

import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.fml.network.NetworkHooks;
import net.minecraftforge.fml.network.FMLPlayMessages;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.world.BiomeLoadingEvent;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.biome.MobSpawnInfo;
import net.minecraft.world.World;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.DamageSource;
import net.minecraft.network.IPacket;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Item;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.monster.MonsterEntity;
import net.minecraft.entity.ai.goal.SwimGoal;
import net.minecraft.entity.ai.goal.RandomWalkingGoal;
import net.minecraft.entity.ai.goal.NearestAttackableTargetGoal;
import net.minecraft.entity.ai.goal.MeleeAttackGoal;
import net.minecraft.entity.ai.goal.LookRandomlyGoal;
import net.minecraft.entity.ai.goal.LookAtGoal;
import net.minecraft.entity.ai.goal.HurtByTargetGoal;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.EntitySpawnPlacementRegistry;
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.Entity;
import net.minecraft.entity.CreatureAttribute;

import net.mcreator.outerrealm.procedures.ThawPlayerCollidesWithThisEntityProcedure;
import net.mcreator.outerrealm.procedures.ThawEntityDiesProcedure;
import net.mcreator.outerrealm.item.AcidEssenceItem;
import net.mcreator.outerrealm.entity.renderer.ThawRenderer;
import net.mcreator.outerrealm.OuterRealmModElements;

import java.util.Map;
import java.util.HashMap;

@OuterRealmModElements.ModElement.Tag
public class ThawEntity extends OuterRealmModElements.ModElement {
    public static EntityType entity = (EntityType.Builder.<CustomEntity>create(CustomEntity::new, EntityClassification.MONSTER)
            .setShouldReceiveVelocityUpdates(true).setTrackingRange(128).setUpdateInterval(3).setCustomClientFactory(CustomEntity::new)
            .size(1.4000000000000001f, 1.7999999999999998f)).build("thaw").setRegistryName("thaw");
    public ThawEntity(OuterRealmModElements instance) {
        super(instance, 81);
        FMLJavaModLoadingContext.get().getModEventBus().register(new ThawRenderer.ModelRegisterHandler());
        FMLJavaModLoadingContext.get().getModEventBus().register(new EntityAttributesRegisterHandler());
        MinecraftForge.EVENT_BUS.register(this);
    }

    @Override
    public void initElements() {
        elements.entities.add(() -> entity);
        elements.items.add(
                () -> new SpawnEggItem(entity, -10027162, -16724992, new Item.Properties().group(ItemGroup.MISC)).setRegistryName("thaw_spawn_egg"));
    }

    @SubscribeEvent
    public void addFeatureToBiomes(BiomeLoadingEvent event) {
        boolean biomeCriteria = false;
        if (new ResourceLocation("outer_realm:rotten_high_land").equals(event.getName()))
            biomeCriteria = true;
        if (new ResourceLocation("outer_realm:rotten_low_land").equals(event.getName()))
            biomeCriteria = true;
        if (new ResourceLocation("outer_realm:acid_deeplake").equals(event.getName()))
            biomeCriteria = true;
        if (!biomeCriteria)
            return;
        event.getSpawns().getSpawner(EntityClassification.MONSTER).add(new MobSpawnInfo.Spawners(entity, 20, 1, 1));
    }

    @Override
    public void init(FMLCommonSetupEvent event) {
        EntitySpawnPlacementRegistry.register(entity, EntitySpawnPlacementRegistry.PlacementType.ON_GROUND, Heightmap.Type.MOTION_BLOCKING_NO_LEAVES,
                MonsterEntity::canMonsterSpawn);
    }
    private static class EntityAttributesRegisterHandler {
        @SubscribeEvent
        public void onEntityAttributeCreation(EntityAttributeCreationEvent event) {
            AttributeModifierMap.MutableAttribute ammma = MobEntity.func_233666_p_();
            ammma = ammma.createMutableAttribute(Attributes.MOVEMENT_SPEED, 0.25);
            ammma = ammma.createMutableAttribute(Attributes.MAX_HEALTH, 30);
            ammma = ammma.createMutableAttribute(Attributes.ARMOR, 8);
            ammma = ammma.createMutableAttribute(Attributes.ATTACK_DAMAGE, 7);
            ammma = ammma.createMutableAttribute(Attributes.KNOCKBACK_RESISTANCE, 0.4);
            ammma = ammma.createMutableAttribute(Attributes.ATTACK_KNOCKBACK, 1);
            ammma = ammma.createMutableAttribute(Attributes.FOLLOW_RANGE, 90D);
            event.put(entity, ammma.create());
        }
    }

    public static class CustomEntity extends MonsterEntity {
        public CustomEntity(FMLPlayMessages.SpawnEntity packet, World world) {
            this(entity, world);
        }

        public CustomEntity(EntityType<CustomEntity> type, World world) {
            super(type, world);
            experienceValue = 20;
            setNoAI(false);
        }

        @Override
        public IPacket<?> createSpawnPacket() {
            return NetworkHooks.getEntitySpawningPacket(this);
        }

        @Override
        protected void registerGoals() {
            super.registerGoals();
            this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1, true));
            this.targetSelector.addGoal(2, new HurtByTargetGoal(this).setCallsForHelp(this.getClass()));
            this.targetSelector.addGoal(3, new NearestAttackableTargetGoal(this, MobEntity.class, true, true));
            this.goalSelector.addGoal(4, new LookAtGoal(this, MobEntity.class, (float) 10));
            this.goalSelector.addGoal(5, new RandomWalkingGoal(this, 1));
            this.goalSelector.addGoal(6, new LookRandomlyGoal(this));
            this.goalSelector.addGoal(7, new SwimGoal(this));
        }

        @Override
        public CreatureAttribute getCreatureAttribute() {
            return CreatureAttribute.UNDEFINED;
        }

        protected void dropSpecialItems(DamageSource source, int looting, boolean recentlyHitIn) {
            super.dropSpecialItems(source, looting, recentlyHitIn);
            this.entityDropItem(new ItemStack(AcidEssenceItem.block, (int) (1)));
        }

        @Override
        public net.minecraft.util.SoundEvent getAmbientSound() {
            return (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("outer_realm:thaw_say"));
        }

        @Override
        public net.minecraft.util.SoundEvent getHurtSound(DamageSource ds) {
            return (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("outer_realm:thaw_hurt"));
        }

        @Override
        public net.minecraft.util.SoundEvent getDeathSound() {
            return (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("outer_realm:thaw_death"));
        }

        @Override
        public boolean attackEntityFrom(DamageSource source, float amount) {
            if (source == DamageSource.CACTUS)
                return false;
            return super.attackEntityFrom(source, amount);
        }

        @Override
        public void onDeath(DamageSource source) {
            super.onDeath(source);
            double x = this.getPosX();
            double y = this.getPosY();
            double z = this.getPosZ();
            Entity sourceentity = source.getTrueSource();
            Entity entity = this;
            {
                Map<String, Object> $_dependencies = new HashMap<>();
                $_dependencies.put("x", x);
                $_dependencies.put("y", y);
                $_dependencies.put("z", z);
                $_dependencies.put("world", world);
                ThawEntityDiesProcedure.executeProcedure($_dependencies);
            }
        }

        @Override
        public void onCollideWithPlayer(PlayerEntity sourceentity) {
            super.onCollideWithPlayer(sourceentity);
            Entity entity = this;
            double x = this.getPosX();
            double y = this.getPosY();
            double z = this.getPosZ();
            {
                Map<String, Object> $_dependencies = new HashMap<>();
                $_dependencies.put("sourceentity", sourceentity);
                ThawPlayerCollidesWithThisEntityProcedure.executeProcedure($_dependencies);
            }
        }
    }
}

Turn out, its require custom code for ai goal

Edited by Zach The Modder on Tue, 06/08/2021 - 16:08