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

Storing many IP's prevent overwriting

Discussion in 'Development' started by InspectorGadget, Dec 11, 2016.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi, I have been cracking my head on how to store multiple IPS. I have checked IPLogger by PEMapModder. I didn't understand it much as it was a little messy. I would like to store multiple ips in a ENUM config file format..
    This is what the config should say:
    PHP:
    InspectorGadget:
      - 
    ip1
      
    ip2
    I have done couple codes and the code still overwrites the config rather than adding a new line or IP.
    PHP:
    public function onJoin(PlayerJoinEvent $e) {
        
    $name $e->getPlayer()->getName();
        
    $ip $e->getPlayer()->getAddress();
    // Here's the part I don't get
       
    $this->ips = new Config($this->getDataFolder() . "ips.txt"Config::ENUM, array());
       
    // would it work like this?
       
    $this->ips->set($name$ips); // this overwrites the config if there's already a IP saved
    }
     
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    An ENUM config only has array keys but no values (values are filled as `true`).

    You can use them like this:

    (BTW, I don't understand why you want to reload the ips.txt every time a player joins instead of loading it in onEnable)
    PHP:
    // startup
    $this->ips = new Config(..., Config::ENUM);

    // want to know if the IP address $ip is already saved
    $this->ips->exists($ip);
    // if you are putting other strings instead of IP addresses here, you may want to add a second parameter `true`
    // to indicate that you are searching case-insensitively

    // player joins with a new or old IP adderss $ip
    $this->ips->set($ip);
    // you may also want to add a value if you want to save something else, such as the current timestamp


    // delete an IP address from record
    $this->ips->remove($ip);

    // get a list of all IP addresses
    $this->ips->getAll(true);
     
  3. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    This worked Thanks :)
    PHP:
    <?php

    namespace IP;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\Listener;

    class 
    Main extends PluginBase implements Listener {

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
          
            
    $this->ips = new Config($this->getDataFolder() . "ips.txt"Config::ENUM, array());
          
        }

        public function 
    onJoin(PlayerJoinEvent $e) {
            
    $player $e->getPlayer();
            
    $name $player->getName();
            
    $ip $player->getAddress();
          
            if(!
    $this->ips->exists($ip)) {
          
                
    $this->ips->set($ip);
                
    $this->getLogger()->info(" Works - Test");
                
    $this->ips->save();
          
            }
        }
      
        public function 
    onDisable() {
        }

    }
     
  4. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    How do I make it work like this
    Config should show:
    PHP:
    InspectorGadget:
      
    Ip1
      ip2
    And so on
     
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I don't think it is a good idea to save all players' data in the same file...
    You should use YAML for this purpose. Config->get($username) to get an array of IPs, manipulate using in_array and the []= array-pushing syntax, then Config->set($username, $ips).

    However I think using SQLite is better for this case if you want to save in the same file:
    PHP:
    $db = new SQLite3($this->getDataFolder() . "ips.db");
    $db->query("CREATE TABLE IF NOT EXISTS ips (username TEXT, ip TEXT)");

    $stmt $db->prepare("INSERT INTO ips (username, ip) VALUES (:name, :ip)");
    $stmt->bindValue(":name"$name);
    $stmt->bindValue(":ip"$ip);
    $stmt->execute();

    $stmt $db->prepare("SELECT ip FROM ips WHERE username = :name");
    $stmt->bindValue(":name"$name);
    $result $stmt->execute();
    $ips = [];
    while(
    is_array($row $stmt->fetchArray(SQLITE3_ASSOC))) $ips[] = $row["ip"];
     
  6. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Ok, if that YAML method doesn't work for me. I'll make YAML file for each and every player so it's easy and well organized.. Thanks
     
  7. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I have an array_push issue
    Error:
    PHP:
    11.12 05:56:28 [ServerINFO Warningarray_push() expects parameter 1 to be array, string given in /plugins/IP/src/IP/Main.php on line 29 11.12 05:56:28 [ServerINFO Warningin_array() expects parameter 2 to be array, boolean given in /plugins/IP/src/IP/Main.php on line 28 11.12 05:56:27 [MulticraftInspectorGadget[/60.48.79.97:14903ran command Message of the Day
    Code:
    PHP:
    <?php

    namespace IP;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\Listener;

    class 
    Main extends PluginBase implements Listener {

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
          
            
    $this->ips = new Config($this->getDataFolder() . "ips.yml"Config::YAML, array());
          
        }

        public function 
    onJoin(PlayerJoinEvent $e) {
            
    $player $e->getPlayer();
            
    $name $player->getName();
            
    $ip $player->getAddress();
          
            if(!
    $this->ips->exists(strtolower($name))) {
                
    $ips $this->ips->get(strtolower($name));
                    if(!(
    in_array($ip$ips))) {
                        
    array_push($ip$ips);
                        
    $this->ips->set($name$ips);
                        
    $this->ips->save();
                    }
            }
            else {
                
    $this->ips->set(strtolower($name, [$ip]));
                
    $this->ips->save();
                
    $this->getLogger()->warning("Worked!");
            }
                
    //$this->ips->set($name, $ip);
                //$this->getLogger()->info(" Works - Test");
                //$this->ips->save();
        
    }
      
        public function 
    onDisable() {
        }

    }
     
  8. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
    Code:
    array_push() expects parameter 1 to be array, string given
    In other words.....

    You added a string / a value that's not a array, It expected it to be a Array....
     
    SOFe likes this.
  9. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Is there any way to switch to array instead of string? I have checked https://php.net , nothing seemed to clear my question. It's killing me that a simple YAML array thing is doing this.. Can anyone correct me once?
     
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    It is unreasonable to switch types! You want to add $ip into $ips, so $ips should be the array and $ip should be the value! You swapped the two parameters! array_push($ips, $ip);

    Actually I don't understand why someone wants to use array_push(). You could simply have used this syntax:
    PHP:
    $ips[] = $ip;
    http://php.net/array-push

    Read this comment. http://php.net/manual/en/function.array-push.php#83388
     
    HimbeersaftLP and Sandertv like this.
  11. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Got it, Thanks. It worked
     
  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.