Skip to content

Commit

Permalink
Can i actually push an update from here? I do hope so...
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Jul 22, 2024
1 parent 701fa51 commit 08af645
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 70 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
- Added support for 1.21. (Thanks @ImUrX!)
- Fix elytra transparency button tooltip.
- Modified mod config loading system to hopefully prevent compatibility issues.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ deps.yarn=1.21+build.2
deps.fabricloader=0.15.11

# Mod Properties
mod_version = 1.11.1
mod_version = 1.11.2
maven_group = nl.enjarai
archives_base_name = show-me-your-skin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class ShowMeYourSkinClient implements ClientModInitializer, CicadaEntrypo

@Override
public void onInitializeClient() {
ModConfig.load();
ModConfig.INSTANCE.ensureValid();

ModKeyBindings.register();
Expand Down
71 changes: 5 additions & 66 deletions src/main/java/nl/enjarai/showmeyourskin/config/ModConfig.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
package nl.enjarai.showmeyourskin.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.util.math.MathHelper;
import nl.enjarai.cicada.api.util.AbstractModConfig;
import nl.enjarai.showmeyourskin.Components;
import nl.enjarai.showmeyourskin.ShowMeYourSkin;
import nl.enjarai.showmeyourskin.ShowMeYourSkinClient;
import nl.enjarai.showmeyourskin.util.CombatLogger;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.UUID;
import java.util.function.Function;

public class ModConfig {
public static final File CONFIG_FILE = new File(FabricLoader.getInstance().getConfigDir() + "/" + ShowMeYourSkin.MODID + ".json");
public static ModConfig INSTANCE;

private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting() // Makes the json use new lines instead of being a "one-liner"
.serializeNulls() // Makes fields with `null` value to be written as well.
.disableHtmlEscaping() // We'll be able to use custom chars without them being saved differently
.create();
public class ModConfig extends AbstractModConfig {
public static final Path CONFIG_FILE = FabricLoader.getInstance().getConfigDir().resolve(ShowMeYourSkin.MODID + ".json");
public static ModConfig INSTANCE = loadConfigFile(CONFIG_FILE, new ModConfig());

public boolean globalEnabled = true;
public boolean overridesEnabledInServerMode = false;
Expand Down Expand Up @@ -137,57 +129,4 @@ public void ensureValid() {

save();
}



public static void load() {
INSTANCE = loadConfigFile(CONFIG_FILE);
}

public void save() {
saveConfigFile(CONFIG_FILE);
}

/**
* Loads config file.
*
* @param file file to load the config file from.
* @return ConfigManager object
*/
private static ModConfig loadConfigFile(File file) {
ModConfig config = null;

if (file.exists()) {
// An existing config is present, we should use its values
try (BufferedReader fileReader = new BufferedReader(
new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)
)) {
// Parses the config file and puts the values into config object
config = GSON.fromJson(fileReader, ModConfig.class);
} catch (IOException e) {
throw new RuntimeException("Problem occurred when trying to load config: ", e);
}
}
// gson.fromJson() can return null if file is empty
if (config == null) {
config = new ModConfig();
}

// Saves the file in order to write new fields if they were added
config.saveConfigFile(file);
return config;
}

/**
* Saves the config to the given file.
*
* @param file file to save config to
*/
private void saveConfigFile(File file) {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
GSON.toJson(this, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}

0 comments on commit 08af645

Please sign in to comment.