diff options
Diffstat (limited to 'src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardConfiguration.kt')
-rw-r--r-- | src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardConfiguration.kt | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardConfiguration.kt b/src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardConfiguration.kt index 3d8a79a..01e2af1 100644 --- a/src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardConfiguration.kt +++ b/src/main/kotlin/tech/thatgravyboat/rewardclaim/RewardConfiguration.kt @@ -1,41 +1,38 @@ package tech.thatgravyboat.rewardclaim import com.google.gson.Gson -import com.google.gson.JsonObject -import net.minecraft.util.JsonUtils +import tech.thatgravyboat.rewardclaim.types.ImageType import tech.thatgravyboat.rewardclaim.types.RewardImage import java.io.IOException import java.net.URL import java.util.* -import java.util.regex.Pattern +import kotlin.collections.HashMap private val GSON = Gson() +private val DEFAULT_IMAGE_TYPE = ImageType(142, 100, false) object RewardConfiguration { - val TEXTURES: MutableMap<String, RewardImage> = HashMap<String, RewardImage>() - var rewardMessageRegex: Pattern = Pattern.compile("Click the link to visit our website and claim your reward: https://rewards\\.hypixel\\.net/claim-reward/(?<id>[A-Za-z0-9]{8})") - var rewardMissedMessageRegex: Pattern = Pattern.compile("We noticed you haven't claimed your free Daily Reward yet!\\nTo choose your reward you have to click the link to visit our website! As a reminder, here's your link for today: https://rewards\\.hypixel\\.net/claim-reward/(?<id>[A-Za-z0-9]{8})") - var userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36" + private lateinit var imageTypes: HashMap<String, ImageType> + lateinit var textures: HashMap<String, RewardImage> + lateinit var rewardMessageRegex: Regex + lateinit var rewardMissedMessageRegex: Regex + lateinit var userAgent: String - fun loadData() { - val json = GSON.fromJson(readData(), JsonObject::class.java) - - json.get("textures")?.apply { - for (texture in asJsonArray) { - val image = texture.asJsonObject - TEXTURES[image["id"].asString] = RewardImage(image["url"].asString, JsonUtils.getInt(image, "height", 142)) - } - } + fun getImageType(type: String?) = if (type == null) DEFAULT_IMAGE_TYPE else imageTypes.getOrDefault(type, DEFAULT_IMAGE_TYPE) - json.get("rewardRegex")?.apply { rewardMessageRegex = Pattern.compile(asString) } - json.get("missedRewardRegex")?.apply { rewardMissedMessageRegex = Pattern.compile(asString) } - json.get("userAgent")?.apply { userAgent = asString } + fun loadData() { + val config = GSON.fromJson(readData(), JsonConfig::class.java) + textures = config.textures + imageTypes = config.imageTypes + rewardMessageRegex = Regex(config.rewardRegex) + rewardMissedMessageRegex = Regex(config.missedRewardRegex) + userAgent = config.userAgent } private fun readData(): String { try { Scanner( - URL("https://gist.githubusercontent.com/ThatGravyBoat/05cf118ea1daced936f040a41a648819/raw/2410c868444b073fd212fbed1da5a063d79dc816/data.json").openStream(), + URL("https://raw.githubusercontent.com/ThatGravyBoat/RewardClaim/master/data.json").openStream(), "UTF-8" ).use { scanner -> scanner.useDelimiter("\\A") @@ -46,4 +43,12 @@ object RewardConfiguration { } return "" } + + private data class JsonConfig( + val imageTypes: HashMap<String, ImageType> = hashMapOf(), + val textures: HashMap<String, RewardImage> = hashMapOf(), + val rewardRegex: String = "Click the link to visit our website and claim your reward: https://rewards\\.hypixel\\.net/claim-reward/(?<id>[A-Za-z0-9]{8})", + val missedRewardRegex: String = "We noticed you haven't claimed your free Daily Reward yet!\\nTo choose your reward you have to click the link to visit our website! As a reminder, here's your link for today: https://rewards\\.hypixel\\.net/claim-reward/(?<id>[A-Za-z0-9]{8})", + val userAgent: String = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36" + ) }
\ No newline at end of file |