2525package org .visuals .legacy .lightconfig .lib .v1 ;
2626
2727import com .google .gson .JsonObject ;
28+ import net .fabricmc .loader .api .FabricLoader ;
2829import net .minecraft .client .gui .screens .Screen ;
2930import org .jetbrains .annotations .Nullable ;
3031import org .slf4j .Logger ;
3536import org .visuals .legacy .lightconfig .lib .v1 .serialization .Json ;
3637import org .visuals .legacy .lightconfig .lib .v1 .type .Type ;
3738
39+ import java .io .File ;
3840import java .nio .file .Files ;
39- import java .nio .file .Path ;
4041import java .util .ArrayList ;
4142import java .util .List ;
4243
4344public abstract class Config {
4445 protected final Logger logger = LoggerFactory .getLogger (this .getClass ());
4546 protected final List <AbstractConfigField <?>> configFields = new ArrayList <>();
4647 protected final String id ;
47- protected final Path path ;
48+ protected final File configFile ;
4849 protected final Json .Serializer serializer ;
4950 protected final Json .Deserializer deserializer ;
5051
51- public Config (final String id , final Path path , final ConfigSerializer <?> serializer , final ConfigDeserializer <?> deserializer ) {
52+ public Config (final String id , final ConfigSerializer <?> serializer , final ConfigDeserializer <?> deserializer ) {
5253 this .id = id ;
53- this .path = path ;
54+ this .configFile = FabricLoader . getInstance (). getConfigDir (). resolve ( id + ".json" ). toFile () ;
5455 if (!(serializer instanceof Json .Serializer && deserializer instanceof Json .Deserializer )) {
5556 throw new RuntimeException ("Only json serialization is currently supported! Please use Json.SERIALIZER/Json.DESERIALIZER!" );
5657 }
@@ -59,8 +60,8 @@ public Config(final String id, final Path path, final ConfigSerializer<?> serial
5960 this .deserializer = (Json .Deserializer ) deserializer ;
6061 }
6162
62- public Config (final String id , final Path path ) {
63- this (id , path , new Json .Serializer (), new Json .Deserializer ());
63+ public Config (final String id ) {
64+ this (id , new Json .Serializer (), new Json .Deserializer ());
6465 }
6566
6667 public BooleanConfigField booleanFieldOf (final String name , final boolean defaultValue ) {
@@ -88,15 +89,15 @@ public <T extends Enum<T>> EnumConfigField<T> enumFieldOf(final String name, fin
8889 }
8990
9091 public void load () {
91- if (!this .path . toFile () .exists ()) {
92+ if (!this .configFile .exists ()) {
9293 this .logger .info ("Config file doesn't exist! Creating one..." );
9394 this .save ();
9495 return ;
9596 }
9697
9798 boolean success = true ;
9899 try {
99- final String json = Files .readString (this .path );
100+ final String json = Files .readString (this .configFile . toPath () );
100101 final JsonObject object = this .deserializer .deserialize (json ).getAsJsonObject ();
101102 if (object == null ) {
102103 this .logger .warn ("Failed to load config! Defaulting to original settings." );
@@ -137,7 +138,7 @@ public void save() {
137138 });
138139
139140 try {
140- Files .write (this .path , this .serializer .serialize (object ));
141+ Files .write (this .configFile . toPath () , this .serializer .serialize (object ));
141142 } catch (Exception ignored ) {
142143 this .logger .warn ("Failed to save config!" );
143144 return ;
@@ -147,7 +148,6 @@ public void save() {
147148 }
148149
149150 public void reset () {
150- // TODO: When implementing the screen system/idk, implement a event listener for like reload resource packs or whatever
151151 this .configFields .forEach (AbstractConfigField ::restore );
152152 this .save ();
153153 }
@@ -161,11 +161,11 @@ public List<AbstractConfigField<?>> getConfigFields() {
161161 }
162162
163163 public String getId () {
164- return id ;
164+ return this . id ;
165165 }
166166
167- public Path getPath () {
168- return path ;
167+ public File getConfigFile () {
168+ return this . configFile ;
169169 }
170170
171171 public abstract Screen getConfigScreen (@ Nullable Screen parent );
0 commit comments