-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventListener.php
90 lines (85 loc) · 3.78 KB
/
EventListener.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
<?php
namespace MonoAdrian23\FunBoats;
use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityDeathEvent;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerQuitEvent;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\network\mcpe\protocol\{
InteractPacket, InventoryTransactionPacket, MoveActorAbsolutePacket as MoveEntityAbsolutePacket, PlayerInputPacket, SetActorMotionPacket as SetEntityMotionPacket, AnimatePacket
};
class EventListener implements Listener{
/**
* @param PlayerQuitEvent $event
*/
public function onPlayerQuitEvent(PlayerQuitEvent $event) : void{
$player = $event->getPlayer();
if($player->getDataFlag(Entity::DATA_FLAGS, Entity::DATA_FLAG_RIDING)){
foreach($player->getLevel()->getNearbyEntities($player->getBoundingBox()->expand(2, 2, 2), $player) as $key => $entity){
if($entity instanceof BoatEntity && $entity->unlink($player)){
return;
}
}
}
}
/**
* @param EntityDeathEvent $event
* @priority MONITOR
*/
public function onDeath(EntityDeathEvent $event): void
{
if($event->getEntity() instanceof BoatEntity){
$event->setDrops([]);
}
}
/**
* @param DataPacketReceiveEvent $event
*/
public function onDataPacketReceiveEvent(DataPacketReceiveEvent $event) : void{
$packet = $event->getPacket();
$player = $event->getPlayer();
if($packet instanceof InventoryTransactionPacket && $packet->transactionType === InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY){
$entity = $player->getLevel()->getEntity($packet->trData->entityRuntimeId);
if($entity instanceof BoatEntity){
if($packet->trData->actionType === InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_INTERACT){
if($entity->canLink($player)){
$entity->link($player);
}
$event->setCancelled();
}
}
}elseif($packet instanceof InteractPacket){
$entity = $player->getLevel()->getEntity($packet->target);
if($entity instanceof BoatEntity){
if($packet->action === InteractPacket::ACTION_LEAVE_VEHICLE && $entity->isRider($player)){
$entity->unlink($player);
}
$event->setCancelled();
}
}elseif($packet instanceof MoveEntityAbsolutePacket){
$entity = $player->getLevel()->getEntity($packet->entityRuntimeId);
if($entity instanceof BoatEntity && $entity->isRider($player)){
$entity->absoluteMove($packet->position, $packet->xRot, $packet->zRot);
$event->setCancelled();
}
}elseif($packet instanceof AnimatePacket){
foreach($player->getLevel()->getEntities() as $entity){
if($entity instanceof BoatEntity && $entity->isRider($player)){
switch($packet->action){
case BoatEntity::ACTION_ROW_RIGHT:
case BoatEntity::ACTION_ROW_LEFT:
$entity->handleAnimatePacket($packet);
$event->setCancelled();
break;
}
break;
}
}
}elseif($packet instanceof PlayerInputPacket || $packet instanceof SetEntityMotionPacket){
if($player->getDataFlag(Entity::DATA_FLAGS, Entity::DATA_FLAG_RIDING)){
//TODO: Handle PlayerInputPacket and SetEntityMotionPacket
$event->setCancelled();
}
}
}
}