This repository has been archived by the owner on Nov 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
/
AdvancementJsonHelper.java
69 lines (60 loc) · 2.06 KB
/
AdvancementJsonHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.hm.achievement.advancement;
import static com.hm.achievement.advancement.AchievementAdvancement.CRITERIA_NAME;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
/**
* Class used to generate JSON strings for advancements.
*
* @author Pyves
*/
public class AdvancementJsonHelper {
public static String toJson(AchievementAdvancement aa) {
return "{\n" +
" \"criteria\":{\n" +
" \"" + CRITERIA_NAME + "\":{\n" +
" \"trigger\":\"minecraft:impossible\"\n" +
" }\n" +
" },\n" +
" \"requirements\":[\n" +
" [\n" +
" \"" + CRITERIA_NAME + "\"\n" +
" ]\n" +
" ],\n" +
" \"display\":{\n" +
" \"icon\":{\n" +
" \"item\":\"" + aa.getIconItem() + "\"" + getIntegerFieldOrEmpty("data", aa.getIconData()) + "\n" +
" },\n" +
" \"title\":\"" + StringEscapeUtils.escapeJson(aa.getTitle()) + "\",\n" +
" \"description\":\"" + StringEscapeUtils.escapeJson(aa.getDescription()) + "\",\n" +
" \"frame\":\"" + aa.getFrame() + "\",\n" +
" \"announce_to_chat\":false" +
getStringFieldOrLineBreak("background", aa.getBackground(), 4) +
" }" +
getStringFieldOrLineBreak("parent", aa.getParent(), 2) +
"}\n";
}
public static String toHiddenJson(String background) {
return "{\n" +
" \"criteria\":{\n" +
" \"" + CRITERIA_NAME + "\":{\n" +
" \"trigger\":\"minecraft:impossible\"\n" +
" }\n" +
" },\n" +
" \"requirements\":[\n" +
" [\n" +
" \"" + CRITERIA_NAME + "\"\n" +
" ]\n" +
" ],\n" +
" \"background\":\"" + background + "\"\n" +
"}\n";
}
private static String getIntegerFieldOrEmpty(String key, String value) {
return value == null ? "" : ",\"" + key + "\":" + value;
}
private static String getStringFieldOrLineBreak(String key, String value, int spacing) {
return value == null ? "\n" : ",\n" + StringUtils.repeat(' ', spacing) + "\"" + key + "\":\"" + value + "\"\n";
}
private AdvancementJsonHelper() {
// Not called.
}
}