-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from altrisi/config-menu
Add config menu using cloth-config
- Loading branch information
Showing
7 changed files
with
147 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 29 additions & 27 deletions
56
src/main/java/mirsario/cameraoverhaul/core/configuration/Configuration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,59 @@ | ||
package mirsario.cameraoverhaul.core.configuration; | ||
|
||
import com.google.gson.*; | ||
import mirsario.cameraoverhaul.common.*; | ||
import net.fabricmc.loader.api.*; | ||
import java.io.*; | ||
import java.nio.file.*; | ||
import com.google.gson.*; | ||
|
||
public final class Configuration | ||
{ | ||
private static Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
|
||
private static final Path configPath = FabricLoader.getInstance().getConfigDir(); | ||
|
||
public static <T extends BaseConfigData> T LoadConfig(Class<T> tClass, String configName, int configVersion) | ||
{ | ||
T configData = null; | ||
Path configFile = configPath.resolve(configName + ".json"); | ||
boolean saveConfig = false; | ||
|
||
try { | ||
Path configDir; | ||
|
||
configDir = Paths.get("", "config", configName + ".json"); | ||
|
||
boolean saveConfig = false; | ||
Files.createDirectories(configPath); | ||
|
||
if(Files.exists(configDir)) { | ||
FileReader fileReader = new FileReader(configDir.toFile()); | ||
if(Files.exists(configFile)) { | ||
BufferedReader fileReader = Files.newBufferedReader(configFile); | ||
configData = gson.fromJson(fileReader, tClass); | ||
fileReader.close(); | ||
|
||
//Save the config on first runs of new versions. | ||
if(configData.configVersion < configVersion) { | ||
saveConfig = true; | ||
} | ||
} else { | ||
try { | ||
configData = (T)tClass.newInstance(); | ||
} | ||
catch(Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
configData = (T)tClass.getDeclaredConstructor().newInstance(); | ||
saveConfig = true; | ||
} | ||
|
||
if(saveConfig) { | ||
Paths.get("", "config").toFile().mkdirs(); | ||
|
||
configData.configVersion = configVersion; | ||
|
||
BufferedWriter writer = new BufferedWriter(new FileWriter(configDir.toFile())); | ||
|
||
writer.write(gson.toJson(configData)); | ||
writer.close(); | ||
} | ||
} catch(Exception e) { | ||
e.printStackTrace(); | ||
CameraOverhaul.Logger.error("Error when initializing config", e); | ||
} | ||
|
||
if(saveConfig) { | ||
SaveConfig(configData, configName, configVersion); | ||
} | ||
|
||
return configData; | ||
} | ||
|
||
public static <T extends BaseConfigData> void SaveConfig(T configData, String configName, int configVersion) { | ||
Path configFile = configPath.resolve(configName+".json"); | ||
|
||
configData.configVersion = configVersion; | ||
|
||
try (BufferedWriter writer = Files.newBufferedWriter(configFile)){ | ||
writer.write(gson.toJson(configData)); | ||
} catch (IOException e) { | ||
CameraOverhaul.Logger.error("Couldn't save config file", e); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/mirsario/cameraoverhaul/fabric/ModMenuConfigIntegration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package mirsario.cameraoverhaul.fabric; | ||
|
||
import java.util.function.*; | ||
import io.github.prospector.modmenu.api.*; | ||
import me.shedaniel.clothconfig2.api.*; | ||
import me.shedaniel.clothconfig2.gui.entries.*; | ||
import mirsario.cameraoverhaul.common.*; | ||
import mirsario.cameraoverhaul.common.configuration.*; | ||
import mirsario.cameraoverhaul.core.configuration.*; | ||
import net.minecraft.client.*; | ||
import net.minecraft.text.*; | ||
|
||
public class ModMenuConfigIntegration implements ModMenuApi | ||
{ | ||
private static final String ConfigEntriesPrefix = "cameraoverhaul.config"; | ||
|
||
@Override | ||
public ConfigScreenFactory<?> getModConfigScreenFactory() | ||
{ | ||
return screen -> GetConfigBuilder().build(); | ||
} | ||
|
||
@SuppressWarnings("resource") //MinecraftClient.getInstance() isn't a resource | ||
public static ConfigBuilder GetConfigBuilder() | ||
{ | ||
ConfigData config = CameraOverhaul.instance.config; | ||
|
||
ConfigBuilder builder = ConfigBuilder.create() | ||
.setParentScreen(MinecraftClient.getInstance().currentScreen) | ||
.setTitle(new TranslatableText("cameraoverhaul.config.title")) | ||
.transparentBackground() | ||
.setSavingRunnable(() -> { | ||
Configuration.SaveConfig(CameraOverhaul.instance.config, CameraOverhaul.configFileName, ConfigData.ConfigVersion); | ||
}); | ||
|
||
ConfigCategory general = builder.getOrCreateCategory(new TranslatableText("cameraoverhaul.config.category.general")); | ||
ConfigEntryBuilder entryBuilder = builder.entryBuilder(); | ||
|
||
//Entries | ||
general.addEntry(CreateBooleanEntry(entryBuilder, "enabled", true, config.enabled, value -> config.enabled = value)); | ||
general.addEntry(CreateFloatFactorEntry(entryBuilder, "strafingRollFactor", 1.0f, config.strafingRollFactor, value -> config.strafingRollFactor = value)); | ||
general.addEntry(CreateFloatFactorEntry(entryBuilder, "yawDeltaRollFactor", 1.0f, config.yawDeltaRollFactor, value -> config.yawDeltaRollFactor = value)); | ||
general.addEntry(CreateFloatFactorEntry(entryBuilder, "verticalVelocityPitchFactor", 1.0f, config.verticalVelocityPitchFactor, value -> config.verticalVelocityPitchFactor = value)); | ||
general.addEntry(CreateFloatFactorEntry(entryBuilder, "forwardVelocityPitchFactor", 1.0f, config.forwardVelocityPitchFactor, value -> config.forwardVelocityPitchFactor = value)); | ||
|
||
return builder; | ||
} | ||
//Entry Helpers | ||
public static BooleanListEntry CreateBooleanEntry(ConfigEntryBuilder entryBuilder, String entryName, Boolean defaultValue, Boolean value, Function<Boolean, Boolean> setter) | ||
{ | ||
String lowerCaseName = entryName.toLowerCase(); | ||
String baseTranslationPath = ConfigEntriesPrefix + "." + lowerCaseName; | ||
|
||
return entryBuilder.startBooleanToggle(new TranslatableText(baseTranslationPath + ".name"), value) | ||
.setDefaultValue(defaultValue) | ||
.setTooltip(new TranslatableText(baseTranslationPath + ".tooltip")) | ||
.setSaveConsumer(newValue -> setter.apply(newValue)) | ||
.build(); | ||
} | ||
public static FloatListEntry CreateFloatFactorEntry(ConfigEntryBuilder entryBuilder, String entryName, float defaultValue, float value, Function<Float, Float> setter) | ||
{ | ||
String lowerCaseName = entryName.toLowerCase(); | ||
String baseTranslationPath = ConfigEntriesPrefix + "." + lowerCaseName; | ||
|
||
return entryBuilder.startFloatField(new TranslatableText(baseTranslationPath + ".name"), value) | ||
.setDefaultValue(defaultValue) | ||
.setTooltip(new TranslatableText(baseTranslationPath + ".tooltip")) | ||
.setSaveConsumer(newValue -> setter.apply(newValue)) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"cameraoverhaul.config.title": "CameraOverhaul Settings", | ||
"cameraoverhaul.config.category.general": "General", | ||
"cameraoverhaul.config.enabled.name": "Enable the mod's effects", | ||
"cameraoverhaul.config.enabled.tooltip": "Toggles all of the mod's features.", | ||
"cameraoverhaul.config.strafingrollfactor.name": "Strafing Roll Factor", | ||
"cameraoverhaul.config.strafingrollfactor.tooltip": "Controls the strength of the camera roll rotation caused by strafing, or sideway horizontal velocity.", | ||
"cameraoverhaul.config.yawdeltarollfactor.name": "Mouselook Roll Factor", | ||
"cameraoverhaul.config.yawdeltarollfactor.tooltip": "Controls the strength of the camera roll rotation caused by turning around horizontally.", | ||
"cameraoverhaul.config.verticalvelocitypitchfactor.name": "Vertical Velocity Pitch Factor", | ||
"cameraoverhaul.config.verticalvelocitypitchfactor.tooltip": "Controls the strength of the camera pitch rotation offset caused by vertical velocity.", | ||
"cameraoverhaul.config.forwardvelocitypitchfactor.name": "Forward Velocity Pitch Factor", | ||
"cameraoverhaul.config.forwardvelocitypitchfactor.tooltip": "Controls the strength of the camera pitch rotation offset caused by moving forward or backwards." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters