feat: submit
This commit is contained in:
52
lib/src/main/java/online/mineroo/velocity/Config.java
Normal file
52
lib/src/main/java/online/mineroo/velocity/Config.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package online.mineroo.velocity;
|
||||
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
import org.spongepowered.configurate.objectmapping.meta.Comment;
|
||||
import org.spongepowered.configurate.objectmapping.meta.Setting;
|
||||
|
||||
@ConfigSerializable
|
||||
public class Config {
|
||||
|
||||
@Comment("Server Bind Name")
|
||||
private String serverName = "";
|
||||
|
||||
@Comment("Server Bind Description")
|
||||
private String description = "A minecraft server";
|
||||
|
||||
@Setting("bind")
|
||||
private ServerSection server = new ServerSection();
|
||||
|
||||
@ConfigSerializable
|
||||
public static class ServerSection {
|
||||
|
||||
@Setting("hostname")
|
||||
@Comment("Server Hostname")
|
||||
private String hostname = "";
|
||||
|
||||
@Setting("token")
|
||||
@Comment("Server Bind Token")
|
||||
private String bindToken = "get token from `mineroo.online/resources/servers` page!";
|
||||
|
||||
public String getHostname() {
|
||||
return hostname;
|
||||
}
|
||||
|
||||
public String getBindToken() {
|
||||
return bindToken;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getServerName() {
|
||||
return serverName;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public ServerSection getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
}
|
||||
89
lib/src/main/java/online/mineroo/velocity/MinerooPlugin.java
Normal file
89
lib/src/main/java/online/mineroo/velocity/MinerooPlugin.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package online.mineroo.velocity;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.command.CommandManager;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||
import com.velocitypowered.api.event.proxy.ProxyPingEvent;
|
||||
import com.velocitypowered.api.plugin.Plugin;
|
||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
|
||||
import online.mineroo.velocity.commands.MainCommand;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||
import org.spongepowered.configurate.loader.ConfigurationLoader;
|
||||
import org.spongepowered.configurate.yaml.NodeStyle;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
|
||||
@Plugin(id = "mineroo-velocity", name = "Mineroo Velocity", version = "0.1.0-SNAPSHOT", url = "https://mineroo.online", description = "Mineroo main plugin", authors = {
|
||||
"YuKun Liu" })
|
||||
public class MinerooPlugin {
|
||||
|
||||
private final ProxyServer server;
|
||||
private final Logger logger;
|
||||
|
||||
private final Path dataDirectory;
|
||||
private Config config;
|
||||
|
||||
@Inject
|
||||
public MinerooPlugin(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
|
||||
this.server = server;
|
||||
this.logger = logger;
|
||||
this.dataDirectory = dataDirectory;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onProxyInitialization(ProxyInitializeEvent event) {
|
||||
loadConfig();
|
||||
|
||||
CommandManager commandManager = server.getCommandManager();
|
||||
commandManager.register(commandManager.metaBuilder("mineroo").build(), new MainCommand());
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onProxyPing(ProxyPingEvent event) {
|
||||
String token = "1";
|
||||
}
|
||||
|
||||
// load & create config
|
||||
private void loadConfig() {
|
||||
try {
|
||||
// create dir if not exists
|
||||
if (Files.notExists(dataDirectory)) {
|
||||
Files.createDirectories(dataDirectory);
|
||||
}
|
||||
|
||||
Path configPath = dataDirectory.resolve("config.yaml");
|
||||
|
||||
// Inital Loader
|
||||
ConfigurationLoader<CommentedConfigurationNode> loader = YamlConfigurationLoader.builder()
|
||||
.path(configPath)
|
||||
.nodeStyle(NodeStyle.BLOCK)
|
||||
.build();
|
||||
|
||||
// Create & load config file
|
||||
if (Files.notExists(configPath)) {
|
||||
logger.warn("Configuration file not found, creating default config.toml...");
|
||||
logger.warn("Please change `config.toml` before bind server.");
|
||||
|
||||
this.config = new Config();
|
||||
CommentedConfigurationNode node = loader.createNode();
|
||||
node.set(Config.class, this.config);
|
||||
loader.save(node);
|
||||
} else {
|
||||
logger.info("Loading configuration...");
|
||||
CommentedConfigurationNode node = loader.load();
|
||||
this.config = node.get(Config.class);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to load or create config.yaml!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package online.mineroo.velocity.commands;
|
||||
|
||||
import com.velocitypowered.api.command.CommandSource;
|
||||
import com.velocitypowered.api.command.SimpleCommand;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
public class MainCommand implements SimpleCommand {
|
||||
@Override
|
||||
public void execute(Invocation invocation) {
|
||||
String[] args = invocation.arguments();
|
||||
if (args.length == 0) {
|
||||
sendHelp(invocation.source());
|
||||
}
|
||||
}
|
||||
|
||||
public void sendHelp(CommandSource source) {
|
||||
source.sendMessage(Component.text("=== # @Mineroo # ===", NamedTextColor.AQUA));
|
||||
source.sendMessage(Component.text(" /mineroo bind - Start to bind server", NamedTextColor.YELLOW));
|
||||
source.sendMessage(Component.text(" /mineroo reload - Reload config", NamedTextColor.YELLOW));
|
||||
source.sendMessage(Component.text("=== mineroo.online ===", NamedTextColor.AQUA));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package online.mineroo.velocity.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class BindRequest {
|
||||
|
||||
private static final String endpoint = "https://oapi.mineroo.online";
|
||||
|
||||
private final Logger logger;
|
||||
private final HttpClient httpClient;
|
||||
|
||||
public BindRequest(Logger logger, HttpClient httpClient) {
|
||||
this.logger = logger;
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public void InitalMotdVerifyRequest(String token, String hostname, int port)
|
||||
throws IOException, InterruptedException {
|
||||
|
||||
// generate uri
|
||||
String uri = endpoint + "/server/motd-verify";
|
||||
|
||||
// build jsonBody
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("server_adress", hostname);
|
||||
json.addProperty("server_port", port);
|
||||
|
||||
String basicAuth = "mbind:" + token;
|
||||
String encodedAuth = Base64.getEncoder().encodeToString(basicAuth.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(uri))
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Authorization", "Basic " + encodedAuth)
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json.toString())).build();
|
||||
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if (response.statusCode() == 200) {
|
||||
logger.debug("test");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user