Skip to content

Commit 8201a9d

Browse files
committed
regex set-count
1 parent bd244d4 commit 8201a9d

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package de.pdinklag.mcstats;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
import java.util.regex.Pattern;
7+
8+
/**
9+
* Reads a set of strings whose keys match a regular expression.
10+
*/
11+
public class RegexSetCountReader extends NestedDataReader {
12+
private final Pattern[] patterns;
13+
14+
/**
15+
* Constructs a regex set count reader.
16+
*
17+
* @param path The JSON object names on the path to the set to read.
18+
* @param patterns The regular expression patterns to match the keys against.
19+
*/
20+
public RegexSetCountReader(String[] path, String[] patterns) {
21+
super(path);
22+
this.patterns = new Pattern[patterns.length];
23+
for (int i = 0; i < patterns.length; i++) {
24+
this.patterns[i] = Pattern.compile(patterns[i]);
25+
}
26+
}
27+
28+
@Override
29+
protected DataValue getDefaultValue() {
30+
return new StringSetValue();
31+
}
32+
33+
@Override
34+
protected DataValue read(JSONObject obj, String key) {
35+
final StringSetValue set = new StringSetValue();
36+
final JSONObject sub = obj.optJSONObject(key);
37+
if (sub != null) {
38+
for (String subKey : sub.keySet()) {
39+
for (Pattern pattern : patterns) {
40+
if (pattern.matcher(subKey).matches()) {
41+
set.add(subKey);
42+
break;
43+
}
44+
}
45+
}
46+
}
47+
return set;
48+
}
49+
50+
@Override
51+
public DataAggregator createDefaultAggregator() {
52+
return new StringSetUnionAggregator();
53+
}
54+
}

src/main/java/de/pdinklag/mcstats/StatParser.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,24 @@ private static SetCountReader parseSetCountReader(JSONObject obj) throws JSONExc
5050
return new SetCountReader(path);
5151
}
5252

53+
private static RegexSetCountReader parseRegexSetCountReader(JSONObject obj) throws JSONException {
54+
JSONArray jsonPath = obj.getJSONArray("path");
55+
String[] path = new String[jsonPath.length()];
56+
for (int i = 0; i < path.length; i++) {
57+
path[i] = jsonPath.getString(i);
58+
}
59+
60+
JSONArray jsonPatterns = obj.getJSONArray("patterns");
61+
String[] patterns = new String[jsonPatterns.length()];
62+
for (int i = 0; i < patterns.length; i++) {
63+
patterns[i] = jsonPatterns.getString(i);
64+
}
65+
return new RegexSetCountReader(path, patterns);
66+
}
67+
5368
private static DataReader parseReader(JSONObject obj) throws JSONException, StatParseException {
5469
String type = obj.getString("$type");
55-
switch(type) {
70+
switch (type) {
5671
case "int":
5772
return parseIntReader(obj);
5873
case "sum":
@@ -61,6 +76,8 @@ private static DataReader parseReader(JSONObject obj) throws JSONException, Stat
6176
return parseMatchSumReader(obj);
6277
case "set-count":
6378
return parseSetCountReader(obj);
79+
case "regex-set-count":
80+
return parseRegexSetCountReader(obj);
6481
default:
6582
throw new StatParseException("unsupported reader type: " + type);
6683
}

stats/advancement.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"id": "advancement",
33
"unit": "int",
44
"reader": {
5-
"$type": "set-count",
6-
"path": ["advancements"]
5+
"$type": "regex-set-count",
6+
"path": ["advancements"],
7+
"patterns": ["^minecraft:(story|nether|end|adventure|husbandry)"]
78
}
89
}

0 commit comments

Comments
 (0)