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

Efficient position checking (and databases)

Comments in 'Plugin Development' started by Tethered_, Dec 30, 2014.

  1. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I'd like to store in a SQLite3 database 2 points: x1, y1, x2, y2, which each stand for corners of an area. I'd like to be able to check if a player is within this area and send them a message if they are. I'd have multiple areas or plots in the database. What's the most efficient way to do this besides iterating through an array during each player movement (which would most likely cause lag?).
  2. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    it would really cause lag at all especially in PlayerMoveEvent due to my experiences with ChunkProtector and MiniFactions :p
    iksaku likes this.
  3. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    for instance i query for data every time the player moves which is not advised thats why im making a way to do this efficiently. some says you can achieve this by storing these data in memory or get this data asynchronously.
  4. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    How can I define a new event that is checked only when a player's floored x or y coordinates change?
  5. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    PlayerMoveEvent
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No need to define. Use a PluginTask and check like every 20 ticks. And use sessioning to check the player's last position.

    Use this SQLite3 statement:
    SELECT owner FROM areas WHERE xmin <= $x AND $x <= xmax;
    Tethered_ likes this.
  7. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    How do you use PluginTask?
  8. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    Brilliant...
  9. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    Nevermind, PluginTask is scheduling lol. Also, is there any particular reason you used SELECT owner and not SELECT *?
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Because we don't need those data maybe.
  11. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    Eurika! Finally i got a semi solution to lessen the lag.
    create a temp session.
    Code:
    private $lastCords = array();
    
    public function onPlayerMove(PlayerMoveEvent $ev){
         $p = $ev->getPlayer();
         $x = intval("your floor mapping code stuff for x");
         $z = intval("your floor mapping code stuff for z");
         if(isset($this->lastCords[$p->getID()])){
              if($this->lastCords[$p->getID()] !== array($x, $z)){
                   unset($this->lastCords[$p->getID()]);
                   $this->lastCords[$p->getID()] = array($x, $z);
                   //do your code here
              }
         }else{
              $this->lastCords[$p->getID()] = array($x, $z);
         }
    }
    
    Tethered_ likes this.
  12. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    this method will only query new data if the floor map code doesnt match with the previous floor map code. In this away, it doesnt query data per tick or player movement.
    Finally a better solution xD implementing it on my project too now.
  13. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I used scheduling as per @PEMapModder's suggestion. I have no way to test lag, but it seems to work for the moment!
  14. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I'm sure that using an event such as PlayerMoveEvent will definitely lag the server as it updates so often... I'll have to compare our code further to find out though.
  15. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    Use EntityMoveEvent to handle just change of full position (From block-to-block movement)
  16. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    PlayerMoveEvent :p
  17. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    well, my method has no delays in getting the current data. but its up to you if you want delays without lag or lag without delays. (atm, the TPS improved from 2-5 to 10-12 on my method at 40 players online).
  18. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    Sorry I meant EntityMotionEvent :p
  19. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    what does EntityMotionEvent do? more like PlayerMoveEvent?
  20. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    It only handles when he makes full position change, for example, his XYZ is 1,1,1 and PlayerMoveEvent is triggered when anything changes, from Head rotation, to decimals of position (1.1,1,1.9) and the EntityMotionEvent is just if the position changes to something like 1,1,2

Share This Page

Advertisement