Solved Block Breaking and SQL

Discussion in 'Plugin Development' started by Nashor, Sep 26, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Nashor

    Hello,

    I'm working on a sort of block protection plugin and had a question related to the subject.
    In my plugin, when the block is broken, I need to create an SQL query to retrieve data and see if the action is allowed. However, I am running the SQL query in another thread using a Task. I was wondering how to deal with this. Could I cancel the BlockBreakEvent at a later time or does it need to be done in the Listener? If I can't do that, then how should I solve this problem?

    Thanks
     
  2. Offline

    fireblast709

    Nashor Can you go more in depth into the cases when an action is allowed or not? Like, are they region based, type based, permission based, etc?
     
  3. Offline

    Nashor

    fireblast709

    It is kind of region based. There are blocks which emit auras of protection and I store their locations in the database.
     
  4. Offline

    fireblast709

    Nashor you should keep an in memory cache. For example:
    • onEnable() loads the data from the database
    • onDisable saves the data on the database
    • When such a block is placed, you add them to your 'local cache' and asynchronously save that block in the database
    • When such a block is removed, you remove them from your 'local cache' and asynchronously remove the block from the database
    'local cache' can be pretty much any collection, from Lists to Sets to Maps. Seeing you most likely need to check all these auras, a List would do.
     
  5. Offline

    Nashor

    fireblast709

    Would that be fine for say, 1000 protection blocks?
     
  6. Offline

    fireblast709

    Nashor It should be. At the very least, I can guarantee you that it will run faster than querying the database every time you need to get data. Since you cannot cancel the event later (in, for example, a scheduled task), you cannot execute it on a separate thread. And since you cannot asynchronize it, it will cause lag.
     
  7. Offline

    Nashor

    fireblast709
    Okay thanks a lot. You're very helpful around here.
     
Thread Status:
Not open for further replies.

Share This Page