Files
SteamWar/TNTLeague/src/de/steamwar/tntleague/game/TNTLeagueTeam.kt
T
2024-12-12 16:09:25 +01:00

167 lines
5.2 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.game
import de.steamwar.tntleague.colorByTeam
import de.steamwar.tntleague.config.TNTLeagueWorldConfig
import de.steamwar.tntleague.config.targetedBlocks
import de.steamwar.tntleague.game.TNTLeagueGame.WinReason
import de.steamwar.tntleague.game.TNTLeagueGame.win
import de.steamwar.tntleague.message
import de.steamwar.tntleague.plugin
import net.kyori.adventure.text.Component
import org.bukkit.GameMode
import org.bukkit.Material
import org.bukkit.Sound
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
data class TNTLeagueTeam(val config: TNTLeagueWorldConfig.TeamConfig, private val team: Team) {
var leader: Player? = null
set(player) {
field = player
if (player != null) {
with(player.inventory) {
clear()
setItem(4, readyItem())
}
}
}
val members = mutableListOf<Player>()
val invites = mutableListOf<Player>()
val name: String
get() = team.name.uppercase()
val color: Char
get() = team.color
var isReady: Boolean = false
set(value) {
field = value
leader?.inventory?.setItem(4, readyItem())
leader?.let { it.playSound(it.location, Sound.BLOCK_NOTE_BLOCK_PLING, 1f, 1f) }
message.broadcastActionbar(if (value) "IS_READY" else "IS_NOT_READY", name.colorByTeam(this))
if (value && opposite.isReady) {
TNTLeagueGame.checkStart()
}
}
var damagedBlocks: Int = 0
set(value) {
field = value
if (value >= targetedBlocks) {
TNTLeagueGame.win(this, TNTLeagueGame.WinReason.DESTROYED)
}
}
var coins: Int = 0
val opposite: TNTLeagueTeam
get() = when (team) {
Team.BLUE -> TNTLeagueGame.redTeam
Team.RED -> TNTLeagueGame.blueTeam
}
fun join(player: Player): Boolean {
members.add(player)
with(player) {
teleport(config.spawnLocation)
gameMode = GameMode.ADVENTURE
inventory.clear()
message.broadcast("JOIN_TEAM", name.colorByTeam(this@TNTLeagueTeam), this@TNTLeagueTeam.name.colorByTeam(this@TNTLeagueTeam))
}
if (leader == null) {
leader = player
}
return true
}
fun readyItem() = if (isReady) {
ItemStack.of(Material.LIME_DYE).apply {
itemMeta = itemMeta.apply {
displayName(Component.text(message.parse("READY", leader!!)))
}
}
} else {
ItemStack.of(Material.RED_DYE).apply {
itemMeta = itemMeta.apply {
displayName(Component.text(message.parse("NOT_READY", leader!!)))
}
}
}
fun start() = members.forEach {
with(it) {
gameMode = GameMode.SURVIVAL
inventory.addItem(ItemStack.of(Material.DIAMOND_PICKAXE).apply {
itemMeta = itemMeta.apply {
isUnbreakable = true
addEnchant(Enchantment.EFFICIENCY, 1, false)
}
})
}
}
fun leave(player: Player) {
members.remove(player)
if (TNTLeagueGame.state != TNTLeagueGame.GameState.RUNNING) {
if (members.isEmpty()) {
plugin.server.onlinePlayers.firstOrNull { it != player && TNTLeagueGame.getTeam(it) == null }?.run {
members.add(this)
}
}
if (leader == player) {
leader = members.firstOrNull()
if (leader == null && TNTLeagueGame.state == TNTLeagueGame.GameState.LOBBY) {
isReady = false
}
}
} else if (TNTLeagueGame.state == TNTLeagueGame.GameState.RUNNING) {
if (members.isEmpty()) {
win(this.opposite, WinReason.LEAVE)
}
}
}
fun remove(player: Player) {
leave(player)
with(player) {
teleport(TNTLeagueWorldConfig.lobby)
gameMode = GameMode.SPECTATOR
inventory.clear()
message.broadcast("QUIT_TEAM", name.colorByTeam(this@TNTLeagueTeam), this@TNTLeagueTeam.name.colorByTeam(this@TNTLeagueTeam))
}
}
enum class Team(val color: Char) {
BLUE('9'),
RED('c');
}
}