Skip to content

Commit 9532b28

Browse files
committed
fix: make code more solid and cleanup
1 parent 41c3694 commit 9532b28

File tree

10 files changed

+62
-55
lines changed

10 files changed

+62
-55
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,59 @@
33
import java.io.IOException;
44
import java.nio.file.Files;
55
import java.nio.file.Path;
6+
import java.util.concurrent.atomic.AtomicBoolean;
67

78
public class AddFlagsUtil {
8-
9-
10-
private static boolean UPDATE_FLAG = false;
11-
private static boolean OVERWRITE_FLAG = false;
9+
private static final AtomicBoolean UPDATE_FLAG = new AtomicBoolean(false);
10+
private static final AtomicBoolean OVERWRITE_FLAG = new AtomicBoolean(false);
1211

1312
public static boolean isUpdateFlag() {
14-
return UPDATE_FLAG;
13+
return UPDATE_FLAG.get();
1514
}
1615

1716
public static void setUpdateFlag(boolean updateFlag) {
18-
UPDATE_FLAG = updateFlag;
17+
UPDATE_FLAG.set(updateFlag);
1918
addFlags();
20-
2119
}
2220

2321
public static boolean isOverwriteFlag() {
24-
return OVERWRITE_FLAG;
22+
return OVERWRITE_FLAG.get();
2523
}
2624

2725
public static void setOverwriteFlag(boolean overwriteFlag) {
28-
OVERWRITE_FLAG = overwriteFlag;
26+
OVERWRITE_FLAG.set(overwriteFlag);
2927
addFlags();
3028
}
3129

3230
private static void addFlags() throws RuntimeException {
3331
Path gameDir = Services.PLATFORM.getGameDir();
34-
if (OVERWRITE_FLAG) {
35-
Path flag = gameDir.resolve("config").resolve("CONFIG_MANAGER_RESET_FLAG");
32+
Path configDir = gameDir.resolve("config");
33+
try {
34+
if (!Files.exists(configDir)) {
35+
Files.createDirectories(configDir);
36+
}
37+
} catch (IOException e) {
38+
Constants.LOGGER.error("Could not create config directory: {}", configDir, e);
39+
throw new RuntimeException("Could not create config directory", e);
40+
}
41+
if (OVERWRITE_FLAG.get()) {
42+
Path flag = configDir.resolve("CONFIG_MANAGER_RESET_FLAG");
3643
try {
3744
Files.createFile(flag);
3845
} catch (IOException e) {
3946
if (!Files.exists(flag)) {
47+
Constants.LOGGER.error("Could not create CONFIG_MANAGER_RESET_FLAG: {}", flag, e);
4048
throw new RuntimeException("Could not create CONFIG_MANAGER_RESET_FLAG", e);
4149
}
4250
// Ignore if file already exists
4351
}
44-
} else if (UPDATE_FLAG) {
45-
Path flag = gameDir.resolve("config").resolve("CONFIG_MANAGER_UPDATE_FLAG");
52+
} else if (UPDATE_FLAG.get()) {
53+
Path flag = configDir.resolve("CONFIG_MANAGER_UPDATE_FLAG");
4654
try {
4755
Files.createFile(flag);
4856
} catch (IOException e) {
4957
if (!Files.exists(flag)) {
58+
Constants.LOGGER.error("Could not create CONFIG_MANAGER_UPDATE_FLAG: {}", flag, e);
5059
throw new RuntimeException("Could not create CONFIG_MANAGER_UPDATE_FLAG", e);
5160
}
5261
// Ignore if file already exists

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.ServiceLoader;
66

7+
78
public class Services {
89

910

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33

44
import io.github.thebossmagnus.mods.config_manager.common.AddFlagsUtil;
5+
import io.github.thebossmagnus.mods.config_manager.common.Constants;
56
import net.minecraft.client.gui.GuiGraphics;
67
import net.minecraft.client.gui.components.Button;
78
import net.minecraft.client.gui.screens.Screen;
89
import net.minecraft.network.chat.Component;
9-
import org.apache.logging.log4j.LogManager;
10-
import org.apache.logging.log4j.Logger;
1110
import org.jetbrains.annotations.NotNull;
1211

1312

1413
public class Gui extends Screen {
15-
public static final Logger LOGGER = LogManager.getLogger();
1614
private static final int buttonWidth = 150;
1715
private static final int buttonHeight = 20;
1816
private final Component updateWarnings = Component.translatable("* %s\n* %s",
@@ -34,36 +32,44 @@ public Gui(Screen screen) {
3432

3533
@Override
3634
protected void init() {
37-
38-
3935
Button updateButton = Button.builder(Component.translatable("config_manager.update_config"), (btn) -> {
4036
if (updateFirstClick) {
4137
btn.setMessage(Component.translatable("config_manager.confirmation").withStyle(style -> style.withColor(0xFF0000))); // Red
4238
updateFirstClick = false;
4339
} else {
44-
AddFlagsUtil.setUpdateFlag(true);
45-
btn.setMessage(Component.translatable("config_manager.success").withStyle(style -> style.withColor(0xFFFFFF)));
46-
btn.active = false;
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+
}
4748
}
4849
}).pos((int) ((this.width - buttonWidth) * 0.15), (int) ((this.height - buttonHeight) * 0.7))
4950
.size(buttonWidth, buttonHeight)
5051
.build();
5152

5253
Button resetButton = Button.builder(Component.translatable("config_manager.reset_config"), (btn) -> {
53-
if (resetFirstClick) {
54-
btn.setMessage(Component.translatable("config_manager.confirmation").withStyle(style -> style.withColor(0xFF0000))); // Red
55-
resetFirstClick = false;
56-
} else {
57-
AddFlagsUtil.setOverwriteFlag(true);
58-
btn.setMessage(Component.translatable("config_manager.success").withStyle(style -> style.withColor(0xFFFFFF)));
59-
btn.active=false;
60-
}
61-
62-
}).pos((int) ((this.width - buttonWidth) * 0.9), (int) ((this.height - buttonHeight) * 0.7)).size(buttonWidth, buttonHeight).size(buttonWidth, buttonHeight).build();
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();
6370

6471
Button closeButton = Button.builder(Component.translatable("config_manager.close"), (btn) -> {
6572
this.onClose();
66-
6773
}).pos((int) ((this.width - buttonWidth) * 0.5), (int) ((this.height - buttonHeight) * 0.95)).size(buttonWidth, buttonHeight).build();
6874

6975
this.addRenderableWidget(resetButton);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.minecraft.client.gui.components.Renderable;
66
import net.minecraft.network.chat.Component;
77
import net.minecraft.util.FormattedCharSequence;
8+
import org.jetbrains.annotations.NotNull;
89

910
import java.util.ArrayList;
1011
import java.util.List;
@@ -48,7 +49,7 @@ public MultilineLabelWidget(Font font, Component text, int x, int y, int width,
4849
* @param partialTick Partial tick
4950
*/
5051
@Override
51-
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
52+
public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
5253
// Collect the main text and its siblings (extra components attached to the main one)
5354
List<Component> allComponents = new ArrayList<>();
5455
allComponents.add(text);

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package io.github.thebossmagnus.mods.config_manager.common_coremod;
22

33

4-
5-
6-
74
import java.nio.file.Files;
85
import java.nio.file.Path;
96

@@ -25,12 +22,12 @@ public static void run(Path gameDir) {
2522
try {
2623
if (Files.exists(resetFlag)) {
2724
LOGGER.info("Flag detected, running a config reset");
28-
ResetAndCopyConfig.run(gameDir, LOGGER);
25+
ResetAndCopyConfig.run(gameDir);
2926
Files.deleteIfExists(resetFlag);
3027
Files.deleteIfExists(updateFlag);
3128
} else if (Files.exists(updateFlag)) {
3229
LOGGER.info("Flag detected, running a config update");
33-
OverwriteConfig.run(gameDir, LOGGER);
30+
OverwriteConfig.run(gameDir);
3431
Files.deleteIfExists(updateFlag);
3532
} else {
3633
LOGGER.info("No flag found, running a usual copy");

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.nio.file.Path;
77

88

9-
109
public final class CopyConfig {
1110

1211
private static final String dirName = "modpack_defaults";

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.github.thebossmagnus.mods.config_manager.common_coremod;
22

3-
import org.apache.logging.log4j.Logger;
4-
53
import java.io.IOException;
64
import java.nio.file.Files;
75
import java.nio.file.Path;
@@ -15,8 +13,7 @@ public final class OverwriteConfig {
1513
private static final String DIR_NAME = "modpack_defaults";
1614

1715

18-
public static void run(Path gameDir, Logger LOGGER) {
19-
16+
public static void run(Path gameDir) {
2017
Path configDir = gameDir.resolve("config");
2118
Path defaultsDir = configDir.resolve(DIR_NAME);
2219
if (!Files.exists(defaultsDir)) {
@@ -38,10 +35,9 @@ public static void run(Path gameDir, Logger LOGGER) {
3835
throw new RuntimeException("Error copying " + source + " to " + target, e);
3936
}
4037
});
41-
LOGGER.info("Config files updated");
38+
Constants.LOGGER.info("Config files updated");
4239
} catch (IOException e) {
4340
throw new RuntimeException("Failed to copy modpack_defaults", e);
4441
}
4542
}
4643
}
47-

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.github.thebossmagnus.mods.config_manager.common_coremod;
22

3-
import org.apache.logging.log4j.Logger;
4-
53
import java.io.IOException;
64
import java.nio.file.Files;
75
import java.nio.file.Path;
@@ -17,7 +15,7 @@ public final class ResetAndCopyConfig {
1715
/**
1816
* Deletes all files and folders in config except modpack_defaults, then copies files from modpacks_defaults.
1917
*/
20-
public static void run(Path gameDir, Logger logger) {
18+
public static void run(Path gameDir) {
2119
Path configDir = gameDir.resolve("config");
2220

2321
// Delete everything in config except modpacks_defaults
@@ -27,7 +25,7 @@ public static void run(Path gameDir, Logger logger) {
2725
} catch (IOException e) {
2826
throw new RuntimeException("Failed to clean config directory", e);
2927
}
30-
OverwriteConfig.run(gameDir, logger);
28+
OverwriteConfig.run(gameDir);
3129
}
3230

3331
/**
@@ -48,4 +46,3 @@ private static void deleteRecursively(Path path) {
4846
}
4947
}
5048
}
51-

neoforge_coremod/src/main/java/io/github/thebossmagnus/mods/config_manager/core_mod/services/ConfigManagerDependencyLocator.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99
import net.neoforged.neoforgespi.locating.IDependencyLocator;
1010
import net.neoforged.neoforgespi.locating.IDiscoveryPipeline;
1111
import net.neoforged.neoforgespi.locating.IModFile;
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
12+
import org.apache.logging.log4j.LogManager;
13+
import org.apache.logging.log4j.Logger;
1414

1515
import java.nio.file.Path;
1616
import java.util.List;
1717
import java.util.Optional;
1818

19+
import static io.github.thebossmagnus.mods.config_manager.common.Constants.MOD_ID;
20+
1921

2022
/**
21-
* Since neoforge doesn't load jar in jar mods from coremods, we should do it by ourselves.
23+
* Since neoforge doesn't load jar in jar mods from coremods, we do it by ourselves.
2224
*/
2325
public class ConfigManagerDependencyLocator extends JarInJarDependencyLocator implements IDependencyLocator {
24-
public static final Logger LOGGER = LoggerFactory.getLogger("ConfigManagerDependencyLocator");
26+
private static final Logger LOGGER = LogManager.getLogger(MOD_ID + "_dependency_locator");
2527

2628
@Override
2729
public void scanMods(List<IModFile> loadedMods, IDiscoveryPipeline pipeline) {

neoforge_coremod/src/main/java/io/github/thebossmagnus/mods/config_manager/core_mod/services/ConfigManagerTransformationService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import cpw.mods.modlauncher.api.ITransformationService;
1010
import cpw.mods.modlauncher.api.ITransformer;
1111
import io.github.thebossmagnus.mods.config_manager.common_coremod.ConfigManagerStartup;
12-
import io.github.thebossmagnus.mods.config_manager.common_coremod.CopyConfig;
1312
import net.neoforged.fml.loading.FMLLoader;
1413
import org.jetbrains.annotations.NotNull;
1514

0 commit comments

Comments
 (0)