the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "net.minecraft.world.entity.ai.control.h"
3#include "net.minecraft.world.entity.ai.navigation.h"
4#include "net.minecraft.world.entity.h"
5#include "net.minecraft.world.entity.animal.h"
6#include "net.minecraft.world.level.h"
7#include "net.minecraft.world.phys.h"
8#include "FollowOwnerGoal.h"
9
10FollowOwnerGoal::FollowOwnerGoal(TamableAnimal *tamable, double speedModifier, float startDistance, float stopDistance)
11{
12 owner = weak_ptr<Mob>();
13 timeToRecalcPath = 0;
14 oldAvoidWater = false;
15
16 this->tamable = tamable;
17 level = tamable->level;
18 this->speedModifier = speedModifier;
19 navigation = tamable->getNavigation();
20 this->startDistance = startDistance;
21 this->stopDistance = stopDistance;
22 setRequiredControlFlags(Control::MoveControlFlag | Control::LookControlFlag);
23}
24
25bool FollowOwnerGoal::canUse()
26{
27 shared_ptr<LivingEntity> owner = dynamic_pointer_cast<LivingEntity>( tamable->getOwner() );
28 if (owner == NULL) return false;
29 if (tamable->isSitting()) return false;
30 if (tamable->distanceToSqr(owner) < startDistance * startDistance) return false;
31 this->owner = weak_ptr<LivingEntity>(owner);
32 return true;
33}
34
35bool FollowOwnerGoal::canContinueToUse()
36{
37 return owner.lock() != NULL && !navigation->isDone() && tamable->distanceToSqr(owner.lock()) > stopDistance * stopDistance && !tamable->isSitting();
38}
39
40void FollowOwnerGoal::start()
41{
42 timeToRecalcPath = 0;
43 oldAvoidWater = tamable->getNavigation()->getAvoidWater();
44 tamable->getNavigation()->setAvoidWater(false);
45}
46
47void FollowOwnerGoal::stop()
48{
49 owner = weak_ptr<Mob>();
50 navigation->stop();
51 tamable->getNavigation()->setAvoidWater(oldAvoidWater);
52}
53
54void FollowOwnerGoal::tick()
55{
56 tamable->getLookControl()->setLookAt(owner.lock(), 10, tamable->getMaxHeadXRot());
57 if (tamable->isSitting()) return;
58
59 if (--timeToRecalcPath > 0) return;
60 timeToRecalcPath = 10;
61
62 if (navigation->moveTo(owner.lock(), speedModifier)) return;
63 if (tamable->isLeashed()) return;
64 if (tamable->distanceToSqr(owner.lock()) < TeleportDistance * TeleportDistance) return;
65
66 // find a good spawn position nearby the owner
67 int sx = Mth::floor(owner.lock()->x) - 2;
68 int sz = Mth::floor(owner.lock()->z) - 2;
69 int y = Mth::floor(owner.lock()->bb->y0);
70 for (int x = 0; x <= 4; x++)
71 {
72 for (int z = 0; z <= 4; z++)
73 {
74 if (x >= 1 && z >= 1 && x <= 3 && z <= 3)
75 {
76 continue;
77 }
78 if (level->isTopSolidBlocking(sx + x, y - 1, sz + z) && !level->isSolidBlockingTile(sx + x, y, sz + z) && !level->isSolidBlockingTile(sx + x, y + 1, sz + z))
79 {
80 tamable->moveTo(sx + x + .5f, y, sz + z + .5f, tamable->yRot, tamable->xRot);
81 navigation->stop();
82 return;
83 }
84 }
85 }
86}