Skip to content

Commit

Permalink
Fix command exception when failing http request
Browse files Browse the repository at this point in the history
  • Loading branch information
MLGPenguin committed Jun 21, 2024
1 parent bf954cc commit 3e78590
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
28 changes: 23 additions & 5 deletions plugin/src/main/kotlin/gg/flyte/pluginportal/plugin/util/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ package gg.flyte.pluginportal.plugin.util
import gg.flyte.pluginportal.common.types.LocalPlugin
import gg.flyte.pluginportal.common.types.MarketplacePlatform
import gg.flyte.pluginportal.common.types.Plugin
import gg.flyte.pluginportal.plugin.chat.*
import gg.flyte.pluginportal.plugin.manager.LocalPluginCache
import net.kyori.adventure.audience.Audience
import net.kyori.adventure.text.Component.text
import java.io.File
import java.io.FileInputStream
import java.net.URL
import java.security.MessageDigest

fun Plugin.download(marketplacePlatform: MarketplacePlatform, targetDirectory: String) {
fun Plugin.download(marketplacePlatform: MarketplacePlatform, targetDirectory: String, audience: Audience? = null): Boolean {

val jarFile = File(targetDirectory, "[PP] $name ($marketplacePlatform).jar")

val file = download(
URL(platforms[marketplacePlatform]!!.download?.url),
jarFile
)
jarFile,
audience
) ?: return false

LocalPluginCache.removeIf { plugin -> plugin.id == id }

Expand All @@ -32,13 +36,27 @@ fun Plugin.download(marketplacePlatform: MarketplacePlatform, targetDirectory: S
)

LocalPluginCache.save()

return true
}

fun download(url: URL, file: File): File {
fun download(url: URL, file: File, audience: Audience?): File? {
val connection = url.openConnection()
connection.setRequestProperty("User-Agent", "Mozilla/5.0")
connection.connect()
val inputStream = connection.getInputStream()
val inputStream = connection.runCatching {
getInputStream()
}.onFailure {
it.printStackTrace()
audience?.sendMessage(text("\n").append(
status(Status.FAILURE, "An error occurred while downloading\n")
.append(textSecondary("- Please try again, or join our ")
.append(SharedComponents.DISCORD_COMPONENT)
.appendSecondary(" for support.")
).append(endLine()))
)
return null
}.getOrNull()!! // Returns above if failure.

file.parentFile.mkdirs()
file.createNewFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import gg.flyte.pluginportal.plugin.chat.suggestCommand
import gg.flyte.pluginportal.plugin.chat.textDark
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.Component.text
import net.kyori.adventure.text.event.ClickEvent
import net.kyori.adventure.text.format.NamedTextColor

object SharedComponents {

val DISCORD_COMPONENT = text("Discord", NamedTextColor.AQUA)
.showOnHover("Click here to join our Discord", NamedTextColor.AQUA)
.clickEvent(ClickEvent.openUrl("https://discord.gg/flyte"))

fun getInstallButton(name: String, installed: Boolean): Component {
val install = if (installed) "uninstall" else "install"
val color = if (installed) NamedTextColor.RED else NamedTextColor.AQUA
Expand All @@ -30,4 +35,5 @@ object SharedComponents {
.showOnHover(hoverText, NamedTextColor.AQUA)
.suggestCommand(clickSuggest)


}

0 comments on commit 3e78590

Please sign in to comment.