Skip to content

Commit e983997

Browse files
authored
refactor: move response data into DTO in preparation for html rendering (#6)
1 parent 12c9ac3 commit e983997

2 files changed

Lines changed: 251 additions & 120 deletions

File tree

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
package net.nitrado.hytale.plugins.query;
2+
3+
import com.hypixel.hytale.common.plugin.PluginIdentifier;
4+
import com.hypixel.hytale.common.util.java.ManifestUtil;
5+
import com.hypixel.hytale.protocol.ProtocolSettings;
6+
import com.hypixel.hytale.server.core.HytaleServer;
7+
import com.hypixel.hytale.server.core.plugin.PluginBase;
8+
import com.hypixel.hytale.server.core.plugin.PluginManager;
9+
import com.hypixel.hytale.server.core.universe.Universe;
10+
import org.bson.Document;
11+
12+
import java.net.Inet6Address;
13+
import java.net.InetSocketAddress;
14+
import java.util.ArrayList;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
18+
public class QueryResponseV1 {
19+
20+
private BasicData basic;
21+
private ServerData server;
22+
private UniverseData universe;
23+
private List<PlayerData> players;
24+
private List<PluginData> plugins;
25+
private InetSocketAddress address;
26+
27+
public QueryResponseV1(InetSocketAddress publicAddress) {
28+
this.address = publicAddress;
29+
}
30+
31+
public BasicData getBasic() {
32+
return basic;
33+
}
34+
35+
public ServerData getServer() {
36+
return server;
37+
}
38+
39+
public UniverseData getUniverse() {
40+
return universe;
41+
}
42+
43+
public List<PlayerData> getPlayers() {
44+
return players;
45+
}
46+
47+
public List<PluginData> getPlugins() {
48+
return plugins;
49+
}
50+
51+
public void addBasicData() {
52+
this.basic = new BasicData(
53+
HytaleServer.get().getServerName(),
54+
ManifestUtil.getImplementationVersion(),
55+
HytaleServer.get().getConfig().getMaxPlayers(),
56+
Universe.get().getPlayerCount(),
57+
address
58+
);
59+
}
60+
61+
public void addServerData() {
62+
this.server = new ServerData(
63+
HytaleServer.get().getServerName(),
64+
ManifestUtil.getImplementationVersion(),
65+
ManifestUtil.getImplementationRevisionId(),
66+
ManifestUtil.getPatchline(),
67+
ProtocolSettings.PROTOCOL_VERSION,
68+
ProtocolSettings.PROTOCOL_HASH,
69+
HytaleServer.get().getConfig().getMaxPlayers(),
70+
address
71+
);
72+
}
73+
74+
public void addUniverseData() {
75+
var defaultWorld = Universe.get().getDefaultWorld();
76+
this.universe = new UniverseData(
77+
Universe.get().getPlayerCount(),
78+
defaultWorld == null ? null : defaultWorld.getName()
79+
);
80+
}
81+
82+
public void addPlayerData() {
83+
this.players = new ArrayList<>();
84+
for (var entry : Universe.get().getWorlds().entrySet()) {
85+
var world = entry.getValue();
86+
for (var ref : world.getPlayerRefs()) {
87+
players.add(new PlayerData(
88+
ref.getUsername(),
89+
ref.getUuid().toString(),
90+
world.getName()
91+
));
92+
}
93+
}
94+
}
95+
96+
public void addPluginData() {
97+
this.plugins = new ArrayList<>();
98+
var pluginsList = PluginManager.get().getPlugins();
99+
var pluginMap = new HashMap<PluginIdentifier, PluginBase>(pluginsList.size());
100+
101+
for (var plugin : pluginsList) {
102+
pluginMap.put(plugin.getIdentifier(), plugin);
103+
}
104+
105+
for (var manifest : PluginManager.get().getAvailablePlugins().values()) {
106+
var pluginIdentifier = new PluginIdentifier(manifest);
107+
var plugin = pluginMap.get(pluginIdentifier);
108+
this.plugins.add(new PluginData(
109+
pluginIdentifier.toString(),
110+
manifest.getVersion().toString(),
111+
plugin != null,
112+
plugin != null ? plugin.isEnabled() : null,
113+
plugin != null ? plugin.getState().toString() : null
114+
));
115+
}
116+
}
117+
118+
public Document toDocument() {
119+
Document doc = new Document();
120+
121+
if (basic != null) {
122+
var basicDoc = new Document();
123+
basicDoc.append("Name", basic.name())
124+
.append("Version", basic.version())
125+
.append("MaxPlayers", basic.maxPlayers())
126+
.append("CurrentPlayers", basic.currentPlayers());
127+
128+
if (basic.address() != null) {
129+
basicDoc.append("Address", formatAddress(basic.address()));
130+
}
131+
132+
doc.append("Basic", basicDoc);
133+
}
134+
135+
if (server != null) {
136+
var serverDoc = new Document();
137+
serverDoc
138+
.append("Name", server.name())
139+
.append("Version", server.version())
140+
.append("Revision", server.revision())
141+
.append("Patchline", server.patchline())
142+
.append("ProtocolVersion", server.protocolVersion())
143+
.append("ProtocolHash", server.protocolHash())
144+
.append("MaxPlayers", server.maxPlayers());
145+
146+
if (server.address() != null) {
147+
serverDoc.append("Address", formatAddress(server.address()));
148+
}
149+
150+
doc.append("Server", serverDoc);
151+
}
152+
153+
if (universe != null) {
154+
doc.append("Universe", new Document()
155+
.append("CurrentPlayers", universe.currentPlayers())
156+
.append("DefaultWorld", universe.defaultWorld())
157+
);
158+
}
159+
160+
if (players != null) {
161+
var playerDocs = new ArrayList<Document>();
162+
for (var player : players) {
163+
playerDocs.add(new Document()
164+
.append("Name", player.name())
165+
.append("UUID", player.uuid())
166+
.append("World", player.world())
167+
);
168+
}
169+
doc.append("Players", playerDocs);
170+
}
171+
172+
if (plugins != null) {
173+
var pluginDoc = new Document();
174+
for (var plugin : plugins) {
175+
var entry = new Document()
176+
.append("Version", plugin.version())
177+
.append("Loaded", plugin.loaded());
178+
if (plugin.loaded()) {
179+
entry.append("Enabled", plugin.enabled())
180+
.append("State", plugin.state());
181+
}
182+
pluginDoc.append(plugin.identifier(), entry);
183+
}
184+
doc.append("Plugins", pluginDoc);
185+
}
186+
187+
return doc;
188+
}
189+
190+
public record BasicData(
191+
String name,
192+
String version,
193+
int maxPlayers,
194+
int currentPlayers,
195+
InetSocketAddress address
196+
) {}
197+
198+
public record ServerData(
199+
String name,
200+
String version,
201+
String revision,
202+
String patchline,
203+
int protocolVersion,
204+
String protocolHash,
205+
int maxPlayers,
206+
InetSocketAddress address
207+
) {}
208+
209+
public record UniverseData(
210+
int currentPlayers,
211+
String defaultWorld
212+
) {}
213+
214+
public record PlayerData(
215+
String name,
216+
String uuid,
217+
String world
218+
) {}
219+
220+
public record PluginData(
221+
String identifier,
222+
String version,
223+
boolean loaded,
224+
Boolean enabled,
225+
String state
226+
) {}
227+
228+
private String formatAddress(InetSocketAddress address) {
229+
String formatted;
230+
231+
formatted = address.getHostString();
232+
if (formatted.contains(":")) {
233+
// IPv6
234+
formatted = "[" + formatted + "]";
235+
}
236+
237+
return formatted + ":" + address.getPort();
238+
}
239+
}

0 commit comments

Comments
 (0)