All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.shynixn.petblocks.bukkit.logic.persistence.configuration.Config.kt Maven / Gradle / Ivy

package com.github.shynixn.petblocks.bukkit.logic.persistence.configuration

import com.github.shynixn.petblocks.api.business.enumeration.ParticleType
import com.github.shynixn.petblocks.api.persistence.entity.Particle
import com.github.shynixn.petblocks.api.persistence.entity.PetMeta
import com.github.shynixn.petblocks.api.persistence.entity.Sound
import com.github.shynixn.petblocks.bukkit.PetBlocksPlugin
import com.github.shynixn.petblocks.bukkit.logic.business.helper.toParticleType
import com.github.shynixn.petblocks.bukkit.nms.NMSRegistry
import com.github.shynixn.petblocks.bukkit.nms.v1_13_R1.MaterialCompatibility13
import com.github.shynixn.petblocks.core.logic.business.helper.ChatBuilder
import com.github.shynixn.petblocks.core.logic.persistence.configuration.Config
import com.github.shynixn.petblocks.core.logic.persistence.entity.ParticleEntity
import com.github.shynixn.petblocks.core.logic.persistence.entity.PetData
import com.github.shynixn.petblocks.core.logic.persistence.entity.SoundEntity
import com.google.inject.Singleton
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.Location
import org.bukkit.configuration.MemorySection
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin
import org.bukkit.plugin.java.JavaPlugin
import java.util.logging.Level

/**
 * 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. */ @Singleton object Config : Config() { internal var plugin: Plugin? = null /** * Returns the pet naming message. * * @return message */ override fun getPetNamingMessage(): ChatBuilder { return ChatBuilder() .text(this.prefix) .component(this.getData("messages.naming-suggest-prefix")).builder() .component(this.getData("messages.naming-suggest-clickable")) .setClickAction(ChatBuilder.ClickAction.SUGGEST_COMMAND, "/" + this.getData("petblocks-gui.command") + " rename ") .setHoverText(this.getData("messages.naming-suggest-hover")).builder() .component(this.getData("messages.naming-suggest-suffix")).builder() } /** * Returns the skin naming message. * * @return message */ override fun getPetSkinNamingMessage(): ChatBuilder { return ChatBuilder() .text(this.prefix) .component(this.getData("messages.skullnaming-suggest-prefix")).builder() .component(this.getData("messages.skullnaming-suggest-clickable")) .setClickAction(ChatBuilder.ClickAction.SUGGEST_COMMAND, "/" + this.getData("petblocks-gui.command") + " skin ") .setHoverText(this.getData("messages.skullnaming-suggest-hover")).builder() .component(this.getData("messages.skullnaming-suggest-suffix")).builder() } override fun fixJoinDefaultPet(petData: PetMeta) { val petMeta = petData as PetData petMeta.setSkin(this.getData("join.settings.id")!!, (this.getData("join.settings.damage") as Int), this.getData("join.settings.skin"), this.getData("join.settings.unbreakable")!!) val optEngineContainer = this.engineController.getContainerFromPosition(this.getData("join.settings.engine")!!) if (!optEngineContainer.isPresent) { throw IllegalArgumentException("Join.settings.engine engine could not be loaded!") } petMeta.setEngine(optEngineContainer.get()) petMeta.petDisplayName = this.getData("join.settings.petname")!!.replace(":player", petMeta.playerMeta.name) petMeta.isEnabled = this.getData("join.settings.enabled")!! petMeta.age = this.getData("join.settings.age")!!.toLong() if (!(this.getData("join.settings.effect.name") as String).equals("none", ignoreCase = true)) { petData.setParticleEffectMeta(generateParticleEffectCompatibility("join.settings.effect")) } } override fun allowPetSpawning(location2: Any?): Boolean { val location = location2 as Location val includedWorlds = this.includedWorlds val excludedWorlds = this.excludedWorlds when { includedWorlds.contains("all") -> return !excludedWorlds.contains(location.world.name) && this.handleRegionSpawn(location) excludedWorlds.contains("all") -> return includedWorlds.contains(location.world.name) && this.handleRegionSpawn(location) else -> Bukkit.getConsoleSender().sendMessage(PetBlocksPlugin.PREFIX_CONSOLE + ChatColor.RED + "Please add 'all' to excluded or included worlds inside of the config.yml") } return true } private fun handleRegionSpawn(location: Location): Boolean { val includedRegions = this.includedRegions val excludedRegions = this.excludedRegion when { includedRegions.contains("all") -> return NMSRegistry.getWorldGuardRegionsFromLocation(location).none { excludedRegions.contains(it) } excludedRegions.contains("all") -> return NMSRegistry.getWorldGuardRegionsFromLocation(location).any { includedRegions.contains(it) } else -> Bukkit.getConsoleSender().sendMessage(PetBlocksPlugin.PREFIX_CONSOLE + ChatColor.RED + "Please add 'all' to excluded or included regions inside of the config.yml") } return true } /** * Returns the feeding click sound. * * @return sound */ fun getFeedingClickSound(): Sound { if (this.feedingClickSoundCache == null) { try { val data = (this.getData("pet.feeding.click-sound") as MemorySection).getValues(false) this.feedingClickSoundCache = SoundEntity(data["name"] as String) this.feedingClickSoundCache.volume = data["volume"] as Double this.feedingClickSoundCache.pitch = data["pitch"] as Double } catch (e: Exception) { PetBlocksPlugin.logger().log(Level.WARNING, "Failed to load feeding click-sound.", e) } } return this.feedingClickSoundCache } /** * Returns the feeding particleEffect. * * @return particleEffect */ fun getFeedingClickParticleEffect(): Particle { if (this.feedingClickParticleCache == null) { try { feedingClickParticleCache = generateParticleEffectCompatibility("pet.feeding.click-particle") } catch (e: Exception) { PetBlocksPlugin.logger().log(Level.WARNING, "Failed to load feeding click-sound.", e) } } return this.feedingClickParticleCache } private fun generateParticleEffectCompatibility(path: String): ParticleEntity { val entityParticle = ParticleEntity(ParticleType.NONE) val values = (this.getData(path) as MemorySection).getValues(false) 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 = MaterialCompatibility13.getMaterialFromId(values["id"] as Int).name } } return entityParticle } /** * Reloads the config. */ override fun reload() { this.plugin = JavaPlugin.getPlugin(PetBlocksPlugin::class.java) this.plugin!!.reloadConfig() super.reload() } /** * Returns data. * * @param path path * @return data */ override fun getData(path: String): T? { if (this.plugin == null) return null var data = this.plugin!!.config.get(path) if (data is String) { data = ChatColor.translateAlternateColorCodes('&', data) } return data as T } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy