Skip to content

Commit

Permalink
Modified Config API
Browse files Browse the repository at this point in the history
  • Loading branch information
XDPXI committed Oct 16, 2024
1 parent f682470 commit 800c7af
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/client/java/dev/xdpxi/xdlib/XDsLibraryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void onInitializeClient() {
configServerName = clientSettings.configServerName;
configServerAddress = clientSettings.configServerAddress;
}
LOGGER.info("[XDLib] - Applying Config...");
LOGGER.info("[XDLib] - Applying config...");
if (discordRPC) {
if (osName.contains("win")) {
DiscordRPCHandler.init();
Expand All @@ -153,7 +153,7 @@ public void onInitializeClient() {
if (sodiumIntegration) {
CustomOptions.integrate();
}
LOGGER.info("[XDLib] - Applied Config!");
LOGGER.info("[XDLib] - Applied config!");

if (clothConfig) {
configHelper.registerSaveListener(discordRPC, sodiumIntegration);
Expand Down
10 changes: 8 additions & 2 deletions src/client/java/dev/xdpxi/xdlib/api/config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -67,10 +68,11 @@ public void loadConfig() {
try (FileReader reader = new FileReader(CONFIG_FILE)) {
switch (fileType) {
case TOML:
configInstance = (T) new Toml().read(reader).to(configInstance.getClass());
Toml toml = new Toml();
configInstance = (T) toml.read(reader).to(configInstance.getClass());
break;
case JSON:
configInstance = GSON.fromJson(reader, (Class<T>) configInstance.getClass());
configInstance = GSON.fromJson(reader, (Class<T>) getGenericType());
break;
}
} catch (IOException ignored) {
Expand All @@ -82,5 +84,9 @@ public T getConfig() {
return configInstance;
}

private Class<?> getGenericType() {
return (Class<?>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

private enum FileType {JSON, TOML}
}
8 changes: 4 additions & 4 deletions src/client/java/dev/xdpxi/xdlib/api/configClass/modmenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ private Screen createConfigScreen(Screen parent, config<? extends Configurable>
public void init() {
int yOffset = this.height / 6 + 24;

this.addDrawableChild(ButtonWidget.builder(Text.of("Save Config"), (button) -> {
this.addDrawableChild(ButtonWidget.builder(Text.of("Save config"), (button) -> {
config.saveConfig();
this.client.getToastManager().add(
SystemToast.create(this.client, SystemToast.Type.WORLD_BACKUP, Text.of("Config Saved!"), null)
SystemToast.create(this.client, SystemToast.Type.WORLD_BACKUP, Text.of("config Saved!"), null)
);
}).dimensions(this.width / 2 - 100, yOffset, 200, 20).build());

this.addDrawableChild(ButtonWidget.builder(Text.of("Reset Config"), (button) -> {
this.addDrawableChild(ButtonWidget.builder(Text.of("Reset config"), (button) -> {
config.loadConfig(); // Assuming loadConfig resets it
this.client.getToastManager().add(
SystemToast.create(this.client, SystemToast.Type.WORLD_BACKUP, Text.of("Config Reset!"), null)
SystemToast.create(this.client, SystemToast.Type.WORLD_BACKUP, Text.of("config Reset!"), null)
);
}).dimensions(this.width / 2 - 100, yOffset + 25, 200, 20).build());

Expand Down

0 comments on commit 800c7af

Please sign in to comment.