Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with importing old configs #8

Merged
merged 5 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 7 additions & 46 deletions src/main/java/yalter/mousetweaks/Config.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,21 @@
package yalter.mousetweaks;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;

import yalter.mousetweaks.config.MTConfig;
import java.nio.file.Files;

public class Config {

private File file;

public Config(File fileName) {
this.file = fileName;
}
public static void handleOldConfig(File file) {
if (!file.exists()) return;

public boolean hasOldConfig() {
try {
FileReader reader = new FileReader(this.file);
Properties tempProps = new Properties();
tempProps.load(reader);
return !tempProps.containsKey("general");
if (Files.readAllLines(file.toPath()).stream().noneMatch(str -> str.contains("general"))) {
file.delete();
}
} catch (IOException e) {
return true; // assume yes if it failed for whatever reason
Constants.LOGGER.error(e);
}
}

public void importOldConfig() {
try {
FileReader reader = new FileReader(this.file);
Properties tempProps = new Properties();
tempProps.load(reader);

if (tempProps.containsKey("RMBTweak"))
MTConfig.RMBTweak = Objects.equals(tempProps.get("RMBTweak").toString(), "1");

if (tempProps.containsKey("LMBTweakWithItem"))
MTConfig.LMBTweakWithItem = Objects.equals(tempProps.get("LMBTweakWithItem").toString(), "1");

if (tempProps.containsKey("LMBTweakWithoutItem"))
MTConfig.LMBTweakWithoutItem = Objects.equals(tempProps.get("LMBTweakWithoutItem").toString(), "1");

if (tempProps.containsKey("WheelTweak"))
MTConfig.WheelTweak = Objects.equals(tempProps.get("WheelTweak").toString(), "1");

if (tempProps.containsKey("WheelSearchOrder"))
MTConfig.WheelSearchOrder = Integer.parseInt(tempProps.get("WheelSearchOrder").toString());

if (tempProps.containsKey("WheelScrollDirection"))
MTConfig.WheelScrollDirection = Integer.parseInt(tempProps.get("WheelScrollDirection").toString());

if (tempProps.containsKey("ScrollItemScaling"))
MTConfig.ScrollItemScaling = Integer.parseInt(tempProps.get("ScrollItemScaling").toString());

file.renameTo(new File(file.getPath() + ".bak"));
} catch (IOException ignored) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public class MouseTweaksForge {
public void preinit(FMLPreInitializationEvent event) {
if (event.getSide().isClient()) {
try {
Config oldConfig = new Config(event.getSuggestedConfigurationFile());
if (oldConfig.hasOldConfig()) {
oldConfig.importOldConfig();
}
Config.handleOldConfig(event.getSuggestedConfigurationFile());
ConfigurationManager.registerConfig(MTConfig.class);
ConfigurationManager.registerBus();
} catch (ConfigException e) {
Expand Down