-
Notifications
You must be signed in to change notification settings - Fork 6
/
PMUtils.php
67 lines (55 loc) · 2.2 KB
/
PMUtils.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
<?php
namespace libproxy;
use Exception;
use pocketmine\network\mcpe\convert\TypeConverter;
use pocketmine\network\mcpe\EntityEventBroadcaster;
use pocketmine\network\mcpe\PacketBroadcaster;
use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\raklib\RakLibInterface;
use pocketmine\Server;
use ReflectionClass;
use function method_exists;
class PMUtils
{
public static function getPacketBroadcaster(Server $server): PacketBroadcaster
{
if (method_exists($server, 'getPacketBroadcaster')) {
return $server->getPacketBroadcaster(ProtocolInfo::CURRENT_PROTOCOL);
}
$packetBroadcaster = self::getRaklibInterfacePropertyValue($server, 'packetBroadcaster');
if ($packetBroadcaster instanceof PacketBroadcaster) {
return $packetBroadcaster;
}
throw new Exception("PacketBroadcaster isn't valid");
}
public static function getEntityEventBroadcaster(Server $server): EntityEventBroadcaster
{
if (method_exists($server, 'getEntityEventBroadcaster')) {
return $server->getEntityEventBroadcaster(self::getPacketBroadcaster($server), TypeConverter::getInstance());
}
$entityEventBroadcaster = self::getRaklibInterfacePropertyValue($server, 'entityEventBroadcaster');
if ($entityEventBroadcaster instanceof EntityEventBroadcaster) {
return $entityEventBroadcaster;
}
throw new Exception("EntityEventBroadcaster isn't valid");
}
private static function getRaklibInterfacePropertyValue(Server $server, string $propertyName): mixed
{
$interface = self::getRaklibInterface($server);
$reflection = new ReflectionClass($interface);
$property = $reflection->getProperty($propertyName);
return $property->getValue($interface);
}
/**
* @throws Exception
*/
private static function getRaklibInterface(Server $server): RakLibInterface
{
foreach ($server->getNetwork()->getInterfaces() as $interface) {
if ($interface instanceof RakLibInterface) {
return $interface;
}
}
throw new Exception("Raklib interface hasn't been registered");
}
}