Skip to content

Commit 214ddce

Browse files
committed
Move retrieveStatus function of MCBOTPanel to MinecraftBOT to make it common for cli/gui
Successfully handle -s/--status arg
1 parent af9995f commit 214ddce

File tree

3 files changed

+82
-72
lines changed

3 files changed

+82
-72
lines changed

src/main/java/re/alwyn974/minecraft/bot/MinecraftBOT.java

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
package re.alwyn974.minecraft.bot;
22

3+
import com.github.steveice10.mc.auth.data.GameProfile;
4+
import com.github.steveice10.mc.auth.service.SessionService;
5+
import com.github.steveice10.mc.protocol.MinecraftConstants;
6+
import com.github.steveice10.mc.protocol.MinecraftProtocol;
7+
import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoHandler;
8+
import com.github.steveice10.mc.protocol.data.status.handler.ServerPingTimeHandler;
9+
import com.github.steveice10.packetlib.BuiltinFlags;
10+
import com.github.steveice10.packetlib.Session;
11+
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
12+
import com.github.steveice10.packetlib.event.session.SessionAdapter;
13+
import com.github.steveice10.packetlib.tcp.TcpClientSession;
314
import org.apache.commons.cli.ParseException;
415
import org.reflections.Reflections;
516
import re.alwyn974.logger.BasicLogger;
617
import re.alwyn974.logger.LoggerFactory;
18+
import re.alwyn974.minecraft.bot.builder.CommandBuilderException;
19+
import re.alwyn974.minecraft.bot.chat.TranslateChat;
720
import re.alwyn974.minecraft.bot.cli.CLIParser;
821
import re.alwyn974.minecraft.bot.cmd.InitSimpleCommand;
9-
import re.alwyn974.minecraft.bot.builder.CommandBuilderException;
1022
import re.alwyn974.minecraft.bot.cmd.utils.CommandHandler;
1123
import re.alwyn974.minecraft.bot.cmd.utils.ICommand;
1224
import re.alwyn974.minecraft.bot.gui.MCBOTFrame;
1325

14-
import java.awt.GraphicsEnvironment;
26+
import java.awt.*;
27+
import java.net.Proxy;
28+
import java.util.ArrayList;
29+
import java.util.List;
1530
import java.util.Set;
1631

1732
/**
@@ -24,12 +39,12 @@
2439
public class MinecraftBOT {
2540

2641
private static final String PROJECT_NAME = "MinecraftBOT";
27-
private static final BasicLogger logger = LoggerFactory.getLogger(getProjectName());
28-
private static final String username = System.getenv("MC_BOT_USERNAME");
29-
private static final String password = System.getenv("MC_BOT_PASSWORD");
30-
private static final String host = System.getenv("MC_BOT_HOST");
31-
private static final String port = System.getenv("MC_BOT_PORT");
32-
private static final String debug = System.getenv("MC_BOT_DEBUG");
42+
private static final BasicLogger LOGGER = LoggerFactory.getLogger(getProjectName());
43+
private static final String USERNAME = System.getenv("MC_BOT_USERNAME");
44+
private static final String PASSWORD = System.getenv("MC_BOT_PASSWORD");
45+
private static final String HOST = System.getenv("MC_BOT_HOST");
46+
private static final String PORT = System.getenv("MC_BOT_PORT");
47+
private static final String DEBUG = System.getenv("MC_BOT_DEBUG");
3348

3449
/**
3550
* The main
@@ -84,7 +99,7 @@ private static void runHeadless(String... args) {
8499
* @return the logger {@link BasicLogger}
85100
*/
86101
public static BasicLogger getLogger() {
87-
return logger;
102+
return LOGGER;
88103
}
89104

90105
/**
@@ -103,7 +118,7 @@ public static String getProjectName() {
103118
* @return the username
104119
*/
105120
public static String getUsername() {
106-
return username != null ? username : "";
121+
return USERNAME != null ? USERNAME : "";
107122
}
108123

109124
/**
@@ -112,7 +127,7 @@ public static String getUsername() {
112127
* @return the password
113128
*/
114129
public static String getPassword() {
115-
return password != null ? password : "";
130+
return PASSWORD != null ? PASSWORD : "";
116131
}
117132

118133
/**
@@ -121,7 +136,7 @@ public static String getPassword() {
121136
* @return the host
122137
*/
123138
public static String getHost() {
124-
return host != null ? host : "127.0.0.1";
139+
return HOST != null ? HOST : "127.0.0.1";
125140
}
126141

127142
/**
@@ -130,7 +145,7 @@ public static String getHost() {
130145
* @return the port
131146
*/
132147
public static String getPort() {
133-
return port != null ? port : "25565";
148+
return PORT != null ? PORT : "25565";
134149
}
135150

136151
/**
@@ -139,7 +154,53 @@ public static String getPort() {
139154
* @return the debug value
140155
*/
141156
public static String getDebug() {
142-
return debug;
157+
return DEBUG;
143158
}
144159

160+
/**
161+
* Retrieve the status of a server
162+
*
163+
* @param host the host
164+
* @param port the port
165+
* @param debug debug value to print some useful things
166+
*/
167+
public static void retrieveStatus(String host, Integer port, boolean debug) {
168+
new Thread(() -> {
169+
SessionService sessionService = new SessionService();
170+
sessionService.setProxy(Proxy.NO_PROXY);
171+
172+
MinecraftProtocol protocol = new MinecraftProtocol();
173+
Session client = new TcpClientSession(host, port, protocol);
174+
client.setFlag(BuiltinFlags.PRINT_DEBUG, debug);
175+
client.setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
176+
client.setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, (ServerInfoHandler) (session, info) -> {
177+
MinecraftBOT.getLogger().info("Version: %s, Protocol Version: %d", info.getVersionInfo().getVersionName(), info.getVersionInfo().getProtocolVersion());
178+
MinecraftBOT.getLogger().info("Player Count: %d/%d", info.getPlayerInfo().getOnlinePlayers(), info.getPlayerInfo().getMaxPlayers());
179+
List<String> players = new ArrayList<>();
180+
for (GameProfile player : info.getPlayerInfo().getPlayers())
181+
players.add(player.getName());
182+
MinecraftBOT.getLogger().info("Players: %s", players.toString());
183+
MinecraftBOT.getLogger().info("Description: %s", TranslateChat.translateComponent(info.getDescription()));
184+
});
185+
client.setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, (ServerPingTimeHandler) (session, pingTime) -> MinecraftBOT.getLogger().info("Server ping took %dms", pingTime));
186+
client.addListener(new SessionAdapter() {
187+
@Override
188+
public void disconnected(DisconnectedEvent event) {
189+
MinecraftBOT.getLogger().info("Disconnected: %s\n%s", event.getReason(), event.getCause() != null ? event.getCause() : "");
190+
}
191+
});
192+
193+
if (debug)
194+
MinecraftBOT.getLogger().debug("Connecting to Minecraft server: %s:%s", host, port);
195+
client.connect();
196+
197+
try {
198+
Thread.sleep(2000L);
199+
client.disconnect("Finished");
200+
} catch (InterruptedException ex) {
201+
MinecraftBOT.getLogger().error("Thread interrupt", ex);
202+
client.disconnect("Finished");
203+
}
204+
}).start();
205+
}
145206
}

src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public static void parse(String... args) throws ParseException {
2727
ParseResult result = parseResult();
2828
if (result.shouldPrintHelp())
2929
printHelp();
30+
if (result.shouldPrintStatus()) {
31+
MinecraftBOT.retrieveStatus(result.getHost(), result.getPort(), result.isDebug());
32+
System.exit(0);
33+
}
34+
3035
}
3136

3237
/**

src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTPanel.java

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
package re.alwyn974.minecraft.bot.gui;
22

3-
import com.github.steveice10.mc.auth.data.GameProfile;
43
import com.github.steveice10.mc.auth.exception.request.RequestException;
5-
import com.github.steveice10.mc.auth.service.SessionService;
6-
import com.github.steveice10.mc.protocol.MinecraftConstants;
7-
import com.github.steveice10.mc.protocol.MinecraftProtocol;
8-
import com.github.steveice10.mc.protocol.data.status.handler.ServerInfoHandler;
9-
import com.github.steveice10.mc.protocol.data.status.handler.ServerPingTimeHandler;
104
import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket;
11-
import com.github.steveice10.packetlib.BuiltinFlags;
12-
import com.github.steveice10.packetlib.Session;
13-
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
14-
import com.github.steveice10.packetlib.event.session.SessionAdapter;
15-
import com.github.steveice10.packetlib.tcp.TcpClientSession;
165
import re.alwyn974.logger.LoggerFactory;
176
import re.alwyn974.minecraft.bot.MinecraftBOT;
18-
import re.alwyn974.minecraft.bot.chat.TranslateChat;
197
import re.alwyn974.minecraft.bot.cmd.utils.CommandHandler;
208
import re.alwyn974.minecraft.bot.entity.EntityBOT;
219
import re.alwyn974.minecraft.bot.logging.JTextAreaLogHandler;
2210

2311
import javax.swing.*;
24-
import java.awt.BorderLayout;
25-
import java.awt.Cursor;
12+
import java.awt.*;
2613
import java.awt.event.ActionEvent;
2714
import java.awt.event.ActionListener;
2815
import java.awt.event.KeyAdapter;
2916
import java.awt.event.KeyEvent;
30-
import java.net.Proxy;
31-
import java.util.ArrayList;
32-
import java.util.List;
3317

3418
/**
3519
* The Panel of the Gui
@@ -156,7 +140,7 @@ public void actionPerformed(ActionEvent e) {
156140
botThread.interrupt();
157141
setFieldsEnabled(true);
158142
} else if (e.getSource() == statusButton)
159-
retrieveStatus();
143+
MinecraftBOT.retrieveStatus(hostField.getText(), Integer.parseInt(portField.getText()), debugBox.isSelected());
160144
else if (e.getSource() == clearButton)
161145
logArea.setText("");
162146
}
@@ -171,44 +155,4 @@ private void setFieldsEnabled(boolean enabled) {
171155
disconnectButton.setEnabled(!enabled);
172156
}
173157

174-
private void retrieveStatus() {
175-
new Thread(() -> {
176-
SessionService sessionService = new SessionService();
177-
sessionService.setProxy(Proxy.NO_PROXY);
178-
179-
MinecraftProtocol protocol = new MinecraftProtocol();
180-
Session client = new TcpClientSession(this.hostField.getText(), Integer.parseInt(this.portField.getText()), protocol);
181-
client.setFlag(BuiltinFlags.PRINT_DEBUG, this.debugBox.isSelected());
182-
client.setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
183-
client.setFlag(MinecraftConstants.SERVER_INFO_HANDLER_KEY, (ServerInfoHandler) (session, info) -> {
184-
MinecraftBOT.getLogger().info("Version: %s, Protocol Version: %d", info.getVersionInfo().getVersionName(), info.getVersionInfo().getProtocolVersion());
185-
MinecraftBOT.getLogger().info("Player Count: %d/%d", info.getPlayerInfo().getOnlinePlayers(), info.getPlayerInfo().getMaxPlayers());
186-
List<String> players = new ArrayList<>();
187-
for (GameProfile player : info.getPlayerInfo().getPlayers())
188-
players.add(player.getName());
189-
MinecraftBOT.getLogger().info("Players: %s", players.toString());
190-
MinecraftBOT.getLogger().info("Description: %s", TranslateChat.translateComponent(info.getDescription()));
191-
});
192-
client.setFlag(MinecraftConstants.SERVER_PING_TIME_HANDLER_KEY, (ServerPingTimeHandler) (session, pingTime) -> MinecraftBOT.getLogger().info("Server ping took %dms", pingTime));
193-
client.addListener(new SessionAdapter() {
194-
@Override
195-
public void disconnected(DisconnectedEvent event) {
196-
MinecraftBOT.getLogger().info("Disconnected: %s\n%s", event.getReason(), event.getCause() != null ? event.getCause() : "");
197-
}
198-
});
199-
200-
if (this.debugBox.isSelected())
201-
MinecraftBOT.getLogger().debug("Connecting to Minecraft server: %s:%s", hostField.getText(), portField.getText());
202-
client.connect();
203-
204-
try {
205-
Thread.sleep(2000L);
206-
client.disconnect("Finished");
207-
} catch (InterruptedException ex) {
208-
MinecraftBOT.getLogger().error("Thread interrupt", ex);
209-
client.disconnect("Finished");
210-
}
211-
}).start();
212-
}
213-
214158
}

0 commit comments

Comments
 (0)