-
Notifications
You must be signed in to change notification settings - Fork 9
/
BSConfigFile.java
85 lines (70 loc) · 2.57 KB
/
BSConfigFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.battleship.utils;
import java.awt.*;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
public class BSConfigFile {
private static final Properties props = new Properties();
public static Path bsConfigFile;
// TODO: write JavaDoc
public static void newFile() {
Properties props = new Properties();
props.setProperty("Name", "UserName");
props.setProperty("Avatar_Path", "src/resources/anonymous.png");
props.setProperty("Resolution_Width", "1000");
props.setProperty("Resolution_Height", "500");
props.setProperty("Color", "BLUE");
Path propertyFile = Paths.get("config.properties");
try {
Writer propWriter = Files.newBufferedWriter(propertyFile);
props.store(propWriter, "Settings");
propWriter.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public static String readProperties(String key) {
String value;
try {
bsConfigFile = Paths.get("config.properties");
Reader reader = Files.newBufferedReader(bsConfigFile);
props.load(reader);
reader.close();
value = props.getProperty(key);
} catch (IOException e) {
newFile();
value = props.getProperty(key);
}
return value;
}
public static void modifyFile(String key, String value) {
try {
props.setProperty(key, value);
Writer writer = Files.newBufferedWriter(bsConfigFile);
props.store(writer, "Settings");
writer.close();
} catch (IOException | NullPointerException ex) {
newFile();
}
}
public static Color manageColors(String colorString) {
return switch (colorString) {
case "CYAN" -> Color.CYAN;
case "GREEN" -> Color.GREEN;
case "MAGENTA" -> Color.MAGENTA;
case "ORANGE" -> Color.ORANGE;
case "BLACK" -> Color.BLACK;
default -> Color.BLUE;
};
}
public static void updateConfiguration(String newName,String setColor, String resolutionWidth, String resolutionHeight){
BSConfigFile.modifyFile("Name", newName);
BSConfigFile.modifyFile("Color", setColor);
BSConfigFile.modifyFile("Resolution_Width", resolutionWidth);
BSConfigFile.modifyFile("Resolution_Height", resolutionHeight);
}
}