Skip to content

Commit

Permalink
changed it to be a 0.5 second threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
RealRTTV committed Nov 28, 2024
1 parent 11e5f0f commit 5a0d641
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 31 deletions.
6 changes: 6 additions & 0 deletions src/main/java/net/earthcomputer/clientcommands/Configs.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,10 @@ public enum PacketDumpMethod {

@Config
public static int maximumPacketFieldDepth = 10;

@Config(temporary = true, setter = @Config.Setter("setMinimumReplyDelaySeconds"))
public static float minimumReplyDelaySeconds = 0.5f;
public static void setMinimumReplyDelaySeconds(float minimumReplyDelaySeconds) {
Configs.minimumReplyDelaySeconds = Math.clamp(minimumReplyDelaySeconds, 0.0f, 10.0f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,53 @@
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.earthcomputer.clientcommands.Configs;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.SharedConstants;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static dev.xpple.clientarguments.arguments.CMessageArgument.*;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*;
import java.util.ArrayList;
import java.util.List;

import static dev.xpple.clientarguments.arguments.CMessageArgument.getMessage;
import static dev.xpple.clientarguments.arguments.CMessageArgument.message;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;

public class ReplyCommand {
public static final float MAXIMUM_REPLY_DELAY_SECONDS = 10.0f;

private static final SimpleCommandExceptionType NO_TARGET_FOUND_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("commands.creply.noTargetFound"));
private static final Dynamic2CommandExceptionType MESSAGE_TOO_LONG_EXCEPTION = new Dynamic2CommandExceptionType((a, b) -> Component.translatable("commands.creply.messageTooLong", a, b));

private static final List<ReplyCandidate> replyCandidates = new ArrayList<>();

@Nullable
private static String mostRecentWhisper = null;
@Nullable
private static String currentTarget = null;
public static String getCurrentTarget() {
long now = System.currentTimeMillis();

public static void refreshCurrentTarget() {
currentTarget = mostRecentWhisper;
for (int i = 0; i < replyCandidates.size(); i++) {
ReplyCandidate candidate = replyCandidates.get(i);
if ((now - candidate.timestampMs) / 1_000.0f > MAXIMUM_REPLY_DELAY_SECONDS) {
replyCandidates.remove(i--);
}
}

for (int i = replyCandidates.size() - 1; i >= 0; i--) {
ReplyCandidate candidate = replyCandidates.get(i);
if ((now - candidate.timestampMs) / 1_000.0f >= Configs.minimumReplyDelaySeconds) {
return candidate.username;
}
}

return null;
}

public static void setMostRecentWhisper(@NotNull String username) {
mostRecentWhisper = username;
public static void addReplyCandidate(String username, long timestamp) {
replyCandidates.add(new ReplyCandidate(username, timestamp));
}

public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) {
Expand All @@ -39,12 +60,13 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
}

public static int reply(FabricClientCommandSource source, Component message) throws CommandSyntaxException {
if (currentTarget == null) {
@Nullable String target = ReplyCommand.getCurrentTarget();
if (target == null) {
throw NO_TARGET_FOUND_EXCEPTION.create();
}

String text = message.getString();
String command = String.format("w %s %s", currentTarget, text);
String command = String.format("w %s %s", target, text);

if (command.length() > SharedConstants.MAX_CHAT_LENGTH) {
throw MESSAGE_TOO_LONG_EXCEPTION.create(SharedConstants.MAX_CHAT_LENGTH - (command.length() - text.length()), text.length());
Expand All @@ -54,4 +76,7 @@ public static int reply(FabricClientCommandSource source, Component message) thr

return Command.SINGLE_SUCCESS;
}

private record ReplyCandidate(String username, long timestampMs) {
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private void onHandlePlayerChat(ClientboundPlayerChatPacket packet, CallbackInfo
if (packet.chatType().chatType().is(ChatType.MSG_COMMAND_INCOMING) || packet.chatType().chatType().is(ChatType.MSG_COMMAND_OUTGOING)) {
PlayerInfo info = getPlayerInfo(packet.sender());
if (info != null) {
ReplyCommand.setMostRecentWhisper(info.getProfile().getName());
ReplyCommand.addReplyCandidate(info.getProfile().getName(), System.currentTimeMillis());
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/mixins.clientcommands.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"commands.generic.CommandSuggestionsMixin",
"commands.glow.LivingEntityRenderStateMixin",
"commands.snap.MinecraftMixin",
"commands.reply.ChatScreenMixin",
"dataqueryhandler.ClientPacketListenerMixin",
"events.ClientPacketListenerMixin",
"lengthextender.ChatScreenMixin",
Expand Down

0 comments on commit 5a0d641

Please sign in to comment.