-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOOIConfig.java
More file actions
219 lines (199 loc) · 8.96 KB
/
Copy pathOOIConfig.java
File metadata and controls
219 lines (199 loc) · 8.96 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.circulation.only_one_item;
import com.circulation.only_one_item.conversion.FluidConversionTarget;
import com.circulation.only_one_item.conversion.ItemConversionTarget;
import com.circulation.only_one_item.emun.Type;
import com.circulation.only_one_item.handler.MatchFluidHandler;
import com.circulation.only_one_item.handler.MatchItemHandler;
import com.circulation.only_one_item.util.BlackMatchItem;
import com.circulation.only_one_item.util.MatchItem;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fml.common.Loader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Set;
public class OOIConfig {
public static final List<ItemConversionTarget> items = new ObjectArrayList<>();
public static final List<FluidConversionTarget> fluids = new ObjectArrayList<>();
public static final Set<BlackMatchItem> blackList = new ObjectOpenHashSet<>();
public static boolean clearDuplicateRecipes = true;
public static boolean initializeBeforeCraftTweakerActions = true;
public static boolean processOreDictionaryAtLoadComplete = false;
public static void readConfig() {
var configPath = Loader.instance().getConfigDir().toPath().resolve("ooi");
var ooiPath = configPath.resolve("ooi_item.json");
var blackPath = configPath.resolve("ooi_item_black_list.json");
var ooiFluidPath = configPath.resolve("ooi_fluid.json");
readGeneralConfig(Loader.instance().getConfigDir());
try {
Files.createDirectories(configPath);
} catch (IOException e) {
throw new RuntimeException(e);
}
var config = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
if (Files.exists(ooiPath)) {
try {
addItemTargets(config.fromJson(new String(Files.readAllBytes(ooiPath), StandardCharsets.UTF_8), (new TypeToken<Set<ItemConversionTarget>>() {
}).getType()));
} catch (Exception ignored) {
OnlyOneItem.LOGGER.error("[OOI]The config/ooi/ooi_item.json file is incorrect!");
}
} else {
ClassLoader classLoader = OOIConfig.class.getClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream("ooi_item.json")) {
if (inputStream != null) {
Files.copy(inputStream, ooiPath);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (Files.exists(ooiFluidPath)) {
try {
addFluidTargets(config.fromJson(new String(Files.readAllBytes(ooiFluidPath), StandardCharsets.UTF_8), (new TypeToken<Set<FluidConversionTarget>>() {
}).getType()));
} catch (Exception ignored) {
OnlyOneItem.LOGGER.error("[OOI]The config/ooi/ooi_fluid.json file is incorrect!");
}
} else {
fluids.add(new FluidConversionTarget(FluidRegistry.WATER.getName()).addMatchFluid(FluidRegistry.WATER.getName()));
try {
Files.write(ooiFluidPath, config.toJson(fluids).getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (Files.exists(blackPath)) {
try {
addBlackList(config.fromJson(new String(Files.readAllBytes(blackPath), StandardCharsets.UTF_8), (new TypeToken<Set<BlackMatchItem>>() {
}).getType()));
} catch (Exception ignored) {
OnlyOneItem.LOGGER.error("[OOI]The config/ooi/ooi_item_black_list.json file is incorrect!");
}
} else {
blackList.add(BlackMatchItem.getInstance("minecraft:gold_ingot", 0));
blackList.add(BlackMatchItem.getInstance(Type.OreDict, "ingotGold"));
blackList.add(BlackMatchItem.getInstance(Type.ModID, "minecraft"));
try {
Files.write(blackPath, config.toJson(blackList).getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
MatchItemHandler.InitTarget();
MatchFluidHandler.Init(OOIConfig.fluids);
}
private static void readGeneralConfig(File configDir) {
Configuration config = new Configuration(new File(configDir, "only_one_item.cfg"));
try {
config.load();
clearDuplicateRecipes = config.getBoolean(
"clearDuplicateRecipes",
"recipes",
true,
"Remove duplicate crafting recipes generated by ore dictionary unification. " +
"Disable this if another recipe manager or CraftTweaker scripts need to control recipe removals."
);
initializeBeforeCraftTweakerActions = config.getBoolean(
"initializeBeforeCraftTweakerActions",
"recipes",
true,
"When CraftTweaker is installed, initialize OOI stack unification at the start of CraftTweaker postInit " +
"so CraftTweaker and ModTweaker removals see already-unified inputs."
);
processOreDictionaryAtLoadComplete = config.getBoolean(
"processOreDictionaryAtLoadComplete",
"recipes",
false,
"Run OOI ore dictionary post-processing during FMLLoadComplete. The old behavior used true, but it can " +
"rewrite machine recipe inputs after CraftTweaker removals have already run."
);
} catch (Exception e) {
OnlyOneItem.LOGGER.error("[OOI]The config/only_one_item.cfg file is incorrect!", e);
} finally {
if (config.hasChanged()) {
config.save();
}
}
}
private static void addItemTargets(Set<ItemConversionTarget> targets) {
if (targets == null) return;
for (ItemConversionTarget target : targets) {
if (target == null || !isRegisteredItem(target.getTargetID()) || target.getMatchItems() == null) {
continue;
}
Set<MatchItem> validMatches = new ObjectOpenHashSet<>();
for (MatchItem matchItem : target.getMatchItems()) {
if (isValidMatchItem(matchItem)) {
validMatches.add(matchItem);
}
}
if (!validMatches.isEmpty()) {
items.add(target.setMatchItem(validMatches));
}
}
}
private static void addFluidTargets(Set<FluidConversionTarget> targets) {
if (targets == null) return;
for (FluidConversionTarget target : targets) {
if (target == null || target.getTarget() == null || target.getMatchFluids() == null) {
continue;
}
Set<String> validMatches = new ObjectOpenHashSet<>();
for (String fluid : target.getMatchFluids()) {
if (isNotBlank(fluid)) {
validMatches.add(fluid);
}
}
if (!validMatches.isEmpty()) {
fluids.add(target.setMatchFluids(validMatches));
}
}
}
private static void addBlackList(Set<BlackMatchItem> targets) {
if (targets == null) return;
for (BlackMatchItem target : targets) {
if (isValidBlackList(target)) {
blackList.add(target);
}
}
}
private static boolean isValidMatchItem(MatchItem matchItem) {
if (matchItem == null) return false;
if (isNotBlank(matchItem.oreName())) return true;
return isValidResourceLocation(matchItem.id());
}
private static boolean isValidBlackList(BlackMatchItem target) {
if (target == null || target.type() == null || !isNotBlank(target.name())) return false;
return switch (target.type()) {
case Item -> isValidResourceLocation(target.name());
case OreDict, ModID -> true;
};
}
private static boolean isRegisteredItem(String id) {
if (!isValidResourceLocation(id)) return false;
return Item.getByNameOrId(id) != null;
}
private static boolean isValidResourceLocation(String id) {
if (!isNotBlank(id)) return false;
try {
new ResourceLocation(id);
return true;
} catch (RuntimeException ignored) {
return false;
}
}
private static boolean isNotBlank(String value) {
return value != null && !value.trim().isEmpty();
}
}