convert more classes to kotlin

This commit is contained in:
Bixilon 2021-06-04 16:20:38 +02:00 committed by Lukas
parent d5f77b79f2
commit bbe5079257
6 changed files with 86 additions and 115 deletions

View File

@ -35,7 +35,6 @@ import de.bixilon.minosoft.util.ServerAddress
import de.bixilon.minosoft.util.logging.Log
import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
import java.util.*
class StatusConnection(
val address: String,
@ -46,7 +45,7 @@ class StatusConnection(
lateinit var realAddress: ServerAddress
private set
private var addresses: LinkedList<ServerAddress>? = null
private var addresses: List<ServerAddress>? = null
var serverVersion: Version? = null
@ -56,7 +55,7 @@ class StatusConnection(
if (this.addresses == null) {
this.addresses = DNSUtil.getServerAddresses(address)
this.realAddress = this.addresses!!.first
this.realAddress = this.addresses!!.first()
}
}

View File

@ -1,59 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import org.xbill.DNS.Record;
import org.xbill.DNS.*;
import java.util.LinkedList;
import java.util.Locale;
public final class DNSUtil {
public static LinkedList<ServerAddress> getServerAddresses(String hostname) throws TextParseException {
ServerAddress fallbackAddress = getServerAddress(hostname);
LinkedList<ServerAddress> ret = new LinkedList<>();
if (hostname.contains(":")) {
// port provided, skip srv check
ret.add(fallbackAddress);
return ret;
}
String query = "_minecraft._tcp." + hostname;
Record[] records = new Lookup(query, Type.SRV).run();
if (records == null) {
ret.add(fallbackAddress);
return ret;
}
for (Record record : records) {
SRVRecord srvRecord = (SRVRecord) record;
ret.add(new ServerAddress(srvRecord.getTarget().toString(true), srvRecord.getPort()));
}
ret.add(fallbackAddress);
return ret;
}
private static ServerAddress getServerAddress(String hostname) {
String[] splitHostname = hostname.split(":", 2);
if (splitHostname.length == 1) {
return new ServerAddress(splitHostname[0], ProtocolDefinition.DEFAULT_PORT);
}
return new ServerAddress(splitHostname[0], Integer.parseInt(splitHostname[1]));
}
public static String correctHostName(String hostname) {
// replaces invalid chars to avoid copy and paste issues (like spaces, ...)
return hostname.replaceAll("\\s+|((https|http):/{2})+|/", "").toLowerCase(Locale.ROOT);
}
}

View File

@ -0,0 +1,58 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import org.xbill.DNS.Lookup
import org.xbill.DNS.SRVRecord
import org.xbill.DNS.Type
import java.util.*
object DNSUtil {
fun getServerAddresses(hostname: String): List<ServerAddress> {
val fallbackAddress = getServerAddress(hostname)
val addresses: MutableList<ServerAddress> = mutableListOf()
if (hostname.contains(":")) {
// port provided, skip srv check
addresses.add(fallbackAddress)
return addresses
}
val query = "_minecraft._tcp.$hostname"
val records = Lookup(query, Type.SRV).run()
if (records == null) {
addresses.add(fallbackAddress)
return addresses
}
for (record in records) {
check(record is SRVRecord)
addresses.add(ServerAddress(record.target.toString(true), record.port))
}
addresses.add(fallbackAddress)
return addresses
}
private fun getServerAddress(hostname: String): ServerAddress {
val splitHostname = hostname.split(":", limit = 2).toTypedArray()
return if (splitHostname.size == 1) {
ServerAddress(splitHostname[0], ProtocolDefinition.DEFAULT_PORT)
} else {
ServerAddress(splitHostname[0], splitHostname[1].toInt())
}
}
@JvmStatic
fun correctHostName(hostname: String): String {
// replaces invalid chars to avoid copy and paste issues (like spaces, ...)
return hostname.replace("\\s+|((https|http):/{2})+|/".toRegex(), "").lowercase(Locale.ROOT)
}
}

View File

@ -21,6 +21,7 @@ import org.apache.commons.cli.*;
import javax.annotation.Nullable;
import java.util.HashMap;
@Deprecated(forRemoval = true)
public class MinosoftCommandLineArguments {
private static final HashMap<Option, CommandLineArgumentHandler> OPTION_HASH_MAP = new HashMap<>();
private static final Options OPTIONS = new Options();

View File

@ -1,53 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util;
import java.util.Objects;
public class ServerAddress {
private final String hostname;
private final int port;
public ServerAddress(String hostname, int port) {
this.hostname = hostname;
this.port = port;
}
@Override
public int hashCode() {
return Objects.hash(this.hostname, this.port);
}
@Override
public boolean equals(Object obj) {
if (super.equals(obj)) {
return true;
}
ServerAddress their = (ServerAddress) obj;
return this.hostname.equals(their.getHostname()) && this.port == their.getPort();
}
@Override
public String toString() {
return getHostname() + ":" + getPort();
}
public String getHostname() {
return this.hostname;
}
public int getPort() {
return this.port;
}
}

View File

@ -0,0 +1,25 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
data class ServerAddress(
val hostname: String,
val port: Int = ProtocolDefinition.DEFAULT_PORT,
) {
override fun toString(): String {
return "$hostname:$port"
}
}