feat: code

This commit is contained in:
2025-12-14 21:59:22 -08:00
parent 4e1faa3edc
commit 5d0ed54cba
13 changed files with 413 additions and 234 deletions

View File

@@ -1,190 +1,200 @@
package online.mineroo.velocity.commands;
import java.util.List;
import java.util.concurrent.CompletionException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
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;
import online.mineroo.common.MessageManager;
import online.mineroo.common.NetworkServiceInterface;
import online.mineroo.velocity.Config;
import online.mineroo.velocity.MinerooCore;
import online.mineroo.common.BindRequest;
import online.mineroo.common.HttpNetworkService;
public class MainCommand implements SimpleCommand {
private final MinerooCore plugin;
public MainCommand(MinerooCore plugin) {
this.plugin = plugin;
}
@Override
public void execute(Invocation invocation) {
String[] args = invocation.arguments();
if (args.length < 1) {
sendHelp(invocation.source());
return;
}
String subCommand = args[0];
if (subCommand.equals("bind")) {
bindServer(invocation.source());
}
if (subCommand.equalsIgnoreCase("reload")) {
reload(invocation.source());
}
}
@Override
public List<String> suggest(Invocation invocation) {
String[] args = invocation.arguments();
if (args.length == 0) {
return List.of("bind", "reload");
}
if (args.length == 1) {
String input = args[0].toLowerCase();
return Stream.of("bind", "reload")
.filter(cmd -> cmd.startsWith(input))
.collect(Collectors.toList());
}
return List.of();
}
public void reload(CommandSource source) {
MessageManager msg = plugin.getMessageManager();
long start = System.currentTimeMillis();
boolean success = plugin.reloadConfig();
if (success) {
long time = System.currentTimeMillis() - start;
source.sendMessage(msg.get("command.reload.success", "time", String.valueOf(time)));
} else {
source.sendMessage(msg.get("command.reload.failed"));
}
return;
}
public void bindServer(CommandSource source) {
Config config = plugin.getConfig();
MessageManager msg = plugin.getMessageManager();
String bind_name = config.getServerName();
String bind_description = config.getDescription();
String bind_token = config.getBind().getBindToken();
String bind_address = config.getBind().getAddress();
Integer bind_port = config.getBind().getPort();
source.sendMessage(msg.get("command.bind.server.start"));
Logger logger = plugin.getLogger();
// Run in a separate worker thread to allow blocking (Thread.sleep)
plugin.getServer().getScheduler().buildTask(plugin, () -> {
// 1. Initialize the Network Service locally (Velocity connects directly)
NetworkServiceInterface networkService = new HttpNetworkService(
"https://oapi.mineroo.online",
"mbind",
bind_token);
BindRequest request = new BindRequest(logger, networkService);
try {
// 2. Check Status
// We use .join() to block the thread until the Future completes.
if (request.checkBindStatus().join()) {
source.sendMessage(msg.get("command.bind.server.already-bound"));
return;
}
// 3. Initial Verification
boolean inital_bind = request.initialMotdVerifyRequest(bind_address, bind_port).join();
if (inital_bind) {
String motdToken = request.getMotdToken();
plugin.getBindListener().setVerificationToken(motdToken);
source.sendMessage(msg.get("command.bind.server.wait"));
// 4. Wait for external refresh (Blocking this worker thread is intended)
try {
Thread.sleep(2 * 60 * 1000 + 5000); // 2m 5s
} catch (InterruptedException e) {
logger.error("Bind thread interrupted", e);
return;
}
// 5. Final Bind & Retry Loop
boolean success = false;
int maxRetries = 3;
for (int i = 1; i <= maxRetries; i++) {
// Check final status synchronously using join()
if (request.finalizeServerBinding(bind_name, bind_description).join()) {
success = true;
break;
}
if (i < maxRetries) {
source.sendMessage(msg.get("command.bind.server.retry",
"current", String.valueOf(i),
"max", String.valueOf(maxRetries)));
try {
Thread.sleep(10000); // Wait 10s before retry
} catch (InterruptedException e) {
break;
}
}
}
if (success) {
source.sendMessage(msg.get("command.bind.server.success"));
} else {
source.sendMessage(msg.get("command.bind.server.failed"));
}
} else {
source.sendMessage(
Component.text("Initial handshake failed. Check your token or network.", NamedTextColor.RED));
}
} catch (CompletionException e) {
// Unpack the wrapper exception from join() to see the real network error
logger.error("Mineroo Bind Network Error: " + e.getCause().getMessage());
} catch (Exception e) {
logger.error("Mineroo Bind Error: " + e.toString());
} finally {
plugin.getBindListener().clearVerificationToken();
}
}).schedule();
}
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));
}
}
// package online.mineroo.velocity.commands;
//
// import java.util.List;
// import java.util.Optional;
// import java.util.concurrent.CompletionException;
// import java.util.stream.Collectors;
// import java.util.stream.Stream;
//
// import org.slf4j.Logger;
//
// 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;
// import online.mineroo.common.MessageManager;
// import online.mineroo.common.NetworkServiceInterface;
// import online.mineroo.velocity.Config;
// import online.mineroo.velocity.MinerooCore;
// import online.mineroo.common.BindRequest;
// import online.mineroo.common.HttpNetworkService;
//
// public class MainCommand implements SimpleCommand {
//
// private final MinerooCore plugin;
//
// public MainCommand(MinerooCore plugin) {
// this.plugin = plugin;
// }
//
// @Override
// public void execute(Invocation invocation) {
// String[] args = invocation.arguments();
// if (args.length < 1) {
// sendHelp(invocation.source());
// return;
// }
//
// String subCommand = args[0];
//
// if (subCommand.equals("bind")) {
// bindServer(invocation.source());
// }
//
// if (subCommand.equalsIgnoreCase("reload")) {
// reload(invocation.source());
// }
//
// }
//
// @Override
// public List<String> suggest(Invocation invocation) {
// String[] args = invocation.arguments();
//
// if (args.length == 0) {
// return List.of("bind", "reload");
// }
//
// if (args.length == 1) {
// String input = args[0].toLowerCase();
// return Stream.of("bind", "reload")
// .filter(cmd -> cmd.startsWith(input))
// .collect(Collectors.toList());
// }
//
// return List.of();
// }
//
// public void reload(CommandSource source) {
// MessageManager msg = plugin.getMessageManager();
//
// long start = System.currentTimeMillis();
// boolean success = plugin.reloadConfig();
//
// if (success) {
// long time = System.currentTimeMillis() - start;
// source.sendMessage(msg.get("command.reload.success", "time",
// String.valueOf(time)));
// } else {
//
// source.sendMessage(msg.get("command.reload.failed"));
// }
// return;
// }
//
// public void bindServer(CommandSource source) {
//
// Config config = plugin.getConfig();
// MessageManager msg = plugin.getMessageManager();
//
// String bind_name = config.getServerName();
// String bind_description = config.getDescription();
//
// String bind_token = config.getBind().getBindToken();
// String bind_address = config.getBind().getAddress();
//
// int bind_port =
// Optional.ofNullable(config.getBind().getPort()).orElse(25565);
//
// source.sendMessage(msg.get("command.bind.server.start"));
//
// Logger logger = plugin.getLogger();
//
// // Run in a separate worker thread to allow blocking (Thread.sleep)
// plugin.getServer().getScheduler().buildTask(plugin, () -> {
//
// // 1. Initialize the Network Service locally (Velocity connects directly)
// NetworkServiceInterface networkService = new HttpNetworkService(
// "https://oapi.mineroo.online",
// "mserver",
// bind_token);
//
// BindRequest request = new BindRequest(logger, networkService);
//
// try {
//
// // 2. Check Status
// // We use .join() to block the thread until the Future completes.
// if (request.checkBindStatus(bind_address, bind_port).join()) {
// source.sendMessage(msg.get("command.bind.server.already-bound"));
// return;
// }
//
// // 3. Initial Verification
// boolean inital_bind = request.initialMotdVerifyRequest(bind_address,
// bind_port).join();
//
// if (inital_bind) {
// String motdToken = request.getMotdToken();
// plugin.getBindListener().setVerificationToken(motdToken);
//
// source.sendMessage(msg.get("command.bind.server.wait"));
//
// // 4. Wait for external refresh (Blocking this worker thread is intended)
// try {
// Thread.sleep(2 * 60 * 1000 + 5000); // 2m 5s
// } catch (InterruptedException e) {
// logger.error("Bind thread interrupted", e);
// return;
// }
//
// // 5. Final Bind & Retry Loop
// boolean success = false;
// int maxRetries = 3;
//
// for (int i = 1; i <= maxRetries; i++) {
// // Check final status synchronously using join()
// if (request.finalizeServerBinding(bind_name, bind_description).join()) {
// success = true;
// break;
// }
//
// if (i < maxRetries) {
// source.sendMessage(msg.get("command.bind.server.retry",
// "current", String.valueOf(i),
// "max", String.valueOf(maxRetries)));
//
// try {
// Thread.sleep(10000); // Wait 10s before retry
// } catch (InterruptedException e) {
// break;
// }
// }
// }
//
// if (success) {
// source.sendMessage(msg.get("command.bind.server.success"));
// } else {
// source.sendMessage(msg.get("command.bind.server.failed"));
// }
//
// } else {
// source.sendMessage(
// Component.text("Initial handshake failed. Check your token or network.",
// NamedTextColor.RED));
// }
//
// } catch (CompletionException e) {
// // Unpack the wrapper exception from join() to see the real network error
// logger.error("Mineroo Bind Network Error: " + e.getCause().getMessage());
// } catch (Exception e) {
// logger.error("Mineroo Bind Error: " + e.toString());
// } finally {
// plugin.getBindListener().clearVerificationToken();
// }
//
// }).schedule();
// }
//
// 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));
// }
// }