com.github.shynixn.petblocks.sponge.logic.business.service.ConfigurationServiceImpl.kt Maven / Gradle / Ivy
package com.github.shynixn.petblocks.sponge.logic.business.service
import com.github.shynixn.petblocks.api.business.entity.GUIItemContainer
import com.github.shynixn.petblocks.api.business.enumeration.GUIPage
import com.github.shynixn.petblocks.api.business.service.ConfigurationService
import com.github.shynixn.petblocks.api.persistence.entity.GUIItem
import com.github.shynixn.petblocks.sponge.logic.business.helper.getDisplayName
import com.github.shynixn.petblocks.sponge.logic.business.helper.getLore
import com.github.shynixn.petblocks.sponge.logic.business.helper.getResource
import com.github.shynixn.petblocks.sponge.logic.persistence.configuration.Config
import com.github.shynixn.petblocks.sponge.logic.persistence.configuration.SpongeStaticGUIItems
import com.github.shynixn.petblocks.sponge.logic.persistence.entity.SpongeGUIItem
import com.github.shynixn.petblocks.sponge.logic.persistence.entity.SpongeItemContainer
import com.google.inject.Inject
import ninja.leaping.configurate.ConfigurationNode
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader
import org.spongepowered.api.config.ConfigDir
import org.spongepowered.api.item.inventory.ItemStack
import org.spongepowered.api.plugin.PluginContainer
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.nio.file.Path
import java.util.*
import java.util.regex.Pattern
import javax.crypto.Cipher
import javax.crypto.CipherInputStream
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
/**
* Created by Shynixn 2018.
*
* Version 1.2
*
* MIT License
*
* Copyright (c) 2018 by Shynixn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
class ConfigurationServiceImpl @Inject constructor(private val plugin: PluginContainer, private val guiItemsController: SpongeStaticGUIItems, @ConfigDir(sharedRoot = false) private val privateConfigDir: Path) : ConfigurationService {
private val cache = HashMap>()
private lateinit var node: ConfigurationNode
init {
try {
val defaultConfig = this.privateConfigDir.resolve("config.yml")
val loader = YAMLConfigurationLoader.builder().setPath(defaultConfig).build()
this.node = loader.load()
guiItemsController.reload()
} catch (e: IOException) {
plugin.logger.warn("Failed to reload config.yml.", e)
}
}
/**
* Tries to return a list of [GUIItem] matching the given path from the config.
* Can be called asynchronly.
*/
override fun findGUIItemCollection(path: String): Optional> {
if (cache.containsKey(path)) {
return Optional.of(cache[path]!!)
}
if (path.startsWith("minecraft-heads-com.")) {
val category = path.split(".")[1]
val items = getItemsFromMinecraftHeadsDatabase(category)
cache[path] = items
return Optional.of(items)
}
val items = ArrayList()
try {
val data = Config.getData