fork download
  1. package com.natvieshub.natvieshub;
  2.  
  3. import com.velocitypowered.api.proxy.ProxyServer;
  4. import com.velocitypowered.api.proxy.Player;
  5. import com.velocitypowered.api.proxy.server.RegisteredServer;
  6. import com.velocitypowered.api.command.CommandSource;
  7. import com.velocitypowered.api.command.SimpleCommand;
  8. import net.kyori.adventure.text.Component;
  9.  
  10.  
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.nio.file.Files;
  14. import java.nio.file.Paths;
  15. import java.util.List;
  16. import java.util.Optional;
  17. import java.util.concurrent.CompletableFuture;
  18.  
  19. public class NatviesHub implements SimpleCommand {
  20.  
  21. private final ProxyServer server;
  22. private String hubServer;
  23.  
  24. public NatviesHub(ProxyServer server) {
  25. this.server = server;
  26. this.hubServer = "hub"; // Default hub server name
  27. }
  28.  
  29. @Override
  30. public void execute(Invocation invocation) {
  31. CommandSource source = invocation.source();
  32. String[] args = invocation.arguments();
  33.  
  34. if (args.length == 0) {
  35. handleHubCommand(source);
  36. } else if ("reload".equalsIgnoreCase(args[0])) {
  37. handleReloadCommand(source);
  38. } else {
  39. source.sendMessage(Component.text("Invalid command!"));
  40. }
  41. }
  42.  
  43. private void handleHubCommand(CommandSource source) {
  44. if (source instanceof Player) {
  45. Player player = (Player) source;
  46. Optional<RegisteredServer> hubServerOpt = server.getServer(hubServer);
  47.  
  48. if (hubServerOpt.isPresent()) {
  49. RegisteredServer hub = hubServerOpt.get();
  50. CompletableFuture<Void> connectionFuture = player.createConnectionRequest(hub).connect();
  51.  
  52. connectionFuture.thenAccept(result -> player.sendMessage(Component.text("Teleporting to the hub server!")))
  53. .exceptionally(ex -> {
  54. player.sendMessage(Component.text("Failed to connect to the hub server!"));
  55. return null;
  56. });
  57. } else {
  58. player.sendMessage(Component.text("Hub server not found!"));
  59. }
  60. } else {
  61. source.sendMessage(Component.text("This command can only be used by players!"));
  62. }
  63. }
  64.  
  65. private void handleReloadCommand(CommandSource source) {
  66. if (source.hasPermission("natvieshub.reload")) {
  67. reloadConfig();
  68. source.sendMessage(Component.text("Plugin configuration reloaded!"));
  69. } else {
  70. source.sendMessage(Component.text("You do not have permission to reload the plugin!"));
  71. }
  72. }
  73.  
  74. private void reloadConfig() {
  75. File configFile = new File("plugins/NatviesHub/config.txt");
  76.  
  77. if (configFile.exists()) {
  78. try {
  79. List<String> lines = Files.readAllLines(Paths.get(configFile.toURI()));
  80. if (!lines.isEmpty()) {
  81. hubServer = lines.get(0).trim();
  82. server.getConsoleCommandSource().sendMessage(Component.text("New hub server set to: " + hubServer));
  83. }
  84. } catch (IOException e) {
  85. server.getConsoleCommandSource().sendMessage(Component.text("Error loading configuration!"));
  86. e.printStackTrace();
  87. }
  88. } else {
  89. server.getConsoleCommandSource().sendMessage(Component.text("Configuration file not found!"));
  90. }
  91. }
  92. }
  93.  
Success #stdin #stdout 0.03s 25936KB
stdin
Standard input is empty
stdout
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.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
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!"));
        }
    }
}