-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
ValueReferences.java
169 lines (154 loc) · 6.1 KB
/
ValueReferences.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Configurate
* Copyright (C) zml and Configurate contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ninja.leaping.configurate.examples;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.reference.ConfigurationReference;
import ninja.leaping.configurate.reference.ValueReference;
import ninja.leaping.configurate.reference.WatchServiceListener;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import ninja.leaping.configurate.transformation.NodePath;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
public class ValueReferences {
private final WatchServiceListener listener;
private final ConfigurationReference<CommentedConfigurationNode> base;
private final ValueReference<String> name;
private final ValueReference<Integer> cookieCount;
private final ValueReference<List<TestObject>> complex;
@ConfigSerializable
static class TestObject {
@Setting
String name;
@Setting
UUID id = UUID.randomUUID();
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TestObject)) return false;
TestObject that = (TestObject) o;
return name.equals(that.name) &&
id.equals(that.id);
}
@Override
public int hashCode() {
return Objects.hash(name, id);
}
@Override
public String toString() {
return "TestObject{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
public ValueReferences(Path configFile) throws IOException, ObjectMappingException {
this.listener = WatchServiceListener.create();
this.base = listener.listenToConfiguration(file -> HoconConfigurationLoader.builder().setDefaultOptions(o -> o.withShouldCopyDefaults(true)).setPath(file).build(), configFile);
this.base.updates().subscribe($ -> System.out.println("Configuration auto-reloaded"));
this.base.errors().subscribe(err -> {
Throwable t = err.getValue();
System.out.println("Unable to " + err.getKey() + " the configuration: " + t.getMessage());
if (t.getCause() != null) {
System.out.println(t.getCause().getMessage());
}
});
name = this.base.referenceTo(String.class, "name");
this.name.subscribe(newName -> System.out.println("Reloaded, name is: " + newName));
cookieCount = this.base.referenceTo(Integer.class, new Object[] {"cookie-count"}, 5);
this.complex = this.base.referenceTo(new TypeToken<List<TestObject>>() {}, "complex");
this.base.save();
}
public void repl() {
boolean running = true;
if (System.console() == null) {
System.err.println("Not at an interactive prompt");
this.printData();
return;
}
while (running) {
String next = System.console().readLine(">");
if (next == null) {
break;
}
String[] cmd = next.split(" ");
if (cmd.length == 0) {
continue;
}
switch (cmd[0]) {
case "stop":
running = false;
break;
case "name":
if (cmd.length < 2) {
System.err.println("Not enough arguments, usage: name <new-name>");
break;
}
name.setAndSave(cmd[1]);
System.out.println("Name: " + this.name.get());
break;
case "dump":
printData();
break;
case "help":
System.out.println("Value reference tester\n" +
"Commands:\n\n" +
"stop: Exit the loop\n" +
"name <name>: Update the name in the config file\n" +
"dump: Dump all accessed data in the config file\n" +
"help: Show this message"
);
break;
default:
System.err.println("Unknown command '" + next + "'");
System.err.println("help for help");
}
}
}
public void printData() {
System.out.println("Name: " + this.name.get());
System.out.println("Cookie count: " + this.cookieCount.get());
System.out.println("Complex: ");
if (this.complex.get().isEmpty()) {
System.out.println("(empty)");
} else {
for (TestObject obj : this.complex.get()) {
System.out.println("- " + obj);
}
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: ./reference-example <file>");
return;
}
final Path path = Paths.get(args[0]);
try {
new ValueReferences(path).repl();
} catch (IOException | ObjectMappingException e) {
System.out.println("Error loading configuration: " + e.getMessage());
e.printStackTrace();
}
}
}