Skip to content

Commit

Permalink
made Commands not a System
Browse files Browse the repository at this point in the history
  • Loading branch information
arlomcwalter committed May 1, 2023
1 parent 1cac537 commit 3807058
Show file tree
Hide file tree
Showing 57 changed files with 220 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands;
package meteordevelopment.meteorclient.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.ArgumentType;
Expand All @@ -12,16 +12,18 @@
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 MinecraftClient mc;
protected static final CommandRegistryAccess REGISTRY_ACCESS = CommandManager.createRegistryAccess(BuiltinRegistries.createWrapperLookup());

private final String name;
private final String title;
Expand All @@ -33,7 +35,6 @@ public Command(String name, String description, String... aliases) {
this.title = Utils.nameToTitle(name);
this.description = description;
Collections.addAll(this.aliases, aliases);
mc = MinecraftClient.getInstance();
}

// Helper methods to painlessly infer the CommandSource generic type argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,30 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands;
package meteordevelopment.meteorclient.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import meteordevelopment.meteorclient.systems.System;
import meteordevelopment.meteorclient.systems.Systems;
import meteordevelopment.meteorclient.systems.commands.commands.*;
import meteordevelopment.meteorclient.commands.commands.*;
import meteordevelopment.meteorclient.utils.PostInit;
import net.minecraft.client.network.ClientCommandSource;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.CommandSource;
import net.minecraft.registry.BuiltinRegistries;
import net.minecraft.server.command.CommandManager;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static meteordevelopment.meteorclient.MeteorClient.mc;

public class Commands extends System<Commands> {
public static final CommandRegistryAccess REGISTRY_ACCESS = CommandManager.createRegistryAccess(BuiltinRegistries.createWrapperLookup());

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

private final List<Command> commands = new ArrayList<>();

public Commands() {
super(null);
}

public static Commands get() {
return Systems.get(Commands.class);
}
private static final List<Command> COMMANDS = new ArrayList<>();

@Override
public void init() {
@PostInit
public static void init() {
add(new VClipCommand());
add(new HClipCommand());
add(new DismountCommand());
Expand Down Expand Up @@ -78,26 +64,26 @@ public void init() {
add(new WaypointCommand());
add(new InputCommand());

commands.sort(Comparator.comparing(Command::getName));
COMMANDS.sort(Comparator.comparing(Command::getName));
}

public void add(Command command) {
commands.removeIf(existing -> existing.getName().equals(command.getName()));
public static void add(Command command) {
COMMANDS.removeIf(existing -> existing.getName().equals(command.getName()));
command.registerTo(DISPATCHER);
commands.add(command);
COMMANDS.add(command);
}

public void dispatch(String message) throws CommandSyntaxException {
public static void dispatch(String message) throws CommandSyntaxException {
ParseResults<CommandSource> results = DISPATCHER.parse(message, COMMAND_SOURCE);
DISPATCHER.execute(results);
}

public List<Command> getAll() {
return commands;
public static List<Command> getAll() {
return COMMANDS;
}

public Command get(String name) {
for (Command command : commands) {
public static Command get(String name) {
for (Command command : COMMANDS) {
if (command.getName().equals(name)) {
return command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import net.minecraft.command.argument.EnumArgumentType;
import net.minecraft.util.math.Direction;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package meteordevelopment.meteorclient.systems.commands.arguments;
/*
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.commands.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package meteordevelopment.meteorclient.systems.commands.arguments;
/*
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.commands.arguments;

import com.google.common.collect.Streams;
import com.mojang.brigadier.StringReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
Expand All @@ -21,12 +21,10 @@
import java.util.concurrent.CompletableFuture;

public class NotebotSongArgumentType implements ArgumentType<Path> {

public static NotebotSongArgumentType create() {
return new NotebotSongArgumentType();
}


@Override
public Path parse(StringReader reader) throws CommandSyntaxException {
final String text = reader.getRemaining();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import com.google.common.collect.Streams;
import com.mojang.brigadier.StringReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import com.google.common.collect.Streams;
import com.mojang.brigadier.StringReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.arguments;
package meteordevelopment.meteorclient.commands.arguments;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.commands;
package meteordevelopment.meteorclient.commands.commands;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import meteordevelopment.meteorclient.systems.commands.Command;
import meteordevelopment.meteorclient.systems.commands.arguments.ModuleArgumentType;
import meteordevelopment.meteorclient.commands.Command;
import meteordevelopment.meteorclient.commands.arguments.ModuleArgumentType;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.systems.modules.Modules;
import net.minecraft.command.CommandSource;
Expand All @@ -24,7 +24,6 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
builder.then(argument("module", ModuleArgumentType.create()).executes(context -> {
Module module = context.getArgument("module", Module.class);
Modules.get().setModuleToBind(module);

module.info("Press a key to bind the module to.");
return SINGLE_SUCCESS;
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.commands;
package meteordevelopment.meteorclient.commands.commands;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import meteordevelopment.meteorclient.systems.commands.Command;
import meteordevelopment.meteorclient.commands.Command;
import meteordevelopment.meteorclient.systems.modules.Module;
import meteordevelopment.meteorclient.systems.modules.Modules;
import meteordevelopment.meteorclient.utils.Utils;
Expand All @@ -18,7 +18,6 @@
import net.minecraft.util.Formatting;

import java.util.List;
import java.util.stream.Collectors;

import static com.mojang.brigadier.Command.SINGLE_SUCCESS;

Expand All @@ -32,8 +31,8 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
builder.executes(context -> {
// Modules
List<Module> modules = Modules.get().getAll().stream()
.filter(module -> module.keybind.isSet())
.collect(Collectors.toList());
.filter(module -> module.keybind.isSet())
.toList();

ChatUtils.info("--- Bound Modules ((highlight)%d(default)) ---", modules.size());

Expand Down Expand Up @@ -61,7 +60,6 @@ public void build(LiteralArgumentBuilder<CommandSource> builder) {
private MutableText getTooltip(Module module) {
MutableText tooltip = Text.literal(Utils.nameToTitle(module.title)).formatted(Formatting.BLUE, Formatting.BOLD).append("\n\n");
tooltip.append(Text.literal(module.description).formatted(Formatting.WHITE));

return tooltip;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.commands;
package meteordevelopment.meteorclient.commands.commands;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import meteordevelopment.meteorclient.systems.commands.Command;
import meteordevelopment.meteorclient.systems.commands.Commands;
import meteordevelopment.meteorclient.commands.Command;
import meteordevelopment.meteorclient.commands.Commands;
import meteordevelopment.meteorclient.systems.config.Config;
import meteordevelopment.meteorclient.utils.Utils;
import meteordevelopment.meteorclient.utils.player.ChatUtils;
Expand All @@ -28,10 +28,10 @@ public CommandsCommand() {
@Override
public void build(LiteralArgumentBuilder<CommandSource> builder) {
builder.executes(context -> {
ChatUtils.info("--- Commands ((highlight)%d(default)) ---", Commands.get().getAll().size());
ChatUtils.info("--- Commands ((highlight)%d(default)) ---", Commands.getAll().size());

MutableText commands = Text.literal("");
Commands.get().getAll().forEach(command -> commands.append(getCommandText(command)));
Commands.getAll().forEach(command -> commands.append(getCommandText(command)));
ChatUtils.sendMsg(commands);

return SINGLE_SUCCESS;
Expand Down Expand Up @@ -59,11 +59,12 @@ private MutableText getCommandText(Command command) {

// Text
MutableText text = Text.literal(Utils.nameToTitle(command.getName()));
if (command != Commands.get().getAll().get(Commands.get().getAll().size() - 1)) text.append(Text.literal(", ").formatted(Formatting.GRAY));
if (command != Commands.getAll().get(Commands.getAll().size() - 1))
text.append(Text.literal(", ").formatted(Formatting.GRAY));
text.setStyle(text
.getStyle()
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, tooltip))
.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, Config.get().prefix.get() + command.getName()))
.getStyle()
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, tooltip))
.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, Config.get().prefix.get() + command.getName()))
);

return text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* Copyright (c) Meteor Development.
*/

package meteordevelopment.meteorclient.systems.commands.commands;
package meteordevelopment.meteorclient.commands.commands;

import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import meteordevelopment.meteorclient.systems.commands.Command;
import meteordevelopment.meteorclient.commands.Command;
import meteordevelopment.meteorclient.systems.modules.Modules;
import meteordevelopment.meteorclient.systems.modules.movement.NoFall;
import meteordevelopment.meteorclient.systems.modules.player.AntiHunger;
Expand All @@ -18,6 +18,7 @@
import net.minecraft.util.math.Vec3d;

import static com.mojang.brigadier.Command.SINGLE_SUCCESS;
import static meteordevelopment.meteorclient.MeteorClient.mc;

public class DamageCommand extends Command {
private final static SimpleCommandExceptionType INVULNERABLE = new SimpleCommandExceptionType(Text.literal("You are invulnerable."));
Expand Down
Loading

0 comments on commit 3807058

Please sign in to comment.