diff options
author | CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> | 2024-02-06 09:01:12 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-05 23:01:12 +0100 |
commit | 6ed818521cd2c9e97743881966632488e598c20e (patch) | |
tree | 1a1f92cc4feeff03bbc82240c4b86ae610a17842 /src/main/java/at/hannibal2/skyhanni/utils | |
parent | ab43b1989ebb75d5ca3ddd59a71e6a8879f040b0 (diff) | |
download | skyhanni-6ed818521cd2c9e97743881966632488e598c20e.tar.gz skyhanni-6ed818521cd2c9e97743881966632488e598c20e.tar.bz2 skyhanni-6ed818521cd2c9e97743881966632488e598c20e.zip |
Send Mining Island Events (#969)
Added sending mining events to Soopy's API to test for new Mining Event feature. #969
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
3 files changed, 59 insertions, 9 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt index 43f262d5b..2fd634d26 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt @@ -6,6 +6,7 @@ import com.google.gson.JsonElement import com.google.gson.JsonObject import com.google.gson.JsonParser import com.google.gson.JsonSyntaxException +import org.apache.http.HttpEntity import org.apache.http.client.config.RequestConfig import org.apache.http.client.methods.HttpGet import org.apache.http.client.methods.HttpPost @@ -26,6 +27,8 @@ object APIUtil { private val parser = JsonParser() private var showApiErrors = false + data class ApiResponse(val success: Boolean, val message: String?, val data: JsonObject) + private val builder: HttpClientBuilder = HttpClients.custom().setUserAgent("SkyHanni/${SkyHanniMod.version}") .setDefaultHeaders( @@ -97,23 +100,26 @@ object APIUtil { return JsonObject() } - fun postJSONIsSuccessful(urlString: String, body: String, silentError: Boolean = false): Boolean { + fun postJSON(urlString: String, body: String, silentError: Boolean = false): ApiResponse { val client = builder.build() + try { val method = HttpPost(urlString) method.entity = StringEntity(body, ContentType.APPLICATION_JSON) client.execute(method).use { response -> val status = response.statusLine + val entity = response.entity - if (status.statusCode >= 200 || status.statusCode < 300) { - return true + if (status.statusCode in 200..299) { + val data = readResponse(entity) + return ApiResponse(true, "Request successful", data) } - println("POST request to '$urlString' returned status ${status.statusCode}") - LorenzUtils.error("SkyHanni ran into an error whilst sending data. Status: ${status.statusCode}") - - return false + val message = "POST request to '$urlString' returned status ${status.statusCode}" + println(message) + LorenzUtils.error("SkyHanni ran into an error. Status: ${status.statusCode}") + return ApiResponse(false, message, JsonObject()) } } catch (throwable: Throwable) { if (silentError) { @@ -122,9 +128,26 @@ object APIUtil { throwable.printStackTrace() LorenzUtils.error("SkyHanni ran into an ${throwable::class.simpleName ?: "error"} whilst sending a resource. See logs for more details.") } + return ApiResponse(false, throwable.message, JsonObject()) } finally { client.close() } + } + + private fun readResponse(entity: HttpEntity): JsonObject { + val retSrc = EntityUtils.toString(entity) + return parser.parse(retSrc) as JsonObject + } + + fun postJSONIsSuccessful(urlString: String, body: String, silentError: Boolean = false): Boolean { + val response = postJSON(urlString, body, silentError) + + if (response.success) { + return true + } + + println(response.message) + LorenzUtils.error(response.message ?: "An error occurred during the API request") return false } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt index f9a279c46..e9d755fab 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/JsonUtils.kt @@ -2,6 +2,7 @@ package at.hannibal2.skyhanni.utils import com.google.gson.Gson import com.google.gson.JsonElement +import com.google.gson.JsonObject import java.io.Reader import kotlin.reflect.jvm.javaType import kotlin.reflect.typeOf @@ -12,3 +13,27 @@ inline fun <reified T : Any> Gson.fromJson(jsonElement: JsonElement): T = this.fromJson(jsonElement, typeOf<T>().javaType) inline fun <reified T : Any> Gson.fromJson(reader: Reader): T = this.fromJson(reader, typeOf<T>().javaType) + +fun JsonObject.getBoolean(key: String): Boolean { + return if (has(key)) { + try { + get(key).asBoolean + } catch (_: Exception) { + false + } + } else { + false + } +} + +fun JsonObject.getStringOrValue(key: String, alternative: String): String { + return if (has(key)) { + try { + get(key).asString + } catch (_: Exception) { + alternative + } + } else { + alternative + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/MinecraftConsoleFilter.kt b/src/main/java/at/hannibal2/skyhanni/utils/MinecraftConsoleFilter.kt index 3aa432cde..3e1919d8b 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/MinecraftConsoleFilter.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/MinecraftConsoleFilter.kt @@ -124,7 +124,8 @@ class MinecraftConsoleFilter(private val loggerConfigName: String) : Filter { val first = cause.stackTrace[0] val firstName = first.toString() if (firstName == "net.minecraft.scoreboard.Scoreboard.removeTeam(Scoreboard.java:229)" || - firstName == "net.minecraft.scoreboard.Scoreboard.removeTeam(Scoreboard.java:262)" + firstName == "net.minecraft.scoreboard.Scoreboard.removeTeam(Scoreboard.java:262)" || + firstName == "net.minecraft.scoreboard.Scoreboard.removeTeam(Scoreboard.java:240)" ) { filterConsole("NullPointerException at Scoreboard.removeTeam") return Filter.Result.DENY @@ -134,7 +135,8 @@ class MinecraftConsoleFilter(private val loggerConfigName: String) : Filter { return Filter.Result.DENY } if (firstName == "net.minecraft.scoreboard.Scoreboard.removeObjective(Scoreboard.java:179)" || - firstName == "net.minecraft.scoreboard.Scoreboard.removeObjective(Scoreboard.java:198)" + firstName == "net.minecraft.scoreboard.Scoreboard.removeObjective(Scoreboard.java:198)" || + firstName == "net.minecraft.scoreboard.Scoreboard.removeObjective(Scoreboard.java:186)" ) { filterConsole("IllegalArgumentException at Scoreboard.removeObjective") return Filter.Result.DENY |