how to use my Middleware
In onDetectPacket you can either set a LoginPacket::class or SetLocalPlayerAsInitializedPacket::class
what are the differences in action between the two packets?
Login Packet:
The Player class doesn't exist (very useful for a tempban synchronization system)
the SetLocalPlayerAsInitializedPacket:
the Player class does exist (widely used for a role or inventory synchronization system)
<?php
/*
*
* _____ _____ _ ______ _____ _ _ _____ _ _ _____
* /\ |_ _| __ \ | | | ____| /\ | __ \| \ | |_ _| \ | |/ ____|
* / \ | | | | | |______| | | |__ / \ | |__) | \| | | | | \| | | __
* / /\ \ | | | | | |______| | | __| / /\ \ | _ /| . ` | | | | . ` | | |_ |
* / ____ \ _| |_| |__| | | |____| |____ / ____ \| | \ \| |\ |_| |_| |\ | |__| |
* /_/ \_\_____|_____/ |______|______/_/ \_\_| \_\_| \_|_____|_| \_|\_____|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author AID-LEARNING
* @link https://github.com/AID-LEARNING
*
*/
declare(strict_types=1);
namespace SenseiTarzan\Exmple\Class\Middleware;
use Generator;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\network\mcpe\protocol\SetLocalPlayerAsInitializedPacket;
use SenseiTarzan\DataBase\Component\DataManager;
use SenseiTarzan\Middleware\Class\AttributeMiddlewarePriority;
use SenseiTarzan\Middleware\Class\IMiddleWare;
use SenseiTarzan\Middleware\Class\MiddlewarePriority;
#[AttributeMiddlewarePriority(MiddlewarePriority::LOWEST)]
class ExempleMiddleware implements IMiddleWare
{
public function getName() : string
{
return "Exemple Middleware";
}
/**
* @inheritDoc
*/
public function onDetectPacket() : string
{
return SetLocalPlayerAsInitializedPacket::class;
}
public function getPromise(DataPacketReceiveEvent $event) : Generator
{
return Await::promise(function ($resolve, $reject) use($event): void {
$player = $event->getOrigin()->getPlayer();
Await::f2c(function() use ($player) {
yield from request_async($player);
}, $resolve, $reject);
});
}
Last updated