Skip to content
This repository was archived by the owner on Apr 12, 2022. It is now read-only.

Commit e10de2e

Browse files
committed
object type handling improvements
1 parent 1bf4edc commit e10de2e

File tree

3 files changed

+72
-17
lines changed

3 files changed

+72
-17
lines changed

src/main/java/com/denizenscript/denizen2core/arguments/TextArgumentBit.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import com.denizenscript.denizen2core.commands.CommandQueue;
44
import com.denizenscript.denizen2core.tags.AbstractTagObject;
5-
import com.denizenscript.denizen2core.tags.objects.IntegerTag;
6-
import com.denizenscript.denizen2core.tags.objects.TextTag;
5+
import com.denizenscript.denizen2core.tags.objects.*;
76
import com.denizenscript.denizen2core.DebugMode;
87
import com.denizenscript.denizen2core.utilities.Action;
98

@@ -12,16 +11,42 @@
1211
public class TextArgumentBit extends ArgumentBit {
1312

1413
public TextArgumentBit(String inputText, boolean quoted) {
15-
AbstractTagObject obj;
14+
wasQuoted = quoted;
1615
try {
17-
Long l = Long.parseLong(inputText);
18-
obj = new IntegerTag(l);
16+
long l = Long.parseLong(inputText);
17+
if (String.valueOf(l).equals(inputText)) {
18+
value = new IntegerTag(l);
19+
return;
20+
}
1921
}
2022
catch (NumberFormatException ex) {
21-
obj = new TextTag(inputText);
23+
// Ignore
2224
}
23-
value = obj;
24-
wasQuoted = quoted;
25+
try {
26+
double d = Double.parseDouble(inputText);
27+
if (String.valueOf(d).equals(inputText)) {
28+
value = new NumberTag(d);
29+
return;
30+
}
31+
}
32+
catch (NumberFormatException ex) {
33+
// Ignore
34+
}
35+
ListTag ltTest = ListTag.getFor(TextArgumentBit::noAction, inputText);
36+
if (ltTest != null && ltTest.toString().equals(inputText)) {
37+
value = ltTest;
38+
return;
39+
}
40+
MapTag mtTest = MapTag.getFor(TextArgumentBit::noAction, inputText);
41+
if (mtTest != null && mtTest.toString().equals(inputText)) {
42+
value = mtTest;
43+
return;
44+
}
45+
value = new TextTag(inputText);
46+
}
47+
48+
public static void noAction(String err) {
49+
// Ignore
2550
}
2651

2752
public AbstractTagObject value;

src/main/java/com/denizenscript/denizen2core/tags/objects/ScriptTag.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.denizenscript.denizen2core.utilities.yaml.StringHolder;
1111

1212
import java.util.HashMap;
13-
import java.util.List;
1413
import java.util.Set;
1514

1615
public class ScriptTag extends AbstractTagObject {
@@ -90,25 +89,21 @@ public CommandScript getInternal() {
9089
return new TextTag(val);
9190
});
9291
// <--ScriptTag
93-
// @Name YamlTag.read_list[<TextTag>]
92+
// @Name ScriptTag.yaml_list[<TextTag>]
9493
// @Updated 2017/04/27
9594
// @Group Identification
9695
// @ReturnType ListTag
97-
// @Returns the contents of the YAML key, as a list of text.
96+
// @Returns the contents of the YAML key, as a list of tag objects.
9897
// -->
9998
handlers.put("yaml_list", (dat, obj) -> {
100-
List<String> val = ((ScriptTag) obj).internal.contents.getStringList(dat.getNextModifier().toString());
99+
ListTag val = ((ScriptTag) obj).internal.contents.getListTag(dat.getNextModifier().toString());
101100
if (val == null) {
102101
if (!dat.hasFallback()) {
103102
dat.error.run("No valid list at the specified YAML key! Does it exist?");
104103
}
105104
return new NullTag();
106105
}
107-
ListTag list = new ListTag();
108-
for (String str : val) {
109-
list.getInternal().add(new TextTag(str));
110-
}
111-
return list;
106+
return val;
112107
});
113108
// <--[tag]
114109
// @Since 0.3.0

src/main/java/com/denizenscript/denizen2core/utilities/yaml/YAMLConfiguration.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.denizenscript.denizen2core.utilities.yaml;
22

3+
import com.denizenscript.denizen2core.arguments.TextArgumentBit;
4+
import com.denizenscript.denizen2core.tags.AbstractTagObject;
5+
import com.denizenscript.denizen2core.tags.objects.ListTag;
6+
import com.denizenscript.denizen2core.tags.objects.MapTag;
7+
import com.denizenscript.denizen2core.tags.objects.TextTag;
38
import com.denizenscript.denizen2core.utilities.CoreUtilities;
49
import org.yaml.snakeyaml.DumperOptions;
510
import org.yaml.snakeyaml.Yaml;
@@ -225,6 +230,36 @@ public boolean isList(String path) {
225230
return true;
226231
}
227232

233+
public AbstractTagObject objectFor(Object obj) {
234+
if (obj instanceof List) {
235+
return listify((List<Object>) obj);
236+
}
237+
if (obj instanceof Map) {
238+
return mapify((Map<Object, Object>) obj);
239+
}
240+
return new TextArgumentBit(obj.toString(), false).value;
241+
}
242+
243+
public MapTag mapify(Map<Object, Object> map) {
244+
MapTag mt = new MapTag();
245+
for (Map.Entry<Object, Object> entry : map.entrySet()) {
246+
mt.getInternal().put(CoreUtilities.toLowerCase(entry.getKey().toString()), objectFor(entry.getValue()));
247+
}
248+
return mt;
249+
}
250+
251+
public ListTag listify(List<Object> list) {
252+
ListTag lt = new ListTag();
253+
for (Object obj : list) {
254+
lt.getInternal().add(objectFor(obj));
255+
}
256+
return lt;
257+
}
258+
259+
public ListTag getListTag(String path) {
260+
return listify(getList(path));
261+
}
262+
228263
public List<Object> getList(String path) {
229264
Object o = get(path);
230265
if (o == null) {

0 commit comments

Comments
 (0)