Skip to content

Commit dcbc196

Browse files
committed
ref: another round of misc refactorings
1 parent 5cc98b3 commit dcbc196

File tree

6 files changed

+62
-64
lines changed

6 files changed

+62
-64
lines changed

common/src/main/java/io/github/thebossmagnus/mods/config_manager/common/AddFlagsUtil.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,22 @@ private static void addFlags() throws RuntimeException {
3333
throw new RuntimeException("Could not create config directory", e);
3434
}
3535
if (OVERWRITE_FLAG.get()) {
36-
Path flag = configDir.resolve("CONFIG_MANAGER_RESET_FLAG");
37-
try {
38-
Files.createFile(flag);
39-
} catch (IOException e) {
40-
if (!Files.exists(flag)) {
41-
Constants.LOGGER.error("Could not create CONFIG_MANAGER_RESET_FLAG: {}", flag, e);
42-
throw new RuntimeException("Could not create CONFIG_MANAGER_RESET_FLAG", e);
43-
}
44-
// Ignore if file already exists
45-
}
36+
createFlagFile(configDir, "CONFIG_MANAGER_RESET_FLAG");
4637
} else if (UPDATE_FLAG.get()) {
47-
Path flag = configDir.resolve("CONFIG_MANAGER_UPDATE_FLAG");
48-
try {
49-
Files.createFile(flag);
50-
} catch (IOException e) {
51-
if (!Files.exists(flag)) {
52-
Constants.LOGGER.error("Could not create CONFIG_MANAGER_UPDATE_FLAG: {}", flag, e);
53-
throw new RuntimeException("Could not create CONFIG_MANAGER_UPDATE_FLAG", e);
54-
}
55-
// Ignore if file already exists
38+
createFlagFile(configDir, "CONFIG_MANAGER_UPDATE_FLAG");
39+
}
40+
}
41+
42+
private static void createFlagFile(Path configDir, String flagFileName) {
43+
Path flag = configDir.resolve(flagFileName);
44+
try {
45+
Files.createFile(flag);
46+
} catch (IOException e) {
47+
if (!Files.exists(flag)) {
48+
Constants.LOGGER.error("Could not create {}: {}", flagFileName, flag, e);
49+
throw new RuntimeException("Could not create " + flagFileName, e);
5650
}
51+
// Ignore if file already exists
5752
}
5853
}
5954
}

common/src/main/java/io/github/thebossmagnus/mods/config_manager/common/screen/Gui.java

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
public class Gui extends Screen {
1414
private static final int buttonWidth = 150;
1515
private static final int buttonHeight = 20;
16+
private static final int COLOR_RED = 0xFF0000;
17+
private static final int COLOR_WHITE = 0xFFFFFF;
1618
private final Component updateWarnings = Component.translatable("* %s\n* %s",
1719
Component.translatable("config_manager.warning.lose_some_config"),
1820
Component.translatable("config_manager.warning.game_restart")
@@ -30,43 +32,43 @@ public Gui(Screen screen) {
3032
super(Component.translatable("config_manager.title"));
3133
}
3234

35+
private Button createConfigButton(Component label, Runnable flagSetter, boolean isFirstClick, java.util.function.Consumer<Boolean> firstClickUpdater) {
36+
return Button.builder(label, (btn) -> {
37+
if (isFirstClick) {
38+
btn.setMessage(Component.translatable("config_manager.confirmation").withStyle(style -> style.withColor(COLOR_RED)));
39+
firstClickUpdater.accept(false);
40+
} else {
41+
try {
42+
flagSetter.run();
43+
btn.setMessage(Component.translatable("config_manager.success").withStyle(style -> style.withColor(COLOR_WHITE)));
44+
btn.active = false;
45+
} catch (Exception e) {
46+
Constants.LOGGER.error("Failed to set flag", e);
47+
btn.setMessage(Component.translatable("config_manager.error").withStyle(style -> style.withColor(COLOR_RED)));
48+
}
49+
}
50+
})
51+
.size(buttonWidth, buttonHeight)
52+
.build();
53+
}
54+
3355
@Override
3456
protected void init() {
35-
Button updateButton = Button.builder(Component.translatable("config_manager.update_config"), (btn) -> {
36-
if (updateFirstClick) {
37-
btn.setMessage(Component.translatable("config_manager.confirmation").withStyle(style -> style.withColor(0xFF0000))); // Red
38-
updateFirstClick = false;
39-
} else {
40-
try {
41-
AddFlagsUtil.setUpdateFlag(true);
42-
btn.setMessage(Component.translatable("config_manager.success").withStyle(style -> style.withColor(0xFFFFFF)));
43-
btn.active = false;
44-
} catch (Exception e) {
45-
Constants.LOGGER.error("Failed to set update flag", e);
46-
btn.setMessage(Component.translatable("config_manager.error").withStyle(style -> style.withColor(0xFF0000)));
47-
}
48-
}
49-
}).pos((int) ((this.width - buttonWidth) * 0.15), (int) ((this.height - buttonHeight) * 0.7))
50-
.size(buttonWidth, buttonHeight)
51-
.build();
52-
53-
Button resetButton = Button.builder(Component.translatable("config_manager.reset_config"), (btn) -> {
54-
if (resetFirstClick) {
55-
btn.setMessage(Component.translatable("config_manager.confirmation").withStyle(style -> style.withColor(0xFF0000))); // Red
56-
resetFirstClick = false;
57-
} else {
58-
try {
59-
AddFlagsUtil.setOverwriteFlag(true);
60-
btn.setMessage(Component.translatable("config_manager.success").withStyle(style -> style.withColor(0xFFFFFF)));
61-
btn.active = false;
62-
} catch (Exception e) {
63-
Constants.LOGGER.error("Failed to set overwrite flag", e);
64-
btn.setMessage(Component.translatable("config_manager.error").withStyle(style -> style.withColor(0xFF0000)));
65-
}
66-
}
67-
}).pos((int) ((this.width - buttonWidth) * 0.9), (int) ((this.height - buttonHeight) * 0.7))
68-
.size(buttonWidth, buttonHeight)
69-
.build();
57+
Button updateButton = createConfigButton(
58+
Component.translatable("config_manager.update_config"),
59+
() -> AddFlagsUtil.setUpdateFlag(true),
60+
updateFirstClick,
61+
(val) -> updateFirstClick = val
62+
);
63+
updateButton.setPosition((int) ((this.width - buttonWidth) * 0.15), (int) ((this.height - buttonHeight) * 0.7));
64+
65+
Button resetButton = createConfigButton(
66+
Component.translatable("config_manager.reset_config"),
67+
() -> AddFlagsUtil.setOverwriteFlag(true),
68+
resetFirstClick,
69+
(val) -> resetFirstClick = val
70+
);
71+
resetButton.setPosition((int) ((this.width - buttonWidth) * 0.7), (int) ((this.height - buttonHeight) * 0.7));
7072

7173
Button closeButton = Button.builder(Component.translatable("config_manager.close"), (btn) -> {
7274
this.onClose();
@@ -88,7 +90,7 @@ protected void init() {
8890
l2 = new MultilineLabelWidget(
8991
this.font,
9092
resetWarnings,
91-
(int) ((this.width - buttonWidth) * 0.9),
93+
(int) ((this.width - buttonWidth) * 0.7),
9294
(int) ((this.height - buttonHeight) * 0.7) - 90,
9395
buttonWidth,
9496
true

common/src/main/java/io/github/thebossmagnus/mods/config_manager/common_coremod/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
public final class Constants {
77
public static final String MOD_ID = "config_manager";
88
public static final Logger LOGGER = LogManager.getLogger(MOD_ID);
9+
public static final String DIR_NAME = "modpack_defaults";
910
}

common/src/main/java/io/github/thebossmagnus/mods/config_manager/common_coremod/CopyConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
import java.nio.file.Files;
66
import java.nio.file.Path;
77

8+
import static io.github.thebossmagnus.mods.config_manager.common_coremod.Constants.DIR_NAME;
89

9-
public final class CopyConfig {
1010

11-
private static final String dirName = "modpack_defaults";
11+
public final class CopyConfig {
1212

1313
public static void init(Path gameDir) {
1414

1515

16-
Path configDir = gameDir.resolve("config").resolve(dirName);
16+
Path configDir = gameDir.resolve("config").resolve(DIR_NAME);
1717
if (!Files.exists(configDir)) {
1818
return;
1919
}
2020

2121
// Avoid overriding the modpack configs
22-
Path nestedDir = configDir.resolve(dirName);
22+
Path nestedDir = configDir.resolve(DIR_NAME);
2323
if (Files.exists(nestedDir)) {
24-
throw new RuntimeException(String.format("A subfolder called \"%s\" is inside config/%s: %s", dirName, dirName, nestedDir));
24+
throw new RuntimeException(String.format("A subfolder called \"%s\" is inside config/%s: %s", DIR_NAME, DIR_NAME, nestedDir));
2525
}
2626

2727

common/src/main/java/io/github/thebossmagnus/mods/config_manager/common_coremod/OverwriteConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
* Copies config files from modpack_defaults, overwriting any existing files in the config directory.
1111
*/
1212
public final class OverwriteConfig {
13-
private static final String DIR_NAME = "modpack_defaults";
13+
1414

1515

1616
public static void run(Path gameDir) {
1717
Path configDir = gameDir.resolve("config");
18-
Path defaultsDir = configDir.resolve(DIR_NAME);
18+
Path defaultsDir = configDir.resolve(Constants.DIR_NAME);
1919
if (!Files.exists(defaultsDir)) {
2020
return;
2121
}

common/src/main/java/io/github/thebossmagnus/mods/config_manager/common_coremod/ResetAndCopyConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Deletes all config files except the modpack_defaults directory, then copies configs from modpack_defaults.
1111
*/
1212
public final class ResetAndCopyConfig {
13-
private static final String DIR_NAME = "modpack_defaults";
13+
1414

1515
/**
1616
* Deletes all files and folders in config except modpack_defaults, then copies files from modpacks_defaults.
@@ -20,7 +20,7 @@ public static void run(Path gameDir) {
2020

2121
// Delete everything in config except modpacks_defaults
2222
try (Stream<Path> stream = Files.list(configDir)) {
23-
stream.filter(path -> !path.getFileName().toString().equals(DIR_NAME))
23+
stream.filter(path -> !path.getFileName().toString().equals(Constants.DIR_NAME))
2424
.forEach(ResetAndCopyConfig::deleteRecursively);
2525
} catch (IOException e) {
2626
throw new RuntimeException("Failed to clean config directory", e);

0 commit comments

Comments
 (0)