Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/main/java/cc/aabss/eventutils/commands/CommandRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import org.jetbrains.annotations.NotNull;


public class CommandRegister {
public static void register(@NotNull CommandDispatcher<FabricClientCommandSource> dispatcher) {
// eventutils
Expand Down Expand Up @@ -77,11 +76,25 @@ public static void register(@NotNull CommandDispatcher<FabricClientCommandSource
return 0;
}).build();

// eventutils detectname
final LiteralCommandNode<FabricClientCommandSource> detectName = ClientCommandManager
.literal("detectname")
.then(ClientCommandManager.argument("word", StringArgumentType.string())
.executes(context -> {
DetectNameCmd.detectName(context, StringArgumentType.getString(context, "word"));
return 0;
}))
.executes(context -> {
context.getSource().sendFeedback(Text.literal("Usage: /detectname <word>").formatted(Formatting.RED));
return 0;
}).build();

// Build command tree
dispatcher.getRoot().addChild(main);
main.addChild(config);
main.addChild(teleport);
main.addChild(priority);
main.addChild(priorityTop);
main.addChild(detectName);
}
}
40 changes: 40 additions & 0 deletions src/main/java/cc/aabss/eventutils/commands/DetectNameCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cc.aabss.eventutils.commands;

import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class DetectNameCmd {
public static void detectName(@NotNull CommandContext<FabricClientCommandSource> context, String searchTerm) {
final MinecraftClient client = context.getSource().getClient();
client.send(() -> {
if (client.player == null || client.player.networkHandler == null) {
context.getSource().sendFeedback(Text.literal("No players found!").formatted(Formatting.RED));
return;
}

final List<PlayerListEntry> players = client.player.networkHandler.getPlayerList();
final long count = players.stream()
.map(entry -> entry.getProfile().getName().toLowerCase())
.filter(name -> name.contains(searchTerm.toLowerCase()))
.count();

MutableText message = Text.literal("Found ")
.append(Text.literal(String.valueOf(count)).formatted(Formatting.RED, Formatting.BOLD, Formatting.ITALIC))
.append(Text.literal(" player(s) with '").formatted(Formatting.GRAY))
.append(Text.literal(searchTerm).formatted(Formatting.WHITE))
.append(Text.literal("' in their name.").formatted(Formatting.GRAY));

context.getSource().sendFeedback(message);
});
}
}