Files
SteamWar/TNTLeague/src/de/steamwar/tntleague/config/TNTLeagueWorldConfig.kt
T
2024-12-17 20:02:12 +01:00

125 lines
4.6 KiB
Kotlin

/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.tntleague.config
import de.steamwar.tntleague.plugin
import de.steamwar.kotlin.util.Area
import net.kyori.adventure.text.Component
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.WanderingTrader
import java.io.File
import kotlin.properties.Delegates
val world by lazy { plugin.server.worlds.first()!! }
private val targetedBlocksRed by lazy { TNTLeagueWorldConfig.redTeam.target.blocks.count { block -> block.type == TNTLeagueWorldConfig.targetMaterial } }
private val targetedBlocksBlue by lazy { TNTLeagueWorldConfig.blueTeam.target.blocks.count { block -> block.type == TNTLeagueWorldConfig.targetMaterial } }
private val targetedBlocksAll: Int
get() = if (targetedBlocksBlue == targetedBlocksRed) targetedBlocksBlue else error("Targeted blocks are not equal")
val targetedBlocks: Int
get() = if (TNTLeagueWorldConfig.target != -1) TNTLeagueWorldConfig.target else targetedBlocksAll
object TNTLeagueWorldConfig {
private val config: YamlConfiguration by lazy {
YamlConfiguration.loadConfiguration(
File(
plugin.server.worlds.first().worldFolder,
"tntleague.yml"
)
)
}
lateinit var blueTeam: TeamConfig
lateinit var redTeam: TeamConfig
lateinit var lobby: Location
lateinit var targetMaterial: Material
var minHeight by Delegates.notNull<Int>()
var target by Delegates.notNull<Int>()
init {
try {
blueTeam = TeamConfig.fromConfig(config.getConfigurationSection("blueTeam")!!)
redTeam = TeamConfig.fromConfig(config.getConfigurationSection("redTeam")!!)
lobby = config.getWorldLocation("lobby", blueTeam.spawnLocation.clone().add(redTeam.spawnLocation).multiply(0.5))
targetMaterial = Material.matchMaterial(config.getString("targetMaterial", "IRON_BLOCK")!!)!!
minHeight = config.getInt("minHeight", 0)
target = config.getInt("target", -1)
} catch (e: NullPointerException) {
Bukkit.shutdown()
}
}
@JvmRecord
data class TeamConfig(
val spawnLocation: Location,
val dealerSpawn: Location,
val itemSpawn: Location,
val target: Area
) {
companion object {
fun fromConfig(config: ConfigurationSection): TeamConfig {
val spawnLocation = config.getWorldLocation("spawn")
val dealerSpawn = config.getWorldLocation("dealerSpawn")
val itemSpawn = config.getWorldLocation("itemSpawn")
val targetPos1 = config.getWorldLocation("targetMin")
val targetPos2 = config.getWorldLocation("targetMax")
spawnDealer(dealerSpawn)
return TeamConfig(spawnLocation, dealerSpawn, itemSpawn, Area(targetPos1, targetPos2))
}
private fun spawnDealer(loc: Location) = world.spawn(loc, WanderingTrader::class.java)
.apply {
customName(Component.text("Shop"))
isCustomNameVisible = false
isInvulnerable = true
isSilent = true
isCollidable = false
isAware = false
setAI(false)
}
}
}
}
fun ConfigurationSection.getWorldLocation(s: String, default: Location? = null): Location {
val section = getConfigurationSection(s) ?: return default!!
val x = section.getDouble("x")
val y = section.getDouble("y")
val z = section.getDouble("z")
val pitch = section.getDouble("pitch")
val yaw = section.getDouble("yaw")
return Location(
plugin.server.worlds.first(),
x, y, z,
yaw.toFloat(), pitch.toFloat()
)
}