1
1
package re .alwyn974 .minecraft .bot ;
2
2
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 ;
3
14
import org .apache .commons .cli .ParseException ;
4
15
import org .reflections .Reflections ;
5
16
import re .alwyn974 .logger .BasicLogger ;
6
17
import re .alwyn974 .logger .LoggerFactory ;
18
+ import re .alwyn974 .minecraft .bot .builder .CommandBuilderException ;
19
+ import re .alwyn974 .minecraft .bot .chat .TranslateChat ;
7
20
import re .alwyn974 .minecraft .bot .cli .CLIParser ;
8
21
import re .alwyn974 .minecraft .bot .cmd .InitSimpleCommand ;
9
- import re .alwyn974 .minecraft .bot .builder .CommandBuilderException ;
10
22
import re .alwyn974 .minecraft .bot .cmd .utils .CommandHandler ;
11
23
import re .alwyn974 .minecraft .bot .cmd .utils .ICommand ;
12
24
import re .alwyn974 .minecraft .bot .gui .MCBOTFrame ;
13
25
14
- import java .awt .GraphicsEnvironment ;
26
+ import java .awt .*;
27
+ import java .net .Proxy ;
28
+ import java .util .ArrayList ;
29
+ import java .util .List ;
15
30
import java .util .Set ;
16
31
17
32
/**
24
39
public class MinecraftBOT {
25
40
26
41
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" );
33
48
34
49
/**
35
50
* The main
@@ -84,7 +99,7 @@ private static void runHeadless(String... args) {
84
99
* @return the logger {@link BasicLogger}
85
100
*/
86
101
public static BasicLogger getLogger () {
87
- return logger ;
102
+ return LOGGER ;
88
103
}
89
104
90
105
/**
@@ -103,7 +118,7 @@ public static String getProjectName() {
103
118
* @return the username
104
119
*/
105
120
public static String getUsername () {
106
- return username != null ? username : "" ;
121
+ return USERNAME != null ? USERNAME : "" ;
107
122
}
108
123
109
124
/**
@@ -112,7 +127,7 @@ public static String getUsername() {
112
127
* @return the password
113
128
*/
114
129
public static String getPassword () {
115
- return password != null ? password : "" ;
130
+ return PASSWORD != null ? PASSWORD : "" ;
116
131
}
117
132
118
133
/**
@@ -121,7 +136,7 @@ public static String getPassword() {
121
136
* @return the host
122
137
*/
123
138
public static String getHost () {
124
- return host != null ? host : "127.0.0.1" ;
139
+ return HOST != null ? HOST : "127.0.0.1" ;
125
140
}
126
141
127
142
/**
@@ -130,7 +145,7 @@ public static String getHost() {
130
145
* @return the port
131
146
*/
132
147
public static String getPort () {
133
- return port != null ? port : "25565" ;
148
+ return PORT != null ? PORT : "25565" ;
134
149
}
135
150
136
151
/**
@@ -139,7 +154,53 @@ public static String getPort() {
139
154
* @return the debug value
140
155
*/
141
156
public static String getDebug () {
142
- return debug ;
157
+ return DEBUG ;
143
158
}
144
159
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
+ }
145
206
}
0 commit comments