Skip to content

Commit

Permalink
MapTag.deep_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 22, 2021
1 parent af75fd1 commit b7f1998
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Expand Up @@ -126,10 +126,10 @@ public String eventArgLowerAt(int index) {
//
// Events that occur at a specific location have the "in:<area>" and "location_flagged" switches.
// This switches will be ignored (not counted one way or the other) for events that don't have a known location.
// For "in:<area>" switches, 'area' is a world, noted cuboid, or noted ellipsoid.
// For "in:<area>" switches, 'area' is a world, noted cuboid, noted ellipsoid, or noted polygon.
// So for example you might have an event line like "on player breaks block in:space:"
// where space is the name of a world or of a noted cuboid.
// This also works as "in:cuboid" or "in:ellipsoid" to match for *any* noted cuboid or ellipsoid.
// This also works as "in:cuboid" or "in:ellipsoid" or "in:polygon" to match for *any* noted cuboid, ellipsoid, or polygon.
// "location_flagged:<flag name>" works just like "server_flagged" or the player "flagged" switches, but for locations.
//
// All script events have priority switches (see <@link language script event priority>),
Expand Down
Expand Up @@ -595,12 +595,32 @@ public int compare(Map.Entry<StringHolder, ObjectTag> e1, Map.Entry<StringHolder
return result;
});

// <--[tag]
// @attribute <MapTag.deep_keys>
// @returns ListTag
// @description
// Returns a list of all keys in this map, including keys in any sub-maps (map values that are in turn MapTags), using deep key paths separated by the '.' symbol.
// No returned key value will refer to a MapTag instance.
// -->
registerTag("deep_keys", (attribute, object) -> {
ListTag result = new ListTag();
for (Map.Entry<StringHolder, ObjectTag> entry : object.map.entrySet()) {
if (entry.getValue() instanceof MapTag) {
((MapTag) entry.getValue()).appendDeepKeys(entry.getKey().str, result);
}
else {
result.add(entry.getKey().str);
}
}
return result;
});

// <--[tag]
// @attribute <MapTag.keys>
// @returns ListTag
// @description
// Returns a list of all keys in this map.
// For example, on a map of "a/1|b/2|c/3|", using "list_keys" will return "a|b|c|".
// For example, on a map of "a/1|b/2|c/3|", using "keys" will return "a|b|c|".
// -->
registerTag("keys", (attribute, object) -> {
ListTag result = new ListTag();
Expand All @@ -615,7 +635,7 @@ public int compare(Map.Entry<StringHolder, ObjectTag> e1, Map.Entry<StringHolder
// @returns ListTag
// @description
// Returns a list of all values in this map.
// For example, on a map of "a/1|b/2|c/3|", using "list_values" will return "1|2|3|".
// For example, on a map of "a/1|b/2|c/3|", using "values" will return "1|2|3|".
// -->
registerTag("values", (attribute, object) -> {
ListTag result = new ListTag();
Expand Down Expand Up @@ -680,6 +700,17 @@ public int compare(Map.Entry<StringHolder, ObjectTag> e1, Map.Entry<StringHolder
});
}

public void appendDeepKeys(String path, ListTag result) {
for (Map.Entry<StringHolder, ObjectTag> entry : map.entrySet()) {
if (entry.getValue() instanceof MapTag) {
((MapTag) entry.getValue()).appendDeepKeys(path + "." + entry.getKey().str, result);
}
else {
result.add(path + "." + entry.getKey().str);
}
}
}

public static ObjectTagProcessor<MapTag> tagProcessor = new ObjectTagProcessor<>();

public static void registerTag(String name, TagRunnable.ObjectInterface<MapTag> runnable, String... variants) {
Expand Down

0 comments on commit b7f1998

Please sign in to comment.