Skip to content

Instantly share code, notes, and snippets.

@ifvictr
Last active June 16, 2016 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifvictr/1cd0bf79d699d2ffb8a36f954753227b to your computer and use it in GitHub Desktop.
Save ifvictr/1cd0bf79d699d2ffb8a36f954753227b to your computer and use it in GitHub Desktop.
OddName for PocketMine-MP.
<?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