Users.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.gmail.nossr50;
  2. import java.io.*;
  3. import java.util.Properties;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import java.util.HashMap;
  7. import org.bukkit.entity.*;
  8. import com.gmail.nossr50.datatypes.PlayerProfile;
  9. public class Users {
  10. private static volatile Users instance;
  11. protected static final Logger log = Logger.getLogger("Minecraft");
  12. String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users";
  13. String directory = "plugins/mcMMO/FlatFileStuff/";
  14. String directoryb = "plugins/mcMMO/FlatFileStuff/Leaderboards/";
  15. //public static ArrayList<PlayerProfile> players;
  16. public static HashMap<Player, PlayerProfile> players = new HashMap<Player, PlayerProfile>();
  17. private Properties properties = new Properties();
  18. //To load
  19. public void load() throws IOException {
  20. properties.load(new FileInputStream(location));
  21. }
  22. //To save
  23. public void save()
  24. {
  25. try
  26. {
  27. properties.store(new FileOutputStream(location), null);
  28. }catch(IOException ex) {
  29. }
  30. }
  31. public void loadUsers()
  32. {
  33. new File(directory).mkdir();
  34. new File(directoryb).mkdir();
  35. File theDir = new File(location);
  36. if(!theDir.exists())
  37. {
  38. //properties = new PropertiesFile(location);
  39. FileWriter writer = null;
  40. try {
  41. writer = new FileWriter(location);
  42. //writer.write("#Storage place for user information\r\n");
  43. } catch (Exception e)
  44. {
  45. log.log(Level.SEVERE, "Exception while creating " + location, e);
  46. } finally {
  47. try {
  48. if (writer != null) {
  49. writer.close();
  50. }
  51. } catch (IOException e) {
  52. log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
  53. }
  54. }
  55. } else {
  56. try {
  57. load();
  58. } catch (IOException e) {
  59. log.log(Level.SEVERE, "Exception while loading " + location, e);
  60. }
  61. }
  62. }
  63. public static void addUser(Player player)
  64. {
  65. players.put(player, new PlayerProfile(player));
  66. }
  67. public static void clearUsers()
  68. {
  69. players.clear();
  70. }
  71. public static HashMap<Player, PlayerProfile> getProfiles(){
  72. return players;
  73. }
  74. public static void removeUser(Player player)
  75. {
  76. PlayerProfile PP = Users.getProfile(player);
  77. if(PP != null)
  78. {
  79. PP.save();
  80. if(players.containsKey(player))
  81. players.remove(player);
  82. }
  83. }
  84. public static PlayerProfile getProfile(Player player){
  85. if(players.get(player) != null)
  86. return players.get(player);
  87. else
  88. {
  89. players.put(player, new PlayerProfile(player));
  90. return players.get(player);
  91. }
  92. }
  93. public static Users getInstance() {
  94. if (instance == null) {
  95. instance = new Users();
  96. }
  97. return instance;
  98. }
  99. }