Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.creepz.puppywuppy.player;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.lang.reflect.Type;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
- import org.bukkit.configuration.file.FileConfiguration;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.JsonIOException;
- import com.google.gson.JsonSyntaxException;
- import com.google.gson.reflect.TypeToken;
- import me.creepz.puppywuppy.core.PuppyWuppyCore;
- /**
- *
- * @author Creepinson
- *
- */
- public class PlayerManager {
- private static HashMap<UUID, User> users = new HashMap<UUID, User>();
- public static User getUserFromUUID(UUID uuid) {
- if (users.get(uuid) == null) {
- users.put(uuid, new User(uuid));
- }
- return users.get(uuid);
- }
- private static FileConfiguration data;
- private static File datafile;
- private static PuppyWuppyCore plugin;
- private static Gson gson = new Gson();
- public static void setup() {
- plugin = PuppyWuppyCore.getInstance();
- if(datafile == null){
- datafile = new File(plugin.getDataFolder(), "users.json");
- }
- if(!datafile.exists()){
- try {
- datafile.createNewFile();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /*
- reloadData();
- getData().createSection("users", users);
- saveData();
- reloadData();*/
- }
- public static void loadUsersFromJSON() {
- if(datafile.exists()){
- try {
- Type typeOfHashMap = new TypeToken<HashMap<UUID, User>>() { }.getType();
- users = gson.fromJson(new FileReader(datafile), typeOfHashMap);
- } catch (JsonSyntaxException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JsonIOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static void saveUsersToJSON() {
- if(!users.isEmpty()){
- String userJson = new GsonBuilder().setPrettyPrinting().create().toJson(users);
- try {
- FileWriter out = new FileWriter(datafile, false);
- out.write(userJson);
- out.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /* public static void loadUsersFromConfig() {
- for (Map.Entry<String, Object> entry : getData().getConfigurationSection("users").getValues(true).entrySet()) {
- ConfigurationSection usersec = (ConfigurationSection) entry.getValue();
- User ur = new User(UUID.fromString(usersec.getName()));
- Map<String, Boolean> pMap = new HashMap<String, Boolean>();
- // List<Map<String,Boolean>> mapList =
- // (List)usersec.getConfigurationSection("permissions");
- // for(Map<String,Boolean> maplist : mapList){
- // for(Map.Entry<String, Boolean> permEntry : maplist.entrySet()){
- // pMap.put(permEntry.getKey(), permEntry.getValue());
- // }
- // }
- ur.setPermissions((Map<String, Boolean>) usersec.getConfigurationSection("permissions"));
- ur.setRank(Utils.getRankFromName(usersec.getString("rank")));
- users.put(UUID.fromString(usersec.getName()), ur);
- }
- }
- public static void saveUsersToConfig() {
- for (Map.Entry<UUID, User> u : users.entrySet()) {
- getData().getConfigurationSection("users").createSection(u.getKey().toString());
- getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString()).set("uuid",
- u.getValue().getUuid().toString());
- if (!getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString())
- .contains("permissions")) {
- if (u.getValue().getPermissions() != null) {
- getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString())
- .createSection("permissions", u.getValue().getPermissions());
- } else {
- getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString())
- .createSection("permissions");
- }
- }else {
- if (u.getValue().getPermissions() != null) {
- for (Map.Entry<String, Boolean> map : u.getValue().getPermissions().entrySet()) {
- getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString())
- .getConfigurationSection("permissions").set(map.getKey(), map.getValue());
- }
- }
- }
- getData().getConfigurationSection("users").getConfigurationSection(u.getKey().toString()).set("rank",
- u.getValue().getRank().getName());
- }*/
- // saveData();
- //
- // reloadData();
- public static File getUsersJson(){
- return datafile;
- }
- /*public static void saveData() {
- if (data == null || dataf == null) {
- return;
- }
- try {
- getData().save(dataf);
- } catch (IOException ex) {
- PuppyWuppyCore.getInstance().getLogger().log(Level.SEVERE, "Could not save config to " + dataf, ex);
- }
- }
- public static void reloadData() {
- if (dataf == null) {
- dataf = new File(PuppyWuppyCore.getInstance().getDataFolder(), "data.yml");
- }
- data = YamlConfiguration.loadConfiguration(dataf);
- * // Look for defaults in the jar Reader defConfigStream = null; try {
- * defConfigStream = new
- * InputStreamReader(PuppyWuppyCore.getInstance().getResource("data.yml"
- * ), "UTF8"); } catch (UnsupportedEncodingException e) { // TODO
- * Auto-generated catch block e.printStackTrace(); } if (defConfigStream
- * != null) { YamlConfiguration defConfig =
- * YamlConfiguration.loadConfiguration(defConfigStream);
- * data.setDefaults(defConfig); }
- }
- public static FileConfiguration getData() {
- if (data == null) {
- reloadData();
- }
- return data;
- }
- */
- public static class User extends Object {
- private UUID uuid;
- private Rank rank;
- private Map<String, Boolean> permissions;
- public User(UUID uuid) {
- super();
- this.uuid = uuid;
- }
- public Map<String, Boolean> getPermissions() {
- return this.permissions;
- }
- public void setPermissions(Map<String, Boolean> permissions) {
- this.permissions = permissions;
- }
- public Rank getRank() {
- return this.rank;
- }
- public void setRank(Rank rank) {
- this.rank = rank;
- }
- public UUID getUuid() {
- return this.uuid;
- }
- public boolean isMember() {
- if (this.rank == Rank.MEMBER) {
- return true;
- } else {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement