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
|
package re.jag.mcqtt;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
public class McqttSettings {
private Properties properties = new Properties();
private boolean changed_config = false;
public final String mqtt_server;
public final String mqtt_user;
public final String mqtt_password;
public final String mqtt_certificate;
public final String mqtt_base_topic;
public final int publish_interval;
public McqttSettings(String _properties_file) {
Path path = FileSystems.getDefault().getPath(_properties_file);
load(path);
this.mqtt_server = get_string("mqtt-server", "tcp://localhost");
this.mqtt_user = get_string("mqtt-user", "username");
this.mqtt_password = get_string("mqtt-password", "p4assword");
this.mqtt_certificate = get_string("mqtt-certificate", "");
this.mqtt_base_topic = get_string("mqtt-base-topic", "minecraft");
this.publish_interval = get_int("publish-interval-mins", 5);
if(changed_config)
save(path);
}
private void load(Path _path) {
try {
InputStream inp = Files.newInputStream(_path);
properties.load(inp);
} catch (IOException e) {
Mcqtt.LOG.warn("Failed to read from " + _path.getFileName());
}
}
private void save(Path _path) {
try {
OutputStream out = Files.newOutputStream(_path);
properties.store(out, "McQTT mod settings");
} catch (IOException e) {
Mcqtt.LOG.error("Failed to write to " + _path.getFileName());
}
}
private String get(String _name, String _default) {
String val = properties.getProperty(_name);
if (val == null) {
properties.setProperty(_name, _default);
changed_config = true;
return _default;
}
return val;
}
private boolean get_boolean(String _name, boolean _default) {
return Boolean.parseBoolean(get(_name, String.valueOf(_default)));
}
private String get_string(String _name, String _default) {
return get(_name, _default);
}
private int get_int(String _name, int _default) {
return Integer.parseInt(get(_name, String.valueOf(_default)));
}
}
|