I am making a lightweight login plugin for my server, but I keep receiving an error about how the server can't get access to the Passwords.yml save file Here is the plugin: PHP: <?phpnamespace jasonwynn10;use pocketmine\command\Command;use pocketmine\command\CommandSender;use pocketmine\event\Listener;use pocketmine\event\player\PlayerChatEvent;use pocketmine\event\player\PlayerInteractEvent;use pocketmine\event\player\PlayerJoinEvent;//use pocketmine\event\player\PlayerMoveEvent;use pocketmine\event\player\PlayerQuitEvent;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\utils\Config;use pocketmine\utils\TextFormat as TF;class Main extends PluginBase implements Listener { private $loggedIn = []; private $block = []; /** @var Config */ private $config; public function onEnable() { if(file_exists($this->getDataFolder()."login.tmp")) { $tmp = new Config($this->getDataFolder()."login.tmp",Config::YAML,[]); $time = $tmp->get("time",time()); if($time <= time()+900) { $all = $tmp->getAll(); foreach ($all as $user) { if($user != "time") { $this->loggedIn[] = $user; } } } } @mkdir($this->getDataFolder()."Passwords.yml"); $this->config = new Config($this->getDataFolder()."Passwords.yml",Config::YAML,[]); $this->getLogger()->notice(TF::GREEN."Enabled!"); } public function onJoin(PlayerJoinEvent $ev) { $p = $ev->getPlayer(); if($p->hasPlayedBefore()) { if(array_search($p->getName(),$this->loggedIn) != false) { $p->sendMessage(TF::GREEN."You have been automatically logged in due to server reload"); return; } $p->sendMessage(TF::YELLOW."Please login using".TF::AQUA." /login <password>"); }else{ $p->sendMessage(TF::GREEN."Please register for an account using /reg <password> <password>"); } } public function chatBlocker(PlayerChatEvent $ev) { $tmp = []; foreach ($ev->getRecipients() as $player) { foreach ($this->block as $blocked) { if($player->getName() !== $blocked) { $tmp[] = $player; } } } $ev->setRecipients($tmp); } public function onLogout(PlayerQuitEvent $ev) { $p = $ev->getPlayer(); unset($this->loggedIn[$p->getName()]); } public function onCommand(CommandSender $sender, Command $command, $label, array $args) { if($command == "l" or $command == "login") { if(!$sender instanceof Player) { return false; } if(count($args) == 0) { return false; } if(is_string($args[0])) { if(!$this->login($sender->getName(),$args[0])) { $sender->sendMessage(TF::RED."Failed to login. Please try again"); } }else{ return false; } } if($command == "reg" or $command == "register") { if(!$sender instanceof Player) { return false; } if(count($args) == 0) { return false; } if($args[0] !== $args[1]) { $sender->sendMessage(TF::RED."Your passwords don't match!"); return false; }else{ if($this->register($sender->getName(),$args[0])) { $sender->sendMessage(TF::RED."Failed to Register. Please try again"); } } } return true; } private function register(string $user, string $password) { $password = base64_encode($password); $this->config->set($user,$password); if($this->config->get($user,"") == $password) { return true; } return false; } private function login(string $user, string $password) { $saved = $this->config->get($user,""); $saved = base64_decode($saved); if($password == $saved) { $this->loggedIn[] = $user; if(array_search($user,$this->loggedIn) == false) { return false; } unset($this->block[$user]); return true; } return false; } // can be commented out to allow players to move without being logged in /* * public function onMove(PlayerMoveEvent $ev) { $p = $ev->getPlayer(); $n = $p->getName(); if(array_search($n,$this->loggedIn) == false) { $ev->setCancelled(); } } */ public function onInteract(PlayerInteractEvent $ev) { $p = $ev->getPlayer(); $n = $p->getName(); if(array_search($n,$this->loggedIn) == false) { $ev->setCancelled(); $p->sendMessage(TF::YELLOW."Please login using".TF::AQUA." /login <password>"); } } public function onDisable() { $tmp = new Config($this->getDataFolder()."login.tmp",Config::YAML,$this->loggedIn); $tmp->set("time",time()); //in case of server reload $this->getLogger()->notice(TF::GREEN."Disabled!"); }}
Change PHP: @mkdir($this->getDataFolder()."Passwords.yml"); To PHP: @mkdir($this->getDataFolder()."Passwords"); //you can't create files with mkdir Then to make the file/access it use PHP: $this->config = new Config($this->getDataFolder()."Passwords/passwords.yml",Config::YAML,[]);
Hint 1: Never save a large amount of user data in a single file. When you get more files it will take a long time to read/write data there. Hint 2: Never store passwords directly. base64 is not for storing passwords; it is reversible. Use password_hash() instead for storing passwords, and use password_verify() to check them.
The plugin is supposed to be very lightweight. I'm trying to make it all in one file like I have done so far
The not accessible part may be due to not executing the command with 'Sudo' on Linux. I always had to do Code: sudo ./start.sh with my PMMP server. I fixed the problem with a simple Code: chmod