Last active
November 14, 2017 04:59
-
-
Save iksaku/28f97f086e22b692fa6f to your computer and use it in GitHub Desktop.
Little PocketMine-MP Script to randomize player's skins
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 SkinR | |
| * @main SkinR\Loader | |
| * @version 1.0.0 | |
| * @api 1.12.0 | |
| * @description Little PocketMine-MP Script to randomize player's skins | |
| * @author iksaku | |
| */ | |
| namespace SkinR{ | |
| use pocketmine\event\Listener; | |
| use pocketmine\event\player\PlayerCreationEvent; | |
| use pocketmine\plugin\PluginBase; | |
| use SkinR\Utils\SkinR; | |
| use SkinR\Utils\SkinRPlayer; | |
| use SkinR\Utils\SkinRTask; | |
| class Loader extends PluginBase implements Listener{ | |
| public function onEnable(){ | |
| $this->getServer()->getPluginManager()->registerEvents($this, $this); | |
| $this->getServer()->getCommandMap()->register("SkinR", new SkinR($this)); | |
| $this->getServer()->getScheduler()->scheduleRepeatingTask(new SkinRTask($this), 6000); // Cada 5 minutos | |
| } | |
| /** | |
| * @param SkinRPlayer $player | |
| * @return bool | |
| */ | |
| public function randomSkin(SkinRPlayer $player){ | |
| if(count($this->getServer()->getOnlinePlayers()) < 2){ | |
| return false; | |
| } | |
| foreach($this->getServer()->getOnlinePlayers() as $p){ | |
| if($p !== $player && $p->isOnline() && $player->isOnline()){ | |
| $player->setSkin($p->getSkinData(), $p->isSkinSlim()); | |
| break; | |
| } | |
| } | |
| return true; | |
| } | |
| /** | |
| * @param PlayerCreationEvent $event | |
| * | |
| * @priority HIGHEST | |
| * @ignoreCancelled true | |
| */ | |
| public function onPlayerCreate(PlayerCreationEvent $event){ | |
| $event->setPlayerClass(SkinR::class); | |
| } | |
| } | |
| } | |
| namespace SkinR\Utils{ | |
| use pocketmine\network\SourceInterface; | |
| use pocketmine\Player; | |
| use pocketmine\scheduler\PluginTask; | |
| use SkinR\Loader; | |
| use pocketmine\command\Command; | |
| use pocketmine\command\CommandSender; | |
| use pocketmine\command\PluginIdentifiableCommand; | |
| abstract class BaseCommand extends Command implements PluginIdentifiableCommand{ | |
| /** @var Loader */ | |
| private $plugin; | |
| /** | |
| * @param Loader $plugin | |
| * @param string $name | |
| * @param string $description | |
| * @param null $usageMessage | |
| * @param array $aliases | |
| */ | |
| public function __construct(Loader $plugin, $name, $description = "", $usageMessage = null, array $aliases = []){ | |
| parent::__construct($name, $description, $usageMessage, $aliases); | |
| $this->plugin = $plugin; | |
| } | |
| /** | |
| * @return Loader | |
| */ | |
| public final function getPlugin(){ | |
| return $this->plugin; | |
| } | |
| } | |
| class SkinR extends BaseCommand{ | |
| public function __construct(Loader $plugin){ | |
| parent::__construct($plugin, "skinr", "Change your skin to a random one!", "/skinr [restore]", []); | |
| } | |
| public function execute(CommandSender $sender, $alias, array $args){ | |
| if(!$sender instanceof SkinRPlayer){ | |
| $sender->sendMessage("You can't acquire a random skin!"); | |
| return false; | |
| } | |
| if(isset($args[0]) && $args[0] === "restore"){ | |
| if(!$sender->restoreSkin()){ | |
| $sender->sendMessage("Something went wrong while restoring your skin D:"); | |
| return false; | |
| } | |
| $sender->sendMessage("Successfully restored your skin :D"); | |
| }else{ | |
| if(!$this->getPlugin()->randomSkin($sender)){ | |
| $sender->sendMessage("Something went wrong while changing your skin D:"); | |
| return false; | |
| } | |
| $sender->sendMessage("Successfully changed your skin :D"); | |
| } | |
| return true; | |
| } | |
| } | |
| class SkinRPlayer extends Player{ | |
| /** | |
| * @param SourceInterface $interface | |
| * @param null $clientID | |
| * @param string $ip | |
| * @param int $port | |
| */ | |
| public function __construct(SourceInterface $interface, $clientID, $ip, $port){ | |
| parent::__construct($interface, $clientID, $ip, $port); | |
| } | |
| /** @var null|array */ | |
| protected $realSkin = null; | |
| /** | |
| * @return bool | |
| */ | |
| public function restoreSkin(){ | |
| if($this->realSkin === null){ | |
| return false; | |
| } | |
| $this->setSkin($this->realSkin[0], $this->realSkin[1]); | |
| return true; | |
| } | |
| /** | |
| * @param string $skin | |
| * @param bool $isSlim | |
| */ | |
| public function setSkin($skin, $isSlim = false){ | |
| if($this->realSkin === null){ | |
| $this->realSkin = [$this->getSkinData(), $this->isSkinSlim()]; | |
| } | |
| parent::setSkin($skin, $isSlim); | |
| } | |
| } | |
| abstract class BaseTask extends PluginTask{ | |
| /** @var Loader $plugin */ | |
| private $plugin; | |
| /** | |
| * @param Loader $plugin | |
| */ | |
| public function __construct(Loader $plugin){ | |
| parent::__construct($plugin); | |
| $this->plugin = $plugin; | |
| } | |
| /** | |
| * @return Loader | |
| */ | |
| public final function getPlugin(){ | |
| return $this->plugin; | |
| } | |
| } | |
| class SkinRTask extends BaseTask{ | |
| /** | |
| * @param Loader $plugin | |
| */ | |
| public function __construct(Loader $plugin){ | |
| parent::__construct($plugin); | |
| } | |
| /** | |
| * @param int $currentTick | |
| */ | |
| public function onRun($currentTick){ | |
| foreach($this->getPlugin()->getServer()->getOnlinePlayers() as $p){ | |
| /** @var SkinRPlayer $p */ | |
| if($p->isOnline()){ | |
| if(!$this->getPlugin()->randomSkin($p)){ | |
| $p->sendMessage("Something went wrong while changing your skin D"); | |
| }else{ | |
| $p->sendMessage("Successfully changed your skin :D"); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment