1-
21package pl .skidam .automodpack_core .config ;
32
4- import com .google .gson .*;
5- import pl .skidam .automodpack_core .utils .AddressHelpers ;
3+ import static pl .skidam .automodpack_core .Constants .*;
64
5+ import com .google .gson .*;
76import java .lang .reflect .Type ;
87import java .net .InetSocketAddress ;
98import java .nio .file .Files ;
109import java .nio .file .Path ;
1110import java .nio .file .StandardOpenOption ;
12-
13- import static pl .skidam .automodpack_core .Constants .*;
11+ import pl .skidam .automodpack_core .utils .AddressHelpers ;
1412
1513public class ConfigTools {
1614
17- public static Gson GSON = new GsonBuilder ()
18- .disableHtmlEscaping ()
19- .setPrettyPrinting ()
20- .registerTypeAdapter (InetSocketAddress .class , new InetSocketAddressTypeAdapter ())
21- .create ();
15+ public static Gson GSON = new GsonBuilder ().disableHtmlEscaping ().setPrettyPrinting ().registerTypeAdapter (InetSocketAddress .class , new InetSocketAddressTypeAdapter ()).create ();
16+
17+ private static class InetSocketAddressTypeAdapter implements JsonSerializer <InetSocketAddress >, JsonDeserializer <InetSocketAddress > {
2218
23- private static class InetSocketAddressTypeAdapter implements JsonSerializer <InetSocketAddress >,JsonDeserializer <InetSocketAddress > {
2419 @ Override
2520 public JsonElement serialize (InetSocketAddress src , Type typeOfSrc , JsonSerializationContext context ) {
2621 return new JsonPrimitive (src .getHostString () + ":" + src .getPort ());
@@ -50,14 +45,14 @@ public static <T> T softLoad(Path configFile, Class<T> configClass) {
5045 String json = Files .readString (configFile );
5146 return GSON .fromJson (json , configClass );
5247 }
53- } catch (Exception ignored ) { }
48+ } catch (Exception ignored ) {}
5449 return null ;
5550 }
5651
5752 public static <T > T load (Path configFile , Class <T > configClass ) {
5853 try {
5954 if (!Files .isDirectory (configFile .getParent ())) {
60- Files .createDirectories (configFile .getParent ());
55+ Files .createDirectories (configFile .getParent ());
6156 }
6257
6358 if (Files .isRegularFile (configFile )) {
@@ -80,7 +75,8 @@ public static <T> T load(Path configFile, Class<T> configClass) {
8075 e .printStackTrace ();
8176 }
8277
83- try { // create new config
78+ try {
79+ // create new config
8480 T obj = getConfigObject (configClass );
8581 save (configFile , obj );
8682 return obj ;
@@ -94,7 +90,7 @@ public static <T> T load(Path configFile, Class<T> configClass) {
9490 public static <T > T load (String json , Class <T > configClass ) {
9591 try {
9692 if (json != null ) {
97- return GSON .fromJson (json , configClass );
93+ return GSON .fromJson (json , configClass );
9894 }
9995 } catch (Exception e ) {
10096 LOGGER .error ("Couldn't load config! " + configClass );
@@ -121,21 +117,20 @@ public static void save(Path configFile, Object configObject) {
121117 }
122118 }
123119
124-
125120 // Modpack content stuff
126- public static Jsons .ModpackContentFields loadModpackContent (Path modpackContentFile ) {
121+ public static Jsons .ModpackContent loadModpackContent (Path modpackContentFile ) {
127122 try {
128123 if (Files .isRegularFile (modpackContentFile )) {
129124 String json = Files .readString (modpackContentFile );
130- return GSON .fromJson (json , Jsons .ModpackContentFields .class );
125+ return GSON .fromJson (json , Jsons .ModpackContent .class );
131126 }
132127 } catch (Exception e ) {
133128 LOGGER .error ("Couldn't load modpack content! {}" , modpackContentFile .toAbsolutePath ().normalize (), e );
134129 }
135130 return null ;
136131 }
137132
138- public static void saveModpackContent (Path modpackContentFile , Jsons .ModpackContentFields configObject ) {
133+ public static void saveModpackContent (Path modpackContentFile , Jsons .ModpackContent configObject ) {
139134 try {
140135 if (!Files .isDirectory (modpackContentFile .getParent ())) {
141136 Files .createDirectories (modpackContentFile .getParent ());
@@ -147,4 +142,33 @@ public static void saveModpackContent(Path modpackContentFile, Jsons.ModpackCont
147142 e .printStackTrace ();
148143 }
149144 }
145+
146+ public static Jsons .ClientSelectionManagerFields loadClientSelectionManager (Path selectionFile ) {
147+ try {
148+ if (Files .isRegularFile (selectionFile )) {
149+ String json = Files .readString (selectionFile );
150+ Jsons .ClientSelectionManagerFields obj = GSON .fromJson (json , Jsons .ClientSelectionManagerFields .class );
151+ if (obj == null ) {
152+ return new Jsons .ClientSelectionManagerFields ();
153+ }
154+ return obj ;
155+ }
156+ } catch (Exception e ) {
157+ LOGGER .debug ("Couldn't load client selection manager file (this is normal on first startup): {}" , e .getMessage ());
158+ }
159+ return new Jsons .ClientSelectionManagerFields ();
160+ }
161+
162+ public static void saveClientSelectionManager (Path selectionFile , Jsons .ClientSelectionManagerFields configObject ) {
163+ try {
164+ if (!Files .isDirectory (selectionFile .getParent ())) {
165+ Files .createDirectories (selectionFile .getParent ());
166+ }
167+
168+ Files .writeString (selectionFile , GSON .toJson (configObject ), StandardOpenOption .CREATE , StandardOpenOption .TRUNCATE_EXISTING );
169+ } catch (Exception e ) {
170+ LOGGER .error ("Couldn't save client selection manager!" );
171+ e .printStackTrace ();
172+ }
173+ }
150174}
0 commit comments