1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#include "stdafx.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.phys.h"
#include "LeashFenceKnotEntity.h"
void LeashFenceKnotEntity::_init()
{
defineSynchedData();
}
LeashFenceKnotEntity::LeashFenceKnotEntity(Level *level) : HangingEntity(level)
{
_init();
}
LeashFenceKnotEntity::LeashFenceKnotEntity(Level *level, int xTile, int yTile, int zTile) : HangingEntity(level, xTile, yTile, zTile, 0)
{
_init();
setPos(xTile + .5, yTile + .5, zTile + .5);
}
void LeashFenceKnotEntity::defineSynchedData()
{
HangingEntity::defineSynchedData();
}
void LeashFenceKnotEntity::setDir(int dir)
{
// override to do nothing, knots don't have directions
}
int LeashFenceKnotEntity::getWidth()
{
return 9;
}
int LeashFenceKnotEntity::getHeight()
{
return 9;
}
bool LeashFenceKnotEntity::shouldRenderAtSqrDistance(double distance)
{
return distance < 32 * 32;
}
void LeashFenceKnotEntity::dropItem(shared_ptr<Entity> causedBy)
{
}
bool LeashFenceKnotEntity::save(CompoundTag *entityTag)
{
// knots are not saved, they are recreated by the entities that are tied
return false;
}
void LeashFenceKnotEntity::addAdditonalSaveData(CompoundTag *tag)
{
}
void LeashFenceKnotEntity::readAdditionalSaveData(CompoundTag *tag)
{
}
bool LeashFenceKnotEntity::interact(shared_ptr<Player> player)
{
shared_ptr<ItemInstance> item = player->getCarriedItem();
bool attachedMob = false;
if (item != NULL && item->id == Item::lead_Id)
{
if (!level->isClientSide)
{
// look for entities that can be attached to the fence
double range = 7;
vector<shared_ptr<Entity> > *mobs = level->getEntitiesOfClass(typeid(Mob), AABB::newTemp(x - range, y - range, z - range, x + range, y + range, z + range));
if (mobs != NULL)
{
for(auto& it : *mobs)
{
shared_ptr<Mob> mob = dynamic_pointer_cast<Mob>( it );
if (mob->isLeashed() && mob->getLeashHolder() == player)
{
mob->setLeashedTo(shared_from_this(), true);
attachedMob = true;
}
}
delete mobs;
}
}
}
if (!level->isClientSide && !attachedMob)
{
remove();
if (player->abilities.instabuild)
{
// if the player is in creative mode, attempt to remove all leashed mobs without dropping additional items
double range = 7;
vector<shared_ptr<Entity> > *mobs = level->getEntitiesOfClass(typeid(Mob), AABB::newTemp(x - range, y - range, z - range, x + range, y + range, z + range));
if (mobs != NULL)
{
for(auto& it : *mobs)
{
shared_ptr<Mob> mob = dynamic_pointer_cast<Mob>( it );
if (mob->isLeashed() && mob->getLeashHolder() == shared_from_this())
{
mob->dropLeash(true, false);
}
}
delete mobs;
}
}
}
return true;
}
bool LeashFenceKnotEntity::survives()
{
// knots are placed on top of fence tiles
int tile = level->getTile(xTile, yTile, zTile);
if (Tile::tiles[tile] != NULL && Tile::tiles[tile]->getRenderShape() == Tile::SHAPE_FENCE)
{
return true;
}
return false;
}
shared_ptr<LeashFenceKnotEntity> LeashFenceKnotEntity::createAndAddKnot(Level *level, int x, int y, int z)
{
shared_ptr<LeashFenceKnotEntity> knot = shared_ptr<LeashFenceKnotEntity>( new LeashFenceKnotEntity(level, x, y, z) );
knot->forcedLoading = true;
level->addEntity(knot);
return knot;
}
shared_ptr<LeashFenceKnotEntity> LeashFenceKnotEntity::findKnotAt(Level *level, int x, int y, int z)
{
vector<shared_ptr<Entity> > *knots = level->getEntitiesOfClass(typeid(LeashFenceKnotEntity), AABB::newTemp(x - 1.0, y - 1.0, z - 1.0, x + 1.0, y + 1.0, z + 1.0));
if (knots != NULL)
{
for (auto& it : *knots )
{
shared_ptr<LeashFenceKnotEntity> knot = dynamic_pointer_cast<LeashFenceKnotEntity>( it );
if (knot->xTile == x && knot->yTile == y && knot->zTile == z)
{
delete knots;
return knot;
}
}
delete knots;
}
return nullptr;
}
|