Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from HypeJet/feat/json-config
Browse files Browse the repository at this point in the history
Add Json config support
  • Loading branch information
Window5000 authored Jan 19, 2023
2 parents be39400 + 4fd2e58 commit 1611f33
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/main/java/org/hypejet/hype/extension/Extension.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.hypejet.hype.extension;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.moandjiezana.toml.Toml;
import lombok.Getter;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -135,4 +137,35 @@ public final void runDelayedSync(@NotNull Runnable runnable, @NotNull Duration d
throw new RuntimeException(e);
}
}

/**
*
* @param config The config type
* @return New config using the config parameter as type
*
* @see <a href="https://github.com/google/gson/blob/master/UserGuide.md#object-examples">Gson Documentation</a>
*
*/
public <T extends Object> T getJsonConfig(T config) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();

Path path = Path.of(String.valueOf(getDataDirectory()), "config.json");

try {
if(!Files.exists(path)) {
if(!Files.exists(getDataDirectory()))
Files.createDirectory(getDataDirectory());
Files.createFile(path);
OutputStream stream = Files.newOutputStream(path);
stream.write(gson.toJson(config).getBytes());
stream.close();
return config;
} else {
return (T) gson.fromJson(Files.readString(path), config.getClass());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 1611f33

Please sign in to comment.