com.github.shynixn.petblocks.sponge.logic.business.service.ConfigurationServiceImpl.kt Maven / Gradle / Ivy
@file:Suppress("UNCHECKED_CAST")
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.ChatClickAction
import com.github.shynixn.petblocks.api.business.enumeration.ChatColor
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.ChatMessage
import com.github.shynixn.petblocks.api.persistence.entity.GUIItem
import com.github.shynixn.petblocks.core.logic.business.extension.chatMessage
import com.github.shynixn.petblocks.core.logic.persistence.entity.ParticleEntity
import com.github.shynixn.petblocks.core.logic.persistence.entity.SoundEntity
import com.github.shynixn.petblocks.sponge.logic.business.helper.*
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
private var namingMessage: ChatMessage? = null
private var skullNamingMessage: ChatMessage? = null
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 load the config value from the given [path].
* Throws a [IllegalArgumentException] if the path could not be correctly
* loaded.
*/
override fun findValue(path: String): C {
if (path == "messages.naming-suggest") {
if (namingMessage == null) {
namingMessage = chatMessage {
text {
findValue("messages.prefix") + findValue("messages.naming-suggest-prefix")
}
component {
text {
findValue("messages.naming-suggest-clickable")
}
clickAction {
ChatClickAction.SUGGEST_COMMAND to "/" + findValue("petblocks-gui.command") + " rename "
}
hover {
findValue("messages.naming-suggest-hover")
}
}
text {
findValue("messages.naming-suggest-suffix")
}
}
}
return namingMessage as C
}
if (path == "messages.skullnaming-suggest") {
if (skullNamingMessage == null) {
skullNamingMessage = chatMessage {
text {
findValue("messages.prefix") + findValue("messages.skullnaming-suggest-prefix")
}
component {
text {
findValue("messages.skullnaming-suggest-clickable")
}
clickAction {
ChatClickAction.SUGGEST_COMMAND to "/" + findValue("petblocks-gui.command") + " skin "
}
hover {
findValue("messages.skullnaming-suggest-hover")
}
}
text {
findValue("messages.skullnaming-suggest-suffix")
}
}
}
return skullNamingMessage as C
}
val items = path.split(Pattern.quote(".").toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val targetNode = this.node.getNode(*items as Array)
var data = targetNode.value
if (data is String) {
data = ChatColor.translateChatColorCodes('&', data)
}
if (data is Map<*, *>) {
if (data.contains("name") && data.contains("speed") && data.contains("amount")) {
val entityParticle = ParticleEntity()
val values = data
with(entityParticle) {
type = (values["name"] as String).toParticleType()
amount = values["amount"] as Int
speed = values["speed"] as Double
}
if (values.containsKey("offx")) {
with(entityParticle) {
offSetX = values["offx"] as Double
offSetY = values["offy"] as Double
offSetZ = values["offz"] as Double
}
}
if (values.containsKey("red")) {
with(entityParticle) {
colorRed = values["red"] as Int
colorGreen = values["green"] as Int
colorBlue = values["blue"] as Int
}
}
if (values.containsKey("id")) {
if (values["id"] is String) {
entityParticle.materialName = values["id"] as String
} else {
entityParticle.materialName = CompatibilityItemType.getFromId(values["id"] as Int).name
}
}
return entityParticle as C
} else if (data.contains("name") && data.contains("volume")) {
val sound = SoundEntity()
val values = data
with(sound) {
name = values["name"] as String
volume = values["volume"] as Double
pitch = values["pitch"] as Double
}
return sound as C
}
}
return data as C
}
/**
* 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