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}
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);
This worked Thanks PHP: <?phpnamespace 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() { }}
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"];
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
I have an array_push issue Error: PHP: 11.12 05:56:28 [Server] INFO Warning: array_push() expects parameter 1 to be array, string given in /plugins/IP/src/IP/Main.php on line 29 11.12 05:56:28 [Server] INFO Warning: in_array() expects parameter 2 to be array, boolean given in /plugins/IP/src/IP/Main.php on line 28 11.12 05:56:27 [Multicraft] InspectorGadget[/60.48.79.97:14903] ran command Message of the Day Code: PHP: <?phpnamespace 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() { }}
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....
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?
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