aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/skyblock/RngMeterAutocomplete.java
blob: 00acc9539ab2f8b5d3c055a548ff0fba67387a85 (plain)
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
package de.hysky.skyblocker.skyblock;

import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

import com.google.gson.JsonParser;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.mojang.logging.LogUtils;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;

import de.hysky.skyblocker.annotations.Init;
import de.hysky.skyblocker.utils.Http;
import de.hysky.skyblocker.utils.Utils;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.command.CommandSource;

public class RngMeterAutocomplete {
	private static final Logger LOGGER = LogUtils.getLogger();
	private static final Codec<Map<String, List<String>>> CODEC = Codec.unboundedMap(Codec.STRING, Codec.STRING.listOf());
	private static Map<String, List<String>> rngMeters = new HashMap<>();
	@Nullable
	public static LiteralCommandNode<FabricClientCommandSource> longCommand;
	@Nullable
	public static LiteralCommandNode<FabricClientCommandSource> shortCommand;

	@Init
	public static void init() {
		CompletableFuture.runAsync(() -> {
			try {
				String json = Http.sendGetRequest("https://hysky.de/api/rngmeters");
				rngMeters = CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(json)).getOrThrow();

				longCommand = createCommandNode("rngmeter");
				shortCommand = createCommandNode("rng");
			} catch (Exception e) {
				LOGGER.error("[Skyblocker RNG Meter Autocomplete] Failed to load RNG Meter data.", e);
			}
		});
	}

	private static LiteralCommandNode<FabricClientCommandSource> createCommandNode(String command) {
		return literal(command)
				.requires(source -> Utils.isOnSkyblock())
				.then(argument("type", StringArgumentType.string())
						.suggests((context, builder) -> CommandSource.suggestMatching(rngMeters.keySet(), builder))
						.then(argument("subtype", StringArgumentType.string())
								.suggests((context, builder) -> CommandSource.suggestMatching(rngMeters.getOrDefault(StringArgumentType.getString(context, "type"), List.of()), builder)))
				).build();
	}
}