Read Write the same file in events

Discussion in 'Plugin Development' started by nerzul0101, Dec 26, 2021.

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

    nerzul0101

    Hi, I am new here and in bukkit development,

    I am working on a plugin which when players die, write in the same file /plugins/namePlugin/fallenPlayers.yml
    And moreover, when a player joins this same file is read.
    Since we expect a lot of players to die and to join/connect at the same time, my question is: have I to deal with synchronization or lock of the file ?
    are events called synchronized in minecraft servers ?
    Does using the FileConfiguration api give any syncronized/safe approach or I can use File and GSON for example, with the same result (I mean in concurrency context because I would prefer json instead of yml) ?

    Thank you in advance, this is my first plugin.

    I will release it when it is finished.
     
  2. Offline

    KarimAKL

    @nerzul0101 There's no need to read the file every time someone dies. Instead, you can load the fallen players from the file into a collection when the server starts. You can then save that collection to the file once in a while or when the server stops.
     
  3. Offline

    nerzul0101

    In that case, would I have to deal with synchronization ?
    Because I don't know if the events in minecraft are called synchronized or you have to deal with sync when you use shared resources (lists, files etc...)
    By the way, I like your approach, perhaps the only problem would be when server suddenly breaks and it is not able to save the collection in the file.
     
  4. Offline

    KarimAKL

    Minecraft is single-threaded, so no.

    That's why you save once in a while. You can also choose to save to the file whenever a change has happened. You're trying to avoid reading from the file, not writing to it.
     
Thread Status:
Not open for further replies.

Share This Page