1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

[Solved] File not accessible error?

Discussion in 'Development' started by jasonwynn10, Dec 9, 2016.

  1. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    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:
    <?php
    namespace 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 $senderCommand $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 $userstring $password) {
            
    $password base64_encode($password);
            
    $this->config->set($user,$password);
            if(
    $this->config->get($user,"") == $password) {
                return 
    true;
            }
            return 
    false;
        }

        private function 
    login(string $userstring $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!");
        }
    }
     
  2. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    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,[]);
     
    jasonwynn10 likes this.
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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.
     
    jasonwynn10 and HimbeersaftLP like this.
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    The plugin is supposed to be very lightweight. I'm trying to make it all in one file like I have done so far
     
  5. Magicode

    Magicode Baby Zombie

    Messages:
    183
    GitHub:
    magicode1
    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
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    No, it's because a directory of the same name exists.
     
    Magicode likes this.
  7. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    that's off topic. I'm on windows 10
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.