Files
src
pom.xml
Code
39 3,243
src/main/java/plumbBow7524285/cherryBungee/command/ConsoleCommandHandler.java
package plumbBow7524285.cherryBungee.command;
import plumbBow7524285.cherryBungee.CherryBungee;
import plumbBow7524285.cherryBungee.player.ProxyPlayer;
import plumbBow7524285.cherryBungee.util.ConsoleColors;
import java.util.Scanner;
public class ConsoleCommandHandler {
private final CherryBungee proxy;
private Thread commandThread;
private volatile boolean running = false;
public ConsoleCommandHandler(CherryBungee proxy) {
this.proxy = proxy;
}
public void start() {
running = true;
commandThread = new Thread(() -> {
Scanner scanner = new Scanner(System.in);
while (running) {
try {
if (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line == null || line.trim().isEmpty()) {
continue;
}
handleCommand(line.trim());
}
} catch (Exception e) {
if (running) {
log(ConsoleColors.error("Error reading command: " + e.getMessage()));
}
}
}
scanner.close();
}, "Console-Command-Thread");
commandThread.setDaemon(false);
commandThread.start();
}
private void handleCommand(String input) {
String[] parts = input.split(" ");
String command = parts[0].toLowerCase();
switch (command) {
case "stop":
case "shutdown":
case "end":
log(ConsoleColors.warn("Shutting down proxy..."));
proxy.shutdown();
break;
case "list":
case "online":
int online = proxy.getPlayerManager().getOnlineCount();
int max = proxy.getConfig().getMaxPlayers();
log(ConsoleColors.info("Online Players: " + ConsoleColors.highlight(online + "/" + max)));
if (online > 0) {
log(ConsoleColors.secondary(" Players:"));
for (ProxyPlayer player : proxy.getPlayerManager().getPlayers()) {
String server = player.getCurrentServer() != null ? player.getCurrentServer() : "connecting";
log(ConsoleColors.secondary(" • ") + ConsoleColors.primary(player.getUsername()) +
ConsoleColors.secondary(" → ") + ConsoleColors.highlight(server));
}
}
break;
case "servers":
int serverCount = proxy.getBackendServerManager().getServers().size();
log(ConsoleColors.info("Backend Servers: " + ConsoleColors.highlight(String.valueOf(serverCount))));
proxy.getBackendServerManager().getServers().forEach((name, server) -> {
String status = server.isOnline() ? ConsoleColors.GREEN + "ONLINE" : ConsoleColors.RED + "OFFLINE";
log(ConsoleColors.secondary(" • ") + ConsoleColors.primary(name) +
ConsoleColors.secondary(" → ") + server.getHost() + ":" + server.getPort() +
" [" + status + ConsoleColors.RESET + "]");
});
break;
case "blacklist":
handleBlacklistCommand(parts);
break;
case "botmode":
boolean botMode = proxy.getAntiBotSystem().isBotMode();
String status = botMode ? ConsoleColors.RED + "ACTIVE" : ConsoleColors.GREEN + "INACTIVE";
log(ConsoleColors.info("Bot mode is currently: " + status + ConsoleColors.RESET));
break;
case "reload":
log(ConsoleColors.info("Reloading configuration..."));
proxy.getConfig().load();
log(ConsoleColors.success("Configuration reloaded successfully!"));
break;
case "info":
case "about":
log("");
log(ConsoleColors.highlight("═══════════════════════════════════════════"));
log(ConsoleColors.primary(" CherryBungee") + ConsoleColors.secondary(" - Secure Minecraft Proxy"));
log(ConsoleColors.highlight("═══════════════════════════════════════════"));
log(ConsoleColors.secondary(" Version: ") + ConsoleColors.highlight("1.0.0"));
log(ConsoleColors.secondary(" Address: ") + ConsoleColors.highlight(proxy.getConfig().getHost() + ":" + proxy.getConfig().getPort()));
log(ConsoleColors.secondary(" Online: ") + ConsoleColors.highlight(proxy.getPlayerManager().getOnlineCount() + "/" + proxy.getConfig().getMaxPlayers()));
log(ConsoleColors.secondary(" Backend Servers: ") + ConsoleColors.highlight(String.valueOf(proxy.getBackendServerManager().getServers().size())));
log(ConsoleColors.secondary(" Security Features:"));
log(ConsoleColors.secondary(" • IP Limiter: ") + (proxy.getConfig().isIpLimitEnabled() ? ConsoleColors.GREEN + "✓" : ConsoleColors.RED + "✗") + ConsoleColors.RESET);
log(ConsoleColors.secondary(" • Rate Limiter: ") + (proxy.getConfig().isRateLimitEnabled() ? ConsoleColors.GREEN + "✓" : ConsoleColors.RED + "✗") + ConsoleColors.RESET);
log(ConsoleColors.secondary(" • Anti-Bot: ") + (proxy.getConfig().isAntiBotEnabled() ? ConsoleColors.GREEN + "✓" : ConsoleColors.RED + "✗") + ConsoleColors.RESET);
log(ConsoleColors.secondary(" • Anti-VPN: ") + (proxy.getConfig().isAntiVPNEnabled() ? ConsoleColors.GREEN + "✓" : ConsoleColors.RED + "✗") + ConsoleColors.RESET);
log(ConsoleColors.secondary(" • IP Blacklist: ") + (proxy.getConfig().isBlacklistEnabled() ? ConsoleColors.GREEN + "✓" : ConsoleColors.RED + "✗") + ConsoleColors.RESET);
log(ConsoleColors.secondary(" • Geo Blocker: ") + (proxy.getConfig().isGeoBlockerEnabled() ? ConsoleColors.GREEN + "✓" : ConsoleColors.RED + "✗") + ConsoleColors.RESET);
log(ConsoleColors.highlight("═══════════════════════════════════════════"));
log("");
break;
case "help":
showHelp();
break;
default:
log(ConsoleColors.error("Unknown command: " + ConsoleColors.highlight(command)));
log(ConsoleColors.secondary("Type ") + ConsoleColors.highlight("help") +
ConsoleColors.secondary(" for a list of commands."));
break;
}
}
private void handleBlacklistCommand(String[] args) {
if (args.length < 2) {
log(ConsoleColors.warn("Usage: blacklist <add|remove|check|list> <ip> [reason] [duration]"));
return;
}
String action = args[1].toLowerCase();
switch (action) {
case "add":
if (args.length < 3) {
log(ConsoleColors.warn("Usage: blacklist add <ip> [reason] [duration_seconds]"));
return;
}
String ip = args[2];
String reason = args.length > 3 ? args[3] : "Blacklisted";
long duration = args.length > 4 ? Long.parseLong(args[4]) * 1000 : 0;
proxy.getIpBlacklist().addIP(ip, reason, duration);
log(ConsoleColors.success("Added " + ConsoleColors.highlight(ip) + " to blacklist"));
if (duration > 0) {
log(ConsoleColors.secondary(" Duration: ") + (duration / 1000) + " seconds");
}
if (reason != null) {
log(ConsoleColors.secondary(" Reason: ") + reason);
}
break;
case "remove":
if (args.length < 3) {
log(ConsoleColors.warn("Usage: blacklist remove <ip>"));
return;
}
String removeIp = args[2];
proxy.getIpBlacklist().removeIP(removeIp);
log(ConsoleColors.success("Removed " + ConsoleColors.highlight(removeIp) + " from blacklist"));
break;
case "check":
if (args.length < 3) {
log(ConsoleColors.warn("Usage: blacklist check <ip>"));
return;
}
String checkIp = args[2];
boolean blacklisted = proxy.getIpBlacklist().isBlacklisted(checkIp);
String status = blacklisted ? ConsoleColors.RED + "BLACKLISTED" : ConsoleColors.GREEN + "NOT BLACKLISTED";
log(ConsoleColors.info("IP " + ConsoleColors.highlight(checkIp) + " is " + status + ConsoleColors.RESET));
break;
case "list":
log(ConsoleColors.info("Blacklisted IPs:"));
proxy.getIpBlacklist().getBlacklistedIPs().forEach(blacklistedIp -> {
log(ConsoleColors.secondary(" • ") + ConsoleColors.highlight(blacklistedIp));
});
break;
default:
log(ConsoleColors.error("Unknown subcommand: " + action));
log(ConsoleColors.secondary("Usage: blacklist <add|remove|check|list> <ip>"));