Last active
June 16, 2016 20:46
-
-
Save ifvictr/1cd0bf79d699d2ffb8a36f954753227b to your computer and use it in GitHub Desktop.
OddName for PocketMine-MP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @name OddName | |
* @main oddname\OddName | |
* @version 1.0.0 | |
* @api 1.12.0 | |
* @load POSTWORLD | |
* @author Gamecrafter | |
* @description Give your players odd names when they join! | |
* @link https://gist.github.com/Gamecrafter/1cd0bf79d699d2ffb8a36f954753227b | |
*/ | |
namespace oddname{ | |
use pocketmine\event\server\DataPacketReceiveEvent; | |
use pocketmine\event\Listener; | |
use pocketmine\network\protocol\Info; | |
use pocketmine\plugin\PluginBase; | |
class OddName extends PluginBase implements Listener{ | |
public function onEnable(){ | |
$this->getServer()->getPluginManager()->registerEvents($this, $this); | |
} | |
/** | |
* @return string | |
*/ | |
public function generateName(){ | |
$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; | |
$out = ""; | |
for($i = 0; $i < 16; $i++){ | |
$out .= $chars[mt_rand(0, strlen($chars) - 1)]; | |
} | |
return $out; //There's always a chance that it'll generate a name already in use, very small chance of that happening | |
} | |
/** | |
* @param DataPacketReceiveEvent $event | |
* @priority HIGHEST | |
* @ignoreCancelled true | |
*/ | |
public function onDataPacketReceive(DataPacketReceiveEvent $event){ | |
$packet = $event->getPacket(); | |
if($packet->pid() === Info::LOGIN_PACKET){ | |
$packet->username = $this->generateName(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment