Files
src
build.gradle.kts
CHANGELOG.md
manifest.kod
README.md
settings.gradle.kts
Code
34 2,307
src/main/kotlin/me/zplugins/zpath/internal/config/ConfigManager.kt
package me.zplugins.zpath.internal.config
import me.zplugins.zpath.ZPathPlugin
import me.zplugins.zpath.api.DestinationType
import me.zplugins.zpath.api.Objective
import me.zplugins.zpath.internal.destination.DestinationImpl
import me.zplugins.zpath.internal.npc.NPCManager
import me.zplugins.zpath.internal.npc.NPCData
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.Player
import org.bukkit.command.CommandSender
import org.bukkit.Sound
import org.bukkit.ChatColor
import org.bukkit.Particle
import java.io.File
import java.nio.file.AtomicMoveNotSupportedException
import java.nio.file.Files
import java.nio.file.StandardCopyOption
internal data class NPCDestinationDetails(val provider: String, val npcId: String)
internal data class ProviderSyncCount(
val discovered: Int,
val synchronized: Int,
val created: Int,
val preserved: Int,
val updated: Int,
val failed: Int
)
internal data class ObjectiveSyncSummary(
val providers: Map<String, ProviderSyncCount>,
val saved: Boolean,
val discoveryFailures: Map<String, Int> = emptyMap()
)
internal data class ObjectiveSettings(
val name: String,
val text: String,
val oneTime: Boolean,
val advancementEnabled: Boolean,
val advancementTitle: String,
val advancementDescription: String,
val advancementDuration: Int
)
internal class ConfigManager(private val plugin: ZPathPlugin) {
private lateinit var config: YamlConfiguration
private lateinit var messages: YamlConfiguration
private var configuredDestinations: Map<String, DestinationImpl> = emptyMap()
private var configuredNpcDetails: Map<String, NPCDestinationDetails> = emptyMap()
private var discoveredDestinations: Map<String, DestinationImpl> = emptyMap()
private var discoveredNpcDetails: Map<String, NPCDestinationDetails> = emptyMap()
private var destinations: Map<String, DestinationImpl> = emptyMap()
private var npcDetails: Map<String, NPCDestinationDetails> = emptyMap()
private var objectives: Map<String, Objective> = emptyMap()
private var objectivesSource = YamlConfiguration()
private var npcManager: NPCManager? = null
private var warnedInvalidDisplayType: String? = null
fun attachNPCManager(manager: NPCManager) {
npcManager = manager
}
fun load() {
plugin.saveDefaultConfig()
val messagesFile = File(plugin.dataFolder, "messages.yml")
if (!messagesFile.exists()) plugin.saveResource("messages.yml", false)
config = YamlConfiguration.loadConfiguration(File(plugin.dataFolder, "config.yml"))
messages = YamlConfiguration.loadConfiguration(messagesFile)
loadObjectives()
loadDestinations()
objectiveDisplayType()
}
fun reload() {
plugin.reloadConfig()
config = YamlConfiguration.loadConfiguration(File(plugin.dataFolder, "config.yml"))
val messagesFile = File(plugin.dataFolder, "messages.yml")
if (!messagesFile.exists()) plugin.saveResource("messages.yml", false)
messages = YamlConfiguration.loadConfiguration(messagesFile)
loadObjectives()
loadDestinations()
objectiveDisplayType()
}
private fun loadObjectives() {
val objectivesFile = File(plugin.dataFolder, "objectives.yml")
if (!objectivesFile.exists()) plugin.saveResource("objectives.yml", false)
val source = YamlConfiguration.loadConfiguration(objectivesFile)
objectivesSource = source
val parsed = linkedMapOf<String, Objective>()
val section = source.getConfigurationSection("objectives")
if (section != null) {
for (npcName in section.getKeys(false)) {
val path = "objectives.$npcName"
val text = source.getString("$path.text")?.takeIf(String::isNotBlank) ?: continue
val oneTime = source.getBoolean("$path.one-time", true)
parsed[npcName.lowercase()] = Objective(npcName, text, oneTime)
source.getString("$path.npc-name")?.takeIf(String::isNotBlank)?.let { displayName ->
parsed.putIfAbsent(displayName.lowercase(), Objective(displayName, text, oneTime))
}
}
}
objectives = parsed.toMap()
}
private fun loadDestinations() {
val parsed = linkedMapOf<String, DestinationImpl>()
val npcBindings = linkedMapOf<String, NPCDestinationDetails>()
val section = config.getConfigurationSection("destinations")
if (section != null) {
for (id in section.getKeys(false)) {
val path = "destinations.$id"
val typeName = config.getString("$path.type")?.uppercase()
val type = runCatching { DestinationType.valueOf(typeName ?: "") }.getOrNull()
val display = config.getString("$path.display-name")?.takeIf(String::isNotBlank) ?: id
val arrival = config.getDouble("$path.arrival-distance", defaultArrivalDistance())
if (arrival <= 0.0) {
plugin.logger.warning("Skipping destination '$id': arrival-distance must be positive.")
continue
}
val destination = when (type) {
DestinationType.NPC -> {
val npcId = config.getString("$path.npc")?.trim().orEmpty()
val provider = config.getString("$path.provider", "auto")?.trim()?.lowercase().orEmpty()
if (npcId.isBlank() || provider !in setOf("auto", "citizens", "fancynpcs")) {
plugin.logger.warning("Skipping destination '$id': NPC id or provider is invalid.")
continue
}
npcBindings[id] = NPCDestinationDetails(provider, npcId)
DestinationImpl(id, display, DestinationType.NPC, arrival) {
npcManager?.destinationLocation(provider, npcId)
}
}
DestinationType.LOCATION -> {
val worldName = config.getString("$path.world")?.trim().orEmpty()
val world = Bukkit.getWorld(worldName)
val x = config.get("$path.x") as? Number
val y = config.get("$path.y") as? Number
val z = config.get("$path.z") as? Number
if (world == null || x == null || y == null || z == null) {
plugin.logger.warning("Skipping destination '$id': world or coordinates are invalid/unavailable.")
continue
}
val location = Location(world, x.toDouble(), y.toDouble(), z.toDouble(),
config.getDouble("$path.yaw", 0.0).toFloat(), config.getDouble("$path.pitch", 0.0).toFloat())
DestinationImpl(id, display, DestinationType.LOCATION, arrival) { location }
}
else -> {
plugin.logger.warning("Skipping destination '$id': unsupported or invalid type '$typeName'.")
continue
}
}
parsed[id.lowercase()] = destination
}
}
configuredDestinations = parsed.toMap()
configuredNpcDetails = npcBindings.toMap()
discoveredDestinations = emptyMap()
discoveredNpcDetails = emptyMap()
rebuildDestinationRegistry()
}
fun destination(id: String): DestinationImpl? = destinations[id.lowercase()]
fun destinations(): Collection<DestinationImpl> = destinations.values.toList()
fun npcDetails(destinationId: String) = npcDetails[destinationId.lowercase()]
fun objective(npcName: String): Objective? = objectives[npcName.lowercase()]
fun objectiveSettings(npcName: String, displayName: String, provider: String, stableId: String): ObjectiveSettings {
val section = objectivesSource.getConfigurationSection("objectives")
val matchingKey = section?.getKeys(false)?.firstOrNull { key ->
objectivesSource.getString("objectives.$key.npc-plugin")?.equals(provider, true) == true &&
objectivesSource.getString("objectives.$key.npc-id") == stableId
} ?: section?.getKeys(false)?.firstOrNull { it.equals(npcName, true) }
val path = matchingKey?.let { "objectives.$it" }
val text = path?.let { objectivesSource.getString("$it.text") }
?.takeIf(String::isNotBlank) ?: "&fGo to &e$displayName"
return ObjectiveSettings(
matchingKey ?: npcName,
text,
path?.let { objectivesSource.getBoolean("$it.one-time", true) } ?: true,
path?.let { objectivesSource.getBoolean("$it.advancement.enabled", true) } ?: true,
path?.let { objectivesSource.getString("$it.advancement.title") }
?.takeIf(String::isNotBlank) ?: config.getString("objectives.advancement.title", "&6NEW OBJECTIVE!")!!,
path?.let { objectivesSource.getString("$it.advancement.description") }
?.takeIf(String::isNotBlank) ?: config.getString("objectives.advancement.description", "&fGo to &e{npc_name}")!!,
path?.let { objectivesSource.getInt("$it.advancement.duration", defaultAdvancementDuration()) }
?: defaultAdvancementDuration()
)
}
fun objectivesEnabled() = config.getBoolean("objectives.enabled", true)
fun advancementEnabled() = config.getBoolean("objectives.advancement.enabled", true)
fun scoreboardEnabled() = config.getBoolean("objectives.scoreboard.enabled", true)
fun bossbarEnabled() = config.getBoolean("objectives.bossbar.enabled", true)
fun objectiveDisplayType(): String {
val value = config.getString("objectives.display.type", "ADVANCEMENT")!!.uppercase()
if (value in setOf("ADVANCEMENT", "SCOREBOARD", "BOSSBAR")) return value
if (warnedInvalidDisplayType != value) {
plugin.logger.warning("Invalid objectives.display.type '$value'; falling back to ADVANCEMENT.")