Hi! I'm developing a plugin for my server that logs chatmessages to a mysql database. Everything works great, except that every message is getting logged twice. There is nothing in my code that does that. So, does Pocketmine run PlayerChatEvent twice? Why? How can i get my plugin to only log it one time? This is my code: PHP: public function onPlayerChat(PlayerChatEvent $event){ $player = $event->getPlayer()->getDisplayName(); $message = $event->getMessage(); $this->db->query("INSERT INTO messages (name, message) VALUES ('$player', '$message')"); }
Yes, thats all my code in PlayerChatEvent. What should I do with PlayerCommandPreprocessEvent? I tried moving my code from PlayerChatEvent to PlayerCommandPreprocessEvent and it gave the same result, adding 2 messages to the database.
can u show us your plugin list ? i think its a problem with some other functions or name declarations...
I came up with a workaround. Now it deletes every entry that have an odd ID, that way only one entry will be saved on each chatevent. Still weird that it runs twice, but it works, lol! PHP: public function onPlayerChat(PlayerChatEvent $event){ $player = $event->getPlayer()->getDisplayName(); $message = $event->getMessage(); $this->db->query("INSERT INTO messages (name, message) VALUES ('$player', '$message')"); $this->db->query("DELETE FROM messages WHERE id mod 2 = 1"); }
This query runs at onEnable. This is also the only other query in the plugin. Code: CREATE TABLE IF NOT EXISTS messages ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), message VARCHAR(255), timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); OMG, why didn't i think of that. Thats sooo fucking stupid of me! Going to fix that now, thanks for pointing it out!
This should be safe against SQL injection, right? PHP: $this->db->query("INSERT INTO messages (name, message) VALUES ('".$this->db->escape_string($player)."', '".$this->db->escape_string($message)."')");