forked from mkram17/Bazaar-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigUtil.java
More file actions
84 lines (69 loc) · 3.13 KB
/
ConfigUtil.java
File metadata and controls
84 lines (69 loc) · 3.13 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
package com.github.mkram17.bazaarutils.config.util;
import com.github.mkram17.bazaarutils.BazaarUtils;
import com.github.mkram17.bazaarutils.config.BUConfig;
import com.github.mkram17.bazaarutils.config.patcher.ConfigPatches;
import com.github.mkram17.bazaarutils.config.util.client.ItemRendererProvider;
import com.github.mkram17.bazaarutils.config.util.client.SlotRendererProvider;
import com.github.mkram17.bazaarutils.utils.Util;
import com.google.gson.JsonObject;
import com.teamresourceful.resourcefulconfig.api.client.ResourcefulConfigScreen;
import com.teamresourceful.resourcefulconfig.api.loader.Configurator;
import com.teamresourceful.resourcefulconfig.api.types.ResourcefulConfig;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ClickableWidget;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.UnaryOperator;
import static com.github.mkram17.bazaarutils.BazaarUtils.CONFIGURATOR;
public class ConfigUtil {
public static final Map<Integer, UnaryOperator<JsonObject>> PATCHES = ConfigPatches.loadPatches();
public static final int VERSION = 1;
private static boolean configSaveScheduled = false;
public static Screen createGUI(Screen parent) {
return ResourcefulConfigScreen.make(BazaarUtils.config)
.withParent(parent)
.build();
}
public static void openGUI() {
MinecraftClient client = MinecraftClient.getInstance();
Screen parent = client.currentScreen;
client.send(() -> client.setScreen(createGUI(parent)));
}
public static void scheduleConfigSave() {
if (!configSaveScheduled) {
configSaveScheduled = true;
Util.tickExecuteLater(20, () -> { // 1 second
CONFIGURATOR.saveConfig(BUConfig.class);
configSaveScheduled = false;
});
}
}
public static List<ClickableWidget> getWidgets(){
List<ClickableWidget> widgets = new ArrayList<>();
//automatically added using @RegisterWidget annotation
return widgets;
}
public static ResourcefulConfig register(Configurator configurator) {
if (PATCHES.size() + 1 != VERSION) {
throw new IllegalStateException(
"BUConfig VERSION (" + VERSION + ") is out of sync with patch count " +
"— expected VERSION = " + (PATCHES.size() + 1)
);
}
ItemRendererProvider.register();
SlotRendererProvider.register();
configurator.register(BUConfig.class, event ->
PATCHES.forEach((version, patch) ->
event.register(version, json -> {
Util.logMessage("Applying patch " + version);
JsonObject result = patch.apply(json);
Util.logMessage("[BUConfig] Patch " + version + " applied successfully");
return result;
})
)
);
return configurator.getConfig(BUConfig.class);
}
}