feat: add mod configuration (#34)
This commit is contained in:
parent
ebc2948e0f
commit
4eb068a2a1
4 changed files with 84 additions and 13 deletions
|
|
@ -4,6 +4,7 @@ import dev.architectury.event.events.common.CommandRegistrationEvent;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.commands.Commands;
|
import net.minecraft.commands.Commands;
|
||||||
import net.trafficlunar.optionsprofiles.gui.ProfilesScreen;
|
import net.trafficlunar.optionsprofiles.gui.ProfilesScreen;
|
||||||
|
import net.trafficlunar.optionsprofiles.profiles.OptionsProfilesModConfiguration;
|
||||||
import net.trafficlunar.optionsprofiles.profiles.Profiles;
|
import net.trafficlunar.optionsprofiles.profiles.Profiles;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
@ -16,6 +17,7 @@ import java.nio.file.Paths;
|
||||||
public class OptionsProfilesMod {
|
public class OptionsProfilesMod {
|
||||||
public static final String MOD_ID = "optionsprofiles";
|
public static final String MOD_ID = "optionsprofiles";
|
||||||
public static final Logger LOGGER = LogManager.getLogger("Options Profiles");
|
public static final Logger LOGGER = LogManager.getLogger("Options Profiles");
|
||||||
|
private static OptionsProfilesModConfiguration CONFIG;
|
||||||
|
|
||||||
public static void init() {
|
public static void init() {
|
||||||
Path profilesDirectory = Paths.get("options-profiles");
|
Path profilesDirectory = Paths.get("options-profiles");
|
||||||
|
|
@ -28,6 +30,8 @@ public class OptionsProfilesMod {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CONFIG = OptionsProfilesModConfiguration.load();
|
||||||
|
|
||||||
// Update / add configuration for existing profiles
|
// Update / add configuration for existing profiles
|
||||||
Profiles.updateProfiles();
|
Profiles.updateProfiles();
|
||||||
|
|
||||||
|
|
@ -41,4 +45,12 @@ public class OptionsProfilesMod {
|
||||||
})
|
})
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static OptionsProfilesModConfiguration config() {
|
||||||
|
if (CONFIG == null) {
|
||||||
|
throw new IllegalStateException("Config not yet available");
|
||||||
|
} else {
|
||||||
|
return CONFIG;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
package net.trafficlunar.optionsprofiles.profiles;
|
||||||
|
|
||||||
|
import net.trafficlunar.optionsprofiles.OptionsProfilesMod;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
public class OptionsProfilesModConfiguration {
|
||||||
|
private static Path configurationFile;
|
||||||
|
|
||||||
|
private boolean showProfilesButton = true;
|
||||||
|
|
||||||
|
public OptionsProfilesModConfiguration save() {
|
||||||
|
OptionsProfilesModConfiguration configuration = new OptionsProfilesModConfiguration();
|
||||||
|
|
||||||
|
Gson gson = new GsonBuilder()
|
||||||
|
.setPrettyPrinting()
|
||||||
|
.create();
|
||||||
|
|
||||||
|
try (BufferedWriter writer = Files.newBufferedWriter(configurationFile)) {
|
||||||
|
gson.toJson(this, writer);
|
||||||
|
OptionsProfilesMod.LOGGER.info("Main configuration saved");
|
||||||
|
} catch (IOException e) {
|
||||||
|
OptionsProfilesMod.LOGGER.error("Unable to write main configuration.json!", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OptionsProfilesModConfiguration load() {
|
||||||
|
OptionsProfilesModConfiguration configuration = new OptionsProfilesModConfiguration();
|
||||||
|
configurationFile = Profiles.PROFILES_DIRECTORY.resolve("configuration.json");
|
||||||
|
|
||||||
|
if (Files.notExists(configurationFile))
|
||||||
|
configuration.save();
|
||||||
|
|
||||||
|
try (BufferedReader reader = Files.newBufferedReader(configurationFile)) {
|
||||||
|
Gson gson = new Gson();
|
||||||
|
configuration = gson.fromJson(reader, OptionsProfilesModConfiguration.class);
|
||||||
|
} catch (IOException e) {
|
||||||
|
OptionsProfilesMod.LOGGER.error("An error occurred when reading the main configuration.json", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean shouldShowProfilesButton() {
|
||||||
|
return showProfilesButton;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setShowProfilesButton(boolean showProfilesButton) {
|
||||||
|
this.showProfilesButton = showProfilesButton;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ import net.minecraft.client.gui.components.Button;
|
||||||
import net.minecraft.client.gui.screens.Screen;
|
import net.minecraft.client.gui.screens.Screen;
|
||||||
import net.minecraft.client.gui.screens.options.OptionsScreen;
|
import net.minecraft.client.gui.screens.options.OptionsScreen;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
|
import net.trafficlunar.optionsprofiles.OptionsProfilesMod;
|
||||||
import net.trafficlunar.optionsprofiles.gui.ProfilesScreen;
|
import net.trafficlunar.optionsprofiles.gui.ProfilesScreen;
|
||||||
import org.spongepowered.asm.mixin.Final;
|
import org.spongepowered.asm.mixin.Final;
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
|
@ -22,13 +23,15 @@ public class MixinOptionsScreen extends Screen {
|
||||||
|
|
||||||
@Inject(at = @At("HEAD"), method = "init")
|
@Inject(at = @At("HEAD"), method = "init")
|
||||||
private void init(CallbackInfo info) {
|
private void init(CallbackInfo info) {
|
||||||
this.addRenderableWidget(
|
if (OptionsProfilesMod.config().shouldShowProfilesButton()) {
|
||||||
Button.builder(
|
this.addRenderableWidget(
|
||||||
Component.translatable("gui.optionsprofiles.profiles-menu"),
|
Button.builder(
|
||||||
(button) -> this.minecraft.setScreen(new ProfilesScreen(this)))
|
Component.translatable("gui.optionsprofiles.profiles-menu"),
|
||||||
.width(75)
|
(button) -> this.minecraft.setScreen(new ProfilesScreen(this)))
|
||||||
.pos(5, 5)
|
.width(75)
|
||||||
.build()
|
.pos(5, 5)
|
||||||
);
|
.build()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -13,7 +13,6 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ProfileConfiguration {
|
public class ProfileConfiguration {
|
||||||
private static final Path profilesDirectory = Profiles.PROFILES_DIRECTORY;
|
|
||||||
private static Path configurationFile;
|
private static Path configurationFile;
|
||||||
private static String profileName;
|
private static String profileName;
|
||||||
|
|
||||||
|
|
@ -41,13 +40,11 @@ public class ProfileConfiguration {
|
||||||
public static ProfileConfiguration get(String profile_name) {
|
public static ProfileConfiguration get(String profile_name) {
|
||||||
ProfileConfiguration configuration = new ProfileConfiguration();
|
ProfileConfiguration configuration = new ProfileConfiguration();
|
||||||
|
|
||||||
Path profile = profilesDirectory.resolve(profile_name);
|
configurationFile = Profiles.PROFILES_DIRECTORY.resolve(profile_name).resolve("configuration.json");
|
||||||
configurationFile = profile.resolve("configuration.json");
|
|
||||||
profileName = profile_name;
|
profileName = profile_name;
|
||||||
|
|
||||||
if (Files.notExists(configurationFile)) {
|
if (Files.notExists(configurationFile))
|
||||||
configuration.save();
|
configuration.save();
|
||||||
}
|
|
||||||
|
|
||||||
try (BufferedReader reader = Files.newBufferedReader(configurationFile)) {
|
try (BufferedReader reader = Files.newBufferedReader(configurationFile)) {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue