1
0
Fork 0
forked from mc/VTools
This commit is contained in:
JerryXiao 2023-03-20 21:05:24 +08:00
parent ee63121964
commit b51a964033
Signed by untrusted user: Jerry
GPG key ID: 22618F758B5BE2E5
10 changed files with 100 additions and 66 deletions

16
pom.xml
View file

@ -13,11 +13,21 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>8</source> <source>17</source>
<target>8</target> <target>17</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>.</directory>
<includes>
<include>velocity-plugin.json</include>
</includes>
</resource>
</resources>
</build> </build>
<repositories> <repositories>
@ -31,7 +41,7 @@
<dependency> <dependency>
<groupId>com.velocitypowered</groupId> <groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId> <artifactId>velocity-api</artifactId>
<version>3.1.0</version> <version>4.0.0-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -1,7 +1,7 @@
package de.strifel.VTools; package de.strifel.VTools;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.lifecycle.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import de.strifel.VTools.commands.*; import de.strifel.VTools.commands.*;
@ -10,12 +10,12 @@ import org.slf4j.Logger;
import javax.inject.Inject; import javax.inject.Inject;
@Plugin(id = "vtools", name="VTools", version="1.0-SNAPSHOT", description="Some commands!") @Plugin(id = "vtools", name="VTools", version="1.0-SNAPSHOT", description="Some commands!", authors="unnamed")
public class VTools { public class VTools {
private final ProxyServer server; private final ProxyServer server;
public static final TextColor COLOR_RED = TextColor.fromCSSHexString("FF5555"); public static final TextColor COLOR_RED = TextColor.fromCSSHexString("#FF5555");
public static final TextColor COLOR_YELLOW = TextColor.fromCSSHexString("FFFF55"); public static final TextColor COLOR_YELLOW = TextColor.fromCSSHexString("#FFFF55");
@Inject @Inject
public VTools(ProxyServer server, Logger logger) { public VTools(ProxyServer server, Logger logger) {
@ -25,13 +25,13 @@ public class VTools {
@Subscribe @Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) { public void onProxyInitialization(ProxyInitializeEvent event) {
server.getCommandManager().register("send", new CommandSend(server)); server.commandManager().register("send", new CommandSend(server));
server.getCommandManager().register("broadcast", new CommandBroadcast(server), "bc", "alert"); server.commandManager().register("broadcast", new CommandBroadcast(server), "bc", "alert");
server.getCommandManager().register("find", new CommandFind(server), "search"); server.commandManager().register("find", new CommandFind(server), "search");
server.getCommandManager().register("staffchat", new CommandStaffChat(server), "sc"); server.commandManager().register("staffchat", new CommandStaffChat(server), "sc");
server.getCommandManager().register("restart", new CommandRestart(server)); server.commandManager().register("restart", new CommandRestart(server));
server.getCommandManager().register("tps", new CommandTp(server), "jump"); server.commandManager().register("tps", new CommandTp(server), "jump");
server.getCommandManager().register("servers", new CommandServers(server), "allservers"); server.commandManager().register("servers", new CommandServers(server), "allservers");
} }
} }

View file

@ -2,7 +2,7 @@ package de.strifel.VTools.commands;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -25,7 +25,7 @@ public class CommandBroadcast implements SimpleCommand {
if (strings.length > 0) { if (strings.length > 0) {
String message = String.join(" ", strings).replace("&", "§"); String message = String.join(" ", strings).replace("&", "§");
for (Player player : server.getAllPlayers()) { for (Player player : server.connectedPlayers()) {
player.sendMessage(Component.text(message)); player.sendMessage(Component.text(message));
} }
} else { } else {

View file

@ -2,13 +2,12 @@ package de.strifel.VTools.commands;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import static de.strifel.VTools.VTools.COLOR_RED; import static de.strifel.VTools.VTools.COLOR_RED;
import static de.strifel.VTools.VTools.COLOR_YELLOW; import static de.strifel.VTools.VTools.COLOR_YELLOW;
@ -27,9 +26,9 @@ public class CommandFind implements SimpleCommand {
String[] strings = invocation.arguments(); String[] strings = invocation.arguments();
if (strings.length == 1) { if (strings.length == 1) {
Optional<Player> player = server.getPlayer(strings[0]); Player player = server.player(strings[0]);
if (player.isPresent() && player.get().getCurrentServer().isPresent()) { if (player != null && player.connectedServer() != null) {
commandSource.sendMessage(Component.text("Player " + strings[0] + " is on " + player.get().getCurrentServer().get().getServerInfo().getName() + "!").color(COLOR_YELLOW)); commandSource.sendMessage(Component.text("Player " + strings[0] + " is on " + player.connectedServer().serverInfo().name() + "!").color(COLOR_YELLOW));
} else { } else {
commandSource.sendMessage(Component.text("The player is not online!").color(COLOR_YELLOW)); commandSource.sendMessage(Component.text("The player is not online!").color(COLOR_YELLOW));
} }
@ -44,8 +43,8 @@ public class CommandFind implements SimpleCommand {
List<String> arg = new ArrayList<>(); List<String> arg = new ArrayList<>();
if (currentArgs.length <= 1 && invocation.source().hasPermission("vtools.find.autocomplete")) { if (currentArgs.length <= 1 && invocation.source().hasPermission("vtools.find.autocomplete")) {
for (Player player : server.getAllPlayers()) { for (Player player : server.connectedPlayers()) {
arg.add(player.getUsername()); arg.add(player.username());
} }
} }
return arg; return arg;

View file

@ -2,7 +2,7 @@ package de.strifel.VTools.commands;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -24,11 +24,11 @@ public class CommandRestart implements SimpleCommand {
if (strings.length > 0) { if (strings.length > 0) {
String message = String.join(" ", strings).replace("&", "§"); String message = String.join(" ", strings).replace("&", "§");
for (Player player : server.getAllPlayers()) { for (Player player : server.connectedPlayers()) {
player.disconnect(Component.text(message)); player.disconnect(Component.text(message));
} }
} }
server.getCommandManager().executeAsync(server.getConsoleCommandSource(), "shutdown"); server.commandManager().execute(server.consoleCommandSource(), "shutdown");
} }
@Override @Override

View file

@ -2,15 +2,18 @@ package de.strifel.VTools.commands;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.player.ConnectionRequestBuilder;
import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import static de.strifel.VTools.VTools.COLOR_RED; import static de.strifel.VTools.VTools.COLOR_RED;
import static de.strifel.VTools.VTools.COLOR_YELLOW; import static de.strifel.VTools.VTools.COLOR_YELLOW;
@ -30,19 +33,12 @@ public class CommandSend implements SimpleCommand {
if (strings.length == 2) { if (strings.length == 2) {
List<Player> oPlayer = new ArrayList<>(); List<Player> oPlayer = new ArrayList<>();
if (strings[0].equals("all")) { if (strings[0].equals("all")) {
for (Player player : server.getAllPlayers()) { oPlayer.addAll(server.connectedPlayers());
oPlayer.add(player);
}
} }
else if (strings[0].equals("current")) { else if (strings[0].equals("current")) {
if (commandSource instanceof Player) { if (commandSource instanceof Player) {
Player playerSource = (Player)commandSource; Player playerSource = (Player)commandSource;
Optional<ServerConnection> conn = playerSource.getCurrentServer(); oPlayer.addAll(playerSource.connectedServer().target().connectedPlayers());
if (conn.isPresent()) {
for (Player player : conn.get().getServer().getPlayersConnected()) {
oPlayer.add(player);
}
}
} }
else { else {
commandSource.sendMessage(Component.text("Command is only for players.").color(COLOR_RED)); commandSource.sendMessage(Component.text("Command is only for players.").color(COLOR_RED));
@ -50,26 +46,47 @@ public class CommandSend implements SimpleCommand {
} }
} }
else { else {
Optional<Player> p = server.getPlayer(strings[0]); Player p = server.player(strings[0]);
if (p.isPresent()) { if (p != null) {
oPlayer.add(p.get()); oPlayer.add(p);
} }
} }
Optional<RegisteredServer> oServer = server.getServer(strings[1]); HashMap<Player, CompletableFuture<ConnectionRequestBuilder.Result>> futures = new HashMap<>();
if (!oPlayer.isEmpty() && oServer.isPresent()) { RegisteredServer server = this.server.server(strings[1]);
RegisteredServer server = oServer.get(); if (!oPlayer.isEmpty() && server != null) {
for (Player player : oPlayer) { for (Player player : oPlayer) {
player.createConnectionRequest(server).connect(); CompletableFuture<ConnectionRequestBuilder.Result> future = player.createConnectionRequest(server).connect();
if (oPlayer.size() <= 1) { futures.put(player, future);
commandSource.sendMessage(Component.text("You send " + player.getUsername() + " to " + server.getServerInfo().getName()).color(COLOR_YELLOW)); if (oPlayer.size() == 1) {
} commandSource.sendMessage(Component.text("Sending " + player.username() + " to " + server.serverInfo().name()).color(COLOR_YELLOW));
if (player.hasPermission("vtools.send.recvmsg")) {
commandSource.sendMessage(Component.text("You got send to " + server.getServerInfo().getName()).color(COLOR_YELLOW));
} }
} }
if (oPlayer.size() > 1) { if (oPlayer.size() > 1) {
commandSource.sendMessage(Component.text(String.format("You send %d players to %s", oPlayer.size(), server.getServerInfo().getName())).color(COLOR_YELLOW)); commandSource.sendMessage(Component.text(String.format("Sending %d players to %s", oPlayer.size(), server.serverInfo().name())).color(COLOR_YELLOW));
} }
new Thread(() -> {
futures.forEach(((player, future) -> {
future.whenComplete(((result, throwable) -> {
if (result.isSuccessful() && player.hasPermission("vtools.send.recvmsg")) {
player.sendMessage(Component.text("You got sent to " + server.serverInfo().name()).color(COLOR_YELLOW));
}
}));
}));
final HashMap<String, Integer> results = new HashMap<>();
futures.forEach(((player, future) -> {
try {
String status = future.get().status().name();
results.put(status, results.getOrDefault(status, 0) + 1);
} catch (InterruptedException | ExecutionException e) {
String status = e.getCause().getClass().getSimpleName();
results.put(status, results.getOrDefault(status, 0) + 1);
}
}));
String sendResults = results.isEmpty() ? "nothing" : results.entrySet().stream().map(
entry -> String.format("%s : %d", entry.getKey(), entry.getValue())
).collect(Collectors.joining("\n"));
commandSource.sendMessage(Component.text(String.format("Send Results:\n%s", sendResults)).color(COLOR_YELLOW));
}).start();
} else { } else {
commandSource.sendMessage(Component.text("The server or user does not exist!").color(COLOR_RED)); commandSource.sendMessage(Component.text("The server or user does not exist!").color(COLOR_RED));
} }
@ -85,13 +102,13 @@ public class CommandSend implements SimpleCommand {
if (currentArgs.length <= 1) { if (currentArgs.length <= 1) {
arg.add("all"); arg.add("all");
arg.add("current"); arg.add("current");
for (Player player : server.getAllPlayers()) { for (Player player : server.connectedPlayers()) {
arg.add(player.getUsername()); arg.add(player.username());
} }
return arg; return arg;
} else if (currentArgs.length == 2) { } else if (currentArgs.length == 2) {
for (RegisteredServer server : server.getAllServers()) { for (RegisteredServer server : server.registeredServers()) {
arg.add(server.getServerInfo().getName()); arg.add(server.serverInfo().name());
} }
} }
return arg; return arg;

View file

@ -25,8 +25,8 @@ public class CommandServers implements SimpleCommand {
String[] strings = invocation.arguments(); String[] strings = invocation.arguments();
StringBuilder servers = new StringBuilder(); StringBuilder servers = new StringBuilder();
for (RegisteredServer server : server.getAllServers()) { for (RegisteredServer server : server.registeredServers()) {
servers.append(server.getServerInfo().getName()); servers.append(server.serverInfo().name());
servers.append(" "); servers.append(" ");
} }
commandSource.sendMessage(Component.text(servers.toString()).color(COLOR_YELLOW)); commandSource.sendMessage(Component.text(servers.toString()).color(COLOR_YELLOW));

View file

@ -2,7 +2,7 @@ package de.strifel.VTools.commands;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -27,8 +27,8 @@ public class CommandStaffChat implements SimpleCommand {
if (strings.length > 0) { if (strings.length > 0) {
String channel = strings[0].startsWith("c:") && !strings[0].equals("c:") ? strings[0].split(":")[1] : null; String channel = strings[0].startsWith("c:") && !strings[0].equals("c:") ? strings[0].split(":")[1] : null;
String message = "§4[Staff]§r " + (commandSource instanceof Player ? ((Player) commandSource).getUsername() : "Console") + (channel != null ? " (" + channel + ")" : "")+ " > " + String.join(" ", Arrays.copyOfRange(strings, channel == null ? 0 : 1, strings.length)).replace("&", "§"); String message = "§4[Staff]§r " + (commandSource instanceof Player ? ((Player) commandSource).username() : "Console") + (channel != null ? " (" + channel + ")" : "")+ " > " + String.join(" ", Arrays.copyOfRange(strings, channel == null ? 0 : 1, strings.length)).replace("&", "§");
for (Player player : server.getAllPlayers()) { for (Player player : server.connectedPlayers()) {
if (player.hasPermission("vtools.staffchat" + (channel != null ? "." + channel : ""))) { if (player.hasPermission("vtools.staffchat" + (channel != null ? "." + channel : ""))) {
player.sendMessage(Component.text(message)); player.sendMessage(Component.text(message));
} }

View file

@ -2,7 +2,7 @@ package de.strifel.VTools.commands;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand; import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.connection.Player;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -28,9 +28,9 @@ public class CommandTp implements SimpleCommand {
if (commandSource instanceof Player) { if (commandSource instanceof Player) {
if (strings.length == 1) { if (strings.length == 1) {
Optional<Player> player = server.getPlayer(strings[0]); Player player = server.player(strings[0]);
if (player.isPresent()) { if (player != null) {
player.get().getCurrentServer().ifPresent(serverConnection -> ((Player) commandSource).createConnectionRequest(serverConnection.getServer()).fireAndForget()); Optional.ofNullable(player.connectedServer()).ifPresent(serverConnection -> ((Player) commandSource).createConnectionRequest(serverConnection.target()).fireAndForget());
commandSource.sendMessage(Component.text("Connecting to the server of " + strings[0]).color(COLOR_YELLOW)); commandSource.sendMessage(Component.text("Connecting to the server of " + strings[0]).color(COLOR_YELLOW));
} else { } else {
commandSource.sendMessage(Component.text("Player does not exists.").color(COLOR_RED)); commandSource.sendMessage(Component.text("Player does not exists.").color(COLOR_RED));
@ -47,8 +47,8 @@ public class CommandTp implements SimpleCommand {
public List<String> suggest(Invocation commandInvocation) { public List<String> suggest(Invocation commandInvocation) {
List<String> arg = new ArrayList<>(); List<String> arg = new ArrayList<>();
if (commandInvocation.arguments().length <= 1) { if (commandInvocation.arguments().length <= 1) {
for (Player player : server.getAllPlayers()) { for (Player player : server.connectedPlayers()) {
arg.add(player.getUsername()); arg.add(player.username());
} }
} }
return arg; return arg;

8
velocity-plugin.json Normal file
View file

@ -0,0 +1,8 @@
{
"id": "vtools",
"name": "VTools",
"version": "1.0-SNAPSHOT",
"authors": [ "unnamed" ],
"dependencies": [],
"main": "de.strifel.VTools.VTools"
}