From 41851d0f8113076d6c9667b73348513a2cd653f3 Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Fri, 18 Sep 2020 21:34:51 +0200 Subject: [PATCH] IPAddressData support --- .../axay/kspigot/ipaddress/IPAddressData.kt | 70 +++++++++++++++++++ .../net/axay/kspigot/main/ValueHolder.kt | 15 ++++ 2 files changed, 85 insertions(+) create mode 100644 src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt create mode 100644 src/main/kotlin/net/axay/kspigot/main/ValueHolder.kt diff --git a/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt b/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt new file mode 100644 index 00000000..f7a95fb4 --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt @@ -0,0 +1,70 @@ +@file:Suppress("unused") + +package net.axay.kspigot.ipaddress + +import com.google.gson.JsonObject +import net.axay.kspigot.main.ValueHolder +import org.bukkit.entity.Player +import java.net.URL + +private const val IP_API = "http://ip-api.com/json/" +private const val IP_API_FIELDS = "12120063" + +val Player.ipAddressData get() = ipAddressData() + +fun Player.ipAddressData(language: IPAddressDataLanguage = IPAddressDataLanguage.ENGLISH): IPAddressData? { + + val hostString = address?.hostString ?: return null + val jsonObject = ValueHolder.gson.fromJson( + URL("$IP_API${hostString}?fields=${IP_API_FIELDS}?lang=${language.code}").readText(), + JsonObject::class.java + ) + + if (jsonObject["status"].toString() == "fail") return null + + return IPAddressData(jsonObject) + +} + +enum class IPAddressDataLanguage(val code: String) { + ENGLISH("en"), + GERMAN("de"), + SPANISH("es"), + PORTUGUESE("pt-BR"), + FRENCH("fr"), + JAPANESE("ja"), + CHINESE("zh-CN"), + RUSSIAN("ru") +} + +class IPAddressData(private val json: JsonObject) { + + val ip get() = json.getString("query") + + // region + val continent get() = json.getString("continent") + val continentCode get() = json.getString("continentCode") + val country get() = json.getString("country") + val countryCode get() = json.getString("countryCode") + val region get() = json.getString("regionName") + val regionCode get() = json.getString("region") + val city get() = json.getString("city") + val district get() = json.getString("district") + val postalCode get() = json.getString("zip") + val timezone get() = json.getString("timezone") + + // position + val latitude get() = json.getString("lat") + val longitude get() = json.getString("lon") + + // information + val internetServiceProvider get() = json.getString("isp") + val organisation get() = json.getString("org") + +} + +private fun JsonObject.getString(key: String) = try { + this[key].toString() +} catch (exc: Exception) { + null +} \ No newline at end of file diff --git a/src/main/kotlin/net/axay/kspigot/main/ValueHolder.kt b/src/main/kotlin/net/axay/kspigot/main/ValueHolder.kt new file mode 100644 index 00000000..db47fe8b --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/main/ValueHolder.kt @@ -0,0 +1,15 @@ +package net.axay.kspigot.main + +import com.google.gson.Gson +import com.google.gson.GsonBuilder + +object ValueHolder { + + private val gsonBuilder by lazy { + GsonBuilder() + } + + val gson: Gson by lazy { gsonBuilder.create() } + val gsonPretty: Gson by lazy { gsonBuilder.setPrettyPrinting().create() } + +} \ No newline at end of file