From c1ee5ad77b27d777b93c2ea5d3882bf62df2cfff Mon Sep 17 00:00:00 2001 From: Jerry Date: Mon, 20 Mar 2023 00:24:18 +0800 Subject: [PATCH] bungeecord-like command send --- pom.xml | 6 +- src/main/java/de/strifel/VTools/VTools.java | 1 - .../strifel/VTools/commands/CommandSend.java | 53 +++++++++++++--- .../VTools/commands/CommandSendall.java | 61 ------------------- 4 files changed, 49 insertions(+), 72 deletions(-) delete mode 100644 src/main/java/de/strifel/VTools/commands/CommandSendall.java diff --git a/pom.xml b/pom.xml index 9bc3ff1..5319d98 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ velocity - https://repo.velocitypowered.com/snapshots/ + https://repo.papermc.io/repository/maven-public/ @@ -31,9 +31,9 @@ com.velocitypowered velocity-api - 3.1.0-SNAPSHOT + 3.1.0 provided - \ No newline at end of file + diff --git a/src/main/java/de/strifel/VTools/VTools.java b/src/main/java/de/strifel/VTools/VTools.java index 4e62a1e..3b02e7b 100644 --- a/src/main/java/de/strifel/VTools/VTools.java +++ b/src/main/java/de/strifel/VTools/VTools.java @@ -26,7 +26,6 @@ public class VTools { @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { server.getCommandManager().register("send", new CommandSend(server)); - server.getCommandManager().register("sendall", new CommandSendall(server)); server.getCommandManager().register("broadcast", new CommandBroadcast(server), "bc", "alert"); server.getCommandManager().register("find", new CommandFind(server), "search"); server.getCommandManager().register("staffchat", new CommandStaffChat(server), "sc"); diff --git a/src/main/java/de/strifel/VTools/commands/CommandSend.java b/src/main/java/de/strifel/VTools/commands/CommandSend.java index 772cbbf..a0b3286 100644 --- a/src/main/java/de/strifel/VTools/commands/CommandSend.java +++ b/src/main/java/de/strifel/VTools/commands/CommandSend.java @@ -4,6 +4,7 @@ import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.server.RegisteredServer; import net.kyori.adventure.text.Component; @@ -25,17 +26,53 @@ public class CommandSend implements SimpleCommand { CommandSource commandSource = invocation.source(); String[] strings = invocation.arguments(); + if (strings.length == 2) { - Optional oPlayer = server.getPlayer(strings[0]); + List oPlayer = new ArrayList<>(); + if (strings[0].equals("all")) { + for (Player player : server.getAllPlayers()) { + oPlayer.add(player); + } + } + else if (strings[0].equals("current")) { + if (commandSource instanceof Player) { + Player playerSource = (Player)commandSource; + Optional conn = playerSource.getCurrentServer(); + if (conn.isPresent()) { + for (Player player : conn.get().getServer().getPlayersConnected()) { + oPlayer.add(player); + } + } + } + else { + commandSource.sendMessage(Component.text("Command is only for players.").color(COLOR_RED)); + return; + } + for (Player player : server.getAllPlayers()) { + oPlayer.add(player); + } + } + else { + Optional p = server.getPlayer(strings[0]); + if (p.isPresent()) { + oPlayer.add(p.get()); + } + } Optional oServer = server.getServer(strings[1]); - if (oPlayer.isPresent() && oServer.isPresent()) { - Player player = oPlayer.get(); + if (!oPlayer.isEmpty() && oServer.isPresent()) { RegisteredServer server = oServer.get(); - player.createConnectionRequest(server).connect(); - commandSource.sendMessage(Component.text("You send " + player.getUsername() + " to " + server.getServerInfo().getName()).color(COLOR_YELLOW)); - commandSource.sendMessage(Component.text("You got send to " + server.getServerInfo().getName()).color(COLOR_YELLOW)); + for (Player player : oPlayer) { + player.createConnectionRequest(server).connect(); + if (oPlayer.size() <= 1) { + commandSource.sendMessage(Component.text("You send " + player.getUsername() + " to " + server.getServerInfo().getName()).color(COLOR_YELLOW)); + } + //commandSource.sendMessage(Component.text("You got send to " + server.getServerInfo().getName()).color(COLOR_YELLOW)); + } + if (oPlayer.size() > 1) { + commandSource.sendMessage(Component.text(String.format("You send %d players to %s", oPlayer.size(), server.getServerInfo().getName())).color(COLOR_YELLOW)); + } } else { - commandSource.sendMessage(Component.text("The server or user does not exists!").color(COLOR_RED)); + commandSource.sendMessage(Component.text("The server or user does not exist!").color(COLOR_RED)); } } else { commandSource.sendMessage(Component.text("Usage: /send ").color(COLOR_RED)); @@ -47,6 +84,8 @@ public class CommandSend implements SimpleCommand { List arg = new ArrayList(); if (currentArgs.length == 1) { + arg.add("all"); + arg.add("current"); for (Player player : server.getAllPlayers()) { arg.add(player.getUsername()); } diff --git a/src/main/java/de/strifel/VTools/commands/CommandSendall.java b/src/main/java/de/strifel/VTools/commands/CommandSendall.java deleted file mode 100644 index b260daa..0000000 --- a/src/main/java/de/strifel/VTools/commands/CommandSendall.java +++ /dev/null @@ -1,61 +0,0 @@ -package de.strifel.VTools.commands; - -import com.velocitypowered.api.command.CommandSource; -import com.velocitypowered.api.command.SimpleCommand; -import com.velocitypowered.api.proxy.Player; -import com.velocitypowered.api.proxy.ProxyServer; -import com.velocitypowered.api.proxy.server.RegisteredServer; -import net.kyori.adventure.text.Component; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -import static de.strifel.VTools.VTools.COLOR_RED; -import static de.strifel.VTools.VTools.COLOR_YELLOW; - -public class CommandSendall implements SimpleCommand { - private final ProxyServer server; - - public CommandSendall(ProxyServer server) { - this.server = server; - } - - @Override - public void execute(SimpleCommand.Invocation invocation) { - CommandSource commandSource = invocation.source(); - String[] strings = invocation.arguments(); - - if (strings.length == 1) { - Optional oServer = server.getServer(strings[0]); - if (oServer.isPresent()) { - for (Player player : server.getAllPlayers()) { - player.createConnectionRequest(oServer.get()).connect(); - player.sendMessage(Component.text("You are being sent to " + strings[0]).color(COLOR_YELLOW)); - } - } else { - commandSource.sendMessage(Component.text("The server does not exists!").color(COLOR_RED)); - } - } else { - commandSource.sendMessage(Component.text("Usage: /sendall ").color(COLOR_RED)); - } - } - - @Override - public List suggest(SimpleCommand.Invocation invocation) { - String[] currentArgs = invocation.arguments(); - - List arg = new ArrayList(); - if (currentArgs.length == 1) { - for (RegisteredServer server : server.getAllServers()) { - arg.add(server.getServerInfo().getName()); - } - } - return arg; - } - - @Override - public boolean hasPermission(SimpleCommand.Invocation invocation) { - return invocation.source().hasPermission("vtools.sendall"); - } -}