Skip to content

Commit

Permalink
Types & safety
Browse files Browse the repository at this point in the history
  • Loading branch information
voruti committed Jul 22, 2023
1 parent f309d64 commit d95793d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Builder;
import lombok.Value;
import lombok.experimental.Accessors;
import org.checkerframework.checker.nullness.qual.Nullable;

@Value
@Builder
Expand Down Expand Up @@ -30,6 +31,7 @@ public class Config {
* <li>{@code %2$s}: placeholder for the server name, on which that player is currently playing on</li>
* </ul>
*/
@Nullable
String serverListEntryFormat;

/**
Expand Down Expand Up @@ -72,5 +74,6 @@ public class Config {
* The version name to be set.
* Used by {@link #fillMissingVersionInfo} and {@link #replaceVersionInfo}.
*/
@Nullable
String versionName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public class PersistenceService {
Path dataDirectory;

@NonNull
final Gson gson = new GsonBuilder()
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();


@NonNull
public Config loadConfig() {
logger.trace("Loading config...");

Expand All @@ -47,17 +48,20 @@ public Config loadConfig() {
}

// load:
return gson.fromJson(
final Config loadedConfig = gson.fromJson(
Files.readString(configPath),
Config.class
);
return loadedConfig != null
? loadedConfig
: Config.DEFAULT;
} catch (IOException | IllegalStateException e) {
logger.error("Error while loading config", e);
return Config.DEFAULT;
}
}

private <T> void writeFile(Path path, T content) throws IOException {
private <T> void writeFile(@NonNull final Path path, @NonNull final T content) throws IOException {
logger.trace("Writing file: {}", path);

Files.write(path, gson.toJson(content).getBytes());
Expand Down

0 comments on commit d95793d

Please sign in to comment.