Deleting player.dat file on server restart

Discussion in 'Plugin Development' started by Techtony96, Oct 31, 2012.

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

    Techtony96

    I'm making a game server that when the server restarts, i need all player data to be erased. So when the log back in, their location, inventory, armor, experience, food, health, etc are all at default. Sort of like the hunger games :D
     
  2. using File.delete(), you can delete files, just find an way to get a file from the player.dat
     
    ZeusAllMighty11 likes this.
  3. Offline

    gjossep

    Im not sure if you can do this, might give errors but you could maybe do something like this:
    Code:
    File BaseFolder = new File(Bukkit.getServer().getWorld("worldName").getWorldFolder(), "players");
           
            for(Player p : Bukkit.getOnlinePlayers())
            {
                p.kickPlayer("Bla bla");
                File playerFile = new File(BaseFolder, p.getName()+".dat");
                playerFile.delete();
            }
    In the onDisable()
    Im not sure if that works, didn't test it yet.

    Tell me if you need more help!
     
  4. Offline

    ZeusAllMighty11


    Not a good idea. Not to be rude.



    Like ferrybig said, check for the data folder and then do delete "/plugins/essentials/userdata/" + player.getName() + ".yml"



    Edit: Use something like that if you want to delete essentials files


    otherwise, use Ferrybig's method
     
  5. I actually meant to delete the world/players/player.dat on enable, and also, not al servers has essentials
     
  6. Offline

    cman1885

    >Assuming anyone who knows how to make even a simple plugin would use essentials
     
  7. Offline

    ZeusAllMighty11


    Ooooh I was thinking he meant the essentials file that stored godmode and such.

    But yeah we had the same idea
     
  8. Offline

    Techtony96

    So i how could i just delete the entire folder? it would delete all the files inside it and it would automatically regenerate, right?
     
  9. when you delete the whole serve,r it should regenerate as it saving
     
  10. Offline

    Techtony96

    I would put this code in onEnable on onDisable? And how would i delete an entire forlder?

    Thanks
     
  11. You can FIle.delete to remove a directory or a file
    its better to put it in onEnable, as on onDisable, some players may still eft inside the game
     
  12. Offline

    Techtony96

    ferrybig
    Ok, so i got the function working, but since not all people run the server in the same directory, how would i make it go into the folder named "world" and then "players", ignoring anything before that, Do you understand what im saying? Here is my code so far...
    Code:Java
    1.  
    2. File players = new File("/world/players");
    3. players.delete();
     
  13. Code:Java
    1.  
    2. remove the / from the beginning, as that says it an absolute path
     
  14. Offline

    Techtony96

    When i was testing this, it doesnt delete anything.. :/
     
  15. does players.delete() returns true or false? if false, there is something not correct
     
  16. Offline

    hatstand

    You can't delete a non-empty directory via file.delete()
     
  17. Offline

    ebildude123

    Code:
    String worldName = "world";
    File playerFilesDir = new File(worldName + "/players");
    if(playerFilesDir.isDirectory()){
    String[] playerDats = playerFilesDir.list();
    for (int i = 0; i < playerDats.length; i++) {
    File datFile = new File(playerFilesDir, playerDats[i]); 
    datFile.delete();
    }
    }
    
     
    BajanAmerican likes this.
Thread Status:
Not open for further replies.

Share This Page