Skip to content

Commit

Permalink
Add SuggestionsHook for custom suggestions requests, and make Plugins…
Browse files Browse the repository at this point in the history
…Command use it
  • Loading branch information
Earthcomputer committed Mar 3, 2024
1 parent 9e5a53f commit b4b3ee2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.tree.CommandNode;
import net.earthcomputer.clientcommands.features.SuggestionsHook;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundCommandSuggestionsPacket;
import net.minecraft.network.protocol.game.ServerboundCommandSuggestionPacket;

import java.util.stream.Collectors;

import static com.mojang.brigadier.arguments.StringArgumentType.*;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*;

public class PluginsCommand {

private static boolean awaitingSuggestionsPacket = false;

private static SuggestionsCallback callback;

public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
dispatcher.register(literal("cplugins")
.executes(ctx -> getPlugins(ctx.getSource()))
Expand All @@ -35,10 +29,8 @@ private static int getPlugins(FabricClientCommandSource source) {
}

private static int getPlugins(FabricClientCommandSource source, String partial) {
awaitingSuggestionsPacket = true;
source.getClient().getConnection().send(new ServerboundCommandSuggestionPacket(1, partial));
callback = packet -> {
String plugins = packet.getSuggestions().getList().stream()
SuggestionsHook.request(partial).whenComplete((suggestions, throwable) -> {
String plugins = suggestions.getList().stream()
.map(Suggestion::getText)
.filter(text -> text.contains(":"))
.map(text -> text.substring(0, text.indexOf(":")))
Expand All @@ -47,7 +39,7 @@ private static int getPlugins(FabricClientCommandSource source, String partial)

source.sendFeedback(Component.translatable("commands.cplugins.found"));
source.sendFeedback(Component.literal(plugins));
};
});
return Command.SINGLE_SUCCESS;
}

Expand All @@ -63,17 +55,4 @@ private static int getPluginsFromDispatcher(FabricClientCommandSource source) {
source.sendFeedback(Component.literal(plugins));
return Command.SINGLE_SUCCESS;
}

public static void onCommandSuggestions(ClientboundCommandSuggestionsPacket packet) {
if (!awaitingSuggestionsPacket) {
return;
}
awaitingSuggestionsPacket = false;
callback.apply(packet);
}
}

@FunctionalInterface
interface SuggestionsCallback {
void apply(ClientboundCommandSuggestionsPacket packet);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.earthcomputer.clientcommands.features;

import com.mojang.brigadier.suggestion.Suggestions;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.protocol.game.ClientboundCommandSuggestionsPacket;
import net.minecraft.network.protocol.game.ServerboundCommandSuggestionPacket;

import java.util.concurrent.CompletableFuture;

public final class SuggestionsHook {
private SuggestionsHook() {
}

private static final int MAGIC_SUGGESTION_ID = -314159265;
private static int currentSuggestionId = MAGIC_SUGGESTION_ID;
private static final Int2ObjectMap<CompletableFuture<Suggestions>> pendingSuggestions = new Int2ObjectOpenHashMap<>();

public static CompletableFuture<Suggestions> request(String command) {
ClientPacketListener connection = Minecraft.getInstance().getConnection();
if (connection == null) {
return Suggestions.empty();
}

currentSuggestionId--;
CompletableFuture<Suggestions> future = new CompletableFuture<>();
pendingSuggestions.put(currentSuggestionId, future);
connection.send(new ServerboundCommandSuggestionPacket(currentSuggestionId, command));
return future;
}

public static boolean onCompletions(ClientboundCommandSuggestionsPacket packet) {
CompletableFuture<Suggestions> future = pendingSuggestions.remove(packet.getId());
if (future == null) {
return false;
}

if (pendingSuggestions.isEmpty()) {
currentSuggestionId = MAGIC_SUGGESTION_ID;
}

future.complete(packet.getSuggestions());
return true;
}

public static void onDisconnect() {
for (CompletableFuture<Suggestions> future : pendingSuggestions.values()) {
future.complete(Suggestions.empty().join());
}
pendingSuggestions.clear();
currentSuggestionId = MAGIC_SUGGESTION_ID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import net.cortex.clientAddon.cracker.SeedCracker;
import net.earthcomputer.clientcommands.ClientcommandsDataQueryHandler;
import net.earthcomputer.clientcommands.Configs;
import net.earthcomputer.clientcommands.command.PluginsCommand;
import net.earthcomputer.clientcommands.features.FishingCracker;
import net.earthcomputer.clientcommands.features.PlayerRandCracker;
import net.earthcomputer.clientcommands.features.SuggestionsHook;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl;
import net.minecraft.client.multiplayer.ClientPacketListener;
Expand Down Expand Up @@ -101,8 +101,10 @@ public ClientcommandsDataQueryHandler clientcommands_getCCDataQueryHandler() {
return ccDataQueryHandler;
}

@Inject(method = "handleCommandSuggestions", at = @At("TAIL"))
@Inject(method = "handleCommandSuggestions", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/PacketUtils;ensureRunningOnSameThread(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketListener;Lnet/minecraft/util/thread/BlockableEventLoop;)V", shift = At.Shift.AFTER), cancellable = true)
private void onHandleCommandSuggestions(ClientboundCommandSuggestionsPacket packet, CallbackInfo ci) {
PluginsCommand.onCommandSuggestions(packet);
if (SuggestionsHook.onCompletions(packet)) {
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.earthcomputer.clientcommands.features.PlayerRandCracker;
import net.earthcomputer.clientcommands.features.Relogger;
import net.earthcomputer.clientcommands.features.RenderSettings;
import net.earthcomputer.clientcommands.features.SuggestionsHook;
import net.earthcomputer.clientcommands.render.RenderQueue;
import net.earthcomputer.clientcommands.task.TaskManager;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -72,6 +73,7 @@ public void onDisconnect(Screen screen, CallbackInfo ci) {
BetterConfigAPI.getInstance().getModConfig("clientcommands").resetTemporaryConfigs();
}
RenderSettings.clearEntityRenderSelectors();
SuggestionsHook.onDisconnect();
}

// Earth annoying his friends <3 nothing to see here
Expand Down

0 comments on commit b4b3ee2

Please sign in to comment.