Skip to content

Commit f646587

Browse files
committed
Client Command Manager
1 parent f067bd3 commit f646587

10 files changed

Lines changed: 461 additions & 185 deletions

File tree

‎src/impl/java/pl/olafcio/avoid_impl/brigadier/BrigadierSupport.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public abstract class BrigadierSupport<Src> {
3434
= new LinkedHashMap<>();
3535

3636
public void finishCommands(CommandDispatcher<Src> dispatcher) {
37-
CommandManager.each(cmd -> {
37+
finishCommands(dispatcher, CommandManager.getInstance());
38+
}
39+
40+
public void finishCommands(CommandDispatcher<Src> dispatcher, CommandManager.Instance<?> commandManager) {
41+
commandManager.each(cmd -> {
3842
var name = cmd.getName();
3943

4044
var root = LiteralArgumentBuilder.<Src>literal(name);

‎src/impl/java/pl/olafcio/avoid_impl/mods/loader/mod/LXCommand.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.jetbrains.annotations.ApiStatus;
44
import pl.olafcio.avoid.api.Avoid;
55
import pl.olafcio.avoid.mods.annotation_processor.AutoCommand;
6+
import pl.olafcio.avoid.net.command.ClientCommand;
7+
import pl.olafcio.avoid.net.command.ClientCommandManager;
68
import pl.olafcio.avoid.net.command.Command;
79
import pl.olafcio.avoid.net.command.CommandManager;
810

@@ -14,12 +16,21 @@ default boolean registerAutoCommand(Class<?> klass)
1416
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
1517
{
1618
if (klass.isAnnotationPresent(AutoCommand.class)) {
17-
if (!Command.class.isAssignableFrom(klass)) {
18-
Avoid.LOGGER.error("@AutoCommand requires the annotated type to extend Command (avoid.net.command)");
19-
return true;
20-
}
19+
if (klass.getDeclaredAnnotation(AutoCommand.class).clientSide()) {
20+
if (!ClientCommand.class.isAssignableFrom(klass)) {
21+
Avoid.LOGGER.error("@AutoCommand(clientSide = true) requires the annotated type to extend ClientCommand (avoid.net.command)");
22+
return true;
23+
}
24+
25+
ClientCommandManager.add((ClientCommand) klass.getDeclaredConstructor().newInstance());
26+
} else {
27+
if (!Command.class.isAssignableFrom(klass)) {
28+
Avoid.LOGGER.error("@AutoCommand requires the annotated type to extend Command (avoid.net.command)");
29+
return true;
30+
}
2131

22-
CommandManager.add((Command) klass.getDeclaredConstructor().newInstance());
32+
CommandManager.add((Command) klass.getDeclaredConstructor().newInstance());
33+
}
2334
}
2435

2536
return false;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,92 @@
11
package pl.olafcio.avoid_impl.mixin;
22

3+
import com.mojang.brigadier.CommandDispatcher;
4+
import com.mojang.brigadier.builder.ArgumentBuilder;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import com.mojang.brigadier.context.ParsedArgument;
37
import net.minecraft.client.multiplayer.ClientPacketListener;
8+
import net.minecraft.client.multiplayer.ClientSuggestionProvider;
49
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
10+
import net.minecraft.network.protocol.game.ClientboundCommandsPacket;
11+
import net.minecraft.server.permissions.Permission;
512
import org.spongepowered.asm.mixin.Mixin;
13+
import org.spongepowered.asm.mixin.Shadow;
614
import org.spongepowered.asm.mixin.injection.At;
715
import org.spongepowered.asm.mixin.injection.Inject;
816
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
17+
import pl.olafcio.avoid.net.client.Client;
18+
import pl.olafcio.avoid.net.command.ClientCommandManager;
19+
import pl.olafcio.avoid.net.command.SyntaxTree;
20+
import pl.olafcio.avoid.net.command.annotation.PermissionLevel;
21+
import pl.olafcio.avoid.net.command.executor.Executor;
22+
import pl.olafcio.avoid_impl.brigadier.BrigadierSupport;
23+
import pl.olafcio.avoid_impl.mixin.accessors.ICommandContext;
924
import pl.olafcio.avoid_impl.net.payload.AvoidPayload;
1025

26+
import java.util.LinkedHashMap;
27+
import java.util.Map;
28+
29+
import static pl.olafcio.avoid_impl.net.permission.PermissionHandlers.createAtom;
30+
1131
@Mixin(ClientPacketListener.class)
1232
public class ClientPacketListenerMixin {
33+
@Shadow
34+
private CommandDispatcher<ClientSuggestionProvider> commands;
35+
1336
@Inject(at = @At("HEAD"), method = "handleCustomPayload", cancellable = true)
1437
public void handleCustomPayload(CustomPacketPayload customPacketPayload, CallbackInfo ci) {
1538
if (customPacketPayload instanceof AvoidPayload.Payload payload) {
1639
ci.cancel(); // Cancelling so that client doesn't call 'handleUnknownCustomPayload'
1740
payload.payload.acceptClient();
1841
}
1942
}
43+
44+
@Inject(at = @At("TAIL"), method = "handleCommands")
45+
public void handleCommands(ClientboundCommandsPacket packet, CallbackInfo ci) {
46+
new BrigadierSupport<ClientSuggestionProvider>() {
47+
@Override
48+
protected <T extends ArgumentBuilder<ClientSuggestionProvider, T>> T addNodePermissions(SyntaxTree entry, T node) {
49+
var perm = entry.getPermission();
50+
if (perm != null) {
51+
if (perm instanceof pl.olafcio.avoid.net.command.annotation.Permission cast) {
52+
var atom = createAtom(cast.value());
53+
54+
node = node.requires(ctx -> {
55+
var perms = ctx.permissions();
56+
return perms.hasPermission(atom);
57+
});
58+
} else if (perm instanceof PermissionLevel cast) {
59+
var atom = createAtom(cast.value());
60+
var cmdLevel = new Permission.HasCommandLevel(cast.level().__get());
61+
62+
node = node.requires(ctx -> {
63+
var perms = ctx.permissions();
64+
return perms.avoid$hasPermission(atom, cmdLevel);
65+
});
66+
} else {
67+
throw new RuntimeException("Invalid command permission");
68+
}
69+
}
70+
71+
return node;
72+
}
73+
74+
@Override
75+
@SuppressWarnings("unchecked")
76+
protected Map<String, ParsedArgument<ClientSuggestionProvider, ?>> getArguments(CommandContext<ClientSuggestionProvider> ctx) {
77+
return ((ICommandContext<ClientSuggestionProvider>) ctx).avoid$arguments();
78+
}
79+
80+
@Override
81+
@SuppressWarnings("unchecked")
82+
protected void setArguments(CommandContext<ClientSuggestionProvider> ctx, LinkedHashMap<String, ParsedArgument<ClientSuggestionProvider, ?>> rekeyed) {
83+
((ICommandContext<ClientSuggestionProvider>) ctx).avoid$arguments(rekeyed);
84+
}
85+
86+
@Override
87+
protected Executor getExecutor(CommandContext<ClientSuggestionProvider> ctx) {
88+
return Client.getPlayer();
89+
}
90+
}.finishCommands(commands, ClientCommandManager.getInstance());
91+
}
2092
}

‎src/main/java/pl/olafcio/avoid/mods/annotation_processor/AutoCommand.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
public @interface AutoCommand {
1818
String usage() default "";
1919
String[] examples() default {};
20+
boolean clientSide() default false;
2021
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package pl.olafcio.avoid.net.command;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
import pl.olafcio.avoid.annotations.refactor.NeverRemoval;
6+
import pl.olafcio.avoid.net.command.executor.Executor;
7+
import pl.olafcio.avoid.net.command.handling.CommandHandler;
8+
import pl.olafcio.avoid.net.command.handling.Usage;
9+
import pl.olafcio.avoid.net.command.parameter.CommandParameter;
10+
11+
public interface AnyCommand {
12+
@NeverRemoval
13+
void sendSyntaxException(
14+
@NotNull Executor executor,
15+
@NotNull Usage ctx,
16+
@Nullable CommandParameter<?> param
17+
);
18+
19+
@NeverRemoval
20+
@NotNull String getName();
21+
22+
@NeverRemoval
23+
@NotNull String getUsage();
24+
25+
@NeverRemoval
26+
String @NotNull[] getExamples();
27+
28+
@NeverRemoval
29+
@NotNull SyntaxTree getSyntaxTree();
30+
31+
@NeverRemoval
32+
@Nullable CommandHandler getUnknownHandler();
33+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package pl.olafcio.avoid.net.command;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
6+
import pl.olafcio.avoid.annotations.refactor.NeverRemoval;
7+
import pl.olafcio.avoid.net.chat.component.Colors;
8+
import pl.olafcio.avoid.net.chat.component.Components;
9+
import pl.olafcio.avoid.net.command.executor.Executor;
10+
import pl.olafcio.avoid.net.command.handling.CommandHandler;
11+
import pl.olafcio.avoid.net.command.handling.Usage;
12+
import pl.olafcio.avoid.net.command.parameter.CommandParameter;
13+
14+
import java.util.Map;
15+
16+
/**
17+
* Specifies the extending class is ready to register as a client command.
18+
*/
19+
@ApiStatus.Experimental
20+
public abstract class ClientCommand implements AnyCommand {
21+
@NeverRemoval
22+
public void sendSyntaxException(
23+
@NotNull Executor executor,
24+
@NotNull Usage ctx,
25+
@Nullable CommandParameter<?> param
26+
) {
27+
// If you just want it simple:
28+
// > executor.sendMessage(Components.literal("§cIncorrect syntax"));
29+
30+
if (param != null && param.sendSyntaxException(executor, ctx))
31+
return;
32+
33+
executor.sendMessage(Components.translationFallback("command.unknown.command", "Unknown or incomplete command. See below for error")
34+
.color(Colors.RED)
35+
.append(Components.literal("\n§7" + ctx.getInput()))
36+
.append(Components.translationFallback("command.context.here", "<--[HERE]").color(Colors.RED).italic(true)));
37+
}
38+
39+
/**
40+
* <b>Note:</b> This method works only after registering the class.
41+
*/
42+
@NeverRemoval
43+
@NotNull
44+
public final String getName() {
45+
return ClientCommandManager.get(this).name();
46+
}
47+
48+
/**
49+
* <b>Note:</b> This method works only after registering the class.
50+
*/
51+
@NeverRemoval
52+
@NotNull
53+
public final String getUsage() {
54+
return ClientCommandManager.get(this).usage();
55+
}
56+
57+
/**
58+
* <b>Note:</b> This method works only after registering the class.
59+
*/
60+
@NeverRemoval
61+
public final String @NotNull[] getExamples() {
62+
return ClientCommandManager.get(this).examples();
63+
}
64+
65+
/**
66+
* <b>Note:</b> This method works only after registering the class.
67+
*/
68+
@NeverRemoval
69+
@NotNull
70+
public final SyntaxTree getSyntaxTree() {
71+
return ClientCommandManager.get(this).syntaxes();
72+
}
73+
74+
/**
75+
* <b>Note:</b> This method works only after registering the class.
76+
*/
77+
@NeverRemoval
78+
@Nullable
79+
public final CommandHandler getUnknownHandler() {
80+
return ClientCommandManager.get(this).unknownhandler();
81+
}
82+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package pl.olafcio.avoid.net.command;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
import org.jetbrains.annotations.Nullable;
5+
import pl.olafcio.avoid.annotations.refactor.WillRefactor;
6+
import pl.olafcio.avoid.net.command.external.ExternalCommand;
7+
import pl.olafcio.avoid.net.command.external.PlayerNotInWorldException;
8+
9+
/**
10+
* A namespace for managing client commands.
11+
* <br/><br/>
12+
* To register a command, use {@link #add(ClientCommand)}.<br/>
13+
* To get a command by its name, use {@link #getByName(String)}.
14+
*/
15+
@WillRefactor(aspect = "name")
16+
public final class ClientCommandManager {
17+
@ApiStatus.Internal
18+
private ClientCommandManager() {}
19+
20+
private static final CommandManager.Instance<ClientCommand> INSTANCE
21+
= new CommandManager.Instance<>() {
22+
@Override
23+
public @Nullable ExternalCommand getByName(String name) throws PlayerNotInWorldException {
24+
return pl.olafcio.avoid_impl.net.command.CommandLookup.queryClient(name);
25+
}
26+
};
27+
28+
public static CommandManager.Instance<ClientCommand> getInstance() {
29+
return INSTANCE;
30+
}
31+
32+
public static void add(ClientCommand cmd) {
33+
INSTANCE.add(cmd);
34+
}
35+
36+
@Nullable
37+
public static ExternalCommand getByName(String name) throws PlayerNotInWorldException {
38+
return INSTANCE.getByName(name);
39+
}
40+
41+
static CommandMetadata get(ClientCommand cmd) {
42+
return INSTANCE.get(cmd);
43+
}
44+
}

‎src/main/java/pl/olafcio/avoid/net/command/Command.java‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
* Specifies the extending class is ready to register as a server command.
1717
*/
1818
@NeverRemoval
19-
public abstract class Command {
19+
public abstract class Command implements AnyCommand {
2020
@NeverRemoval
21+
@Override
2122
public void sendSyntaxException(
2223
@NotNull Executor executor,
2324
@NotNull Usage ctx,
@@ -53,24 +54,25 @@ public void sendSyntaxException(
5354
* <b>Note:</b> This method works only after registering the class.
5455
*/
5556
@NeverRemoval
56-
@NotNull
57-
public final String getName() {
57+
@Override
58+
public final @NotNull String getName() {
5859
return CommandManager.get(this).name();
5960
}
6061

6162
/**
6263
* <b>Note:</b> This method works only after registering the class.
6364
*/
6465
@NeverRemoval
65-
@NotNull
66-
public final String getUsage() {
66+
@Override
67+
public final @NotNull String getUsage() {
6768
return CommandManager.get(this).usage();
6869
}
6970

7071
/**
7172
* <b>Note:</b> This method works only after registering the class.
7273
*/
7374
@NeverRemoval
75+
@Override
7476
public final String @NotNull[] getExamples() {
7577
return CommandManager.get(this).examples();
7678
}
@@ -79,17 +81,17 @@ public final String getUsage() {
7981
* <b>Note:</b> This method works only after registering the class.
8082
*/
8183
@NeverRemoval
82-
@NotNull
83-
public final SyntaxTree getSyntaxTree() {
84+
@Override
85+
public final @NotNull SyntaxTree getSyntaxTree() {
8486
return CommandManager.get(this).syntaxes();
8587
}
8688

8789
/**
8890
* <b>Note:</b> This method works only after registering the class.
8991
*/
9092
@NeverRemoval
91-
@Nullable
92-
public final CommandHandler getUnknownHandler() {
93+
@Override
94+
public final @Nullable CommandHandler getUnknownHandler() {
9395
return CommandManager.get(this).unknownhandler();
9496
}
9597
}

0 commit comments

Comments
 (0)