static void modified() { try (FileWriter writer = new FileWriter(getFile())) { JsonObject obj = deepCopy(INSTANCE.everythingElse); obj.addProperty("clientToken", INSTANCE.clientToken); obj.add("authenticationDatabase", GSON.toJsonTree(INSTANCE.authenticationDatabase.profiles)); GSON.toJson(obj, writer); System.out.println("Profiles saved"); } catch (IOException e) { e.printStackTrace(); } }
public static LauncherProfiles load() throws IOException { System.out.println("Loading Minecraft profiles from " + DevLauncher.workingDirectory); try (FileReader reader = new FileReader(getFile())) { LauncherProfiles profiles = new LauncherProfiles(); JsonObject e = new JsonParser().parse(reader).getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : e.entrySet()) { if (entry.getKey().equals("clientToken")) { profiles.clientToken = entry.getValue().getAsString(); } else if (entry.getKey().equals("authenticationDatabase")) { JsonObject o = entry.getValue().getAsJsonObject(); for (Map.Entry<String, JsonElement> entry1 : o.entrySet()) { profiles.authenticationDatabase.profiles.put( UUIDTypeAdapter.fromString(entry1.getKey()), GSON.fromJson(entry1.getValue(), OnlineProfile.class)); } } else { profiles.everythingElse.add(entry.getKey(), entry.getValue()); } } INSTANCE = profiles; return INSTANCE; } finally { if (INSTANCE == null) { INSTANCE = new LauncherProfiles(); } INSTANCE.markLoaded(); } }
public void load() throws IOException { File file = getFile(); if (file.exists()) { JsonObject rootObject = JSON_PARSER.parse(new FileReader(file)).getAsJsonObject(); for (JsonElement element : rootObject.getAsJsonArray("players")) { Player player = new Player(element.getAsJsonObject()); playerDB.put(player.getName(), player); } for (JsonElement element : rootObject.getAsJsonArray("groups")) { Group group = new Group(element.getAsJsonObject()); groupDB.put(group.getName(), group); } } else { //noinspection ResultOfMethodCallIgnored file.createNewFile(); JsonObject rootObject = new JsonObject(); rootObject.add("players", new JsonArray()); rootObject.add("groups", new JsonArray()); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(GSON.toJson(rootObject)); bw.close(); } }
public void save() { try { File file = getFile(); if (!file.exists()) // noinspection ResultOfMethodCallIgnored file.createNewFile(); JsonObject rootObject = new JsonObject(); JsonArray players = new JsonArray(); for (Player player : playerDB.values()) players.add(player.toJson()); rootObject.add("players", players); JsonArray groups = new JsonArray(); for (Group group : groupDB.values()) groups.add(group.toJson()); rootObject.add("groups", groups); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(GSON.toJson(rootObject)); bw.close(); } catch (IOException e) { e.printStackTrace(); } }