Skip to content

Commit

Permalink
Send clientcommands command execution to server if the server is list…
Browse files Browse the repository at this point in the history
…ening for it
  • Loading branch information
Earthcomputer committed Aug 27, 2023
1 parent ef92aba commit 14665f5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
37 changes: 37 additions & 0 deletions src/main/java/net/earthcomputer/clientcommands/ClientCommands.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
package net.earthcomputer.clientcommands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.mojang.logging.LogUtils;
import dev.xpple.betterconfig.api.ModConfigBuilder;
import io.netty.buffer.Unpooled;
import net.earthcomputer.clientcommands.command.*;
import net.earthcomputer.clientcommands.render.RenderQueue;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
import net.minecraft.util.math.Vec3d;
import org.slf4j.Logger;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ClientCommands implements ClientModInitializer {
private static final Logger LOGGER = LogUtils.getLogger();
public static Path configDir;
private static final Set<String> clientcommandsCommands = new HashSet<>();
public static final Identifier COMMAND_EXECUTION_PACKET_ID = new Identifier("clientcommands", "command_execution");

public static final boolean SCRAMBLE_WINDOW_TITLE = Util.make(() -> {
String playerUUID = MinecraftClient.getInstance().getSession().getProfile().getId().toString();
Expand Down Expand Up @@ -68,7 +79,26 @@ public void onInitializeClient() {
ItemGroupCommand.registerItemGroups();
}

private static Set<String> getCommands(CommandDispatcher<?> dispatcher) {
return dispatcher.getRoot().getChildren().stream().flatMap(node -> node instanceof LiteralCommandNode<?> literal ? Stream.of(literal.getLiteral()) : Stream.empty()).collect(Collectors.toSet());
}

public static void sendCommandExecutionToServer(String command) {
StringReader reader = new StringReader(command);
reader.skipWhitespace();
String theCommand = reader.readUnquotedString();
if (clientcommandsCommands.contains(theCommand) && !"cwe".equals(theCommand)) { // avoid sending end-to-end encrypted messages
if (ClientPlayNetworking.canSend(COMMAND_EXECUTION_PACKET_ID)) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeString(command);
ClientPlayNetworking.send(COMMAND_EXECUTION_PACKET_ID, buf);
}
}
}

public static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
Set<String> existingCommands = getCommands(dispatcher);

AuditMixinsCommand.register(dispatcher);
BookCommand.register(dispatcher);
LookCommand.register(dispatcher);
Expand Down Expand Up @@ -121,5 +151,12 @@ public static void registerCommands(CommandDispatcher<FabricClientCommandSource>
PosCommand.register(dispatcher);
CrackRNGCommand.register(dispatcher);
WeatherCommand.register(dispatcher);

clientcommandsCommands.clear();
for (String command : getCommands(dispatcher)) {
if (!existingCommands.contains(command)) {
clientcommandsCommands.add(command);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ static CommandFunction load(CommandDispatcher<FabricClientCommandSource> dispatc
//noinspection ConstantConditions
throw CommandManager.getException(command);
}
entries.add(new ParsedEntry(command));
entries.add(new ParsedEntry(line, command));
}
}
} catch (IOException e) {
Expand All @@ -243,17 +243,20 @@ private interface Entry {
void execute(CommandDispatcher<FabricClientCommandSource> dispatcher, FabricClientCommandSource source) throws CommandSyntaxException;
}

private record ParsedEntry(ParseResults<FabricClientCommandSource> command) implements Entry {
private record ParsedEntry(String commandString, ParseResults<FabricClientCommandSource> command) implements Entry {
@Override
public void execute(CommandDispatcher<FabricClientCommandSource> dispatcher, FabricClientCommandSource source) throws CommandSyntaxException {
ClientCommands.sendCommandExecutionToServer(commandString);
dispatcher.execute(command);
}
}

private record LazyEntry(String command) implements Entry {
@Override
public void execute(CommandDispatcher<FabricClientCommandSource> dispatcher, FabricClientCommandSource source) throws CommandSyntaxException {
dispatcher.execute(VarCommand.replaceVariables(command), source);
String replacedCommand = VarCommand.replaceVariables(command);
ClientCommands.sendCommandExecutionToServer(replacedCommand);
dispatcher.execute(replacedCommand, source);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.earthcomputer.clientcommands.mixin;

import net.earthcomputer.clientcommands.ClientCommands;
import net.earthcomputer.clientcommands.command.VarCommand;
import net.minecraft.client.gui.screen.ChatScreen;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -13,6 +14,10 @@ public class MixinChatScreen {
// but ensure the message is added to the history in its raw form.
@ModifyVariable(method = "sendMessage", at = @At(value = "INVOKE", target = "Ljava/lang/String;startsWith(Ljava/lang/String;)Z", remap = false), argsOnly = true)
private String onSendMessage(String message) {
return VarCommand.replaceVariables(message);
String command = VarCommand.replaceVariables(message);
if (command.startsWith("/")) {
ClientCommands.sendCommandExecutionToServer(command.substring(1));
}
return command;
}
}

0 comments on commit 14665f5

Please sign in to comment.