Skip to content

Commit

Permalink
Move retrieveStatus function of MCBOTPanel to MinecraftBOT to make it…
Browse files Browse the repository at this point in the history
… common for cli/gui

Successfully handle -s/--status arg
  • Loading branch information
alwyn974 committed Jul 23, 2021
1 parent af9995f commit 214ddce
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 72 deletions.
89 changes: 75 additions & 14 deletions src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
package re.alwyn974.minecraft.bot;

import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.auth.service.SessionService;
import com.github.steveice10.mc.protocol.MinecraftConstants;
import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoHandler;
import com.github.steveice10.mc.protocol.data.status.handler.ServerPingTimeHandler;
import com.github.steveice10.packetlib.BuiltinFlags;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.tcp.TcpClientSession;
import org.apache.commons.cli.ParseException;
import org.reflections.Reflections;
import re.alwyn974.logger.BasicLogger;
import re.alwyn974.logger.LoggerFactory;
import re.alwyn974.minecraft.bot.builder.CommandBuilderException;
import re.alwyn974.minecraft.bot.chat.TranslateChat;
import re.alwyn974.minecraft.bot.cli.CLIParser;
import re.alwyn974.minecraft.bot.cmd.InitSimpleCommand;
import re.alwyn974.minecraft.bot.builder.CommandBuilderException;
import re.alwyn974.minecraft.bot.cmd.utils.CommandHandler;
import re.alwyn974.minecraft.bot.cmd.utils.ICommand;
import re.alwyn974.minecraft.bot.gui.MCBOTFrame;

import java.awt.GraphicsEnvironment;
import java.awt.*;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
Expand All @@ -24,12 +39,12 @@
public class MinecraftBOT {

private static final String PROJECT_NAME = "MinecraftBOT";
private static final BasicLogger logger = LoggerFactory.getLogger(getProjectName());
private static final String username = System.getenv("MC_BOT_USERNAME");
private static final String password = System.getenv("MC_BOT_PASSWORD");
private static final String host = System.getenv("MC_BOT_HOST");
private static final String port = System.getenv("MC_BOT_PORT");
private static final String debug = System.getenv("MC_BOT_DEBUG");
private static final BasicLogger LOGGER = LoggerFactory.getLogger(getProjectName());
private static final String USERNAME = System.getenv("MC_BOT_USERNAME");
private static final String PASSWORD = System.getenv("MC_BOT_PASSWORD");
private static final String HOST = System.getenv("MC_BOT_HOST");
private static final String PORT = System.getenv("MC_BOT_PORT");
private static final String DEBUG = System.getenv("MC_BOT_DEBUG");

/**
* The main
Expand Down Expand Up @@ -84,7 +99,7 @@ private static void runHeadless(String... args) {
* @return the logger {@link BasicLogger}
*/
public static BasicLogger getLogger() {
return logger;
return LOGGER;
}

/**
Expand All @@ -103,7 +118,7 @@ public static String getProjectName() {
* @return the username
*/
public static String getUsername() {
return username != null ? username : "";
return USERNAME != null ? USERNAME : "";
}

/**
Expand All @@ -112,7 +127,7 @@ public static String getUsername() {
* @return the password
*/
public static String getPassword() {
return password != null ? password : "";
return PASSWORD != null ? PASSWORD : "";
}

/**
Expand All @@ -121,7 +136,7 @@ public static String getPassword() {
* @return the host
*/
public static String getHost() {
return host != null ? host : "127.0.0.1";
return HOST != null ? HOST : "127.0.0.1";
}

/**
Expand All @@ -130,7 +145,7 @@ public static String getHost() {
* @return the port
*/
public static String getPort() {
return port != null ? port : "25565";
return PORT != null ? PORT : "25565";
}

/**
Expand All @@ -139,7 +154,53 @@ public static String getPort() {
* @return the debug value
*/
public static String getDebug() {
return debug;
return DEBUG;
}

/**
* Retrieve the status of a server
*
* @param host the host
* @param port the port
* @param debug debug value to print some useful things
*/
public static void retrieveStatus(String host, Integer port, boolean debug) {
new Thread(() -> {
SessionService sessionService = new SessionService();
sessionService.setProxy(Proxy.NO_PROXY);

MinecraftProtocol protocol = new MinecraftProtocol();
Session client = new TcpClientSession(host, port, protocol);
client.setFlag(BuiltinFlags.PRINT_DEBUG, debug);
client.setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
client.setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, (ServerInfoHandler) (session, info) -> {
MinecraftBOT.getLogger().info("Version: %s, Protocol Version: %d", info.getVersionInfo().getVersionName(), info.getVersionInfo().getProtocolVersion());
MinecraftBOT.getLogger().info("Player Count: %d/%d", info.getPlayerInfo().getOnlinePlayers(), info.getPlayerInfo().getMaxPlayers());
List<String> players = new ArrayList<>();
for (GameProfile player : info.getPlayerInfo().getPlayers())
players.add(player.getName());
MinecraftBOT.getLogger().info("Players: %s", players.toString());
MinecraftBOT.getLogger().info("Description: %s", TranslateChat.translateComponent(info.getDescription()));
});
client.setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, (ServerPingTimeHandler) (session, pingTime) -> MinecraftBOT.getLogger().info("Server ping took %dms", pingTime));
client.addListener(new SessionAdapter() {
@Override
public void disconnected(DisconnectedEvent event) {
MinecraftBOT.getLogger().info("Disconnected: %s\n%s", event.getReason(), event.getCause() != null ? event.getCause() : "");
}
});

if (debug)
MinecraftBOT.getLogger().debug("Connecting to Minecraft server: %s:%s", host, port);
client.connect();

try {
Thread.sleep(2000L);
client.disconnect("Finished");
} catch (InterruptedException ex) {
MinecraftBOT.getLogger().error("Thread interrupt", ex);
client.disconnect("Finished");
}
}).start();
}
}
5 changes: 5 additions & 0 deletions src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public static void parse(String... args) throws ParseException {
ParseResult result = parseResult();
if (result.shouldPrintHelp())
printHelp();
if (result.shouldPrintStatus()) {
MinecraftBOT.retrieveStatus(result.getHost(), result.getPort(), result.isDebug());
System.exit(0);
}

}

/**
Expand Down
60 changes: 2 additions & 58 deletions src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTPanel.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
package re.alwyn974.minecraft.bot.gui;

import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.auth.exception.request.RequestException;
import com.github.steveice10.mc.auth.service.SessionService;
import com.github.steveice10.mc.protocol.MinecraftConstants;
import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoHandler;
import com.github.steveice10.mc.protocol.data.status.handler.ServerPingTimeHandler;
import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket;
import com.github.steveice10.packetlib.BuiltinFlags;
import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.tcp.TcpClientSession;
import re.alwyn974.logger.LoggerFactory;
import re.alwyn974.minecraft.bot.MinecraftBOT;
import re.alwyn974.minecraft.bot.chat.TranslateChat;
import re.alwyn974.minecraft.bot.cmd.utils.CommandHandler;
import re.alwyn974.minecraft.bot.entity.EntityBOT;
import re.alwyn974.minecraft.bot.logging.JTextAreaLogHandler;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

/**
* The Panel of the Gui
Expand Down Expand Up @@ -156,7 +140,7 @@ public void actionPerformed(ActionEvent e) {
botThread.interrupt();
setFieldsEnabled(true);
} else if (e.getSource() == statusButton)
retrieveStatus();
MinecraftBOT.retrieveStatus(hostField.getText(), Integer.parseInt(portField.getText()), debugBox.isSelected());
else if (e.getSource() == clearButton)
logArea.setText("");
}
Expand All @@ -171,44 +155,4 @@ private void setFieldsEnabled(boolean enabled) {
disconnectButton.setEnabled(!enabled);
}

private void retrieveStatus() {
new Thread(() -> {
SessionService sessionService = new SessionService();
sessionService.setProxy(Proxy.NO_PROXY);

MinecraftProtocol protocol = new MinecraftProtocol();
Session client = new TcpClientSession(this.hostField.getText(), Integer.parseInt(this.portField.getText()), protocol);
client.setFlag(BuiltinFlags.PRINT_DEBUG, this.debugBox.isSelected());
client.setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
client.setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, (ServerInfoHandler) (session, info) -> {
MinecraftBOT.getLogger().info("Version: %s, Protocol Version: %d", info.getVersionInfo().getVersionName(), info.getVersionInfo().getProtocolVersion());
MinecraftBOT.getLogger().info("Player Count: %d/%d", info.getPlayerInfo().getOnlinePlayers(), info.getPlayerInfo().getMaxPlayers());
List<String> players = new ArrayList<>();
for (GameProfile player : info.getPlayerInfo().getPlayers())
players.add(player.getName());
MinecraftBOT.getLogger().info("Players: %s", players.toString());
MinecraftBOT.getLogger().info("Description: %s", TranslateChat.translateComponent(info.getDescription()));
});
client.setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, (ServerPingTimeHandler) (session, pingTime) -> MinecraftBOT.getLogger().info("Server ping took %dms", pingTime));
client.addListener(new SessionAdapter() {
@Override
public void disconnected(DisconnectedEvent event) {
MinecraftBOT.getLogger().info("Disconnected: %s\n%s", event.getReason(), event.getCause() != null ? event.getCause() : "");
}
});

if (this.debugBox.isSelected())
MinecraftBOT.getLogger().debug("Connecting to Minecraft server: %s:%s", hostField.getText(), portField.getText());
client.connect();

try {
Thread.sleep(2000L);
client.disconnect("Finished");
} catch (InterruptedException ex) {
MinecraftBOT.getLogger().error("Thread interrupt", ex);
client.disconnect("Finished");
}
}).start();
}

}

0 comments on commit 214ddce

Please sign in to comment.