Skip to content

Commit

Permalink
Code improvements for commands (#4575)
Browse files Browse the repository at this point in the history
  • Loading branch information
RacoonDog committed May 8, 2024
1 parent 142cf9a commit 25d1135
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,34 @@
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.systems.config.Config;
import meteordevelopment.meteorclient.utils.Utils;
import meteordevelopment.meteorclient.utils.player.ChatUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.CommandSource;
import net.minecraft.registry.BuiltinRegistries;
import net.minecraft.server.command.CommandManager;
import net.minecraft.text.Text;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class Command {
protected static final CommandRegistryAccess REGISTRY_ACCESS = CommandManager.createRegistryAccess(BuiltinRegistries.createWrapperLookup());
protected static final int SINGLE_SUCCESS = com.mojang.brigadier.Command.SINGLE_SUCCESS;
protected static final MinecraftClient mc = MeteorClient.mc;

private final String name;
private final String title;
private final String description;
private final List<String> aliases = new ArrayList<>();
private final List<String> aliases;

public Command(String name, String description, String... aliases) {
this.name = name;
this.title = Utils.nameToTitle(name);
this.description = description;
Collections.addAll(this.aliases, aliases);
this.aliases = List.of(aliases);
}

// Helper methods to painlessly infer the CommandSource generic type argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import meteordevelopment.meteorclient.commands.commands.*;
import meteordevelopment.meteorclient.pathing.PathManagers;
import meteordevelopment.meteorclient.utils.PostInit;
import net.minecraft.client.network.ClientCommandSource;
import net.minecraft.command.CommandSource;

import java.util.ArrayList;
Expand All @@ -21,7 +20,6 @@

public class Commands {
public static final CommandDispatcher<CommandSource> DISPATCHER = new CommandDispatcher<>();
public static final CommandSource COMMAND_SOURCE = new ClientCommandSource(null, mc);
public static final List<Command> COMMANDS = new ArrayList<>();

@PostInit(dependencies = PathManagers.class)
Expand Down Expand Up @@ -74,7 +72,7 @@ public static void add(Command command) {
}

public static void dispatch(String message) throws CommandSyntaxException {
DISPATCHER.execute(message, COMMAND_SOURCE);
DISPATCHER.execute(message, mc.getNetworkHandler().getCommandSource());
}

public static Command get(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import java.util.concurrent.CompletableFuture;

import static meteordevelopment.meteorclient.MeteorClient.mc;

@Mixin(ChatInputSuggestor.class)
public abstract class ChatInputSuggestorMixin {
@Shadow private ParseResults<CommandSource> parse;
Expand All @@ -46,7 +48,7 @@ public void onRefresh(CallbackInfo ci, String string, StringReader reader) {
reader.setCursor(reader.getCursor() + length);

if (this.parse == null) {
this.parse = Commands.DISPATCHER.parse(reader, Commands.COMMAND_SOURCE);
this.parse = Commands.DISPATCHER.parse(reader, mc.getNetworkHandler().getCommandSource());
}

int cursor = textField.getCursor();
Expand Down

0 comments on commit 25d1135

Please sign in to comment.