Java SDK: Add DNS records to MC servers
All checks were successful
Publish Java SDK / docker (17, 3.8.5) (push) Successful in 29s

This commit is contained in:
Braydon 2024-04-26 17:22:20 -04:00
parent ad215b7b72
commit 08acdaef03
8 changed files with 247 additions and 9 deletions

@ -25,6 +25,8 @@ package cc.restfulmc.sdk.client;
import cc.restfulmc.sdk.command.impl.AsyncClientCommands;
import cc.restfulmc.sdk.command.impl.SyncClientCommands;
import cc.restfulmc.sdk.response.server.dns.DNSRecord;
import cc.restfulmc.sdk.serializer.DNSRecordSerializer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.AccessLevel;
@ -39,6 +41,7 @@ import lombok.experimental.Accessors;
public final class RESTfulMCClient {
public static final Gson GSON = new GsonBuilder()
.serializeNulls()
.registerTypeAdapter(DNSRecord.class, new DNSRecordSerializer())
.create();
/**

@ -23,6 +23,7 @@
*/
package cc.restfulmc.sdk.response.server;
import cc.restfulmc.sdk.response.server.dns.DNSRecord;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
@ -55,10 +56,10 @@ public final class BedrockMinecraftServer extends MinecraftServer {
*/
@NonNull private final GameMode gamemode;
public BedrockMinecraftServer(@NonNull String hostname, String ip, int port, GeoLocation geo,@NonNull Players players,
@NonNull MOTD motd, @NonNull String id, @NonNull Edition edition, @NonNull Version version,
@NonNull GameMode gamemode) {
super(hostname, ip, port, geo, players, motd);
public BedrockMinecraftServer(@NonNull String hostname, String ip, int port, DNSRecord[] records, GeoLocation geo,
@NonNull Players players, @NonNull MOTD motd, @NonNull String id, @NonNull Edition edition,
@NonNull Version version, @NonNull GameMode gamemode) {
super(hostname, ip, port, records, geo, players, motd);
this.id = id;
this.edition = edition;
this.version = version;

@ -23,6 +23,7 @@
*/
package cc.restfulmc.sdk.response.server;
import cc.restfulmc.sdk.response.server.dns.DNSRecord;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
@ -107,11 +108,12 @@ public class JavaMinecraftServer extends MinecraftServer {
*/
private final boolean mojangBanned;
public JavaMinecraftServer(@NonNull String hostname, String ip, int port, GeoLocation geo, @NonNull Players players,
@NonNull MOTD motd, @NonNull Version version, Favicon favicon, String software, Plugin[] plugins,
ModInfo modInfo, ForgeData forgeData, String world, boolean queryEnabled, boolean previewsChat,
boolean enforcesSecureChat, boolean preventsChatReports, boolean mojangBanned) {
super(hostname, ip, port, geo, players, motd);
public JavaMinecraftServer(@NonNull String hostname, String ip, int port, DNSRecord[] records, GeoLocation geo,
@NonNull Players players, @NonNull MOTD motd, @NonNull Version version, Favicon favicon,
String software, Plugin[] plugins, ModInfo modInfo, ForgeData forgeData, String world,
boolean queryEnabled, boolean previewsChat, boolean enforcesSecureChat, boolean preventsChatReports,
boolean mojangBanned) {
super(hostname, ip, port, records, geo, players, motd);
this.version = version;
this.favicon = favicon;
this.software = software;

@ -24,6 +24,7 @@
package cc.restfulmc.sdk.response.server;
import cc.restfulmc.sdk.response.CacheableResponse;
import cc.restfulmc.sdk.response.server.dns.DNSRecord;
import lombok.*;
import java.util.UUID;
@ -52,6 +53,11 @@ public abstract class MinecraftServer extends CacheableResponse {
*/
@EqualsAndHashCode.Include private final int port;
/**
* The DNS records resolved for this server, null if none.
*/
private final DNSRecord[] records;
/**
* The Geo location of this server, null if unknown.
*/

@ -0,0 +1,63 @@
/*
* MIT License
*
* Copyright (c) 2024 Braydon (Rainnny).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package cc.restfulmc.sdk.response.server.dns;
import cc.restfulmc.sdk.response.server.dns.impl.ARecord;
import cc.restfulmc.sdk.response.server.dns.impl.SRVRecord;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
/**
* A representation of a DNS record.
*
* @author Braydon
*/
@AllArgsConstructor @Getter @ToString
public abstract class DNSRecord {
/**
* The type of this record.
*/
@NonNull private final Type type;
/**
* The TTL (Time To Live) of this record.
*/
private final long ttl;
/**
* Types of a record.
*/
@AllArgsConstructor @Getter
public enum Type {
A(ARecord.class),
SRV(SRVRecord.class);
/**
* The record class for this type.
*/
@NonNull private final Class<? extends DNSRecord> recordClass;
}
}

@ -0,0 +1,47 @@
/*
* MIT License
*
* Copyright (c) 2024 Braydon (Rainnny).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package cc.restfulmc.sdk.response.server.dns.impl;
import cc.restfulmc.sdk.response.server.dns.DNSRecord;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
/**
* An A record implementation.
*
* @author Braydon
*/
@Getter @ToString(callSuper = true)
public final class ARecord extends DNSRecord {
/**
* The address of this record, null if unresolved.
*/
private final String address;
public ARecord(@NonNull Type type, long ttl, String address) {
super(type, ttl);
this.address = address;
}
}

@ -0,0 +1,65 @@
/*
* MIT License
*
* Copyright (c) 2024 Braydon (Rainnny).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package cc.restfulmc.sdk.response.server.dns.impl;
import cc.restfulmc.sdk.response.server.dns.DNSRecord;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
/**
* An SRV record implementation.
*
* @author Braydon
*/
@Getter @ToString(callSuper = true)
public final class SRVRecord extends DNSRecord {
/**
* The priority of this record.
*/
private final int priority;
/**
* The weight of this record.
*/
private final int weight;
/**
* The port of this record.
*/
private final int port;
/**
* The target of this record.
*/
@NonNull private final String target;
public SRVRecord(@NonNull Type type, long ttl, int priority, int weight, int port, @NonNull String target) {
super(type, ttl);
this.priority = priority;
this.weight = weight;
this.port = port;
this.target = target;
}
}

@ -0,0 +1,51 @@
/*
* MIT License
*
* Copyright (c) 2024 Braydon (Rainnny).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package cc.restfulmc.sdk.serializer;
import cc.restfulmc.sdk.client.RESTfulMCClient;
import cc.restfulmc.sdk.response.server.dns.DNSRecord;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
/**
* A custom deserializer for {@link DNSRecord}'s.
* <p>
* This serializer will take the "type" in a DNS
* record, and convert the record to the correct type.
* E.g: A -> ARecord, SRV -> SRVRecord, etc
* </p>
*
* @author Braydon
*/
public final class DNSRecordSerializer implements JsonDeserializer<DNSRecord> {
@Override
public DNSRecord deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
DNSRecord.Type recordType = DNSRecord.Type.valueOf(jsonElement.getAsJsonObject().get("type").getAsString()); // Get the record type
return RESTfulMCClient.GSON.fromJson(jsonElement, recordType.getRecordClass()); // Convert the record to the correct type
}
}