-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SuggestionsHook for custom suggestions requests, and make Plugins…
…Command use it
- Loading branch information
1 parent
9e5a53f
commit b4b3ee2
Showing
4 changed files
with
66 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/net/earthcomputer/clientcommands/features/SuggestionsHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters