Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.12] Custom AI not working

Recommended Posts

I am creating a dinosaur mod where dinosaurs can guard a nest that they have already laid. The AI for laying a nest is working, but the guarding AI I coded doesn't work.

The guarding AI is supposed to accomplish 2 tasks:

a. Make sure the dinosaur can't move out of range from the nest

b. Attack any EntityLiving within the range specified

Currently, none of them are working. Here is my code:

public class EntityAIGuardNest extends EntityAIBase {

        private EntityDinosaurTameable dinosaur;

        World world;

        private double speed;
        private double distance;

        public EntityAIGuardNest(EntityDinosaurTameable dinosaur, double distance ,double speed){
            this.dinosaur = dinosaur;
            this.world = this.dinosaur.world;

            this.speed = speed;
            this.distance = distance;

        public boolean shouldExecute(){
            return this.dinosaur.nest != null && !this.dinosaur.isTamed() && this.dinosaur.nestBlockPos != null;

        public void updateTask(){
            if(MathHelper.sqrt(this.dinosaur.getDistanceSq(this.dinosaur.nestBlockPos)) > this.distance){
                this.dinosaur.getNavigator().tryMoveToXYZ(this.dinosaur.nestBlockPos.getX(), this.dinosaur.nestBlockPos.getY(), this.dinosaur.nestBlockPos.getZ(), this.speed);
            } else {
                List<EntityLiving> list = this.dinosaur.world.<EntityLiving>getEntitiesWithinAABB(EntityLiving.class, this.dinosaur.nest.getCollisionBoundingBox(this.dinosaur.nest.getDefaultState(), this.dinosaur.world, nestBlockPos).grow(this.distance));
                for (EntityLiving attacker : list)
                    if (!(attacker instanceof EntityEgg) && attacker.getDistanceSq(this.dinosaur.nestBlockPos) < this.distance)

        public boolean shouldContinueExecuting()
            return this.dinosaur.nest != null;


Link to post
Share on other sites

Can't say much without seeing the dinosaur entity code as well... But already can tell that dinosaur.setAttackTarget() part should be in the shouldExecute() function, and the for loop must be broken upon finding the target.

Link to post
Share on other sites

I added a break in the for loop, and I found out the problem. The code I have used to get the block's bounding box is not working, as I changed 

List<EntityLiving> list = this.dinosaur.world.<EntityLiving>getEntitiesWithinAABB(EntityLiving.class, this.dinosaur.nest.getCollisionBoundingBox(this.dinosaur.nest.getDefaultState(), this.dinosaur.world, nestBlockPos).grow(this.distance));


                List<EntityLiving> list = this.dinosaur.world.<EntityLiving>getEntitiesWithinAABB(EntityLiving.class, this.dinosaur.getEntityBoundingBox().grow(this.distance));

And it began to attack other mobs. The problem is that I don't want it to attack other entities within a certain range of the dinosaur, I want it to attack certain entities within a certain range of the nest. So I need to know why the 1st snippet of code is not working

Edited by Geometrically
Link to post
Share on other sites

Your code uses a nestBlockPos variable as if it existed in the AI class, but it doesn't. How does this code even compile? And show your entity class, where is the nestBlockPos variable set? And do not call deprecated methods in the Block class, use the IBlockState equivalents.

Link to post
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.

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.

  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • i currently try to find out how the world gen works, at the moment im working on a structure but i have some problems with the generation. i use this  as an example, i created everything for the generation, but the structure is not generating this is my structure this is the structure registry this is my world load event and this my biome loading event thanks for replies
    • I'm currently trying to make a new cauldron-type block, and I'm having difficulty implementing blockstates. Here's the relevant part of the alchemical cauldron's code:   public class AlchemicalCauldronBlock extends Block { public static final IntegerProperty LEVEL = IntegerProperty.create("level", 1, 3); private static final VoxelShape INSIDE = box(2.0D, 4.0D, 2.0D, 14.0D, 16.0D, 14.0D); protected static final VoxelShape SHAPE = VoxelShapes.join(VoxelShapes.block(), VoxelShapes.or(box(0.0D, 0.0D, 4.0D, 16.0D, 3.0D, 12.0D), box(4.0D, 0.0D, 0.0D, 12.0D, 3.0D, 16.0D), box(2.0D, 0.0D, 2.0D, 14.0D, 3.0D, 14.0D), INSIDE), IBooleanFunction.ONLY_FIRST); public AlchemicalCauldronBlock() { super(Properties.of(Material.METAL).sound(SoundType.METAL).harvestLevel(3).strength(2F,2F)); // this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, 0)); // this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, Integer.valueOf(0))); } ... When I try to run this with either of the two attempts at registerDefaultState uncommented, I get this error: https://pastebin.com/vPf3UryC. For convenience, I believe this part is most relevant: How can I avoid this? My registry classes look like this, if it helps: public class registry { public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, AlchemyPlus.MODID); public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, AlchemyPlus.MODID); public static void register() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); BLOCKS.register(modEventBus); ITEMS.register(modEventBus); registerItems.register(); registerBlocks.register(); } } public class registerBlocks { public static final RegistryObject<Block> ALCHEMICAL_CAULDRON = register("alchemical_cauldron", AlchemicalCauldronBlock::new); static void register() {} private static <T extends Block> RegistryObject<T> registerNoItem(String name, Supplier<T> block) { return registry.BLOCKS.register(name, block); } private static <T extends Block> RegistryObject<T> register(String name, Supplier<T> block) { RegistryObject<T> ret = registerNoItem(name, block); registry.ITEMS.register(name, () -> new BlockItem(ret.get(), new Item.Properties().tab(ItemGroup.TAB_BREWING))); return ret; } } Full GitHub repository available here.   (Other minor problem(?):  I copied the methods getShape and getInteractionShape from net.minecraft.block.CauldronBlock and I'm getting these weird warnings. What's up with that? Do I need to worry about it?)  
    • Personally I use IForgeRegistryEntry#getRegistryName and then ResourceLocation#getNameSpace to get the id of the object.
    • you can get a block form a ResourceLocation, take a look at NBTUtil#readBlockState, do somthing like that
    • I am running forge version 1.16.5 36.1.31 on a server with a lot of mods and when a player refreshes the server list, it sends a long error in the server console. Additionally, the server list displays "Can't connect to server", despite refreshing and actually being able to connect to the server. here's the links to the logs: https://gist.github.com/WaffleTraits/83885b1539d5711fabff73a91924e665 https://gist.github.com/WaffleTraits/5ae78e6eaa330422bbc9f7226faf60ce Here's what's displayed in console when a player refreshes server listing: https://hastebin.com/ativexeyoy.properties server is running on ubuntu 20.04 and is using java 8 for this forge instance.
  • Topics

  • Who's Online (See full list)

  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.