Change how ports are defined in requests

This commit is contained in:
Braydon 2024-04-10 07:08:42 -04:00
parent a4a3486341
commit 9725237885
2 changed files with 12 additions and 14 deletions

@ -66,7 +66,6 @@ public final class ServerController {
* *
* @param platform the platform of the server * @param platform the platform of the server
* @param hostname the hostname of the server * @param hostname the hostname of the server
* @param port the port of the server, null for default
* @return the server * @return the server
* @throws BadRequestException if the hostname, platform, or port is invalid * @throws BadRequestException if the hostname, platform, or port is invalid
* @throws ResourceNotFoundException if the server isn't found * @throws ResourceNotFoundException if the server isn't found
@ -75,10 +74,9 @@ public final class ServerController {
@ResponseBody @ResponseBody
public ResponseEntity<CachedMinecraftServer> getServer( public ResponseEntity<CachedMinecraftServer> getServer(
@Parameter(description = "The platform of the server", example = "java") @PathVariable @NonNull String platform, @Parameter(description = "The platform of the server", example = "java") @PathVariable @NonNull String platform,
@Parameter(description = "The hostname of the server", example = "hypixel.net") @PathVariable @NonNull String hostname, @Parameter(description = "The hostname of the server", example = "hypixel.net") @PathVariable @NonNull String hostname
@Parameter(description = "The port of the server", example = "25565") @RequestParam(required = false) String port
) throws BadRequestException, ResourceNotFoundException { ) throws BadRequestException, ResourceNotFoundException {
return ResponseEntity.ofNullable(mojangService.getMinecraftServer(platform, hostname, port)); return ResponseEntity.ofNullable(mojangService.getMinecraftServer(platform, hostname));
} }
/** /**
@ -103,18 +101,16 @@ public final class ServerController {
* server by its platform and hostname. * server by its platform and hostname.
* *
* @param hostname the hostname of the server * @param hostname the hostname of the server
* @param port the port of the server, null for default
* @return the server icon * @return the server icon
*/ */
@GetMapping(value = "/icon/{hostname}", produces = MediaType.IMAGE_PNG_VALUE) @GetMapping(value = "/icon/{hostname}", produces = MediaType.IMAGE_PNG_VALUE)
@ResponseBody @ResponseBody
public ResponseEntity<byte[]> getServerFavicon( public ResponseEntity<byte[]> getServerFavicon(
@Parameter(description = "The hostname of the server", example = "hypixel.net") @PathVariable @NonNull String hostname, @Parameter(description = "The hostname of the server", example = "hypixel.net") @PathVariable @NonNull String hostname
@Parameter(description = "The port of the server", example = "25565") @RequestParam(required = false) String port
) { ) {
return ResponseEntity.ok() return ResponseEntity.ok()
.contentType(MediaType.IMAGE_PNG) .contentType(MediaType.IMAGE_PNG)
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=%s.png".formatted(hostname)) .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=%s.png".formatted(hostname))
.body(mojangService.getServerFavicon(hostname, port)); .body(mojangService.getServerFavicon(hostname));
} }
} }

@ -271,14 +271,13 @@ public final class MojangService {
* </p> * </p>
* *
* @param hostname the hostname of the server * @param hostname the hostname of the server
* @param port the port of the server, null for default
* @return the server favicon * @return the server favicon
* @see #DEFAULT_SERVER_ICON for the default server icon * @see #DEFAULT_SERVER_ICON for the default server icon
*/ */
public byte[] getServerFavicon(@NonNull String hostname, String port) { public byte[] getServerFavicon(@NonNull String hostname) {
String icon = null; // The server base64 icon String icon = null; // The server base64 icon
try { try {
JavaMinecraftServer.Favicon favicon = ((JavaMinecraftServer) getMinecraftServer(MinecraftServer.Platform.JAVA.name(), hostname, port).getValue()).getFavicon(); JavaMinecraftServer.Favicon favicon = ((JavaMinecraftServer) getMinecraftServer(MinecraftServer.Platform.JAVA.name(), hostname).getValue()).getFavicon();
if (favicon != null) { // Use the server's favicon if (favicon != null) { // Use the server's favicon
icon = favicon.getBase64(); icon = favicon.getBase64();
icon = icon.substring(icon.indexOf(",") + 1); // Remove the data type from the server icon icon = icon.substring(icon.indexOf(",") + 1); // Remove the data type from the server icon
@ -345,13 +344,12 @@ public final class MojangService {
* *
* @param platformName the name of the platform * @param platformName the name of the platform
* @param hostname the hostname of the server * @param hostname the hostname of the server
* @param portString the port of the server, null for default
* @return the resolved Minecraft server * @return the resolved Minecraft server
* @throws BadRequestException if the hostname, platform, or port is invalid * @throws BadRequestException if the hostname, platform, or port is invalid
* @throws ResourceNotFoundException if the server isn't found * @throws ResourceNotFoundException if the server isn't found
*/ */
@NonNull @NonNull
public CachedMinecraftServer getMinecraftServer(@NonNull String platformName, @NonNull String hostname, String portString) public CachedMinecraftServer getMinecraftServer(@NonNull String platformName, @NonNull String hostname)
throws BadRequestException, ResourceNotFoundException { throws BadRequestException, ResourceNotFoundException {
MinecraftServer.Platform platform = EnumUtils.getEnumConstant(MinecraftServer.Platform.class, platformName.toUpperCase()); MinecraftServer.Platform platform = EnumUtils.getEnumConstant(MinecraftServer.Platform.class, platformName.toUpperCase());
if (platform == null) { // Invalid platform if (platform == null) { // Invalid platform
@ -360,7 +358,11 @@ public final class MojangService {
String lookupHostname = hostname; // The hostname used to lookup the server String lookupHostname = hostname; // The hostname used to lookup the server
int port = platform.getDefaultPort(); // Port to ping int port = platform.getDefaultPort(); // Port to ping
if (portString != null) { String portString = null;
if (hostname.contains(":")) { // Hostname contains a port
String[] split = hostname.split(":");
hostname = split[0];
portString = split[1];
try { // Try and parse the port try { // Try and parse the port
port = Integer.parseInt(portString); port = Integer.parseInt(portString);
} catch (NumberFormatException ex) { // Invalid port } catch (NumberFormatException ex) { // Invalid port