5
5
import java .io .FileNotFoundException ;
6
6
import java .io .FileOutputStream ;
7
7
import java .io .IOException ;
8
- import java .util .HashMap ;
8
+ import java .util .ArrayList ;
9
9
import java .util .InvalidPropertiesFormatException ;
10
- import java .util .Map ;
10
+ import java .util .LinkedHashMap ;
11
+ import java .util .List ;
11
12
import java .util .Properties ;
12
13
14
+ import com .minecrafttas .mctcommon .Configuration .ConfigOptions ;
15
+ import com .minecrafttas .mctcommon .registry .AbstractRegistry ;
16
+ import com .minecrafttas .mctcommon .registry .Registerable ;
13
17
14
18
/**
15
19
* A <i>very</i> simple configuration class
20
+ *
16
21
* @author Scribble
17
- *
18
22
*/
19
23
20
- public class Configuration {
21
-
24
+ public class Configuration extends AbstractRegistry < ConfigOptions > {
25
+
22
26
private File file ;
23
-
27
+
24
28
private Properties properties ;
25
29
26
30
private String comment ;
27
-
31
+
28
32
public Configuration (String comment , File configFile ) {
33
+ super ("Configuration" , new LinkedHashMap <>());
34
+
29
35
file = configFile ;
30
36
this .comment = comment ;
37
+ }
38
+
39
+ protected final List <ConfigOptions > configRegistry = new ArrayList <>();
40
+
41
+ @ Override
42
+ public void register (ConfigOptions registryObject ) {
43
+ if (registryObject == null ) {
44
+ return ;
45
+ }
46
+
47
+ if (configRegistry .contains (registryObject )) {
48
+ return ;
49
+ }
50
+
51
+ configRegistry .add (registryObject );
52
+ }
53
+
54
+ @ Override
55
+ public void unregister (ConfigOptions registryObject ) {
56
+ if (registryObject == null ) {
57
+ return ;
58
+ }
59
+
60
+ if (!configRegistry .contains (registryObject )) {
61
+ return ;
62
+ }
31
63
32
- if (file .exists ()) {
33
- properties = load ();
64
+ configRegistry .remove (registryObject );
65
+ }
66
+
67
+ public void load () {
68
+ if (file .exists ()) {
69
+ properties = loadInner ();
34
70
}
35
- if (properties == null || !file .exists ()) {
71
+ if (properties == null || !file .exists ()) {
36
72
properties = generateDefault ();
37
73
save ();
38
- }
74
+ }
39
75
}
40
76
41
- public Properties load () {
77
+ private Properties loadInner () {
42
78
FileInputStream fis ;
43
79
Properties newProp = new Properties ();
44
80
try {
@@ -57,11 +93,11 @@ public Properties load() {
57
93
}
58
94
return newProp ;
59
95
}
60
-
96
+
61
97
public void save () {
62
98
save (file );
63
99
}
64
-
100
+
65
101
public void save (File file ) {
66
102
try {
67
103
FileOutputStream fos = new FileOutputStream (file );
@@ -71,73 +107,62 @@ public void save(File file) {
71
107
e .printStackTrace ();
72
108
}
73
109
}
74
-
110
+
75
111
public Properties generateDefault () {
76
112
Properties newProperties = new Properties ();
77
- newProperties .putAll (ConfigOptions .getDefaultValues ());
113
+ configRegistry .forEach ((configOption )->{
114
+ newProperties .put (configOption .getConfigKey (), configOption .getDefaultValue ());
115
+ });
78
116
return newProperties ;
79
117
}
80
-
118
+
81
119
public String get (ConfigOptions configOption ) {
82
- return properties .getProperty (configOption .configKey );
120
+ return properties .getProperty (configOption .getConfigKey (), configOption . getDefaultValue () );
83
121
}
84
-
122
+
85
123
public int getInt (ConfigOptions configOption ) {
86
124
return Integer .parseInt (get (configOption ));
87
125
}
88
-
126
+
89
127
public boolean getBoolean (ConfigOptions configOption ) {
90
128
return Boolean .parseBoolean (get (configOption ));
91
129
}
92
-
130
+
93
131
public boolean has (ConfigOptions configOption ) {
94
- return properties .contains (configOption .configKey );
132
+ return properties .contains (configOption .getConfigKey () );
95
133
}
96
-
134
+
97
135
public void set (ConfigOptions configOption , String value ) {
98
- properties .setProperty (configOption .configKey , value );
136
+ if (properties == null ) {
137
+ throw new NullPointerException ("Config needs to be loaded first, before trying to set a value" );
138
+ }
139
+ properties .setProperty (configOption .getConfigKey (), value );
99
140
save ();
100
141
}
101
-
142
+
102
143
public void set (ConfigOptions configOption , int value ) {
103
144
String val = Integer .toString (value );
104
145
set (configOption , val );
105
146
}
106
-
147
+
107
148
public void set (ConfigOptions configOption , boolean value ) {
108
149
String val = Boolean .toString (value );
109
150
set (configOption , val );
110
151
}
111
-
152
+
112
153
public void reset (ConfigOptions configOption ) {
113
- set (configOption , configOption .defaultValue );
154
+ set (configOption , configOption .getDefaultValue () );
114
155
}
115
-
156
+
116
157
public void delete (ConfigOptions configOption ) {
117
158
properties .remove (configOption );
118
159
save ();
119
160
}
120
-
121
- public static enum ConfigOptions {
122
- FileToOpen ("fileToOpen" , "" ),
123
- ServerConnection ("serverConnection" , "" );
124
-
125
- private String configKey ;
126
- private String defaultValue ;
127
-
128
- private ConfigOptions (String configKey , String defaultValue ) {
129
- this .configKey = configKey ;
130
- this .defaultValue = defaultValue ;
131
- }
132
-
133
- public static Map <String , String > getDefaultValues () {
134
- Map <String , String > out = new HashMap <>();
135
- for (ConfigOptions configthing : values ()) {
136
- if (configthing .defaultValue !=null ) {
137
- out .put (configthing .configKey , configthing .defaultValue );
138
- }
139
- }
140
- return out ;
141
- }
161
+
162
+ public interface ConfigOptions extends Registerable {
163
+
164
+ public String getDefaultValue ();
165
+
166
+ public String getConfigKey ();
142
167
}
143
168
}
0 commit comments