package com.natvieshub.natvieshub;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import net.kyori.adventure.text.Component;
import java.io.IOException;
import java
.nio
.file.Files
;import java
.nio
.file.Paths
;import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class NatviesHub implements SimpleCommand {
private final ProxyServer server;
private String hubServer;
public NatviesHub(ProxyServer server) {
this.server = server;
this.hubServer = "hub"; // Default hub server name
}
@Override
public void execute(Invocation invocation) {
CommandSource source = invocation.source();
String[] args = invocation.arguments();
if (args.length == 0) {
handleHubCommand(source);
} else if ("reload".equalsIgnoreCase(args[0])) {
handleReloadCommand(source);
} else {
source.sendMessage(Component.text("Invalid command!"));
}
}
private void handleHubCommand(CommandSource source) {
if (source instanceof Player) {
Player player = (Player) source;
Optional<RegisteredServer> hubServerOpt = server.getServer(hubServer);
if (hubServerOpt.isPresent()) {
RegisteredServer hub = hubServerOpt.get();
CompletableFuture<Void> connectionFuture = player.createConnectionRequest(hub).connect();
connectionFuture.thenAccept(result -> player.sendMessage(Component.text("Teleporting to the hub server!")))
.exceptionally(ex -> {
player.sendMessage(Component.text("Failed to connect to the hub server!"));
return null;
});
} else {
player.sendMessage(Component.text("Hub server not found!"));
}
} else {
source.sendMessage(Component.text("This command can only be used by players!"));
}
}
private void handleReloadCommand(CommandSource source) {
if (source.hasPermission("natvieshub.reload")) {
reloadConfig();
source.sendMessage(Component.text("Plugin configuration reloaded!"));
} else {
source.sendMessage(Component.text("You do not have permission to reload the plugin!"));
}
}
private void reloadConfig() {
File configFile
= new File("plugins/NatviesHub/config.txt");
if (configFile.exists()) {
try {
List<String> lines = Files.readAllLines(Paths.get(configFile.toURI()));
if (!lines.isEmpty()) {
hubServer
= lines
.get
(0).trim(); server.getConsoleCommandSource().sendMessage(Component.text("New hub server set to: " + hubServer));
}
} catch (IOException e) {
server.getConsoleCommandSource().sendMessage(Component.text("Error loading configuration!"));
e.printStackTrace();
}
} else {
server.getConsoleCommandSource().sendMessage(Component.text("Configuration file not found!"));
}
}
}