From c68915b2613cd14870c7bb9f96170de268cb85c5 Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Sat, 15 Jun 2024 15:05:26 +0200 Subject: [PATCH] Add /viaversion player command (#3932) --- .../commands/ViaCommandHandler.java | 2 + .../commands/defaultsubs/PlayerSubCmd.java | 75 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/PlayerSubCmd.java diff --git a/common/src/main/java/com/viaversion/viaversion/commands/ViaCommandHandler.java b/common/src/main/java/com/viaversion/viaversion/commands/ViaCommandHandler.java index 38e4d79e7..bc7bd4ff8 100644 --- a/common/src/main/java/com/viaversion/viaversion/commands/ViaCommandHandler.java +++ b/common/src/main/java/com/viaversion/viaversion/commands/ViaCommandHandler.java @@ -30,6 +30,7 @@ import com.viaversion.viaversion.commands.defaultsubs.DumpSubCmd; import com.viaversion.viaversion.commands.defaultsubs.ListSubCmd; import com.viaversion.viaversion.commands.defaultsubs.PPSSubCmd; import com.viaversion.viaversion.commands.defaultsubs.ReloadSubCmd; +import com.viaversion.viaversion.commands.defaultsubs.PlayerSubCmd; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -194,5 +195,6 @@ public abstract class ViaCommandHandler implements ViaVersionCommand { registerSubCommand(new DontBugMeSubCmd()); registerSubCommand(new AutoTeamSubCmd()); registerSubCommand(new ReloadSubCmd()); + registerSubCommand(new PlayerSubCmd()); } } diff --git a/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/PlayerSubCmd.java b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/PlayerSubCmd.java new file mode 100644 index 000000000..8f309d024 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/commands/defaultsubs/PlayerSubCmd.java @@ -0,0 +1,75 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.commands.defaultsubs; + +import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.command.ViaCommandSender; +import com.viaversion.viaversion.api.command.ViaSubCommand; +import com.viaversion.viaversion.api.connection.ProtocolInfo; +import com.viaversion.viaversion.api.connection.UserConnection; +import java.util.ArrayList; +import java.util.List; + +public class PlayerSubCmd implements ViaSubCommand { + @Override + public String name() { + return "player"; + } + + @Override + public String description() { + return "Shows connection information about one or all players."; + } + + @Override + public String usage() { + return "player "; + } + + @Override + public boolean execute(final ViaCommandSender sender, final String[] args) { + if (args.length == 0) { + return false; + } + for (final UserConnection connection : Via.getManager().getConnectionManager().getConnections()) { + final ProtocolInfo info = connection.getProtocolInfo(); + if (args[0].equalsIgnoreCase(info.getUsername()) || args[0].equals("*")) { + sendMessage(sender, "&7[&6" + info.getUsername() + "&7] UUID: &2" + info.getUuid() + " &7Client-Protocol: &2" + info.protocolVersion().getName() + " &7Server-Protocol: &2" + info.serverProtocolVersion().getName()); + } + } + return true; + } + + @Override + public List onTabComplete(final ViaCommandSender sender, final String[] args) { + if (args.length == 1) { + final String input = args[0].toLowerCase(); + + final List matches = new ArrayList<>(); + for (final UserConnection connection : Via.getManager().getConnectionManager().getConnections()) { + final String name = connection.getProtocolInfo().getUsername(); + if (input.isEmpty() || name.toLowerCase().startsWith(input)) { + matches.add(name); + } + } + matches.add("*"); + return matches; + } + return ViaSubCommand.super.onTabComplete(sender, args); + } +}