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

[1.18.1] - Add custom goals to vanilla Entities


Davide_24
 Share

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

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

 Share



×
×
  • Create New...

Important Information

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