Skip to content

Commit c0526d5

Browse files
AzureAaronEmirlol
andauthored
Sack Item Autocomplete (SkyblockerMod#1062)
* Sack Item Autocomplete * Add amount argument --------- Co-authored-by: Rime <[email protected]>
1 parent fcf969f commit c0526d5

File tree

2 files changed

+98
-7
lines changed

2 files changed

+98
-7
lines changed

src/main/java/de/hysky/skyblocker/mixins/CommandTreeS2CPacketMixin.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
44
import com.mojang.brigadier.tree.CommandNode;
55
import com.mojang.brigadier.tree.LiteralCommandNode;
6+
7+
import de.hysky.skyblocker.skyblock.SackItemAutocomplete;
68
import de.hysky.skyblocker.skyblock.WarpAutocomplete;
79
import de.hysky.skyblocker.utils.Utils;
810
import net.minecraft.command.CommandSource;
@@ -11,11 +13,18 @@
1113

1214
@Mixin(targets = "net.minecraft.network.packet.s2c.play.CommandTreeS2CPacket$CommandTree")
1315
public class CommandTreeS2CPacketMixin {
14-
@ModifyExpressionValue(method = "getNode", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/CommandTreeS2CPacket$CommandTree;getNode(I)Lcom/mojang/brigadier/tree/CommandNode;", ordinal = 1))
15-
public CommandNode<? extends CommandSource> modifyCommandSuggestions(CommandNode<CommandSource> original) {
16-
if (Utils.isOnHypixel() && WarpAutocomplete.commandNode != null && original instanceof LiteralCommandNode<?> literalCommandNode && literalCommandNode.getLiteral().equals("warp")) {
17-
return WarpAutocomplete.commandNode;
18-
}
19-
return original;
20-
}
16+
@ModifyExpressionValue(method = "getNode", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/packet/s2c/play/CommandTreeS2CPacket$CommandTree;getNode(I)Lcom/mojang/brigadier/tree/CommandNode;", ordinal = 1))
17+
public CommandNode<? extends CommandSource> modifyCommandSuggestions(CommandNode<CommandSource> original) {
18+
if (Utils.isOnHypixel() && original instanceof LiteralCommandNode<?> literalCommandNode) {
19+
return switch (literalCommandNode.getLiteral()) {
20+
case String s when s.equals("warp") && WarpAutocomplete.commandNode != null -> WarpAutocomplete.commandNode;
21+
case String s when s.equals("getfromsacks") && SackItemAutocomplete.longCommandNode != null -> SackItemAutocomplete.longCommandNode;
22+
case String s when s.equals("gfs") && SackItemAutocomplete.shortCommandNode != null -> SackItemAutocomplete.shortCommandNode;
23+
24+
default -> original;
25+
};
26+
}
27+
28+
return original;
29+
}
2130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)