Skip to content

Commit

Permalink
✨ better spectator system
Browse files Browse the repository at this point in the history
  • Loading branch information
b8daniel committed May 1, 2022
1 parent 62b8c0a commit 1b25ad2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class SpectateCommand : ArgumentBuilder<CommandSender, SpectateCommand>() {
.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(
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/de/crightgames/blxckoxymoron/paintball/game/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,28 @@ object Game {
private var time = Duration.ZERO
private var gameLoopTask: BukkitTask? = null

val spectators = mutableListOf<UUID>()
private val temporarySpectators = mutableListOf<Player>()

fun addSpectator(p: Player) {
p.gameMode = GameMode.SPECTATOR
temporarySpectators.add(p)
Paintball.gameConfig.teams.forEach { it.addSpectator(p) }
}

val permanentSpectators = mutableListOf<Player>()
private val spectators
get() = temporarySpectators + permanentSpectators

val players: MutableList<Player>
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})`"
Expand All @@ -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})`"
Expand Down Expand Up @@ -150,6 +156,7 @@ object Game {
}

fun restart() {
temporarySpectators.clear()
val pl = players

Bukkit.broadcastMessage(ThemeBuilder.themed(
Expand Down Expand Up @@ -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" +
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1b25ad2

Please sign in to comment.