From 1b25ad2c121721e76b604af27565e893ab831c7a Mon Sep 17 00:00:00 2001 From: Oxymoron Date: Sun, 1 May 2022 22:09:05 +0200 Subject: [PATCH] :sparkles: better spectator system --- .../paintball/commands/SpectateCommand.kt | 6 ++--- .../blxckoxymoron/paintball/game/Game.kt | 25 +++++++++++++------ .../paintball/game/PlayerJoinLeave.kt | 7 ++++-- .../paintball/game/config/ConfigTeam.kt | 4 +++ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/SpectateCommand.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/SpectateCommand.kt index 50108db..acd1ce7 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/SpectateCommand.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/SpectateCommand.kt @@ -22,11 +22,11 @@ class SpectateCommand : ArgumentBuilder() { .executes { ctx -> val player = ctx.source as Player - val wasSpectator = Game.spectators.remove(player.uniqueId) - if (!wasSpectator) Game.spectators.add(player.uniqueId) + val wasSpectator = Game.permanentSpectators.remove(player) + if (!wasSpectator) Game.permanentSpectators.add(player) player.sendThemedMessage( - "You are *${if (wasSpectator) "no longer" else "now"}* a spectator." + "You are *${if (wasSpectator) "no longer" else "now"}* permanently a spectator." ) Bukkit.broadcastMessage( diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Game.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Game.kt index 629e4bc..39e38db 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Game.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Game.kt @@ -61,22 +61,28 @@ object Game { private var time = Duration.ZERO private var gameLoopTask: BukkitTask? = null - val spectators = mutableListOf() + private val temporarySpectators = mutableListOf() + + fun addSpectator(p: Player) { + p.gameMode = GameMode.SPECTATOR + temporarySpectators.add(p) + Paintball.gameConfig.teams.forEach { it.addSpectator(p) } + } + + val permanentSpectators = mutableListOf() + private val spectators + get() = temporarySpectators + permanentSpectators val players: MutableList get() { return Bukkit.getOnlinePlayers() - .filter { !spectators.contains(it.uniqueId) } + .filter { !spectators.contains(it) } .toMutableList() } - private fun isInTeam(p: Player): Boolean { - return Paintball.gameConfig.teams.any { it.players.contains(p) } - } - fun getPlayerJoinMessage(player: Player): String { return ThemeBuilder.themed( - if (spectators.contains(player.uniqueId) || (state == GameState.RUNNING && !isInTeam(player))) + if (spectators.contains(player)) ":GOLD:»:: *${player.name}* `(ᴢᴜꜱᴄʜᴀᴜᴇʀ)`" else ":GREEN:»:: *${player.name}* `(${players.size}/${Paintball.gameConfig.minimumPlayers})`" @@ -85,7 +91,7 @@ object Game { fun getPlayerLeaveMessage(player: Player): String { return ThemeBuilder.themed( - if (spectators.contains(player.uniqueId) || (state == GameState.RUNNING && !isInTeam(player))) + if (spectators.contains(player)) ":GOLD:«:: *${player.name}* `(ᴢᴜꜱᴄʜᴀᴜᴇʀ)`" else ":RED:«:: *${player.name}* `(${players.also { it.remove(player) }.size}/${Paintball.gameConfig.minimumPlayers})`" @@ -150,6 +156,7 @@ object Game { } fun restart() { + temporarySpectators.clear() val pl = players Bukkit.broadcastMessage(ThemeBuilder.themed( @@ -187,6 +194,8 @@ object Game { } Paintball.gameConfig.teams.forEach { team -> + spectators.forEach { team.addSpectator(it) } + val spawnLocation = team.spawnPosInGame ?: return@forEach run { Bukkit.broadcastMessage(ThemeBuilder.themed( ":RED:Can't teleport players of team ::${team.displayName}\n" + diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/PlayerJoinLeave.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/PlayerJoinLeave.kt index f618802..f60946d 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/PlayerJoinLeave.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/PlayerJoinLeave.kt @@ -1,5 +1,6 @@ package de.crightgames.blxckoxymoron.paintball.game +import de.crightgames.blxckoxymoron.paintball.Paintball import de.crightgames.blxckoxymoron.paintball.util.ThemeBuilder import net.md_5.bungee.api.ChatMessageType import net.md_5.bungee.api.chat.TextComponent @@ -14,13 +15,15 @@ class PlayerJoinLeave : Listener{ @EventHandler fun onPlayerJoin(e: PlayerJoinEvent) { if (!e.player.isOp) e.player.gameMode = GameMode.SPECTATOR - Game.arenaWorld?.spawnLocation?.let { e.player.teleport(it) } - e.player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent(ThemeBuilder.themed( "Willkommen zu *Paintball*, *${e.player.name}*!" ))) + val wasPlaying = Paintball.gameConfig.teams.any { tm -> tm.players.any { it.uniqueId == e.player.uniqueId } } + + if (!wasPlaying) Game.arenaWorld?.spawnLocation?.let { e.player.teleport(it) } + if (Game.state == Game.GameState.RUNNING && !wasPlaying) Game.addSpectator(e.player) e.joinMessage = Game.getPlayerJoinMessage(e.player) diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ConfigTeam.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ConfigTeam.kt index e7840c5..812282d 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ConfigTeam.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ConfigTeam.kt @@ -67,6 +67,10 @@ class ConfigTeam (val material: IncMaterial, val displayName: String, var spawnP bossBar.addPlayer(p) } + fun addSpectator(p: Player) { + bossBar.addPlayer(p) + } + fun removePlayer(p: Player) { players.remove(p) bossBar.removePlayer(p)