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.
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().
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.
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.
The data sets will prob be massive, judging by the number of players that were registered. But it's ok now.
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}
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.