From 2424c6ebac4f637227fc5272fdfbcd313e183766 Mon Sep 17 00:00:00 2001 From: Dawson Date: Thu, 16 Nov 2023 16:48:24 -0500 Subject: [PATCH] refactor(reorganize): Reorganized code. --- .../main/kotlin/gg/flyte/common/util/HTTP.kt | 100 ------------------ .../gg/flyte/pluginPortal/PluginPortal.kt | 18 +--- .../pluginPortal/command/CommandManager.kt | 4 +- .../command/downloadable/InstallSubCommand.kt | 13 +-- .../command/downloadable/UpdateSubCommand.kt | 12 +-- .../command/info/InfoSubCommand.kt | 5 +- .../command/info/ListSubCommand.kt | 4 +- .../command/info/SearchSubCommand.kt | 18 ---- .../command/info/display/InfoDisplay.kt | 4 +- .../pluginPortal/{type => }/manager/Config.kt | 2 +- .../{type => }/manager/PPPluginCache.kt | 2 +- .../{type => }/manager/PluginManager.kt | 8 +- .../{type => }/manager/language/Language.kt | 0 .../language/LanguageLoadingException.kt | 0 .../{type => }/manager/language/Message.kt | 7 +- .../language}/PlayerExtension.kt | 5 +- 16 files changed, 33 insertions(+), 169 deletions(-) delete mode 100644 common/src/main/kotlin/gg/flyte/common/util/HTTP.kt delete mode 100644 spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/SearchSubCommand.kt rename spigot/src/main/kotlin/gg/flyte/pluginPortal/{type => }/manager/Config.kt (93%) rename spigot/src/main/kotlin/gg/flyte/pluginPortal/{type => }/manager/PPPluginCache.kt (98%) rename spigot/src/main/kotlin/gg/flyte/pluginPortal/{type => }/manager/PluginManager.kt (89%) rename spigot/src/main/kotlin/gg/flyte/pluginPortal/{type => }/manager/language/Language.kt (100%) rename spigot/src/main/kotlin/gg/flyte/pluginPortal/{type => }/manager/language/LanguageLoadingException.kt (100%) rename spigot/src/main/kotlin/gg/flyte/pluginPortal/{type => }/manager/language/Message.kt (98%) rename spigot/src/main/kotlin/gg/flyte/pluginPortal/{type/extension => manager/language}/PlayerExtension.kt (82%) diff --git a/common/src/main/kotlin/gg/flyte/common/util/HTTP.kt b/common/src/main/kotlin/gg/flyte/common/util/HTTP.kt deleted file mode 100644 index 6ec34f2..0000000 --- a/common/src/main/kotlin/gg/flyte/common/util/HTTP.kt +++ /dev/null @@ -1,100 +0,0 @@ -package gg.flyte.common.util - -import okhttp3.Request -import java.net.URL -import java.net.URLDecoder -import java.net.URLEncoder -import javax.net.ssl.HttpsURLConnection - -fun getStringFromURL(url: String): String { - return okHttpClient.newCall( - Request.Builder() - .url(url) - .header("User-Agent", USER_AGENT) - .build() - ).execute().run { - this.body?.string().run { - close() - this ?: "" - } - } - -} - -fun isDirectDownload(urlString: String): Boolean { - if (urlString.isEmpty()) return false - - return runCatching { - val url = URL(urlString) - val connection = url.openConnection() as HttpsURLConnection - connection.requestMethod = "HEAD" - connection.connect() - - val contentType = connection.contentType - val contentLength = connection.contentLength - - connection.disconnect() - - contentType == "application/octet-stream" && contentLength != -1 - }.getOrDefault(false) -} - -fun isJARFileDownload(url: String): Boolean { - if (url.isEmpty()) return false - - val request = Request.Builder() - .url(getFinalRedirect(url)) - .build() - val response = okHttpClient.newCall(request).execute().run { - this - } - - return runCatching { - val contentType = response.header("Content-Type") - val contentDisposition = response.header("Content-Disposition") - - return (contentType != null && contentType == "application/java-archive") - || (contentDisposition != null && contentDisposition.contains(".jar")) - || (url.endsWith(".jar")) - }.getOrDefault(false) -} - -private fun getFinalRedirect(url: String): String { - var request = Request.Builder().url(url).head().build() - var response = okHttpClient.newCall(request).execute().run { - this - } - - while (response.isRedirect) { - response.close() - val redirectUrl: String = response.header("Location") ?: "none" - request = Request.Builder().url(redirectUrl).head().build() - response = okHttpClient.newCall(request).execute() - } - - return response.request.url.toString() -} - -//fun makePostRequest(url: String, data: T): String { -// val request = Request.Builder() -// .url(url) -// .method( -// "POST", -// objectMapper.writeValueAsString(data).toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull()) -// ) -// .build() -// -// client.newCall(request).execute().use { response -> -// log(url, response.code.getStatusType(), LogType.POST) -// -// if (!response.isSuccessful) throw IOException("Unexpected code $response") -// -// return response.body?.string().run { -// response.close() -// this ?: "" -// } -// } -//} - -fun Any.encodeURL() = URLEncoder.encode(this.toJson(), "UTF-8") -fun String.decodeURL() = URLDecoder.decode(this.toJson(), "UTF-8") diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/PluginPortal.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/PluginPortal.kt index cf74e73..aa06e08 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/PluginPortal.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/PluginPortal.kt @@ -1,8 +1,8 @@ package gg.flyte.pluginPortal import gg.flyte.pluginPortal.command.CommandManager -import gg.flyte.pluginPortal.type.manager.Config -import gg.flyte.pluginPortal.type.manager.PPPluginCache +import gg.flyte.pluginPortal.manager.Config +import gg.flyte.pluginPortal.manager.PPPluginCache import gg.flyte.twilight.twilight import io.papermc.lib.PaperLib import org.bstats.bukkit.Metrics @@ -17,25 +17,17 @@ class PluginPortal : JavaPlugin() { override fun onEnable() { instance = this - Config.init(this) twilight(this) {} + Config.init(this) CommandManager.init() - PPPluginCache.loadInstalledPlugins( -// dataFolder.apply { mkdir() }.parentFile, -// SpigotInstalledPluginLoader.apply { -// loadInstalledPlugins() -// } - ) + PPPluginCache.loadInstalledPlugins() Metrics(this, 18005) PaperLib.suggestPaper(this) } - override fun onDisable() { - PPPluginCache.saveInstalledPlugins() - logger.info("PluginPortal has been disabled!") - } + override fun onDisable() { PPPluginCache.saveInstalledPlugins() } } \ No newline at end of file diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/CommandManager.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/CommandManager.kt index 2529bcb..5150703 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/CommandManager.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/CommandManager.kt @@ -8,8 +8,7 @@ import gg.flyte.pluginPortal.command.downloadable.UpdateSubCommand import gg.flyte.pluginPortal.command.info.HelpSubCommand import gg.flyte.pluginPortal.command.info.InfoSubCommand import gg.flyte.pluginPortal.command.info.ListSubCommand -import gg.flyte.pluginPortal.command.info.SearchSubCommand -import gg.flyte.pluginPortal.type.manager.PPPluginCache +import gg.flyte.pluginPortal.manager.PPPluginCache import gg.flyte.twilight.scheduler.async import net.kyori.adventure.platform.bukkit.BukkitAudiences import org.bukkit.Bukkit @@ -36,7 +35,6 @@ object CommandManager { HelpSubCommand(), InfoSubCommand(), ListSubCommand(), - SearchSubCommand(), ) } diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/downloadable/InstallSubCommand.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/downloadable/InstallSubCommand.kt index 61bbb7e..55a50ef 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/downloadable/InstallSubCommand.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/downloadable/InstallSubCommand.kt @@ -1,14 +1,11 @@ package gg.flyte.pluginPortal.command.downloadable -import gg.flyte.common.api.API -import gg.flyte.common.api.plugins.schemas.MarketplacePlugin import gg.flyte.pluginPortal.command.CommandManager -import gg.flyte.pluginPortal.type.extension.sendError -import gg.flyte.pluginPortal.type.extension.sendInfo -import gg.flyte.pluginPortal.type.extension.sendSuccess -import gg.flyte.pluginPortal.type.manager.PPPluginCache -import gg.flyte.pluginPortal.type.manager.PPPluginCache.isInstalled -import gg.flyte.pluginPortal.type.manager.PluginManager +import gg.flyte.pluginPortal.manager.language.sendError +import gg.flyte.pluginPortal.manager.language.sendInfo +import gg.flyte.pluginPortal.manager.language.sendSuccess +import gg.flyte.pluginPortal.manager.PPPluginCache.isInstalled +import gg.flyte.pluginPortal.manager.PluginManager import net.kyori.adventure.audience.Audience import revxrsal.commands.annotation.AutoComplete import revxrsal.commands.annotation.Command diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/downloadable/UpdateSubCommand.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/downloadable/UpdateSubCommand.kt index 95c2019..5bdb6ed 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/downloadable/UpdateSubCommand.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/downloadable/UpdateSubCommand.kt @@ -2,14 +2,12 @@ package gg.flyte.pluginPortal.command.downloadable import gg.flyte.common.api.API import gg.flyte.common.api.plugins.schemas.InstalledPlugin -import gg.flyte.common.api.plugins.schemas.MarketplacePlugin import gg.flyte.pluginPortal.command.CommandManager -import gg.flyte.pluginPortal.type.extension.sendError -import gg.flyte.pluginPortal.type.extension.sendInfo -import gg.flyte.pluginPortal.type.extension.sendSuccess -import gg.flyte.pluginPortal.type.manager.PPPluginCache -import gg.flyte.pluginPortal.type.manager.PPPluginCache.isInstalled -import gg.flyte.pluginPortal.type.manager.PluginManager +import gg.flyte.pluginPortal.manager.language.sendError +import gg.flyte.pluginPortal.manager.language.sendInfo +import gg.flyte.pluginPortal.manager.language.sendSuccess +import gg.flyte.pluginPortal.manager.PPPluginCache +import gg.flyte.pluginPortal.manager.PluginManager import net.kyori.adventure.audience.Audience import revxrsal.commands.annotation.* import revxrsal.commands.bukkit.annotation.CommandPermission diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/InfoSubCommand.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/InfoSubCommand.kt index 927cdc1..a922604 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/InfoSubCommand.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/InfoSubCommand.kt @@ -2,10 +2,9 @@ package gg.flyte.pluginPortal.command.info import gg.flyte.pluginPortal.command.CommandManager import gg.flyte.pluginPortal.command.info.display.InfoDisplay -import gg.flyte.pluginPortal.type.extension.sendError -import gg.flyte.pluginPortal.type.extension.sendInfo +import gg.flyte.pluginPortal.manager.language.sendError +import gg.flyte.pluginPortal.manager.language.sendInfo import net.kyori.adventure.audience.Audience -import org.bukkit.Bukkit import revxrsal.commands.annotation.* import revxrsal.commands.bukkit.annotation.CommandPermission diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/ListSubCommand.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/ListSubCommand.kt index 10b9fc5..0e5a692 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/ListSubCommand.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/ListSubCommand.kt @@ -1,8 +1,6 @@ package gg.flyte.pluginPortal.command.info -import gg.flyte.pluginPortal.type.extension.sendInfo -import gg.flyte.pluginPortal.type.manager.PPPluginCache -import gg.flyte.pluginPortal.type.manager.language.Message.toComponent +import gg.flyte.pluginPortal.manager.PPPluginCache import gg.flyte.twilight.extension.solidLine import net.kyori.adventure.audience.Audience import net.kyori.adventure.text.Component diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/SearchSubCommand.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/SearchSubCommand.kt deleted file mode 100644 index 3d3ccad..0000000 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/SearchSubCommand.kt +++ /dev/null @@ -1,18 +0,0 @@ -package gg.flyte.pluginPortal.command.info - -import gg.flyte.pluginPortal.type.manager.language.Message.toComponent -import org.bukkit.Bukkit -import revxrsal.commands.annotation.Command -import revxrsal.commands.annotation.Subcommand -import revxrsal.commands.bukkit.annotation.CommandPermission - -@Command("pp", "pluginportal", "ppm", "pportal") -class SearchSubCommand { - - @Subcommand("search", "s") - @CommandPermission("pluginportal.search") - fun onSearchCommand(int: Int) { - Bukkit.broadcast(int.toString().toComponent()) - } - -} \ No newline at end of file diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/display/InfoDisplay.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/display/InfoDisplay.kt index 2e30a49..62f09db 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/display/InfoDisplay.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/command/info/display/InfoDisplay.kt @@ -1,8 +1,8 @@ package gg.flyte.pluginPortal.command.info.display import gg.flyte.common.api.plugins.schemas.MarketplacePlugin -import gg.flyte.pluginPortal.type.manager.language.Message.serialize -import gg.flyte.pluginPortal.type.manager.language.Message.toComponent +import gg.flyte.pluginPortal.manager.language.Message.serialize +import gg.flyte.pluginPortal.manager.language.Message.toComponent import gg.flyte.twilight.extension.solidLine import net.kyori.adventure.text.Component import net.kyori.adventure.text.TextComponent diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/Config.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/Config.kt similarity index 93% rename from spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/Config.kt rename to spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/Config.kt index 1cc9f63..899f7ec 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/Config.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/Config.kt @@ -1,4 +1,4 @@ -package gg.flyte.pluginPortal.type.manager +package gg.flyte.pluginPortal.manager import gg.flyte.pluginPortal.PluginPortal import org.bukkit.configuration.file.FileConfiguration diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/PPPluginCache.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/PPPluginCache.kt similarity index 98% rename from spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/PPPluginCache.kt rename to spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/PPPluginCache.kt index 1a78991..377df5d 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/PPPluginCache.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/PPPluginCache.kt @@ -1,4 +1,4 @@ -package gg.flyte.pluginPortal.type.manager +package gg.flyte.pluginPortal.manager import com.github.benmanes.caffeine.cache.Cache import com.github.benmanes.caffeine.cache.Caffeine diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/PluginManager.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/PluginManager.kt similarity index 89% rename from spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/PluginManager.kt rename to spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/PluginManager.kt index 38896f2..b11f60b 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/PluginManager.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/PluginManager.kt @@ -1,12 +1,12 @@ -package gg.flyte.pluginPortal.type.manager +package gg.flyte.pluginPortal.manager import gg.flyte.common.api.plugins.schemas.InstalledPlugin import gg.flyte.common.api.plugins.schemas.MarketplacePlugin import gg.flyte.common.util.getHashes import gg.flyte.common.util.pluginApiInterface -import gg.flyte.pluginPortal.type.manager.PPPluginCache.isInstalled -import gg.flyte.pluginPortal.type.manager.PPPluginCache.pluginFolder -import gg.flyte.pluginPortal.type.manager.PPPluginCache.updateFolder +import gg.flyte.pluginPortal.manager.PPPluginCache.isInstalled +import gg.flyte.pluginPortal.manager.PPPluginCache.pluginFolder +import gg.flyte.pluginPortal.manager.PPPluginCache.updateFolder import gg.flyte.twilight.scheduler.async import java.io.File diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/language/Language.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/language/Language.kt similarity index 100% rename from spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/language/Language.kt rename to spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/language/Language.kt diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/language/LanguageLoadingException.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/language/LanguageLoadingException.kt similarity index 100% rename from spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/language/LanguageLoadingException.kt rename to spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/language/LanguageLoadingException.kt diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/language/Message.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/language/Message.kt similarity index 98% rename from spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/language/Message.kt rename to spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/language/Message.kt index bb33da3..b389be6 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/manager/language/Message.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/language/Message.kt @@ -1,7 +1,7 @@ -package gg.flyte.pluginPortal.type.manager.language +package gg.flyte.pluginPortal.manager.language import gg.flyte.pluginPortal.PluginPortal -import gg.flyte.pluginPortal.type.manager.Config +import gg.flyte.pluginPortal.manager.Config import link.portalbox.pluginportal.type.language.Language import link.portalbox.pluginportal.type.language.LanguageLoadingException import net.kyori.adventure.platform.bukkit.BukkitAudiences @@ -122,4 +122,5 @@ object Message { return configString } -} \ No newline at end of file +} + diff --git a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/extension/PlayerExtension.kt b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/language/PlayerExtension.kt similarity index 82% rename from spigot/src/main/kotlin/gg/flyte/pluginPortal/type/extension/PlayerExtension.kt rename to spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/language/PlayerExtension.kt index 457686b..33ca875 100644 --- a/spigot/src/main/kotlin/gg/flyte/pluginPortal/type/extension/PlayerExtension.kt +++ b/spigot/src/main/kotlin/gg/flyte/pluginPortal/manager/language/PlayerExtension.kt @@ -1,8 +1,7 @@ -package gg.flyte.pluginPortal.type.extension +package gg.flyte.pluginPortal.manager.language -import gg.flyte.pluginPortal.type.manager.language.Message.toComponent +import gg.flyte.pluginPortal.manager.language.Message.toComponent import net.kyori.adventure.audience.Audience -import org.bukkit.entity.Player var prefix = "[PP] > "