Skip to content

Commit b899e04

Browse files
committed
Fix Forge-exclusive crash
1 parent d1a5dcd commit b899e04

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

build.gradle.kts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ dependencies {
141141
minecraftRuntimeLibraries(jws)
142142
}
143143

144-
includeOrShade("org.apache.httpcomponents:httpcore:4.4.16")
145-
includeOrShade("org.apache.httpcomponents:httpclient:4.5.13")
146-
includeOrShade("org.apache.httpcomponents:httpcore:4.4.16")
147-
includeOrShade("commons-logging:commons-logging:1.3.4")
144+
if (!mcData.isForgeLike) {// fuck you Forge
145+
includeOrShade("commons-logging:commons-logging:1.3.4")
146+
includeOrShade("org.apache.httpcomponents:httpcore:4.4.16")
147+
includeOrShade("org.apache.httpcomponents:httpclient:4.5.13")
148+
}
148149
}
149150

150151
toolkitReleases {

src/main/kotlin/xyz/bluspring/unitytranslate/util/HttpHelper.kt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,84 @@ import org.apache.http.client.methods.HttpPost
1010
import org.apache.http.entity.StringEntity
1111
import org.apache.http.impl.client.HttpClients
1212
import org.apache.http.util.EntityUtils
13+
import xyz.bluspring.unitytranslate.UnityTranslate
14+
import java.io.BufferedInputStream
15+
import java.io.BufferedOutputStream
16+
import java.net.HttpURLConnection
17+
import java.net.URL
1318

1419
object HttpHelper {
20+
private var hasApache: Boolean? = null
21+
1522
fun post(uri: String, body: JsonObject, headers: Map<String, String> = mapOf()): JsonElement {
23+
if (hasApache == null) {
24+
try {
25+
// This is literally only here because of Forge.
26+
Class.forName("org.apache.http.impl.client.HttpClients")
27+
Class.forName("org.apache.http.HttpHeaders")
28+
Class.forName("org.apache.http.util.EntityUtils")
29+
Class.forName("org.apache.commons.logging.LogFactory")
30+
hasApache = true
31+
} catch (e: Throwable) {
32+
UnityTranslate.logger.error("Detected that Apache HTTP support is unavailable! Translations may fail after a certain period of time.")
33+
UnityTranslate.logger.error("For more information, view: https://github.com/BluSpring/UnityTranslate/issues/4")
34+
e.printStackTrace()
35+
hasApache = false
36+
}
37+
}
38+
39+
return if (hasApache == true) {
40+
postApache(uri, body, headers)
41+
} else {
42+
postJava(uri, body, headers)
43+
}
44+
}
45+
46+
fun postJava(uri: String, body: JsonObject, headers: Map<String, String> = mapOf()): JsonElement {
47+
val url = URL(uri)
48+
var connection: HttpURLConnection? = null
49+
50+
val data = body.toString().toByteArray(Charsets.UTF_8)
51+
52+
try {
53+
connection = url.openConnection() as HttpURLConnection
54+
connection.requestMethod = "POST"
55+
connection.connectTimeout = 60_000
56+
connection.readTimeout = 60_000
57+
58+
connection.setRequestProperty("Accept", "application/json")
59+
connection.setRequestProperty("Content-Type", "application/json")
60+
headers.forEach { (key, value) ->
61+
connection.setRequestProperty(key, value)
62+
}
63+
64+
connection.doOutput = true
65+
connection.setFixedLengthStreamingMode(data.size)
66+
67+
val outputStream = BufferedOutputStream(connection.outputStream)
68+
69+
outputStream.write(data)
70+
outputStream.flush()
71+
outputStream.close()
72+
73+
if (connection.responseCode / 100 != 2) {
74+
throw Exception("Failed to load $uri (code: ${connection.responseCode})")
75+
} else {
76+
val inputStream = BufferedInputStream(connection.inputStream)
77+
val reader = inputStream.bufferedReader(Charsets.UTF_8)
78+
val result = JsonParser.parseReader(reader)
79+
80+
reader.close()
81+
inputStream.close()
82+
83+
return result
84+
}
85+
} finally {
86+
connection?.disconnect()
87+
}
88+
}
89+
90+
fun postApache(uri: String, body: JsonObject, headers: Map<String, String> = mapOf()): JsonElement {
1691
HttpClients.createDefault().use { httpClient ->
1792
val request = HttpPost(uri)
1893
request.config = RequestConfig.custom()

0 commit comments

Comments
 (0)