54 if(isset(self::$cache[$handler::class])){
55 return self::$cache[$handler::class];
59 $interface = new \ReflectionClass(PacketHandlerInterface::class);
60 $handlerReflect = new \ReflectionClass($handler);
62 foreach($interface->getMethods(\ReflectionMethod::IS_PUBLIC) as $handlerMethod){
64 $implementation = $handlerReflect->getMethod($handlerMethod->getName());
65 }
catch(\ReflectionException $e){
66 throw new AssumptionFailedError(
"PacketHandler implements PacketHandlerInterface, this should be impossible");
69 $packetArg = $implementation->getParameters()[0] ??
throw new AssumptionFailedError(
"PacketHandlerInterface method should always have a packet as the first argument");
70 $packetArgType = $packetArg->getType();
71 if(!$packetArgType instanceof \ReflectionNamedType){
74 $packetClass = $packetArgType->getName();
75 assert(is_a($packetClass, DataPacket::class,
true));
77 $implementor = $implementation->getDeclaringClass()->getName();
78 $whitelist[$packetClass] = $implementor !== PacketHandler::class ? PacketHandlerAction::HANDLED : PacketHandlerAction::DISCARD_WITH_DEBUG;
81 foreach($handlerReflect->getAttributes(SilentDiscard::class) as $attribute){
82 $info = $attribute->newInstance();
83 $packetClass = $info->packetClass;
84 if(isset($whitelist[$packetClass]) && $whitelist[$packetClass] !== PacketHandlerAction::DISCARD_WITH_DEBUG){
85 $shortName = (new \ReflectionClass($packetClass))->getShortName();
86 \GlobalLogger::get()->warning(
"#[SilentDiscard($shortName)] has no effect on " . $handler::class .
", as the handler for $shortName is implemented");
89 $whitelist[$packetClass] = PacketHandlerAction::DISCARD_SILENT;
93 foreach($whitelist as $packetClass => $action){
94 if($action === PacketHandlerAction::HANDLED){
95 $allowedPackets[] = (new \ReflectionClass($packetClass))->getShortName();
98 sort($allowedPackets, SORT_STRING);
99 \GlobalLogger::get()->debug(
"Packets handled by " . $handler::class .
": " . implode(
', ', $allowedPackets));
101 return self::$cache[$handler::class] = $whitelist;