Remove duplicated code

This commit is contained in:
Braydon 2024-04-10 09:46:19 -04:00
parent 2df4a7978c
commit 3a098433e0
4 changed files with 20 additions and 21 deletions

@ -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");
}

@ -30,16 +30,17 @@ import me.braydon.mc.model.MinecraftServer;
* A {@link MinecraftServerPinger} is
* used to ping a {@link MinecraftServer}.
*
* @author Braydon
* @param <T> the type of server to ping
* @author Braydon
*/
public interface MinecraftServerPinger<T extends MinecraftServer> {
/**
* 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
*/
T ping(@NonNull String hostname, int port);
T ping(@NonNull String hostname, String ip, int port);
}

@ -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

@ -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<Ja
* 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
* @throws BadRequestException if the hostname is unknown
* @throws ResourceNotFoundException if the server could not be found
*/
@Override
public JavaMinecraftServer ping(@NonNull String hostname, int port) throws BadRequestException, ResourceNotFoundException {
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 JavaMinecraftServer ping(@NonNull String hostname, String ip, int port) {
log.info("Pinging {}:{}...", hostname, port);
long before = System.currentTimeMillis(); // Timestamp before pinging