Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PlayerChatEvent runs twice?

Comments in 'Plugin Development' started by Timmpann, Jul 31, 2015.

  1. Timmpann
    Offline

    Timmpann New Member

    Joined:
    Feb 5, 2015
    Posts:
    24
    Minecraft User:
    Timmpann
    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')");
        }
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Is that all your code in PlayerChatEvent? And did you like, handle PlayerCommandPreprocessEvent?
  3. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    maybe u dont set a primary key in mysql database
    Last edited: Jul 31, 2015
  4. Timmpann
    Offline

    Timmpann New Member

    Joined:
    Feb 5, 2015
    Posts:
    24
    Minecraft User:
    Timmpann
    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.
  5. Timmpann
    Offline

    Timmpann New Member

    Joined:
    Feb 5, 2015
    Posts:
    24
    Minecraft User:
    Timmpann
    I've done that
  6. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    can u show us your plugin list ? i think its a problem with some other functions or name declarations...
  7. Timmpann
    Offline

    Timmpann New Member

    Joined:
    Feb 5, 2015
    Posts:
    24
    Minecraft User:
    Timmpann
    The only other plugin running on the dev server is DevTools.
  8. Timmpann
    Offline

    Timmpann New Member

    Joined:
    Feb 5, 2015
    Posts:
    24
    Minecraft User:
    Timmpann
    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");
        }
  9. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    How did you create the table? And another other queries in your plugin?
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    BTW this simple chat message can kill you:
    Code:
    '); DROP TABLE messages; -- 
    exploits_of_a_mom.png
  12. Timmpann
    Offline

    Timmpann New Member

    Joined:
    Feb 5, 2015
    Posts:
    24
    Minecraft User:
    Timmpann
    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! :)
  13. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    Why can't i break out of string on chat input like ". die ."
  14. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    omg. imagine the lag with that :eek:
    iksaku likes this.
  15. Timmpann
    Offline

    Timmpann New Member

    Joined:
    Feb 5, 2015
    Posts:
    24
    Minecraft User:
    Timmpann
    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)."')");
    HotFireyDeath and PEMapModder like this.

Share This Page

Advertisement