tangled
alpha
login
or
join now
codexarchonic.nekoweb.org
/
ProjectInfinity
0
fork
atom
Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
0
fork
atom
overview
issues
6
pulls
pipelines
Dropdowns!
cassian.cc
9 months ago
c98561db
69312a89
+32
-24
1 changed file
expand all
collapse all
unified
split
common
src
main
java
net
lerariemann
infinity
compat
cloth
AmendmentConfigFactory.java
+32
-24
common/src/main/java/net/lerariemann/infinity/compat/cloth/AmendmentConfigFactory.java
···
1
1
package net.lerariemann.infinity.compat.cloth;
2
2
3
3
+
import com.google.common.collect.Lists;
3
4
import com.google.gson.JsonArray;
4
5
import com.google.gson.JsonElement;
5
6
import com.google.gson.JsonObject;
6
7
import me.shedaniel.clothconfig2.api.ConfigBuilder;
7
8
import me.shedaniel.clothconfig2.api.ConfigCategory;
8
8
-
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
9
9
import me.shedaniel.clothconfig2.impl.builders.DropdownMenuBuilder;
10
10
import me.shedaniel.clothconfig2.impl.builders.SubCategoryBuilder;
11
11
import net.lerariemann.infinity.util.core.CommonIO;
12
12
-
import net.minecraft.item.Item;
13
13
-
import net.minecraft.item.Items;
14
12
import net.minecraft.nbt.NbtCompound;
15
13
import net.minecraft.nbt.NbtElement;
16
14
import net.minecraft.nbt.NbtList;
17
15
import net.minecraft.nbt.NbtString;
18
18
-
import net.minecraft.registry.Registries;
19
16
import net.minecraft.text.Style;
20
17
import net.minecraft.text.Text;
21
18
22
19
import java.util.*;
23
23
-
import java.util.stream.Collectors;
24
20
25
21
import static net.lerariemann.infinity.compat.cloth.ClothConfigFactory.*;
26
22
27
23
public class AmendmentConfigFactory {
24
24
+
enum AreasEnum {
25
25
+
BLOCKS,
26
26
+
FLUIDS,
27
27
+
ITEMS,
28
28
+
STRUCTURES,
29
29
+
MOBS;
30
30
+
}
31
31
+
28
32
public static void build(ConfigBuilder builder) {
33
33
+
34
34
+
29
35
ConfigCategory amendmentCategory = builder.getOrCreateCategory(Text.translatable("config.infinity.title.amendments"));
30
36
JsonObject amendmentList = readJson(configPath()+("/amendments.json")).getAsJsonObject();
31
37
var elements = amendmentList.getAsJsonArray("elements");
···
33
39
for (JsonElement amendmentElement : elements) {
34
40
SubCategoryBuilder subCategory = builder.entryBuilder().startSubCategory(Text.translatable("config.infinity.amendment", String.valueOf(i)));
35
41
JsonObject amendment = amendmentElement.getAsJsonObject();
36
36
-
addStringOption("area", builder, subCategory, i, amendment);
42
42
+
addStringDropdownOption("area", builder, subCategory, i, amendment, Lists.newArrayList("blocks", "fluids", "items", "structures", "trees", "mobs"));
37
43
addStringOption("mod", builder, subCategory, i, amendment);
38
38
-
addStringOption("selector", builder, subCategory, i, amendment);
44
44
+
addStringDropdownOption("selector", builder, subCategory, i, amendment, Lists.newArrayList("all", "matching", "matching_any", "matching_block_tag", "containing"));
39
45
if (amendment.get("selector").getAsString().equals("matching_any"))
40
46
addListOption("matching", builder, subCategory, i, amendment);
41
47
else if (amendment.get("selector").getAsString().equals("matching")) {
42
48
addStringOption("matching", builder, subCategory, i, amendment);
43
49
}
44
44
-
addStringOption("results", builder, subCategory, i, amendment);
50
50
+
addStringDropdownOption("results", builder, subCategory, i, amendment, Lists.newArrayList("set_value", "set_field", "erase"));
45
51
if (amendment.get("results").getAsString().equals("set_value"))
46
52
addDoubleOption("value", builder, subCategory, i, amendment);
47
53
else if (amendment.get("results").getAsString().equals("set_field"))
48
48
-
addStringOption("field_name", builder, subCategory, i, amendment);
54
54
+
addStringDropdownOption("field_name", builder, subCategory, i, amendment, Lists.newArrayList("full", "float", "top", "laggy"));
49
55
50
56
amendmentCategory.addEntry(subCategory.build());
51
57
i++;
···
65
71
66
72
}
67
73
74
74
+
private static void addStringDropdownOption(String name, ConfigBuilder builder, SubCategoryBuilder subCategory, int i, JsonObject amendment, List<String> options) {
75
75
+
String current;
76
76
+
if (amendment.get(name) == null) current = "";
77
77
+
else current = amendment.get(name).getAsString();
78
78
+
subCategory.add(builder.entryBuilder().startDropdownMenu(Text.translatable("config.infinity.amendments."+name), DropdownMenuBuilder.TopCellElementBuilder.of(current, (s) -> s))
79
79
+
.setTooltip(amendmentTooltip(name))
80
80
+
.setSuggestionMode(false)
81
81
+
.setSaveConsumer((value)-> amendmentSetter(name, String.valueOf(value), i))
82
82
+
.setSelections(options).build());
83
83
+
}
84
84
+
68
85
private static void addDoubleOption(String name, ConfigBuilder builder, SubCategoryBuilder subCategory, int i, JsonObject amendment) {
69
86
subCategory.add(builder.entryBuilder().startDoubleField(
70
87
Text.translatable("config.infinity.amendments."+name),
71
88
amendment.get(name).getAsDouble())
72
89
.setTooltip(amendmentTooltip(name))
73
90
.setSaveConsumer((value)-> amendmentSetter(name, value, i)).build());
74
74
-
75
91
}
76
92
77
93
private static void addListOption(String name, ConfigBuilder builder, SubCategoryBuilder subCategory, int i, JsonObject amendment) {
94
94
+
var list = convertNbtList(amendment.get(name).getAsJsonArray());
95
95
+
if (!Objects.equals(list.getLast(), ""))
96
96
+
list.add("");
78
97
subCategory.add(builder.entryBuilder().startStrList(
79
98
Text.translatable("config.infinity.amendments."+name),
80
80
-
convertNbtList(amendment.get(name).getAsJsonArray()))
99
99
+
list)
81
100
.setTooltip(amendmentTooltip(name))
82
101
.setSaveConsumer((value)-> amendmentSetter(name, value, i)).build());
83
83
-
84
102
}
85
103
86
86
-
87
87
-
88
88
-
89
104
static void amendmentSetter(String name, String newValue, int amendmentIndex) {
90
105
NbtCompound elements = readNbt(configPath()+("/amendments.json"));
91
106
NbtCompound amendment = elements.getList("elements", 10).getCompound(amendmentIndex);
···
107
122
}
108
123
109
124
static void amendmentSetter(String name, List<String> newValue, int amendmentIndex) {
125
125
+
if (Objects.equals(newValue.getLast(), ""))
126
126
+
newValue.removeLast();
110
127
NbtCompound elements = readNbt(configPath()+("/amendments.json"));
111
128
NbtCompound amendment = elements.getList("elements", NbtElement.COMPOUND_TYPE).getCompound(amendmentIndex);
112
129
// TODO Check if an amendment should be changed before writing
113
130
amendment.put(name, convertNbtList(newValue));
114
131
CommonIO.write(elements, configPath(), "amendments.json");
115
115
-
116
132
}
117
133
118
134
static NbtList convertNbtList(List<String> list) {
···
121
137
nbtList.add(NbtString.of(s));
122
138
}
123
139
return nbtList;
124
124
-
}
125
125
-
126
126
-
static List<String> convertNbtList(NbtList nbtList) {
127
127
-
ArrayList<String> list = new ArrayList<>();
128
128
-
for (NbtElement s : nbtList) {
129
129
-
list.add(s.asString());
130
130
-
}
131
131
-
return list;
132
140
}
133
141
134
142
static List<String> convertNbtList(JsonArray nbtList) {