Skip to content

Commit 1c31029

Browse files
committed
Created model StatusMCModel + optimizations and corrections
1 parent 2a25013 commit 1c31029

3 files changed

Lines changed: 99 additions & 84 deletions

File tree

src/main/java/com/devparada/frame/InitFrame.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.devparada.logic.ImageServer;
2020
import com.devparada.logic.StatusMCServer;
21+
import com.devparada.model.StatusMCModel;
2122
import com.devparada.persistency.DBManager;
2223
import com.formdev.flatlaf.FlatDarkLaf;
2324
import java.awt.Color;
@@ -55,7 +56,7 @@ public class InitFrame extends javax.swing.JFrame {
5556
*/
5657
public InitFrame() {
5758
initComponents();
58-
timer.schedule(task, 0, 15000);
59+
timer.schedule(task, 15000);
5960
}
6061

6162
/**
@@ -238,8 +239,8 @@ public void run() {
238239
};
239240

240241
private void refreshServer() {
241-
collectAddServers();
242242
jPnlInfo.removeAll();
243+
collectAddServers();
243244
}
244245

245246
/**
@@ -250,7 +251,9 @@ private void refreshServer() {
250251
* @param id server id
251252
*/
252253
protected void addPanel(String ipServer, int port, String id) {
253-
StatusMCServer statusServer = new StatusMCServer(ipServer, port);
254+
StatusMCModel statusMC = new StatusMCModel(ipServer, port);
255+
256+
StatusMCServer statusServer = new StatusMCServer(statusMC);
254257
String ipServerPort = ipServer + ":" + port;
255258

256259
// JPanel create
@@ -271,6 +274,7 @@ protected void addPanel(String ipServer, int port, String id) {
271274
jBtnEdit.setForeground(new Color(0, 0, 0));
272275
jBtnEdit.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
273276
jBtnEdit.setHorizontalAlignment(JTextField.CENTER);
277+
jBtnEdit.setEnabled(false);
274278

275279
// JButton delete
276280
JButton jBtnDelete = new JButton("Delete");

src/main/java/com/devparada/logic/StatusMCServer.java

Lines changed: 43 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
*/
1717
package com.devparada.logic;
1818

19+
import com.devparada.model.StatusMCModel;
1920
import com.google.gson.JsonElement;
2021
import com.google.gson.JsonObject;
2122
import com.google.gson.JsonParser;
2223
import com.google.gson.JsonSyntaxException;
23-
import java.io.BufferedReader;
2424
import java.io.IOException;
25-
import java.io.InputStreamReader;
26-
import java.net.HttpURLConnection;
27-
import java.net.URL;
25+
import java.net.URI;
26+
import java.net.http.HttpClient;
27+
import java.net.http.HttpRequest;
28+
import java.net.http.HttpResponse;
2829
import javax.swing.JOptionPane;
2930

3031
/**
@@ -33,116 +34,77 @@
3334
*/
3435
public class StatusMCServer {
3536

36-
public String host;
37-
public int port;
37+
private final StatusMCModel statusMC;
38+
private final String ipServer;
39+
/**
40+
* The value is OK in HTTPConnection
41+
*/
42+
public static final int HTTP_OK = 200;
3843

39-
public StatusMCServer(String host, int port) {
40-
this.host = host;
41-
this.port = port;
44+
public StatusMCServer(StatusMCModel statusMC) {
45+
this.statusMC = statusMC;
46+
ipServer = this.statusMC.getHost() + ":" + this.statusMC.getPort();
4247
}
4348

44-
public JsonObject fetchData(String ipServer) {
49+
private JsonObject fetchData() {
4550
if (!ipServer.isEmpty()) {
46-
String urlJSON = "https://api.mcstatus.io/v2/status/java/" + ipServer; // URL of the remote JSON
51+
String url = "https://api.mcstatus.io/v2/status/java/" + ipServer; // URL of the remote JSON
4752
try {
48-
URL url = new URL(urlJSON);
49-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
50-
conn.setRequestMethod("GET");
51-
conn.setRequestProperty("Accept", "application/json");
53+
HttpClient client = HttpClient.newHttpClient();
5254

53-
if (conn.getResponseCode() == 200) {
54-
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
55-
StringBuilder sb = new StringBuilder();
56-
String output;
57-
while ((output = br.readLine()) != null) {
58-
sb.append(output);
59-
}
55+
HttpRequest request = HttpRequest.newBuilder()
56+
.uri(URI.create(url))
57+
.header("Accept", "application/json")
58+
.build();
6059

61-
conn.disconnect();
60+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
6261

63-
JsonObject jsonReceived = JsonParser.parseString(sb.toString()).getAsJsonObject();
62+
if (response.statusCode() == HTTP_OK) {
63+
// Parse the JSON response body
64+
JsonObject jsonReceived = JsonParser.parseString(response.body()).getAsJsonObject();
6465
return jsonReceived;
66+
} else {
67+
JOptionPane.showMessageDialog(null, "Not data", "An error occurred", JOptionPane.ERROR_MESSAGE);
6568
}
66-
} catch (JsonSyntaxException | IOException e) {
69+
} catch (JsonSyntaxException | IOException | InterruptedException e) {
6770
JOptionPane.showMessageDialog(null, e, "An error occurred", JOptionPane.WARNING_MESSAGE);
6871
}
6972
}
7073
return null;
7174
}
7275

73-
public JsonObject obtainDataJSON(String ipServer) {
74-
return fetchData(ipServer);
75-
}
76-
77-
public String obtainData(String ipServer) {
78-
return showData(fetchData(ipServer));
79-
}
80-
81-
public String showData(JsonObject jsonReceived) {
82-
String textResult = "";
83-
84-
if (jsonReceived != null) {
85-
for (String key : jsonReceived.keySet()) {
86-
JsonElement value = jsonReceived.get(key);
87-
88-
if ("online".equals(key)) {
89-
String statusServerOnline = "offline";
90-
91-
if ("true".equals(value.getAsString())) {
92-
statusServerOnline = "online";
93-
}
94-
textResult = "The server is " + statusServerOnline + "\n";
95-
}
96-
97-
if ("version".equals(key)) {
98-
String nameClean = value.getAsJsonObject().get("name_clean").getAsString();
99-
textResult += "Server version " + nameClean + "\n";
100-
}
101-
102-
if ("players".equals(key)) {
103-
String playersNumber = value.getAsJsonObject().get("online").getAsString();
104-
textResult += "Players playing " + playersNumber + "\n";
105-
}
106-
107-
}
108-
}
109-
return textResult;
110-
}
111-
11276
public String showDataSection(String ipServer, String section) {
113-
JsonObject jsonReceived = obtainDataJSON(ipServer);
77+
JsonObject jsonReceived = fetchData();
11478
String textResult = "N/A";
11579

11680
if (jsonReceived != null) {
11781
for (String key : jsonReceived.keySet()) {
118-
JsonElement value = jsonReceived.get(key);
119-
120-
switch (section) {
121-
case "online" -> {
122-
String statusServerOnline;
123-
if (section.equals(key)) {
82+
if (section.equals(key)) {
83+
JsonElement value = jsonReceived.get(key);
84+
switch (section) {
85+
case "online" -> {
86+
String statusServerOnline;
12487
statusServerOnline = "Offline";
12588
if ("true".equals(value.getAsString())) {
12689
statusServerOnline = "Online";
12790
}
12891
textResult = statusServerOnline;
12992
}
130-
}
131-
case "version" -> {
132-
if (section.equals(key)) {
93+
case "version" -> {
13394
String nameClean = value.getAsJsonObject().get("name_clean").getAsString();
134-
textResult = nameClean;
95+
String versionName = nameClean;
96+
97+
if (nameClean.isEmpty()) {
98+
versionName = "Unknown";
99+
}
100+
textResult = "Version: " + versionName;
135101
}
136-
}
137-
case "players" -> {
138-
if (section.equals(key)) {
102+
case "players" -> {
139103
String playersNumber = value.getAsJsonObject().get("online").getAsString();
140104
String maxPlayers = value.getAsJsonObject().get("max").getAsString();
141105
textResult = playersNumber + "/" + maxPlayers + " players";
142106
}
143-
}
144-
case "icon" -> {
145-
if (section.equals(key)) {
107+
case "icon" -> {
146108
String icon = value.getAsString();
147109

148110
if (icon.contains(",")) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of StatusMCServerTool.
3+
*
4+
* StatusMCServerTool is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* StatusMCServerTool is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with StatusMCServerTool. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
package com.devparada.model;
18+
19+
/**
20+
*
21+
* @author devparada
22+
*/
23+
public class StatusMCModel {
24+
25+
public String host;
26+
public int port;
27+
28+
public StatusMCModel(String host, int port) {
29+
this.host = host;
30+
this.port = port;
31+
}
32+
33+
// Getters and Setters
34+
public String getHost() {
35+
return host;
36+
}
37+
38+
public void setHost(String host) {
39+
this.host = host;
40+
}
41+
42+
public int getPort() {
43+
return port;
44+
}
45+
46+
public void setPort(int port) {
47+
this.port = port;
48+
}
49+
}

0 commit comments

Comments
 (0)