diff --git a/src/main/java/cc/aabss/eventutils/commands/NameDetect.java b/src/main/java/cc/aabss/eventutils/commands/NameDetect.java new file mode 100644 index 0000000..4ff6f74 --- /dev/null +++ b/src/main/java/cc/aabss/eventutils/commands/NameDetect.java @@ -0,0 +1,47 @@ +package cc.aabss.eventutils.commands; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.tree.LiteralCommandNode; + +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +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.Text; + +import org.jetbrains.annotations.NotNull; + +public class NameDetectCommand { + public static void register(@NotNull CommandDispatcher dispatcher) { + // detectname command + final LiteralCommandNode detectName = ClientCommandManager + .literal("detectname") + .then(ClientCommandManager.argument("word", StringArgumentType.string()) + .executes(context -> { + String searchTerm = StringArgumentType.getString(context, "word").toLowerCase(); + detectName(context.getSource(), searchTerm); + return Command.SINGLE_SUCCESS; + })) + .build(); + + dispatcher.getRoot().addChild(detectName); + } + + private static void detectName(FabricClientCommandSource source, String searchTerm) { + MinecraftClient client = MinecraftClient.getInstance(); + if (client.player == null || client.player.networkHandler == null) return; + + // Get the list of online players and count those whose names contain the search term + long count = client.player.networkHandler.getPlayerList().stream() + .map(PlayerListEntry::getProfile) + .map(profile -> profile.getName().toLowerCase()) + .filter(name -> name.contains(searchTerm)) + .count(); + + // Send the result message to the player + source.getPlayer().sendMessage(Text.of("§7Found §4§o§l" + count + "§r§7 player(s) with '" + searchTerm + "' in their name."), false); + } +}