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

Executing SQL statements on BlockBreakEvent

Discussion in 'Help' started by DiamondGamer30, Jun 25, 2018.

  1. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    Is there a better way then executing SQL statements every time a player breaks a block? The purpose of this is to check if a block exists within a plot and the x-axis and z-axis are stored in a MySQL database. I don't want to make too many queries and lag the crap out of my server. If you are wondering what is the purpose, it is claims for factions.
     
  2. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    You could use MySQL to store the plot data, but load it all into memory when the plugin is enabled, update in memory when necessary (plots claimed, deleted), and save intermittently or in onDisable().
     
    corytortoise likes this.
  3. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    I mean as in like checking if the player is in a plot. I already cache plot data. I was told it is very inefficient to search data through variables. A better way is to identify the coords by searching it via MySQL database. But I would have to do that every time a player breaks block so is there a better way? That’s what I’m asking. Btw I already store things in MySQL.
     
  4. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Who told you that? For massive data sets using memory is clearly not a good solution, but your plot data wouldn't take much memory and it is much faster to retrieve data directly from RAM, which is actually what happens when you use SQLite. If you create an array of plot data from MySQL when the plugin starts you'll be able to check on events such as BlockBreakEvent without lag as long as your plot-checking algorithm is efficient... To be sure, you could add some timings to your plugin and compare the 2 approaches.
     
  5. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    The data sets will prob be massive, judging by the number of players that were registered. But it's ok now.
     
  6. Primus

    Primus Zombie Pigman

    Messages:
    749
    That is what I do: MySQLDataProvider#163
     
    Awzaw likes this.
  7. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    Ooof, I needa deeply explain this. I'll show code to clarify what I mean.

    PHP:
    public function onBreak(BlockBreakEvent $event) {
        
    // mysql stuff. query = "SELECT faction FROM claims WHERE $x <= x1 AND $x >= x2 AND $z <= z1 AND $z >= z2;
        
    if(players faction is the faction) {
            
    allow them to break
            return;
        }
        
    $event->setCancelled();
        
    // To lazy to write things down but you should prob kn
    }
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Only load relevant data into memory. For example, you should only load the 32 blocks around the player; update it asynchronously as the player moves. If the player moves too far or breaks a distant block, he's probably hacking, and you can correctly block it. If the player just teleported, I think breaking a block after teleporting usually takes more time than downloading the nearby plot claims asynchronously.

    If your server is very massive and many players are sparsely located in the map, e.g. you have 1000 online players on a single server (is it even possible?) and you are looking for performance, you could construct an R-tree to store plot data. Look up Wikipedia for details. But I think that's an overkill if you have less than 100 players. Pho won't allow you to optimize very well anyway.
     
  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.