Skip to content
This repository was archived by the owner on Feb 22, 2021. It is now read-only.

Commit 8108cd6

Browse files
committed
Extract actual value type from an object
1 parent 91c0225 commit 8108cd6

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/main/java/de/kaleidox/javacord/util/server/properties/ServerPropertiesManager.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,12 @@ public Object propertyCommand(Command.Parameters param) {
140140
.setColor(Color.RED)
141141
.setDescription("Unknown property: `" + args[0] + "`");
142142

143-
if (args[1].equalsIgnoreCase("#default#"))
144-
args[1] = String.valueOf(propertySet.getDefaultValue().getValue());
143+
Object setTo = extractValue(args[1]);
145144

146-
value.setter().toString(args[1]);
145+
if (setTo.equals("#default#"))
146+
setTo = String.valueOf(propertySet.getDefaultValue().getValue());
147+
148+
value.setter().toObject(setTo);
147149

148150
return embedSupplier.get()
149151
.setDescription("Changed property `" + args[0] + "` to new value: `" + value.asString() + "`")
@@ -187,6 +189,27 @@ public void storeData() throws IOException {
187189
stream.close();
188190
}
189191

192+
private Object extractValue(String val) {
193+
if (val.matches("\\d+")) {
194+
// is number without decimals
195+
long longVal = Long.parseLong(val);
196+
197+
if (longVal <= Byte.MAX_VALUE) return (byte) longVal;
198+
else if (longVal <= Short.MAX_VALUE) return (short) longVal;
199+
else if (longVal <= Integer.MAX_VALUE) return (int) longVal;
200+
else return longVal;
201+
} else if (val.matches("\\d+\\.\\d+")) {
202+
// is number with decimals
203+
return Double.parseDouble(val);
204+
} else if (val.toLowerCase().matches("(true)|(false)")) {
205+
// is boolean
206+
return Boolean.valueOf(val);
207+
} else {
208+
// is plain string
209+
return val;
210+
}
211+
}
212+
190213
private void readData() throws IOException {
191214
int c = 0;
192215
JsonNode node = new ObjectMapper().readTree(new FileInputStream(propertiesFile));

0 commit comments

Comments
 (0)