Jump to content

[1.18.1] - Add custom goals to vanilla Entities


Davide_24

Recommended Posts

So I am making a catnip carpet type block, and I want to make cats go to it and lie on it. I have already made the code for the AI goal itself. Only issue is adding that AI goal to the cat. I couldn't find anything while searching and now I want to ask here. The only way that comes to my mind is changing the registerGoals() method in Cat.java, but that seems like a bad idea.

Link to comment
Share on other sites

23 hours ago, diesieben07 said:

You can register new goals to entities in EntityJoinWorldEvent.

So I tried this and it won't work. This is the code: 

public class events {

    private static final Logger LOGGER = LogManager.getLogger();

    @SubscribeEvent
    public static void EntityJoined(EntityJoinWorldEvent event) {
        LOGGER.info("Entity Joined World!");
        Entity entity = event.getEntity();
        if (entity instanceof Cat) {
            Cat cat = (Cat) entity;
            ((Cat) event.getEntity()).goalSelector.addGoal(7, new CatLieOnCatnipGoal(cat, 1.1D, 8));
        }
    }
}

 

The event does fire which is confirmed by the logger.

 

EDIT: Sorry for the late reply!

Edited by Davide_24
Link to comment
Share on other sites

47 minutes ago, diesieben07 said:

Use the debugger - are the methods on your Goal called?

So the isValidTarget() Method is firing but nothing else. The cats won't move to the target block. (temporarily set to slab blocks). I found that the tick() method should also fire while doing some research but it doesn't.

public class CatLieOnCatnipGoal extends MoveToBlockGoal {
    private final Cat cat;

    private static final Logger LOGGER = LogManager.getLogger();

    public CatLieOnCatnipGoal(Cat cat, double p_25136_, int p_25137_) {
        super(cat, p_25136_, p_25137_, 6);
        this.cat = cat;
        this.verticalSearchStart = -2;
        this.setFlags(EnumSet.of(Goal.Flag.JUMP, Goal.Flag.MOVE));
    }

    @Override
    public boolean canUse() {
        return !this.cat.isOrderedToSit() && !this.cat.isLying() && super.canUse();
    }

    @Override
    public void start() {
        LOGGER.info("CAT LIE ON CATNIP GOAL STARTED");
        super.start();
        this.cat.setInSittingPose(false);
    }

    @Override
    protected int nextStartTick(PathfinderMob mob) {
        return 40;
    }

    @Override
    public void stop() {
        LOGGER.info("STOPPED");
        super.stop();
        this.cat.setLying(false);
    }

    @Override
    public void tick() {
        LOGGER.info("CAT LIE ON CATNIP GOAL TICK!");
        super.tick();
        this.cat.setInSittingPose(false);
        if (!this.isReachedTarget()) {
            this.cat.setLying(false);
        } else if (!this.cat.isLying()) {
            this.cat.setLying(true);
        }

    }

    @Override
    protected boolean isValidTarget(LevelReader levelReader, BlockPos pos) {
        LOGGER.info("CAT LIE ON CATNIP GOAL SETTING TARGET!");
        return levelReader.isEmptyBlock(pos.above()) && levelReader.getBlockState(pos).is(BlockTags.SLABS);
    }
}

 

Edited by Davide_24
Added more explanation
Link to comment
Share on other sites

  • 5 months later...

Old thread I know, but thought I would share the behavior that caused this since it is very interesting.

 

One problem I found in the code above is that in the CatLieOnCatnipGoal constructor, you have this line:

this.verticalSearchStart = -2;

While counter-intuitive and a bit obfuscated, the find nearest block uses this variable like so:

for(int k = this.verticalSearchStart; k <= j; k = k > 0 ? -k : 1 - k) {

This is a part of a series of for loops to search every block within the range. The line above will perform a search going away from a vertical start in a pseudo- absolute value fashion. Basically it alternates between going up then down. If the starting value is 0, the pattern is:

START k=0
EVALUATE k= k > 0 ? -k : 1 - k (FALSE, so evaluate right side of ":")
EVALUATE k=1-k (1-0)
END k=1 
(do stuff and loop again)
EVALUATE k= k > 0 ? -k : 1 - k (TRUE, so evaluate left side of ":")
EVALUATE k = -k (-1)
END k=-1
(skipping ahead)
k=2
k=-2
k=3
k=-3

By starting at -2, the search will never land on any blocks within 1 vertical block of your cats. 

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

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



×
×
  • Create New...

Important Information

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