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.goal.h"
3#include "net.minecraft.world.entity.h"
4#include "net.minecraft.world.level.h"
5#include "SynchedEntityData.h"
6#include "ParticleTypes.h"
7#include "TamableAnimal.h"
8
9TamableAnimal::TamableAnimal(Level *level) : Animal(level)
10{
11 sitGoal = new SitGoal(this);
12}
13
14TamableAnimal::~TamableAnimal()
15{
16 if(sitGoal != NULL) delete sitGoal;
17}
18
19void TamableAnimal::defineSynchedData()
20{
21 Animal::defineSynchedData();
22 entityData->define(DATA_FLAGS_ID, (byte) 0);
23 entityData->define(DATA_OWNERUUID_ID, L"");
24}
25
26void TamableAnimal::addAdditonalSaveData(CompoundTag *tag)
27{
28 Animal::addAdditonalSaveData(tag);
29#ifdef _XBOX_ONE
30 // 4J Stu Added from later Java version to remove owners from save transfer saves. We will probably want this on other platforms in the future
31 if (getOwnerUUID().empty())
32 {
33 tag->putString(L"OwnerUUID", L"");
34 }
35 else
36 {
37 tag->putString(L"OwnerUUID", getOwnerUUID());
38 }
39#else
40 if (getOwnerUUID().empty())
41 {
42 tag->putString(L"Owner", L"");
43 }
44 else
45 {
46 tag->putString(L"Owner", getOwnerUUID());
47 }
48#endif
49 tag->putBoolean(L"Sitting", isSitting());
50}
51
52void TamableAnimal::readAdditionalSaveData(CompoundTag *tag)
53{
54 Animal::readAdditionalSaveData(tag);
55#ifdef _XBOX_ONE
56 // 4J Stu Added from later Java version to remove owners from save transfer saves. We will probably want this on other platforms in the future
57 wstring owner = L"";
58 if(tag->contains(L"OwnerUUID") )
59 {
60 owner = tag->getString(L"OwnerUUID");
61 }
62#else
63 wstring owner = tag->getString(L"Owner");
64#endif
65 if (owner.length() > 0)
66 {
67 setOwnerUUID(owner);
68 setTame(true);
69 }
70 sitGoal->wantToSit(tag->getBoolean(L"Sitting"));
71 setSitting(tag->getBoolean(L"Sitting"));
72}
73
74void TamableAnimal::spawnTamingParticles(bool success)
75{
76 ePARTICLE_TYPE particle = eParticleType_heart;
77 if (!success)
78 {
79 particle = eParticleType_smoke;
80 }
81 for (int i = 0; i < 7; i++)
82 {
83 double xa = random->nextGaussian() * 0.02;
84 double ya = random->nextGaussian() * 0.02;
85 double za = random->nextGaussian() * 0.02;
86 level->addParticle(particle, x + random->nextFloat() * bbWidth * 2 - bbWidth, y + .5f + random->nextFloat() * bbHeight, z + random->nextFloat() * bbWidth * 2 - bbWidth, xa, ya, za);
87 }
88}
89
90void TamableAnimal::handleEntityEvent(byte id)
91{
92 if (id == EntityEvent::TAMING_SUCCEEDED)
93 {
94 spawnTamingParticles(true);
95 }
96 else if (id == EntityEvent::TAMING_FAILED)
97 {
98 spawnTamingParticles(false);
99 }
100 else
101 {
102 Animal::handleEntityEvent(id);
103 }
104}
105
106bool TamableAnimal::isTame()
107{
108 return (entityData->getByte(DATA_FLAGS_ID) & 0x04) != 0;
109}
110
111void TamableAnimal::setTame(bool value)
112{
113 byte current = entityData->getByte(DATA_FLAGS_ID);
114 if (value)
115 {
116 entityData->set(DATA_FLAGS_ID, (byte) (current | 0x04));
117 }
118 else
119 {
120 entityData->set(DATA_FLAGS_ID, (byte) (current & ~0x04));
121 }
122}
123
124bool TamableAnimal::isSitting()
125{
126 return (entityData->getByte(DATA_FLAGS_ID) & 0x01) != 0;
127}
128
129void TamableAnimal::setSitting(bool value)
130{
131 byte current = entityData->getByte(DATA_FLAGS_ID);
132 if (value)
133 {
134 entityData->set(DATA_FLAGS_ID, (byte) (current | 0x01));
135 }
136 else
137 {
138 entityData->set(DATA_FLAGS_ID, (byte) (current & ~0x01));
139 }
140}
141
142wstring TamableAnimal::getOwnerUUID()
143{
144 return entityData->getString(DATA_OWNERUUID_ID);
145}
146
147void TamableAnimal::setOwnerUUID(const wstring &name)
148{
149 entityData->set(DATA_OWNERUUID_ID, name);
150}
151
152shared_ptr<Entity> TamableAnimal::getOwner()
153{
154 return level->getPlayerByUUID(getOwnerUUID());
155}
156
157SitGoal *TamableAnimal::getSitGoal()
158{
159 return sitGoal;
160}
161
162bool TamableAnimal::wantsToAttack(shared_ptr<LivingEntity> target, shared_ptr<LivingEntity> owner)
163{
164 return true;
165}
166
167Team *TamableAnimal::getTeam()
168{
169 if (isTame())
170 {
171 shared_ptr<LivingEntity> owner = dynamic_pointer_cast<LivingEntity>(getOwner());
172 if (owner != NULL)
173 {
174 return owner->getTeam();
175 }
176 }
177 return Animal::getTeam();
178}
179
180bool TamableAnimal::isAlliedTo(shared_ptr<LivingEntity> other)
181{
182 if (isTame())
183 {
184 shared_ptr<LivingEntity> owner = dynamic_pointer_cast<LivingEntity>(getOwner());
185 if (other == owner)
186 {
187 return true;
188 }
189 if (owner != NULL)
190 {
191 return owner->isAlliedTo(other);
192 }
193 }
194 return Animal::isAlliedTo(other);
195}