Skip to content

Commit 5b44e1a

Browse files
committed
Update GroovyPreferences to work with Arrays and Collections

1 parent 577bbc9 commit 5b44e1a

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

src/main/java/jaci/openrio/toast/core/loader/groovy/GroovyPreferences.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import jaci.openrio.toast.core.ToastBootstrap;
77

88
import java.io.*;
9+
import java.util.Arrays;
10+
import java.util.List;
911

1012
/**
1113
* The class for all Groovy-Based preference files. Groovy preference files
@@ -106,8 +108,7 @@ public Object invokePreferenceMethod(String methodName, Object... args) {
106108
public void writeKey(String key, Object value, String... comment) {
107109
try {
108110
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(parentFile, true)));
109-
if (value instanceof String)
110-
value = "\"" + value + "\"";
111+
value = convertValue(value);
111112
if (comment != null)
112113
for (String c : comment) out.println("// " + c);
113114
out.println(key + " = " + value);
@@ -117,6 +118,21 @@ public void writeKey(String key, Object value, String... comment) {
117118

118119
}
119120

121+
public Object convertValue(Object value) {
122+
if (value instanceof String)
123+
value = "\"" + value + "\"";
124+
if (value instanceof List) {
125+
convertValue(((List) value).toArray());
126+
} else if (value.getClass().isArray()) {
127+
Object[] array = (Object[]) value;
128+
Object[] v = new Object[array.length];
129+
for (int i = 0; i < array.length; i++)
130+
v[i] = convertValue(array[i]);
131+
return Arrays.toString(v);
132+
}
133+
return value;
134+
}
135+
120136
// PRIMS //
121137

122138
/**
@@ -130,11 +146,19 @@ public boolean keyExists(String key) {
130146
* Get an object, with a default value if it doesn't exist
131147
*/
132148
public Object getObject(String key, Object defaultValue, String... comment) {
149+
Object val = defaultValue;
133150
if (keyExists(key))
134-
return getPreferenceObject(key);
135-
else
151+
val = getPreferenceObject(key);
152+
else {
136153
writeKey(key, defaultValue, comment);
137-
return defaultValue;
154+
try {
155+
load();
156+
val = getPreferenceObject(key);
157+
} catch (Exception e) {
158+
e.printStackTrace();
159+
}
160+
}
161+
return val;
138162
}
139163

140164
/**

0 commit comments

Comments
 (0)