Skip to content

Commit af3d518

Browse files
authored
Merge pull request #3 from gnmyt/features/master-integration
🔗 Added the master integration
2 parents 4830fe5 + b960926 commit af3d518

File tree

4 files changed

+130
-6
lines changed

4 files changed

+130
-6
lines changed

pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>de.gnmyt</groupId>
88
<artifactId>MCDash</artifactId>
9-
<version>1.0.2</version>
9+
<version>1.0.3</version>
1010

1111
<properties>
1212
<maven.compiler.source>8</maven.compiler.source>
@@ -49,7 +49,6 @@
4949
<version>1.4</version>
5050
</dependency>
5151

52-
5352
<!-- Reflections API -->
5453
<dependency>
5554
<groupId>org.reflections</groupId>
@@ -88,6 +87,13 @@
8887
<scope>provided</scope>
8988
</dependency>
9089

90+
<!-- HTTP Client API -->
91+
<dependency>
92+
<groupId>com.squareup.okhttp3</groupId>
93+
<artifactId>okhttp</artifactId>
94+
<version>4.9.0</version>
95+
</dependency>
96+
9197
</dependencies>
9298

9399
</project>

src/main/java/de/gnmyt/mcdash/MinecraftDashboard.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.sun.net.httpserver.HttpServer;
44
import de.gnmyt.mcdash.api.config.ConfigurationManager;
55
import de.gnmyt.mcdash.api.handler.DefaultHandler;
6+
import de.gnmyt.mcdash.connector.MasterConnector;
67
import org.bukkit.Bukkit;
78
import org.bukkit.plugin.java.JavaPlugin;
89
import org.reflections.Reflections;
@@ -27,11 +28,12 @@ public void onEnable() {
2728
server.setExecutor(null);
2829
server.start();
2930
} catch (IOException e) {
30-
System.out.println("Could not open the port for the web server: " + e.getMessage());
31-
Bukkit.getPluginManager().disablePlugin(Bukkit.getPluginManager().getPlugin(getName()));
31+
disablePlugin("Could not open the port for the web server: " + e.getMessage());
3232
}
3333

3434
registerRoutes();
35+
36+
new MasterConnector().register();
3537
}
3638

3739
@Override
@@ -52,6 +54,16 @@ public void registerRoutes() {
5254
});
5355
}
5456

57+
58+
/**
59+
* Disables the plugin
60+
* @param message The reason why the plugin should be disabled
61+
*/
62+
public static void disablePlugin(String message) {
63+
System.out.println(getPrefix()+message);
64+
Bukkit.getPluginManager().disablePlugin(Bukkit.getPluginManager().getPlugin(getInstance().getName()));
65+
}
66+
5567
/**
5668
* Gets the dashboard configuration
5769
* @return the dashboard configuration
@@ -84,4 +96,12 @@ public static HttpServer getHttpServer() {
8496
public static String getRoutePackageName() {
8597
return getInstance().getClass().getPackage().getName()+".panel.routes";
8698
}
99+
100+
/**
101+
* Gets the prefix of the plugin
102+
* @return the prefix
103+
*/
104+
public static String getPrefix() {
105+
return "["+getInstance().getName()+"] ";
106+
}
87107
}

src/main/java/de/gnmyt/mcdash/api/config/ConfigurationManager.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ public boolean configExists() {
3333
* Generates a default configuration
3434
*/
3535
public void generateDefault() {
36+
37+
// Server configuration
38+
config.set("identifier", Integer.parseInt(String.format("%04d", new Random().nextInt(10000))));
39+
3640
// Master configuration
37-
config.set("masterIP", "localhost:5232");
41+
config.set("masterIP", "http://localhost:5232");
3842
config.set("masterKey", "your-master-key");
3943

4044
// Wrapper configuration
@@ -62,6 +66,15 @@ public Integer getInt(String path) {
6266
return config.getInt(path);
6367
}
6468

69+
/**
70+
* Checks if the configuration file contains an string
71+
* @param path The path you want to check
72+
* @return <code>true</code> if the provided path exists in the config, otherwise <code>false</code>
73+
*/
74+
public boolean hasString(String path) {
75+
return config.getString(path) != null;
76+
}
77+
6578
/**
6679
* Gets the master ip from the configuration
6780
* @return the master ip
@@ -74,7 +87,7 @@ public String getMasterIP() {
7487
* Gets the master key from the configuration
7588
* @return the master key
7689
*/
77-
public String masterKey() {
90+
public String getMasterKey() {
7891
return getString("masterKey");
7992
}
8093

@@ -94,6 +107,15 @@ public String getWrapperKey() {
94107
return getString("wrapperKey");
95108
}
96109

110+
111+
/**
112+
* Gets the server identifier
113+
* @return the server identifier
114+
*/
115+
public int getIdentifier() {
116+
return getInt("identifier");
117+
}
118+
97119
/**
98120
* Saves the current configuration
99121
*/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package de.gnmyt.mcdash.connector;
2+
3+
import de.gnmyt.mcdash.MinecraftDashboard;
4+
import de.gnmyt.mcdash.api.config.ConfigurationManager;
5+
import okhttp3.FormBody;
6+
import okhttp3.OkHttpClient;
7+
import okhttp3.Request;
8+
import okhttp3.Response;
9+
10+
import java.net.InetAddress;
11+
import java.net.UnknownHostException;
12+
13+
public class MasterConnector {
14+
15+
private final String REQUEST_FORMAT = "%s/api/register";
16+
17+
private final OkHttpClient client = new OkHttpClient().newBuilder().build();
18+
private final ConfigurationManager config = MinecraftDashboard.getDashboardConfig();
19+
20+
private int attempted_trys = 0;
21+
22+
/**
23+
* Gets the local ip address of the computer
24+
* @return the local ip address of the computer
25+
*/
26+
public String getLocalAddress() {
27+
try {
28+
return config.hasString("customIP") ? config.getString("customIP") : InetAddress.getLocalHost().getHostAddress();
29+
} catch (UnknownHostException ignored) { }
30+
return "localhost";
31+
}
32+
33+
/**
34+
* Prepares the registration request
35+
* @return the prepared registration request
36+
*/
37+
public Request prepareRequest() {
38+
return new Request.Builder()
39+
.url(String.format(REQUEST_FORMAT, config.getMasterIP()))
40+
.post(new FormBody.Builder()
41+
.addEncoded("identifier", String.valueOf(config.getIdentifier()))
42+
.addEncoded("wrapperIP", getLocalAddress()+":"+config.getWrapperPort())
43+
.addEncoded("wrapperKey", config.getWrapperKey())
44+
.build())
45+
.header("Authorization", "Bearer " + config.getMasterKey())
46+
.build();
47+
}
48+
49+
/**
50+
* Updates the wrapper data in the master backend
51+
*/
52+
public void register() {
53+
attempted_trys++;
54+
55+
if (attempted_trys > 5) return;
56+
57+
try {
58+
Response response = client.newCall(prepareRequest()).execute();
59+
response.body().close();
60+
if (response.code() > 200) {
61+
throw new Exception("Request not successful");
62+
}
63+
System.out.println(MinecraftDashboard.getPrefix() + "Successfully registered the server");
64+
} catch (Exception e) {
65+
66+
if (attempted_trys == 5) {
67+
MinecraftDashboard.disablePlugin("Could not connect to master, please check the config.yml file");
68+
} else System.out.println(String.format("%sRegistration failed, retrying in 5 seconds... (try %d/5)", MinecraftDashboard.getPrefix(), attempted_trys));
69+
70+
try { Thread.sleep(5000); } catch (InterruptedException ignored) { }
71+
72+
register();
73+
}
74+
}
75+
76+
}

0 commit comments

Comments
 (0)