-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoatEntity.php
260 lines (221 loc) · 7.79 KB
/
BoatEntity.php
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace MonoAdrian23\FunBoats;
use pocketmine\{entity\EntityIds, math\Vector3, Player, Server};
use pocketmine\block\Planks;
use pocketmine\entity\Entity;
use pocketmine\entity\Vehicle;
use pocketmine\event\entity\{
EntityDamageByEntityEvent, EntityDamageEvent, EntityRegainHealthEvent
};
use pocketmine\item\Item;
use pocketmine\network\mcpe\protocol\{
ActorEventPacket as EntityEventPacket, SetActorLinkPacket as SetEntityLinkPacket, AnimatePacket, AddActorPacket
};
use pocketmine\network\mcpe\protocol\types\EntityLink;
class BoatEntity extends Vehicle{
public const NETWORK_ID = self::BOAT;
public const TAG_WOOD_ID = "WoodID";
public const ACTION_ROW_RIGHT = 128;
public const ACTION_ROW_LEFT = 129;
/** @var float */
public $height = 0.455;
/** @var float */
public $width = 1.4;
/** @var float */
public $gravity = 0.0;
/** @var float */
public $drag = 0.1;
/** @var Entity */
public $rider;
private $killed = false;
public function initEntity() : void{
parent::initEntity();
$woodId = $this->namedtag->getInt(self::TAG_WOOD_ID, Planks::OAK);
if($woodId > 5 || $woodId < 0){
$woodId = Planks::OAK;
}
$this->setWoodId($woodId);
$this->setMaxHealth(0.5);
$this->setGenericFlag(self::DATA_FLAG_STACKABLE, true);
}
public function saveNBT() : void{
parent::saveNBT();
$this->namedtag->setInt(self::TAG_WOOD_ID, $this->getWoodId());
}
/**
* @param EntityDamageEvent $source
*/
public function attack(EntityDamageEvent $source) : void{
parent::attack($source);
if(!$source->isCancelled() && !$this->killed){
$pk = new EntityEventPacket();
$pk->entityRuntimeId = $this->id;
$pk->event = EntityEventPacket::HURT_ANIMATION;
foreach ($this->getViewers() as $viewer){
$viewer->dataPacket($pk);
}
$this->kill();
}
}
/**
* Called by spawnTo() to send whatever packets needed to spawn the entity to the client.
*
* @param Player $player
* @override
*/
protected function sendSpawnPacket(Player $player) : void{
$pk = new AddActorPacket();
$pk->entityRuntimeId = $this->getId();
$pk->type = AddActorPacket::LEGACY_ID_MAP_BC[self::NETWORK_ID];
$pk->position = $this->asVector3();
$pk->motion = $this->getMotion();
$pk->yaw = $this->yaw;
$pk->headYaw = $this->yaw;
$pk->pitch = $this->pitch;
$pk->attributes = $this->attributeMap->getAll();
$pk->metadata = $this->propertyManager->getAll();
if($this->rider !== null){
$pk->links[] = new EntityLink($this->getId(), $this->rider->getId(), EntityLink::TYPE_RIDER, true, true);
}
$player->dataPacket($pk);
}
public function kill() : void{
parent::kill();
if($this->lastDamageCause instanceof EntityDamageByEntityEvent){
$damager = $this->lastDamageCause->getDamager();
if($damager instanceof Player and $damager->isCreative()){
return;
}
}
$this->killed = true;
foreach($this->getDrops() as $item){
$this->getLevel()->dropItem($this, $item->setCount(1));
}
}
/**
* @param int $currentTick
*
* @return bool
*/
public function onUpdate(int $currentTick) : bool{
if($this->closed){
return false;
}
//Regenerate health 1⁄10 per tick
if($this->getHealth() < $this->getMaxHealth() && $currentTick % 10 === 0){
$this->heal(new EntityRegainHealthEvent($this, 1, EntityRegainHealthEvent::CAUSE_REGEN));
}
return parent::onUpdate($currentTick);
}
/**
* @return Item[]
*/
public function getDrops() : array{
return [
new BoatItem($this->getWoodId())
];
}
/**
* @return int
*/
public function getWoodId() : int{
return $this->propertyManager->getInt(self::DATA_VARIANT);
}
/**
* @param int $woodId
*/
public function setWoodId(int $woodId) : void{
$this->propertyManager->setInt(self::DATA_VARIANT, $woodId);
}
/**
* @param Entity $rider
*
* @return bool
*/
public function canLink(Entity $rider) : bool{
return $this->rider === null;
}
/**
* @param Entity $rider
*
* @return bool
*/
public function link(Entity $rider) : bool{
if($this->rider === null){
$rider->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_RIDING, true);
//Set the rider seat position to y + 1
$rider->getDataPropertyManager()->setVector3(Entity::DATA_RIDER_SEAT_POSITION, new Vector3(0, 1, 0));
//Lock the rider rotation -90 to 90
$rider->getDataPropertyManager()->setByte(self::DATA_RIDER_ROTATION_LOCKED, true);
$rider->getDataPropertyManager()->setFloat(self::DATA_RIDER_MAX_ROTATION, 90);
$rider->getDataPropertyManager()->setFloat(self::DATA_RIDER_MIN_ROTATION, -90);
//Link entity to boat
$pk = new SetEntityLinkPacket();
$pk->link = new EntityLink($this->getId(), $rider->getId(), EntityLink::TYPE_RIDER, true, true);
Server::getInstance()->broadcastPacket($this->getViewers(), $pk);
$this->rider = $rider;
return true;
}
return false;
}
/**
* @param Entity $rider
*
* @return bool
*/
public function unlink(Entity $rider) : bool{
if($this->rider === $rider){
$rider->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_RIDING, false);
//Reset the rider seat position
$rider->getDataPropertyManager()->setVector3(Entity::DATA_RIDER_SEAT_POSITION, new Vector3(0, 0, 0));
//Unlock the rider rotation
$rider->getDataPropertyManager()->setByte(self::DATA_RIDER_ROTATION_LOCKED, false);
//Unlink entity from boat
$pk = new SetEntityLinkPacket();
$pk->link = new EntityLink($this->getId(), $rider->getId(), EntityLink::TYPE_REMOVE, true, true);
Server::getInstance()->broadcastPacket($this->getViewers(), $pk);
$this->rider = null;
return true;
}
return false;
}
/**
* @param Vector3 $pos
* @param float $yaw = 0
* @param float $pitch = 0
*/
public function absoluteMove(Vector3 $pos, float $yaw = 0, float $pitch = 0) : void{
$this->setComponents($pos->x, $pos->y, $pos->z);
$this->setRotation($yaw, $pitch);
$this->updateMovement();
}
/**
* @param AnimatePacket $packet
*/
public function handleAnimatePacket(AnimatePacket $packet) : void{
if($this->rider !== null){
switch($packet->action){
case self::ACTION_ROW_RIGHT:
$this->propertyManager->setFloat(self::DATA_PADDLE_TIME_RIGHT, $packet->float);
break;
case self::ACTION_ROW_LEFT:
$this->propertyManager->setFloat(self::DATA_PADDLE_TIME_LEFT, $packet->float);
break;
}
}
}
/**
* @return null|Entity
*/
public function getRider() : ?Entity{
return $this->rider;
}
/**
* @param Entity $rider
*
* @return bool
*/
public function isRider(Entity $rider) : bool{
return $this->rider === $rider;
}
}