From 3a098433e0e2794fdb543128fe78f757f7eb7db2 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Wed, 10 Apr 2024 09:46:19 -0400 Subject: [PATCH] Remove duplicated code --- .../java/me/braydon/mc/service/MojangService.java | 9 ++++++++- .../mc/service/pinger/MinecraftServerPinger.java | 7 ++++--- .../pinger/impl/BedrockMinecraftServerPinger.java | 14 ++++++-------- .../pinger/impl/JavaMinecraftServerPinger.java | 11 ++--------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/java/me/braydon/mc/service/MojangService.java b/src/main/java/me/braydon/mc/service/MojangService.java index a2fd925..b81b728 100644 --- a/src/main/java/me/braydon/mc/service/MojangService.java +++ b/src/main/java/me/braydon/mc/service/MojangService.java @@ -55,6 +55,7 @@ import org.springframework.http.HttpMethod; import org.springframework.stereotype.Service; import java.io.InputStream; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -399,8 +400,14 @@ public final class MojangService { port = address.getPort(); } + InetAddress inetAddress = DNSUtils.resolveA(hostname); // Resolve the hostname to an IP address + String ip = inetAddress == null ? null : inetAddress.getHostAddress(); // Get the IP address + if (ip != null) { // Was the IP resolved? + log.info("Resolved hostname: {} -> {}", hostname, ip); + } + // Build our server model, cache it, and then return it - MinecraftServer response = platform.getPinger().ping(hostname, port); // Ping the server and await a response + MinecraftServer response = platform.getPinger().ping(hostname, ip, port); // Ping the server and await a response if (response == null) { // No response from ping throw new ResourceNotFoundException("Server didn't respond to ping"); } diff --git a/src/main/java/me/braydon/mc/service/pinger/MinecraftServerPinger.java b/src/main/java/me/braydon/mc/service/pinger/MinecraftServerPinger.java index a37d337..7ff922a 100644 --- a/src/main/java/me/braydon/mc/service/pinger/MinecraftServerPinger.java +++ b/src/main/java/me/braydon/mc/service/pinger/MinecraftServerPinger.java @@ -30,16 +30,17 @@ import me.braydon.mc.model.MinecraftServer; * A {@link MinecraftServerPinger} is * used to ping a {@link MinecraftServer}. * - * @author Braydon * @param the type of server to ping + * @author Braydon */ public interface MinecraftServerPinger { /** * Ping the server with the given hostname and port. * * @param hostname the hostname of the server - * @param port the port of the server + * @param ip the ip of the server, null if unresolved + * @param port the port of the server * @return the server that was pinged */ - T ping(@NonNull String hostname, int port); + T ping(@NonNull String hostname, String ip, int port); } \ No newline at end of file diff --git a/src/main/java/me/braydon/mc/service/pinger/impl/BedrockMinecraftServerPinger.java b/src/main/java/me/braydon/mc/service/pinger/impl/BedrockMinecraftServerPinger.java index 3fcf26f..f2e0e2d 100644 --- a/src/main/java/me/braydon/mc/service/pinger/impl/BedrockMinecraftServerPinger.java +++ b/src/main/java/me/braydon/mc/service/pinger/impl/BedrockMinecraftServerPinger.java @@ -25,7 +25,6 @@ package me.braydon.mc.service.pinger.impl; import lombok.NonNull; import lombok.extern.log4j.Log4j2; -import me.braydon.mc.common.DNSUtils; import me.braydon.mc.common.packet.impl.bedrock.BedrockPacketUnconnectedPing; import me.braydon.mc.common.packet.impl.bedrock.BedrockPacketUnconnectedPong; import me.braydon.mc.exception.impl.BadRequestException; @@ -34,7 +33,10 @@ import me.braydon.mc.model.server.BedrockMinecraftServer; import me.braydon.mc.service.pinger.MinecraftServerPinger; import java.io.IOException; -import java.net.*; +import java.net.DatagramSocket; +import java.net.InetSocketAddress; +import java.net.SocketTimeoutException; +import java.net.UnknownHostException; /** * The {@link MinecraftServerPinger} for pinging @@ -50,16 +52,12 @@ public final class BedrockMinecraftServerPinger implements MinecraftServerPinger * Ping the server with the given hostname and port. * * @param hostname the hostname of the server + * @param ip the ip of the server, null if unresolved * @param port the port of the server * @return the server that was pinged */ @Override - public BedrockMinecraftServer ping(@NonNull String hostname, int port) { - InetAddress inetAddress = DNSUtils.resolveA(hostname); // Resolve the hostname to an IP address - String ip = inetAddress == null ? null : inetAddress.getHostAddress(); // Get the IP address - if (ip != null) { // Was the IP resolved? - log.info("Resolved hostname: {} -> {}", hostname, ip); - } + public BedrockMinecraftServer ping(@NonNull String hostname, String ip, int port) { log.info("Pinging {}:{}...", hostname, port); long before = System.currentTimeMillis(); // Timestamp before pinging diff --git a/src/main/java/me/braydon/mc/service/pinger/impl/JavaMinecraftServerPinger.java b/src/main/java/me/braydon/mc/service/pinger/impl/JavaMinecraftServerPinger.java index 76e284d..71daa7d 100644 --- a/src/main/java/me/braydon/mc/service/pinger/impl/JavaMinecraftServerPinger.java +++ b/src/main/java/me/braydon/mc/service/pinger/impl/JavaMinecraftServerPinger.java @@ -25,7 +25,6 @@ package me.braydon.mc.service.pinger.impl; import lombok.NonNull; import lombok.extern.log4j.Log4j2; -import me.braydon.mc.common.DNSUtils; import me.braydon.mc.common.JavaMinecraftVersion; import me.braydon.mc.common.packet.impl.java.JavaPacketHandshakingInSetProtocol; import me.braydon.mc.common.packet.impl.java.JavaPacketStatusInStart; @@ -55,18 +54,12 @@ public final class JavaMinecraftServerPinger implements MinecraftServerPinger {}", hostname, ip); - } + public JavaMinecraftServer ping(@NonNull String hostname, String ip, int port) { log.info("Pinging {}:{}...", hostname, port); long before = System.currentTimeMillis(); // Timestamp before pinging