|
| 1 | +package de.hysky.skyblocker.skyblock; |
| 2 | + |
| 3 | +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; |
| 4 | +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; |
| 5 | + |
| 6 | +import java.io.InputStream; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.regex.Pattern; |
| 10 | +import java.util.stream.Collectors; |
| 11 | + |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | +import org.slf4j.Logger; |
| 14 | + |
| 15 | +import com.google.gson.JsonArray; |
| 16 | +import com.google.gson.JsonElement; |
| 17 | +import com.google.gson.JsonObject; |
| 18 | +import com.google.gson.JsonParser; |
| 19 | +import com.mojang.brigadier.arguments.IntegerArgumentType; |
| 20 | +import com.mojang.brigadier.arguments.StringArgumentType; |
| 21 | +import com.mojang.brigadier.tree.LiteralCommandNode; |
| 22 | +import com.mojang.logging.LogUtils; |
| 23 | + |
| 24 | +import de.hysky.skyblocker.annotations.Init; |
| 25 | +import de.hysky.skyblocker.utils.NEURepoManager; |
| 26 | +import de.hysky.skyblocker.utils.Utils; |
| 27 | +import io.github.moulberry.repo.data.NEUItem; |
| 28 | +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; |
| 29 | +import net.minecraft.command.CommandSource; |
| 30 | +import net.minecraft.util.Formatting; |
| 31 | + |
| 32 | +public class SackItemAutocomplete { |
| 33 | + private static final Logger LOGGER = LogUtils.getLogger(); |
| 34 | + private static final Pattern BAD_CHARACTERS = Pattern.compile("[α☘☠✎✧❁❂❈❤⸕]"); |
| 35 | + |
| 36 | + @Nullable |
| 37 | + public static LiteralCommandNode<FabricClientCommandSource> longCommandNode; |
| 38 | + @Nullable |
| 39 | + public static LiteralCommandNode<FabricClientCommandSource> shortCommandNode; |
| 40 | + |
| 41 | + @Init |
| 42 | + public static void init() { |
| 43 | + NEURepoManager.runAsyncAfterLoad(SackItemAutocomplete::loadSackItems); |
| 44 | + } |
| 45 | + |
| 46 | + private static void loadSackItems() { |
| 47 | + try (InputStream stream = NEURepoManager.NEU_REPO.file("constants/sacks.json").stream()) { |
| 48 | + JsonObject sacks = JsonParser.parseString(new String(stream.readAllBytes())).getAsJsonObject().getAsJsonObject("sacks"); |
| 49 | + |
| 50 | + Set<String> sackItemIds = sacks.entrySet().stream() |
| 51 | + .map(entry -> entry.getValue().getAsJsonObject()) |
| 52 | + .map(sack -> sack.getAsJsonArray("contents")) |
| 53 | + .map(JsonArray::asList) |
| 54 | + .flatMap(List::stream) |
| 55 | + .map(JsonElement::getAsString) |
| 56 | + .collect(Collectors.toUnmodifiableSet()); |
| 57 | + Set<String> sackItems = sackItemIds.stream() |
| 58 | + .map(neuId -> { |
| 59 | + NEUItem stack = NEURepoManager.NEU_REPO.getItems().getItemBySkyblockId(neuId); |
| 60 | + |
| 61 | + return stack != null ? Formatting.strip(stack.getDisplayName()) : neuId; |
| 62 | + }) |
| 63 | + .map(name -> BAD_CHARACTERS.matcher(name).replaceAll("").trim()) |
| 64 | + .collect(Collectors.toUnmodifiableSet()); |
| 65 | + |
| 66 | + longCommandNode = createCommandNode("getfromsacks", sackItems); |
| 67 | + shortCommandNode = createCommandNode("gfs", sackItems); |
| 68 | + } catch (Exception e) { |
| 69 | + LOGGER.error("[Skyblocker Sack Item Autocomplete] Failed to load sacks data from the NEU Repo.", e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static LiteralCommandNode<FabricClientCommandSource> createCommandNode(String command, Set<String> sackItems) { |
| 74 | + return literal(command) |
| 75 | + .requires(fccs -> Utils.isOnSkyblock()) |
| 76 | + .then(argument("item", StringArgumentType.greedyString()) |
| 77 | + .suggests((context, builder) -> CommandSource.suggestMatching(sackItems, builder)) |
| 78 | + .then(argument("amount", IntegerArgumentType.integer(0))) // Adds a nice <amount> text to the suggestion when any number is entered after the item string |
| 79 | + ) |
| 80 | + .build(); |
| 81 | + } |
| 82 | +} |
0 commit comments