Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

So what I'm trying to do is make it so that when a mob gets below a certain percentage of its original health, it flees from the player. I've managed to make it run away, but it's still trying to target the player, and it can still hurt the player if they get to close. Is there any way to change this?

 

Spoiler

package com.doggo.moboverhaul.handlers;

import net.minecraft.entity.ai.EntityAIAvoidEntity;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@EventBusSubscriber

public class MobAIHandler {
    
    @SubscribeEvent
    public static void onEntitySpawn(EntityJoinWorldEvent event)
    {
        if(event.getEntity() instanceof EntitySpider)
        {
            System.out.println("Spider spawned");
            EntitySpider spider = (EntitySpider)event.getEntity();
            spider.tasks.addTask(5, new EntityAIAvoidEntity<EntityPlayer>(spider, EntityPlayer.class, 3.0F, 1.0D, 1.2D));
        }
    }
    
    @SubscribeEvent
    public static void onEntityHurt(LivingHurtEvent event)
    {
        if(event.getEntity() instanceof EntitySpider)
        {
            System.out.println("Spider hurt");
            EntitySpider spider = (EntitySpider)event.getEntity();
            if(spider.getHealth() <= (spider.getMaxHealth()/2))
            {
                System.out.println("Spider's health is "+spider.getHealth());
                spider.tasks.addTask(1, new EntityAIAvoidEntity<EntityPlayer>(spider, EntityPlayer.class, 6.0F, 1.0D, 1.2D));
                
            }
        }
        
        if(event.getEntity() instanceof EntityZombie)
        {
            System.out.println("Zombie hurt");
            EntityZombie zombie = (EntityZombie)event.getEntity();
            if(zombie.getHealth() <= (zombie.getMaxHealth()/2))
            {
                System.out.println("Zombie's health is "+zombie.getHealth());
                zombie.tasks.addTask(1, new EntityAIAvoidEntity<EntityPlayer>(zombie, EntityPlayer.class, 6.0F, 1.0D, 1.2D));
            }
        }
    }
}
 

 

 

You can use EntityAITasks#removeTask.

Alternatively you can abuse the mutex bits of tasks. Basically each AI task has an int that represents a collection of bits(so 32 bits total). When the game wants to make the AI run it checks whether it conflicts with another higher priority AI by comparing their mutex bits field. The game performs a bitwise AND operation on them. If the AND returns 0 then both tasks can run concurrently, otherwise the one with the higher priority is the one that will run. The mutex of the targeting AI is 1.

  • Author

Thanks. I tried both of them, these were my results, I'm clearly doing something wrong with both of them

 

Spoiler

@SubscribeEvent
    public static void onEntityHurt(LivingHurtEvent event)
    {
        if(event.getEntity() instanceof EntitySpider)
        {
            System.out.println("Spider hurt");
            EntitySpider spider = (EntitySpider)event.getEntity();
            if(spider.getHealth() <= (spider.getMaxHealth()/2))
            {
                System.out.println("Spider's health is "+spider.getHealth());
                spider.tasks.addTask(1, new EntityAIAvoidEntity<EntityPlayer>(spider, EntityPlayer.class, 6.0F, 1.0D, 1.2D));


                //My attempt of setMutexBits (error: "Cannot make a static reference to the non-static method setMutexBits(int) from the type EntityAIBase". This could be because my class is static)
                EntityAINearestAttackableTarget.setMutexBits(1);


                //My attempt of removeTask (error: "The method removeTask(EntityAIBase) in the type EntityAITasks is not applicable for the arguments (EntitySpider)" because spider is an EntitySpider and not an EntityAIBase)
                EntityAITasks.removeTask(spider);
            }
        }

 

 

How well do you know java? You are trying to invoke instance methods as if they were static and are passing wring parameters to them. If you don't know the basics of java you should learn it before trying to create a mod.

  • Author

Ah, fair enough. Thanks for the advice. I'll go back to learning for now then, and come back to this when I'm more familiar with Java

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.