-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
eae83ae
commit 670f2b8
Showing
13 changed files
with
180 additions
and
61 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/java/net/earthcomputer/clientcommands/command/Argument.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,41 @@ | ||
package net.earthcomputer.clientcommands.command; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
public final class Argument<T> { | ||
private final String name; | ||
private final T defaultValue; | ||
|
||
private Argument(String name, @Nullable T defaultValue) { | ||
this.name = name; | ||
this.defaultValue = defaultValue; | ||
} | ||
|
||
public static <T> Argument<@Nullable T> of(String name) { | ||
return new Argument<>(name, null); | ||
} | ||
|
||
public static <T> Argument<T> ofDefaulted(String name, T defaultValue) { | ||
return new Argument<>(name, defaultValue); | ||
} | ||
|
||
public static Argument<Boolean> ofFlag(String name) { | ||
return new Argument<>(name, false); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getFlag() { | ||
return "--" + name; | ||
} | ||
|
||
public static boolean isFlag(String argument) { | ||
return argument.startsWith("--"); | ||
} | ||
|
||
public T getDefaultValue() { | ||
return defaultValue; | ||
} | ||
} |
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
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
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
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
18 changes: 18 additions & 0 deletions
18
src/main/java/net/earthcomputer/clientcommands/interfaces/IClientCommandSource.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,18 @@ | ||
package net.earthcomputer.clientcommands.interfaces; | ||
|
||
import com.mojang.brigadier.context.ParsedCommandNode; | ||
import com.mojang.brigadier.suggestion.Suggestion; | ||
import net.earthcomputer.clientcommands.command.Argument; | ||
import net.minecraft.command.CommandSource; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
|
||
public interface IClientCommandSource { | ||
<T> T clientcommands_getArg(Argument<T> arg); | ||
|
||
<T> IClientCommandSource clientcommands_withArg(Argument<T> arg, T value); | ||
|
||
@Nullable | ||
List<Suggestion> clientcommands_filterSuggestions(List<Suggestion> suggestions, List<ParsedCommandNode<CommandSource>> parsedNodes); | ||
} |
9 changes: 0 additions & 9 deletions
9
src/main/java/net/earthcomputer/clientcommands/interfaces/IFlaggedCommandSource.java
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
src/main/java/net/earthcomputer/clientcommands/mixin/MixinChatInputSuggestor.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,43 @@ | ||
package net.earthcomputer.clientcommands.mixin; | ||
|
||
import com.mojang.brigadier.ParseResults; | ||
import com.mojang.brigadier.suggestion.Suggestion; | ||
import com.mojang.brigadier.suggestion.Suggestions; | ||
import net.earthcomputer.clientcommands.interfaces.IClientCommandSource; | ||
import net.minecraft.client.gui.screen.ChatInputSuggestor; | ||
import net.minecraft.command.CommandSource; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.objectweb.asm.Opcodes; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.Slice; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Mixin(ChatInputSuggestor.class) | ||
public class MixinChatInputSuggestor { | ||
@Shadow private @Nullable ParseResults<CommandSource> parse; | ||
@Shadow private @Nullable CompletableFuture<Suggestions> pendingSuggestions; | ||
|
||
@Inject( | ||
method = "refresh", | ||
slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayNetworkHandler;getCommandDispatcher()Lcom/mojang/brigadier/CommandDispatcher;")), | ||
at = @At(value = "FIELD", target = "Lnet/minecraft/client/gui/screen/ChatInputSuggestor;pendingSuggestions:Ljava/util/concurrent/CompletableFuture;", opcode = Opcodes.PUTFIELD, shift = At.Shift.AFTER, ordinal = 0) | ||
) | ||
private void filterSuggestions(CallbackInfo ci) { | ||
assert this.pendingSuggestions != null && this.parse != null; | ||
this.pendingSuggestions = this.pendingSuggestions.thenApply(suggestions -> { | ||
CommandSource source = this.parse.getContext().getSource(); | ||
if (source instanceof IClientCommandSource mySource) { | ||
List<Suggestion> newSuggestions = mySource.clientcommands_filterSuggestions(suggestions.getList(), this.parse.getContext().getNodes()); | ||
return newSuggestions == null ? suggestions : new Suggestions(suggestions.getRange(), newSuggestions); | ||
} else { | ||
return suggestions; | ||
} | ||
}); | ||
} | ||
} |
51 changes: 41 additions & 10 deletions
51
src/main/java/net/earthcomputer/clientcommands/mixin/MixinClientCommandSource.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 |
---|---|---|
@@ -1,40 +1,71 @@ | ||
package net.earthcomputer.clientcommands.mixin; | ||
|
||
import net.earthcomputer.clientcommands.interfaces.IFlaggedCommandSource; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.mojang.brigadier.context.ParsedCommandNode; | ||
import com.mojang.brigadier.suggestion.Suggestion; | ||
import com.mojang.brigadier.tree.LiteralCommandNode; | ||
import net.earthcomputer.clientcommands.command.Argument; | ||
import net.earthcomputer.clientcommands.interfaces.IClientCommandSource; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.network.ClientCommandSource; | ||
import net.minecraft.client.network.ClientPlayNetworkHandler; | ||
|
||
import net.minecraft.command.CommandSource; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
|
||
@Mixin(ClientCommandSource.class) | ||
public class MixinClientCommandSource implements IFlaggedCommandSource { | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Mixin(ClientCommandSource.class) | ||
public class MixinClientCommandSource implements IClientCommandSource { | ||
@Shadow | ||
@Final | ||
private ClientPlayNetworkHandler networkHandler; | ||
|
||
@Shadow | ||
@Final | ||
private MinecraftClient client; | ||
|
||
@Unique | ||
private int flags; | ||
private ImmutableMap<Argument<?>, Object> arguments = ImmutableMap.of(); | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public int getFlags() { | ||
return this.flags; | ||
public <T> T clientcommands_getArg(Argument<T> arg) { | ||
return (T) this.arguments.getOrDefault(arg, arg.getDefaultValue()); | ||
} | ||
|
||
@Override | ||
public IFlaggedCommandSource withFlags(int flags) { | ||
public <T> IClientCommandSource clientcommands_withArg(Argument<T> arg, T value) { | ||
MixinClientCommandSource source = (MixinClientCommandSource) (Object) new ClientCommandSource(this.networkHandler, this.client); | ||
source.flags = flags; | ||
|
||
source.arguments = ImmutableMap.<Argument<?>, Object>builderWithExpectedSize(this.arguments.size() + 1).putAll(this.arguments).put(arg, value).build(); | ||
return source; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public List<Suggestion> clientcommands_filterSuggestions(List<Suggestion> suggestions, List<ParsedCommandNode<CommandSource>> parsedNodes) { | ||
Set<String> seenFlags = new HashSet<>(); | ||
for (Argument<?> arg : arguments.keySet()) { | ||
seenFlags.add(arg.getFlag()); | ||
} | ||
for (ParsedCommandNode<CommandSource> parsedNode : parsedNodes) { | ||
if (parsedNode.getNode() instanceof LiteralCommandNode<CommandSource> literal) { | ||
String flag = literal.getLiteral(); | ||
if (Argument.isFlag(flag)) { | ||
seenFlags.add(flag); | ||
} | ||
} | ||
} | ||
|
||
if (seenFlags.isEmpty()) { | ||
return null; | ||
} else { | ||
return suggestions.stream().filter(suggestion -> !seenFlags.contains(suggestion.getText())).toList(); | ||
} | ||
} | ||
} |
Oops, something went wrong.