-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_main.cpp
104 lines (92 loc) · 3.68 KB
/
example_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*********************************************
* compiles on gcc 4.7.2 with:
* g++ mcquery.cpp example_main.cpp -o mcquery.run -lboost_system -pthread -lboost_thread -std=c++11
*
* run as:
* ./mcquery.run [host] [port] [timeoutsecs]
**********************************************/
#include "mcquery.hpp"
using namespace std;
int main(int argc, char *argv[]) {
const char* hostname = "localhost";
const char* port = "25565";
int timeout = 5;
switch(argc){
case 4:
timeout = atoi(argv[3]);
case 3:
port = argv[2];
case 2:
hostname = argv[1];
}
/**************** Simple TCP Query *****************************************
*
* This works with every Minecraft server, even if queries are disabled
* Does not work on Minecraft 1.3 or earlier
*
* constructor signature:
*
* mcQuerySimple(const char* host = "localhost",
* const char* port = "25565",
* const int timeoutsecs = 5);
*
********************************************************************/
mcQuerySimple qs{hostname, port, timeout};
auto sData = qs.get();
if(sData.success) {
cout<< "motd: " << sData.motd << endl;
cout<< "numplayers: " << sData.numplayers << endl;
cout<< "maxplayers: " << sData.maxplayers << endl;
cout<< "version: " << sData.version << endl;
}
else cout<< "error: " << sData.error << endl;
cout<<endl;
/**************** UDP Query *****************************************
*
* The Minecraft server needs to have "enable-query=true" in server.properties
*
* The full and basic UDP queries are done through the same object.
* constructor signature:
*
* mcQuery(const char* host = "localhost",
* const char* port = "25565",
* const int timeoutsecs = 5);
*
********************************************************************/
mcQuery q{hostname, port, timeout};
cout<< "Basic query: " << endl;
auto data = q.getBasic();
if(data.success) {
cout<< "motd: " << data.motd << endl;
cout<< "gametype: " << data.gametype << endl;
cout<< "map: " << data.map << endl;
cout<< "numplayers: " << data.numplayers << endl;
cout<< "maxplayers: " << data.maxplayers << endl;
cout<< "hostport: " << data.hostport << endl;
cout<< "hostip: " << data.hostip << endl;
}
else cout<< "error: " << data.error << endl;
cout<<endl;
cout<< "Full query: " << endl;
auto fData = q.getFull();
if(fData.success) {
cout<< "motd: " << fData.motd << endl; // also accessable as data.hostname
cout<< "gametype: " << fData.gametype << endl;
cout<< "game_id: " << fData.game_id << endl;
cout<< "version: " << fData.version << endl;
cout<< "plugins:" << endl; // only used by bukkit. First member is the craftbukkit version
for( auto plugin : fData.plugins )
cout<< " " << plugin << endl;
cout<< "map: " << fData.map << endl;
cout<< "numplayers: " << fData.numplayers << endl;
cout<< "maxplayers: " << fData.maxplayers << endl;
cout<< "hostport: " << fData.hostport << endl;
cout<< "hostip: " << fData.hostip << endl;
cout<< "playernames:" << endl;
for( auto name : fData.playernames )
cout<< " " << name << endl;
}
else cout<< "error: " << fData.error << endl;
cout<<endl;
return 0;
}