Advertisement
Creepinson

pl

Feb 17th, 2018
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. package me.creepz.puppywuppy.player;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.lang.reflect.Type;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.UUID;
  12.  
  13. import org.bukkit.configuration.file.FileConfiguration;
  14.  
  15. import com.google.gson.Gson;
  16. import com.google.gson.GsonBuilder;
  17. import com.google.gson.JsonIOException;
  18. import com.google.gson.JsonSyntaxException;
  19. import com.google.gson.reflect.TypeToken;
  20.  
  21. import me.creepz.puppywuppy.core.PuppyWuppyCore;
  22.  
  23. /**
  24. *
  25. * @author Creepinson
  26. *
  27. */
  28. public class PlayerManager {
  29.  
  30. private static HashMap<UUID, User> users = new HashMap<UUID, User>();
  31.  
  32. public static User getUserFromUUID(UUID uuid) {
  33. if (users.get(uuid) == null) {
  34. users.put(uuid, new User(uuid));
  35. }
  36. return users.get(uuid);
  37. }
  38.  
  39. private static FileConfiguration data;
  40. private static File datafile;
  41. private static PuppyWuppyCore plugin;
  42. private static Gson gson = new Gson();
  43. public static void setup() {
  44. plugin = PuppyWuppyCore.getInstance();
  45.  
  46. if(datafile == null){
  47. datafile = new File(plugin.getDataFolder(), "users.json");
  48.  
  49. }
  50. if(!datafile.exists()){
  51. try {
  52. datafile.createNewFile();
  53. } catch (IOException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. }
  58.  
  59.  
  60.  
  61.  
  62. /*
  63. reloadData();
  64. getData().createSection("users", users);
  65. saveData();
  66. reloadData();*/
  67.  
  68. }
  69.  
  70. public static void loadUsersFromJSON() {
  71. if(datafile.exists()){
  72. try {
  73. Type typeOfHashMap = new TypeToken<HashMap<UUID, User>>() { }.getType();
  74. users = gson.fromJson(new FileReader(datafile), typeOfHashMap);
  75. } catch (JsonSyntaxException e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. } catch (JsonIOException e) {
  79. // TODO Auto-generated catch block
  80. e.printStackTrace();
  81. } catch (FileNotFoundException e) {
  82. // TODO Auto-generated catch block
  83. e.printStackTrace();
  84. }
  85. }
  86. }
  87.  
  88. public static void saveUsersToJSON() {
  89.  
  90. if(!users.isEmpty()){
  91. String userJson = new GsonBuilder().setPrettyPrinting().create().toJson(users);
  92. try {
  93. FileWriter out = new FileWriter(datafile, false);
  94. out.write(userJson);
  95. out.close();
  96. } catch (FileNotFoundException e) {
  97. // TODO Auto-generated catch block
  98. e.printStackTrace();
  99. } catch (IOException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. }
  103. }
  104. }
  105.  
  106. /* public static void loadUsersFromConfig() {
  107. for (Map.Entry<String, Object> entry : getData().getConfigurationSection("users").getValues(true).entrySet()) {
  108. ConfigurationSection usersec = (ConfigurationSection) entry.getValue();
  109. User ur = new User(UUID.fromString(usersec.getName()));
  110. Map<String, Boolean> pMap = new HashMap<String, Boolean>();
  111. // List<Map<String,Boolean>> mapList =
  112. // (List)usersec.getConfigurationSection("permissions");
  113. // for(Map<String,Boolean> maplist : mapList){
  114. // for(Map.Entry<String, Boolean> permEntry : maplist.entrySet()){
  115. // pMap.put(permEntry.getKey(), permEntry.getValue());
  116. // }
  117. // }
  118. ur.setPermissions((Map<String, Boolean>) usersec.getConfigurationSection("permissions"));
  119. ur.setRank(Utils.getRankFromName(usersec.getString("rank")));
  120. users.put(UUID.fromString(usersec.getName()), ur);
  121. }
  122. }
  123.  
  124. public static void saveUsersToConfig() {
  125. for (Map.Entry<UUID, User> u : users.entrySet()) {
  126. getData().getConfigurationSection("users").createSection(u.getKey().toString());
  127. getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString()).set("uuid",
  128. u.getValue().getUuid().toString());
  129.  
  130. if (!getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString())
  131. .contains("permissions")) {
  132. if (u.getValue().getPermissions() != null) {
  133. getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString())
  134. .createSection("permissions", u.getValue().getPermissions());
  135. } else {
  136. getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString())
  137. .createSection("permissions");
  138. }
  139. }else {
  140. if (u.getValue().getPermissions() != null) {
  141. for (Map.Entry<String, Boolean> map : u.getValue().getPermissions().entrySet()) {
  142. getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString())
  143. .getConfigurationSection("permissions").set(map.getKey(), map.getValue());
  144. }
  145. }
  146. }
  147. getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString()).set("rank",
  148. u.getValue().getRank().getName());
  149.  
  150. }*/
  151.  
  152. // saveData();
  153. //
  154. // reloadData();
  155.  
  156.  
  157. public static File getUsersJson(){
  158. return datafile;
  159. }
  160.  
  161. /*public static void saveData() {
  162. if (data == null || dataf == null) {
  163. return;
  164. }
  165. try {
  166. getData().save(dataf);
  167. } catch (IOException ex) {
  168. PuppyWuppyCore.getInstance().getLogger().log(Level.SEVERE, "Could not save config to " + dataf, ex);
  169. }
  170. }
  171.  
  172. public static void reloadData() {
  173. if (dataf == null) {
  174. dataf = new File(PuppyWuppyCore.getInstance().getDataFolder(), "data.yml");
  175. }
  176. data = YamlConfiguration.loadConfiguration(dataf);
  177.  
  178.  
  179. * // Look for defaults in the jar Reader defConfigStream = null; try {
  180. * defConfigStream = new
  181. * InputStreamReader(PuppyWuppyCore.getInstance().getResource("data.yml"
  182. * ), "UTF8"); } catch (UnsupportedEncodingException e) { // TODO
  183. * Auto-generated catch block e.printStackTrace(); } if (defConfigStream
  184. * != null) { YamlConfiguration defConfig =
  185. * YamlConfiguration.loadConfiguration(defConfigStream);
  186. * data.setDefaults(defConfig); }
  187.  
  188. }
  189.  
  190. public static FileConfiguration getData() {
  191. if (data == null) {
  192. reloadData();
  193. }
  194. return data;
  195. }
  196. */
  197.  
  198. public static class User extends Object {
  199.  
  200. private UUID uuid;
  201. private Rank rank;
  202. private Map<String, Boolean> permissions;
  203.  
  204. public User(UUID uuid) {
  205. super();
  206. this.uuid = uuid;
  207. }
  208.  
  209. public Map<String, Boolean> getPermissions() {
  210. return this.permissions;
  211. }
  212.  
  213. public void setPermissions(Map<String, Boolean> permissions) {
  214. this.permissions = permissions;
  215. }
  216.  
  217. public Rank getRank() {
  218. return this.rank;
  219. }
  220.  
  221. public void setRank(Rank rank) {
  222. this.rank = rank;
  223. }
  224.  
  225. public UUID getUuid() {
  226. return this.uuid;
  227. }
  228.  
  229. public boolean isMember() {
  230. if (this.rank == Rank.MEMBER) {
  231. return true;
  232. } else {
  233. return false;
  234. }
  235. }
  236. }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement