From 0374f4d99285dcd364db20aaf00efb40852b2a7d Mon Sep 17 00:00:00 2001 From: ThatGravyBoat Date: Thu, 14 Oct 2021 17:27:48 -0230 Subject: Reformatted most classes and added double click option therefore closes #3 and made it so game no crash when GitHub offline. --- .../tech/thatgravyboat/rewardclaim/Command.kt | 2 +- .../tech/thatgravyboat/rewardclaim/Config.kt | 7 +- .../rewardclaim/ExternalConfiguration.kt | 7 +- .../thatgravyboat/rewardclaim/MappedImageCache.kt | 2 +- .../tech/thatgravyboat/rewardclaim/RewardClaim.kt | 93 +++++++++-------- .../thatgravyboat/rewardclaim/types/RewardData.kt | 21 ++-- .../thatgravyboat/rewardclaim/types/RewardImage.kt | 2 +- .../thatgravyboat/rewardclaim/ui/RewardClaimGui.kt | 114 +++++++++++---------- .../tech/thatgravyboat/rewardclaim/ui/UIButton.kt | 3 +- .../tech/thatgravyboat/rewardclaim/ui/UIPopup.kt | 14 ++- .../tech/thatgravyboat/rewardclaim/ui/UIReward.kt | 4 +- .../rewardclaim/ui/UISelectedReward.kt | 5 +- 12 files changed, 144 insertions(+), 130 deletions(-) (limited to 'src/main/kotlin/tech') diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/Command.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/Command.kt index 8ba49e0..ac1f087 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/Command.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/Command.kt @@ -10,4 +10,4 @@ class Command : Command("rewardclaim") { fun handle() { EssentialAPI.getGuiUtil().openScreen(Config.gui()) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/Config.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/Config.kt index 682b109..8cc2f6d 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/Config.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/Config.kt @@ -10,9 +10,12 @@ import java.net.URI @Suppress("unused") object Config : Vigilant(File("./config/rewardclaim.toml")) { - @Property(type = PropertyType.SWITCH, "Show Confirmation", "General", description = "Shows a confirmation before you claim an item to make sure you don't by accidently claim an reward you didn't want.") + @Property(type = PropertyType.SWITCH, "Show Confirmation", "General", description = "Shows a confirmation before you claim an item to make sure you don't accidentally claim a reward you didn't want.") var showConfirmation = true + @Property(type = PropertyType.SWITCH, "Show Double Click Confirmation", "General", description = "Shows a confirmation before you double click claim an item to make sure you don't accidentally claim a reward you didn't want.") + var showDoubleClickConfirmation = true + @Property(type = PropertyType.BUTTON, "Discord", "General", "Self Promotion", placeholder = "Visit") fun discord() { UDesktop.browse(URI("https://discord.gg/jRhkYFmpCa")) @@ -36,4 +39,4 @@ object Config : Vigilant(File("./config/rewardclaim.toml")) { init { initialize() } -} \ No newline at end of file +} diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ExternalConfiguration.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ExternalConfiguration.kt index 89ad572..55f5f4d 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ExternalConfiguration.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ExternalConfiguration.kt @@ -4,6 +4,7 @@ import com.google.gson.Gson import gg.essential.api.utils.WebUtil import tech.thatgravyboat.rewardclaim.types.ImageType import tech.thatgravyboat.rewardclaim.types.RewardImage +import java.lang.Exception private val GSON = Gson() private val DEFAULT_IMAGE_TYPE = ImageType(142, 100, false) @@ -17,11 +18,11 @@ object ExternalConfiguration { var disabled = false lateinit var disabledMessage: String - fun getImageType(type: String?) = if (type == null) DEFAULT_IMAGE_TYPE else imageTypes.getOrDefault(type, DEFAULT_IMAGE_TYPE) + fun getImageType(type: String?) = imageTypes.getOrDefault(type, DEFAULT_IMAGE_TYPE) fun loadData() { WebUtil.fetchString("https://raw.githubusercontent.com/ThatGravyBoat/RewardClaim/master/data.json")?.let { - val config = GSON.fromJson(it, JsonConfig::class.java) + val config = try { GSON.fromJson(it, JsonConfig::class.java) } catch (e: Exception) { JsonConfig() } textures = config.textures imageTypes = config.imageTypes rewardMessageRegex = Regex(config.rewardRegex) @@ -41,4 +42,4 @@ object ExternalConfiguration { val disabled: Boolean = false, val disabledMessage: String = "Reward Claim was disabled by the mod author for an unknown reason." ) -} \ No newline at end of file +} diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/MappedImageCache.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/MappedImageCache.kt index e47c649..e3dc1b3 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/MappedImageCache.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/MappedImageCache.kt @@ -13,4 +13,4 @@ object MappedImageCache : ImageCache { override fun set(url: URL, image: BufferedImage) { IMAGES[url] = image } -} \ No newline at end of file +} diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardClaim.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardClaim.kt index 788b6d6..d4ba80b 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardClaim.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardClaim.kt @@ -15,58 +15,61 @@ import tech.thatgravyboat.rewardclaim.ui.RewardClaimGui name = "RewardClaim", modid = "gravyrewardclaim", version = "1.0.0", - modLanguageAdapter = "gg.essential.api.utils.KotlinAdapter") + modLanguageAdapter = "gg.essential.api.utils.KotlinAdapter" +) object RewardClaim { - private var rewardClaimTime: Long = 0 + private var rewardClaimTime: Long = 0 - @Mod.EventHandler - fun onFMLInitialization(event: FMLInitializationEvent?) { - MinecraftForge.EVENT_BUS.register(this) - EssentialAPI.getCommandRegistry().registerCommand(Command()) - } + @Mod.EventHandler + fun onFMLInitialization(event: FMLInitializationEvent?) { + MinecraftForge.EVENT_BUS.register(this) + EssentialAPI.getCommandRegistry().registerCommand(Command()) + } - @Mod.EventHandler - fun onPreInit(event: FMLPreInitializationEvent?) { - ExternalConfiguration.loadData() - } + @Mod.EventHandler + fun onPreInit(event: FMLPreInitializationEvent?) { + ExternalConfiguration.loadData() + } - @SubscribeEvent - fun onChatMessage(event: ClientChatReceivedEvent) { - ExternalConfiguration.rewardMessageRegex.matchEntire(event.message.unformattedText.trim()) - ?.apply { - if (!ExternalConfiguration.disabled) { - EssentialAPI.getGuiUtil().openScreen(RewardClaimGui(groups["id"]!!.value)) - rewardClaimTime = System.currentTimeMillis() - } else { - EssentialAPI.getNotifications() - .push("Mod Disabled", ExternalConfiguration.disabledMessage) - } - } + @SubscribeEvent + fun onChatMessage(event: ClientChatReceivedEvent) { + ExternalConfiguration.rewardMessageRegex.matchEntire(event.message.unformattedText.trim()) + ?.apply { + if (!ExternalConfiguration.disabled) { + EssentialAPI.getGuiUtil().openScreen(RewardClaimGui(groups["id"]!!.value)) + rewardClaimTime = System.currentTimeMillis() + } else { + EssentialAPI.getNotifications() + .push("Mod Disabled", ExternalConfiguration.disabledMessage) + } + } - ExternalConfiguration.rewardMissedMessageRegex.matchEntire(event.message.unformattedText.trim()) - ?.apply { - EssentialAPI.getNotifications().push( - "Reward Claim Missed!", - "You missed a reward claim, click on this to open the reward claim gui to claim your reward.") { - if (!ExternalConfiguration.disabled) { - EssentialAPI.getGuiUtil().openScreen(RewardClaimGui(groups["id"]!!.value)) - } else { - EssentialAPI.getNotifications() - .push("Mod Disabled", ExternalConfiguration.disabledMessage) + ExternalConfiguration.rewardMissedMessageRegex.matchEntire(event.message.unformattedText.trim()) + ?.apply { + EssentialAPI.getNotifications().push( + "Reward Claim Missed!", + "You missed a reward claim, click on this to open the reward claim gui to claim your reward." + ) { + if (!ExternalConfiguration.disabled) { + EssentialAPI.getGuiUtil().openScreen(RewardClaimGui(groups["id"]!!.value)) + } else { + EssentialAPI.getNotifications() + .push("Mod Disabled", ExternalConfiguration.disabledMessage) + } + } + event.isCanceled = true } - } - event.isCanceled = true - } - } + } - @SubscribeEvent - fun onScreen(event: GuiOpenEvent) { - if (EssentialAPI.getGuiUtil().openedScreen() is RewardClaimGui && - event.gui is GuiScreenBook && - System.currentTimeMillis() - rewardClaimTime <= 3000) { - event.isCanceled = true - rewardClaimTime = 0 + @SubscribeEvent + fun onScreen(event: GuiOpenEvent) { + if (EssentialAPI.getGuiUtil().openedScreen() is RewardClaimGui && + event.gui is GuiScreenBook && + System.currentTimeMillis() - rewardClaimTime <= 3000 + ) { + event.isCanceled = true + rewardClaimTime = 0 + } } - } } diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/types/RewardData.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/types/RewardData.kt index b2d1b5c..fd621a6 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/types/RewardData.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/types/RewardData.kt @@ -4,7 +4,6 @@ import com.google.gson.annotations.SerializedName import tech.thatgravyboat.rewardclaim.ExternalConfiguration import java.util.* - private val ARMOR_REGEX = Regex("(^[a-z0-9_]+)_([a-z]+)$", RegexOption.IGNORE_CASE) data class RewardData( @@ -21,12 +20,12 @@ data class RewardData( rewardPackage?.let { item -> if (reward.equals("housing_package", ignoreCase = true)) { return "${rarity.color}${ - language.translate( - "housing.skull." + item.replace( - "specialoccasion_reward_card_skull_", - "" - ) + language.translate( + "housing.skull." + item.replace( + "specialoccasion_reward_card_skull_", + "" ) + ) }" } } @@ -35,9 +34,9 @@ data class RewardData( val armorMatcher = ARMOR_REGEX.find(key) if ("suit" in key && armorMatcher != null) { return "${rarity.color}${language.translate("vanity." + armorMatcher.groups[1]!!.value)} ${ - language.translate( - "vanity.armor." + armorMatcher.groups[2]!!.value - ) + language.translate( + "vanity.armor." + armorMatcher.groups[2]!!.value + ) }" } else if ("emote" in key || "taunt" in key) { return "${rarity.color}${language.translate("vanity.$key")}" @@ -63,7 +62,7 @@ data class RewardData( } return if (reward.equals("tokens", ignoreCase = true) || reward.equals("coins", ignoreCase = true)) { "${rarity.color}${ - language.translate("type.$reward.description").replace("{\$game}", gameType!!.displayName) + language.translate("type.$reward.description").replace("{\$game}", gameType!!.displayName) }" } else { "${rarity.color}${language.translate("type.$reward.description")}" @@ -90,4 +89,4 @@ data class RewardData( } return ExternalConfiguration.textures[id] } -} \ No newline at end of file +} diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/types/RewardImage.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/types/RewardImage.kt index d70143b..4b9dfbf 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/types/RewardImage.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/types/RewardImage.kt @@ -13,4 +13,4 @@ data class RewardImage(@SerializedName("url") private val urlIn: String, val ima null } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/RewardClaimGui.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/RewardClaimGui.kt index ce47c4c..db04abe 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/RewardClaimGui.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/RewardClaimGui.kt @@ -29,59 +29,9 @@ class RewardClaimGui(private val id: String) : WindowScreen() { private var state: State = State.LOADING private var selected = -1 - //Raw Data + // Raw Data private lateinit var data: WebData - init { - runAsync(Runnable { - try { - val cookieManager = CookieManager() - CookieHandler.setDefault(cookieManager) - val url = URL("https://rewards.hypixel.net/claim-reward/$id") - (url.openConnection() as HttpURLConnection).apply { - requestMethod = "GET" - useCaches = true - addRequestProperty("User-Agent", ExternalConfiguration.userAgent) - readTimeout = 15000 - connectTimeout = 15000 - doOutput = true - inputStream.use { - val html = IOUtils.toString(it, Charset.defaultCharset()) - val securityMatcher = SECURITY_REGEX.find(html) - val dataMatcher = DATA_REGEX.find(html) - val i18nMatcher = I18N_REGEX.find(html) - - if (securityMatcher != null && dataMatcher != null && i18nMatcher != null) { - data = WebData(securityMatcher, dataMatcher, i18nMatcher) - - if (data.rewards.isEmpty()) { - state = State.FAILED_REWARDS - errorPopup("Rewards were empty.") - } else { - state = State.SUCCESSFUL - updateElements() - } - - if (data.skippable || data.duration == 0) { - adPopup(true) - } else { - schedule({ adPopup(true) }, data.duration.toLong(), TimeUnit.SECONDS) - } - } else { - state = State.FAILED_REWARDS - errorPopup("Regex could not be found.\nSecurity: ${securityMatcher != null}\nI18n: ${i18nMatcher != null}\nData: $${dataMatcher != null}") - } - } - } - } catch (e: Exception) { - state = State.FAILED - errorPopup("Error: " + e.message) - e.printStackTrace() - } - }) - adPopup(false) - } - private val background = UIBlock(VigilancePalette.getBackground()).constrain { width = 100.percent() height = 100.percent() @@ -192,12 +142,68 @@ class RewardClaimGui(private val id: String) : WindowScreen() { selected = j selectedReward.updateInfo(data.rewards[selected], data.language) } + if (event.clickCount >= 2) { + if (Config.showDoubleClickConfirmation) confirmPopup() + else claimReward() + } } } reward.hide(true) } } + init { + runAsync( + Runnable { + try { + val cookieManager = CookieManager() + CookieHandler.setDefault(cookieManager) + val url = URL("https://rewards.hypixel.net/claim-reward/$id") + (url.openConnection() as HttpURLConnection).apply { + requestMethod = "GET" + useCaches = true + addRequestProperty("User-Agent", ExternalConfiguration.userAgent) + readTimeout = 15000 + connectTimeout = 15000 + doOutput = true + inputStream.use { + val html = IOUtils.toString(it, Charset.defaultCharset()) + val securityMatcher = SECURITY_REGEX.find(html) + val dataMatcher = DATA_REGEX.find(html) + val i18nMatcher = I18N_REGEX.find(html) + + if (securityMatcher != null && dataMatcher != null && i18nMatcher != null) { + data = WebData(securityMatcher, dataMatcher, i18nMatcher) + + if (data.rewards.isEmpty()) { + state = State.FAILED_REWARDS + errorPopup("Rewards were empty.") + } else { + state = State.SUCCESSFUL + updateElements() + } + + if (data.skippable || data.duration == 0) { + adPopup(true) + } else { + schedule({ adPopup(true) }, data.duration.toLong(), TimeUnit.SECONDS) + } + } else { + state = State.FAILED_REWARDS + errorPopup("Regex could not be found.\nSecurity: ${securityMatcher != null}\nI18n: ${i18nMatcher != null}\nData: $${dataMatcher != null}") + } + } + } + } catch (e: Exception) { + state = State.FAILED + errorPopup("Error: " + e.message) + e.printStackTrace() + } + } + ) + adPopup(false) + } + private fun updateElements() { data.let { for (i in 0 until data.streak.progress) streaks[i].setColor(VigilancePalette.getAccent()) @@ -229,7 +235,7 @@ class RewardClaimGui(private val id: String) : WindowScreen() { { restorePreviousScreen() }, "${ChatColor.BOLD}Close", UIImage.ofResourceCached("/rewardclaim/external_link.png"), - { UDesktop.browse(URI("https://rewards.hypixel.net/claim-reward/${id}")) }, + { UDesktop.browse(URI("https://rewards.hypixel.net/claim-reward/$id")) }, "${ChatColor.BOLD}Reward" ) childOf this.window } @@ -310,7 +316,7 @@ class RewardClaimGui(private val id: String) : WindowScreen() { } } - private fun getAd() : URI { + private fun getAd(): URI { return if (this::data.isInitialized) URI(data.adLink) else URI("https://store.hypixel.net/?utm_source=rewards-video&utm_medium=website&utm_content=TRsCiBNYY7M&utm_campaign=Rewardss") } @@ -324,4 +330,4 @@ class RewardClaimGui(private val id: String) : WindowScreen() { SUCCESSFUL, FAILED_REWARDS } -} \ No newline at end of file +} diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIButton.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIButton.kt index ec92f25..28ffc53 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIButton.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIButton.kt @@ -49,9 +49,8 @@ class UIButton( this.onMouseEnter { this.setColor(BUTTON_HOVER) } this.onMouseLeave { this.setColor(VigilancePalette.getAccent()) } } - } enum class Alignment { LEFT, RIGHT, MIDDLE -} \ No newline at end of file +} diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIPopup.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIPopup.kt index 62734c4..5962a26 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIPopup.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIPopup.kt @@ -31,9 +31,14 @@ class UIPopup private constructor(title: String, text: String) : UIBlock(getBack } constructor( - title: String, text: String, - image1: UIImage? = null, event1: (UIComponent.(event: UIClickEvent) -> Unit), buttonText: String, - image2: UIImage? = null, event2: (UIComponent.(event: UIClickEvent) -> Unit), buttonText2: String + title: String, + text: String, + image1: UIImage? = null, + event1: (UIComponent.(event: UIClickEvent) -> Unit), + buttonText: String, + image2: UIImage? = null, + event2: (UIComponent.(event: UIClickEvent) -> Unit), + buttonText2: String ) : this(title, text) { val btn1Text = UIText(buttonText, false) @@ -84,5 +89,4 @@ class UIPopup private constructor(title: String, text: String) : UIBlock(getBack y = 5.percent() + 11.pixel() } childOf box } - -} \ No newline at end of file +} diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIReward.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIReward.kt index ffc698a..a5ad0f0 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIReward.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UIReward.kt @@ -11,8 +11,8 @@ import gg.essential.elementa.effects.OutlineEffect import gg.essential.elementa.utils.withAlpha import gg.essential.universal.ChatColor import gg.essential.vigilance.gui.VigilancePalette -import tech.thatgravyboat.rewardclaim.MappedImageCache import tech.thatgravyboat.rewardclaim.ExternalConfiguration +import tech.thatgravyboat.rewardclaim.MappedImageCache import tech.thatgravyboat.rewardclaim.types.RewardData import tech.thatgravyboat.rewardclaim.types.RewardLanguage @@ -108,4 +108,4 @@ class UIReward(xConstraint: XConstraint, yConstraint: YConstraint) : } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UISelectedReward.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UISelectedReward.kt index 447c3c5..9dadd9c 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UISelectedReward.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/ui/UISelectedReward.kt @@ -11,8 +11,8 @@ import gg.essential.elementa.effects.OutlineEffect import gg.essential.elementa.utils.withAlpha import gg.essential.universal.ChatColor import gg.essential.vigilance.gui.VigilancePalette -import tech.thatgravyboat.rewardclaim.MappedImageCache import tech.thatgravyboat.rewardclaim.ExternalConfiguration +import tech.thatgravyboat.rewardclaim.MappedImageCache import tech.thatgravyboat.rewardclaim.types.RewardData import tech.thatgravyboat.rewardclaim.types.RewardLanguage @@ -88,6 +88,5 @@ class UISelectedReward(middle: XConstraint) : UIBlock(VigilancePalette.getHighli } childOf imageBackground } } - } -} \ No newline at end of file +} -- cgit