dfghjkl;'

This commit is contained in:
kawaiizenbo 2022-12-06 21:59:13 -07:00
parent baf8d849b6
commit 412cabff8a
23 changed files with 706 additions and 18 deletions

View file

@ -0,0 +1,84 @@
package me.kawaiizenbo.moonlight.command;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.CommandSource;
public abstract class Command
{
protected static MinecraftClient mc;
private final String name;
private final String description;
private final List<String> aliases = new ArrayList<>();
public int SINGLE_SUCCESS = com.mojang.brigadier.Command.SINGLE_SUCCESS;
public Command(String name, String description, String... aliases) {
this.name = name;
this.description = description;
Collections.addAll(this.aliases, aliases);
mc = MinecraftClient.getInstance();
}
// Helper methods to painlessly infer the CommandSource generic type argument
protected static <T> RequiredArgumentBuilder<CommandSource, T> argument(final String name, final ArgumentType<T> type) {
return RequiredArgumentBuilder.argument(name, type);
}
protected static LiteralArgumentBuilder<CommandSource> literal(final String name) {
return LiteralArgumentBuilder.literal(name);
}
public final void registerTo(CommandDispatcher<CommandSource> dispatcher) {
register(dispatcher, name);
for (String alias : aliases) register(dispatcher, alias);
}
public void register(CommandDispatcher<CommandSource> dispatcher, String name) {
LiteralArgumentBuilder<CommandSource> builder = LiteralArgumentBuilder.literal(name);
build(builder);
dispatcher.register(builder);
}
public abstract void build(LiteralArgumentBuilder<CommandSource> builder);
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public List<String> getAliases() {
return aliases;
}
public String toString() {
return CommandManager.get().getPrefix() + name;
}
public String toString(String... args) {
StringBuilder base = new StringBuilder(toString());
for (String arg : args)
base.append(' ').append(arg);
return base.toString();
}
public static String nameToTitle(String name) {
return Arrays.stream(name.split("-")).map(StringUtils::capitalize).collect(Collectors.joining(" "));
}
}

View file

@ -0,0 +1,86 @@
package me.kawaiizenbo.moonlight.command;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientCommandSource;
import net.minecraft.command.CommandSource;
import me.kawaiizenbo.moonlight.command.commands.*;
public class CommandManager
{
private static MinecraftClient mc = MinecraftClient.getInstance();
private final CommandDispatcher<CommandSource> DISPATCHER = new CommandDispatcher<>();
private final CommandSource COMMAND_SOURCE = new ChatCommandSource(mc);
private final List<Command> commands = new ArrayList<>();
private final Map<Class<? extends Command>, Command> commandInstances = new HashMap<>();
private CommandManager() {
add(new VClip());
add(new Help());
add(new Toggle());
commands.sort(Comparator.comparing(Command::getName));
}
public static CommandManager get() {
return new CommandManager();
}
public void dispatch(String message) throws CommandSyntaxException {
dispatch(message, new ChatCommandSource(mc));
}
public void dispatch(String message, CommandSource source) throws CommandSyntaxException {
ParseResults<CommandSource> results = DISPATCHER.parse(message, source);
DISPATCHER.execute(results);
}
public CommandDispatcher<CommandSource> getDispatcher() {
return DISPATCHER;
}
public CommandSource getCommandSource() {
return COMMAND_SOURCE;
}
private final static class ChatCommandSource extends ClientCommandSource {
public ChatCommandSource(MinecraftClient client) {
super(null, client);
}
}
public void add(Command command) {
commands.removeIf(command1 -> command1.getName().equals(command.getName()));
commandInstances.values().removeIf(command1 -> command1.getName().equals(command.getName()));
command.registerTo(DISPATCHER);
commands.add(command);
commandInstances.put(command.getClass(), command);
}
public int getCount() {
return commands.size();
}
public List<Command> getAll() {
return commands;
}
@SuppressWarnings("unchecked")
public <T extends Command> T get(Class<T> klass) {
return (T) commandInstances.get(klass);
}
public String getPrefix() {
return "?";
}
}

View file

@ -0,0 +1,63 @@
package me.kawaiizenbo.moonlight.command;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import me.kawaiizenbo.moonlight.module.Module_;
import me.kawaiizenbo.moonlight.module.ModuleManager;
import net.minecraft.command.CommandSource;
import net.minecraft.text.Text;
public class ModuleArgumentType implements ArgumentType<Module_>
{
private static final Collection<String> EXAMPLES = ModuleManager.INSTANCE.modules
.stream()
.limit(3)
.map(module -> module.name)
.collect(Collectors.toList());
private static final DynamicCommandExceptionType NO_SUCH_MODULE = new DynamicCommandExceptionType(o ->
Text.literal("Module with name " + o + " doesn't exist."));
public static ModuleArgumentType module()
{
return new ModuleArgumentType();
}
public static Module_ getModule(final CommandContext<?> context, final String name)
{
return context.getArgument(name, Module_.class);
}
@Override
public Module_ parse(StringReader reader) throws CommandSyntaxException
{
String argument = reader.readString();
Module_ module = ModuleManager.INSTANCE.getModuleByName(argument);
if (module == null) throw NO_SUCH_MODULE.create(argument);
return module;
}
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder)
{
return CommandSource.suggestMatching(ModuleManager.INSTANCE.modules.stream().map(module -> module.name), builder);
}
@Override
public Collection<String> getExamples()
{
return EXAMPLES;
}
}

View file

@ -0,0 +1,32 @@
package me.kawaiizenbo.moonlight.command.commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.kawaiizenbo.moonlight.command.Command;
import me.kawaiizenbo.moonlight.command.CommandManager;
import me.kawaiizenbo.moonlight.util.ChatUtils;
import me.kawaiizenbo.moonlight.util.ColorUtils;
import net.minecraft.command.CommandSource;
public class Help extends Command
{
public Help()
{
super("help", "Gives you a list of all of the commands");
}
@Override
public void build(LiteralArgumentBuilder<CommandSource> builder)
{
builder.executes(context ->
{
for (Command cmd : CommandManager.get().getAll()) {
ChatUtils.sendMsg(ColorUtils.aqua + "Command: " + ColorUtils.gray + cmd.getName());
ChatUtils.sendMsg(ColorUtils.gray + cmd.getDescription());
}
return SINGLE_SUCCESS;
});
}
}

View file

@ -0,0 +1,34 @@
package me.kawaiizenbo.moonlight.command.commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.kawaiizenbo.moonlight.command.Command;
import me.kawaiizenbo.moonlight.command.ModuleArgumentType;
import me.kawaiizenbo.moonlight.module.Module_;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.command.CommandSource;
public class Toggle extends Command
{
public Toggle()
{
super("toggle", "Toggle a module.");
}
@Override
public void build(LiteralArgumentBuilder<CommandSource> builder)
{
builder.then(argument("module", new ModuleArgumentType()).executes(context ->
{
ClientPlayerEntity player = mc.player;
assert player != null;
Module_ m = context.getArgument("module", Module_.class);
m.toggle();
return SINGLE_SUCCESS;
}));
}
}

View file

@ -0,0 +1,39 @@
package me.kawaiizenbo.moonlight.command.commands;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.kawaiizenbo.moonlight.command.Command;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.command.CommandSource;
import net.minecraft.entity.Entity;
public class VClip extends Command
{
public VClip()
{
super("vclip", "Teleports you vertically.");
}
@Override
public void build(LiteralArgumentBuilder<CommandSource> builder)
{
builder.then(argument("blocks", DoubleArgumentType.doubleArg()).executes(context ->
{
ClientPlayerEntity player = mc.player;
assert player != null;
double blocks = context.getArgument("blocks", Double.class);
if (player.hasVehicle())
{
Entity vehicle = player.getVehicle();
vehicle.setPosition(vehicle.getX(), vehicle.getY() + blocks, vehicle.getZ());
}
player.setPosition(player.getX(), player.getY() + blocks, player.getZ());
return SINGLE_SUCCESS;
}));
}
}