Skip to content

Commit

Permalink
Merge pull request #7 from altrisi/config-menu
Browse files Browse the repository at this point in the history
Add config menu using cloth-config
  • Loading branch information
Mirsario authored Dec 24, 2020
2 parents 5ac9b54 + 436e2f8 commit c80e71d
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 44 deletions.
22 changes: 14 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ dependencies {
// Fabric API. This is technically optional, but you probably want it anyway.
//modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.

//ModMenu - Gave up on this because it's annoying. Feel free to do a PR!
//modImplementation("io.github.prospector:modmenu:${project.modmenu_version}") {
// exclude(group: "net.fabricmc.fabric-api")
// exclude(group: "net.fabricmc.fabric-loader")
//}
// Cloth Config
modApi("me.shedaniel.cloth:config-2:${project.cloth_config_version}") {
exclude(group: "net.fabricmc.fabric-api")
exclude(group: "net.fabricmc") // Prevent preparing two loader versions in cache. Not needed
}
include "me.shedaniel.cloth:config-2:${project.cloth_config_version}"

// Fabric Resource Loader (needed for localization, which Cloth Config needs)
// Actually not needed, because this is only used with ModMenu, and ModMenu includes it
//modApi(fabricApi.module("fabric-resource-loader-v0", project.fabric_version))
//include fabricApi.module("fabric-resource-loader-v0", project.fabric_version)

// ModMenu API, to add the Config Screen to it
modImplementation "io.github.prospector:modmenu:${project.modmenu_version}"
}

processResources {
Expand Down
12 changes: 7 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.16.3
yarn_mappings=1.16.3+build.9
loader_version=0.4.8+build.157
# actually on https://modmuss50.me/fabric.html
minecraft_version=1.16.4
yarn_mappings=1.16.4+build.7
loader_version=0.10.8

# Mod Properties
mod_version = 1.0.1-fabric-1.16
Expand All @@ -14,5 +15,6 @@ org.gradle.jvmargs=-Xmx1G

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.3.0+build.200
# modmenu_version=1.14.6+build.31
fabric_version=0.28.3+1.16
cloth_config_version=4.8.3
modmenu_version=1.14.13+build.19
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public class CameraOverhaul

public static CameraOverhaul instance;

public static String configFileName = "cameraoverhaul";

public CameraSystem cameraSystem;
public ConfigData config;

public void onInitializeClient()
{
config = Configuration.LoadConfig(ConfigData.class, "cameraoverhaul", ConfigData.ConfigVersion);
config = Configuration.LoadConfig(ConfigData.class, configFileName, ConfigData.ConfigVersion);

cameraSystem = new CameraSystem();
}
Expand Down
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);
}
}
}
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();
}
}
14 changes: 14 additions & 0 deletions src/main/resources/assets/cameraoverhaul/lang/en_us.json
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."
}
12 changes: 9 additions & 3 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"id": "cameraoverhaul",
"version": "${version}",
"name": "Camera Overhaul",
"description": "Makes gameplay more satisfying through the use of various camera tilting. Compatible with everything.\nConfigurable through '.minecraft/config/cameraoverhaul.json'.",
"description": "Makes gameplay more satisfying through the use of various camera tilting. Compatible with everything.\nConfigurable through the gear button in the top right of ModMenu or through the '.minecraft/config/cameraoverhaul.json' file for finer control.",
"license": "MIT",
"icon": "assets/cameraoverhaul/icon.png",
"environment": "client",
Expand All @@ -12,6 +12,10 @@
"Mirsario"
],

"contributors": [
"altrisi"
],

"contact": {
"homepage": "https://curseforge.com/minecraft/mc-mods/cameraoverhaul",
"sources": "https://github.com/Mirsario/Minecraft-CameraOverhaul",
Expand All @@ -20,14 +24,16 @@
},

"entrypoints": {
"client": [ "mirsario.cameraoverhaul.fabric.FabricClientModInitializer" ]
"client": [ "mirsario.cameraoverhaul.fabric.FabricClientModInitializer" ],
"modmenu": [ "mirsario.cameraoverhaul.fabric.ModMenuConfigIntegration" ]
},

"mixins": [
"cameraoverhaul.mixins.json"
],

"depends": {
"fabricloader": ">=0.4.0"
"fabricloader": ">=0.4.0",
"cloth-config2": ">=4.8.3"
}
}

0 comments on commit c80e71d

Please sign in to comment.